Skip to content

Add tests for the cli Source Generator #4617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7c59034
Add test project for GitVersion.Cli.Generator
arturcic May 7, 2025
15b401d
Refactor module naming and improve code consistency
arturcic May 7, 2025
8e69d19
Add unit tests for CommandImplGenerator functionality`Add tests for C…
arturcic May 7, 2025
b6bebbc
Refactor and rename command generators for clarity
arturcic May 7, 2025
d5cea5a
Adds global usings to generated code
arturcic May 7, 2025
36a4920
Make output-file option required and enable nullable contexts
arturcic May 7, 2025
3dde520
Refactor property requirement check in CommandBaseGenerator
arturcic May 7, 2025
ec1a617
Refactor namespace constants into `Content` class
arturcic May 7, 2025
c3cee98
Add CLI module and refactor app initialization
arturcic May 7, 2025
cb3d56e
Moves CliModule to SystemCommandline namespace
arturcic May 9, 2025
6a8c6ca
adds test command
arturcic May 9, 2025
66f823f
Refactors container registration to use `IServiceCollection`
arturcic May 9, 2025
48913f9
uses microsoft logging
arturcic May 9, 2025
4f61da0
refactors: use readonly backing fields
arturcic May 9, 2025
2d242d3
Refactors logger injection in command classes
arturcic May 9, 2025
fc3c3da
improves command registration in IoC container
arturcic May 12, 2025
8a00c7e
Renames Content class to SystemCommandlineContent
arturcic May 12, 2025
36cb36c
implements cli application infrastructure
arturcic May 12, 2025
263ef09
Adds extensions namespace to code generator
arturcic May 12, 2025
ed9f37a
adds support for command options
arturcic May 12, 2025
33174aa
Qualifies commands module
arturcic May 12, 2025
8b99fde
adds LoggingEnricher and updates IoC and tests
arturcic Jul 8, 2025
47f00e1
Refactors `RegisterTypes` method to return `IServiceCollection`
arturcic Jul 8, 2025
144a291
Updates `GitVersionApp` to implement `ICliApp`
arturcic Jul 8, 2025
95ec97d
separates format and build workflows
arturcic Jul 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
root = true
# http://editorconfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
Expand All @@ -10,6 +10,12 @@ end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

# Microsoft .NET properties
dotnet_style_qualification_for_event = false:none
dotnet_style_qualification_for_field = false:none
dotnet_style_qualification_for_method = false:none
dotnet_style_qualification_for_property = false:none

[*.yml]
indent_size = 2

Expand Down Expand Up @@ -156,18 +162,18 @@ resharper_csharp_align_multiline_parameter = true
resharper_csharp_instance_members_qualify_members = field

# IDE0005: Using directive is unnecessary.
dotnet_diagnostic.IDE0005.severity = warning
dotnet_diagnostic.ide0005.severity = warning

# RCS1037: Remove trailing white-space.
dotnet_diagnostic.RCS1037.severity = error
dotnet_diagnostic.rcs1037.severity = error

# RCS1036: Remove redundant empty line.
dotnet_diagnostic.RCS1036.severity = error
dotnet_diagnostic.rcs1036.severity = error

xml_space_before_self_closing = true

resharper_arrange_object_creation_when_type_not_evident_highlighting = none

resharper_unused_auto_property_accessor_global_highlighting = none

resharper_unused_method_return_value_global_highlighting = none
resharper_unused_method_return_value_global_highlighting = none
6 changes: 0 additions & 6 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ jobs:
-
name: Run Format 'ci' solution
run: dotnet format ./build/ --verify-no-changes
-
name: Build 'new-cli' solution
run: dotnet build ./new-cli
-
name: Run Format 'new-cli' solution
run: dotnet format ./new-cli/ --exclude ~/.nuget/packages --verify-no-changes
-
name: Run Format 'GitVersion' solution
run: dotnet format ./src/ --exclude **/AddFormats/ --verify-no-changes
56 changes: 56 additions & 0 deletions .github/workflows/new-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build (new-cli)
on:
push:
branches:
- main
- 'fix/*'
- 'feature/*'
- 'poc/*'
- 'support/*'
paths:
- '**'
- '!docs/**'
- '!.github/**'
- .github/workflows/new-cli.yml

pull_request:
branches:
- main
- 'support/*'
paths:
- '**'
- '!docs/**'
- '!.github/**'
- .github/workflows/new-cli.yml

permissions:
contents: read

env:
DOTNET_ROLL_FORWARD: "Major"
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: 1

jobs:
format:
runs-on: ubuntu-24.04
name: Build & Test (new-cli)
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
-
name: Build 'new-cli' solution
run: dotnet build ./new-cli
-
name: Run Format 'new-cli' solution
run: dotnet format ./new-cli --exclude ~/.nuget/packages --verify-no-changes
-
name: Test 'new-cli' solution
run: dotnet test ./new-cli --no-build --verbosity normal
1 change: 1 addition & 0 deletions build/CI.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<File Path="../.github/workflows/format.yml" />
<File Path="../.github/workflows/homebrew.yml" />
<File Path="../.github/workflows/mkdocs.yml" />
<File Path="../.github/workflows/new-cli.yml" />
<File Path="../.github/workflows/release.yml" />
<File Path="../.github/workflows/stale.yml" />
<File Path="../.github/workflows/winget.yml" />
Expand Down
25 changes: 25 additions & 0 deletions new-cli/.run/TestCommand.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestCommand" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.Cli/bin/Debug/net9.0/gitversion" />
<option name="PROGRAM_PARAMETERS" value="test -i test.txt" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="ENV_FILE_PATHS" value="" />
<option name="REDIRECT_INPUT_PATH" value="" />
<option name="PTY_MODE" value="Auto" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="AUTO_ATTACH_CHILDREN" value="0" />
<option name="MIXED_MODE_DEBUG" value="0" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/GitVersion.Cli/GitVersion.Cli.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net9.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
14 changes: 12 additions & 2 deletions new-cli/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
<PackageVersion Include="LibGit2Sharp" Version="0.31.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="4.14.0" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.7" />
<PackageVersion Include="Polly" Version="8.6.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.7" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageVersion Include="NUnit" Version="4.3.2" />
<PackageVersion Include="NUnit.Analyzers" Version="4.9.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageVersion Include="Polly" Version="8.6.1" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.13.1" />
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.13.1" />
<!-- specific packages -->
Expand All @@ -24,4 +34,4 @@
<PackageVersion Include="System.IO.Abstractions" Version="22.0.15" />
<PackageVersion Include="System.Text.Json" Version="9.0.7" />
</ItemGroup>
</Project>
</Project>
20 changes: 10 additions & 10 deletions new-cli/GitVersion.Calculation/CalculateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Infrastructure;
using Microsoft.Extensions.Logging;

namespace GitVersion.Commands;

public record CalculateSettings : GitVersionSettings;

[Command("calculate", "Calculates the version object from the git history.")]
public class CalculateCommand(ILogger logger, IService service, IGitRepository repository) : ICommand<CalculateSettings>
public class CalculateCommand(ILogger<CalculateCommand> logger, IService service, IGitRepository repository) : ICommand<CalculateSettings>
{
private readonly ILogger logger = logger.NotNull();
private readonly IService service = service.NotNull();
private readonly IGitRepository repository = repository.NotNull();
private readonly ILogger _logger = logger.NotNull();
private readonly IService _service = service.NotNull();
private readonly IGitRepository _repository = repository.NotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we started prefixing fields with _?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure yet, I guess this used the configurations I had at my current work. That is still much a wip, so whenever it's at a more advanced stage we can get back to the conventions


public Task<int> InvokeAsync(CalculateSettings settings, CancellationToken cancellationToken = default)
{
var value = service.Call();
var value = _service.Call();
if (settings.WorkDir != null)
{
this.repository.DiscoverRepository(settings.WorkDir.FullName);
var branches = this.repository.Branches.ToList();
this.logger.LogInformation("Command : 'calculate', LogFile : '{logFile}', WorkDir : '{workDir}' ",
_repository.DiscoverRepository(settings.WorkDir.FullName);
var branches = _repository.Branches.ToList();
_logger.LogInformation("Command : 'calculate', LogFile : '{logFile}', WorkDir : '{workDir}' ",
settings.LogFile, settings.WorkDir);
this.logger.LogInformation("Found {count} branches", branches.Count);
_logger.LogInformation("Found {count} branches", branches.Count);
}

return Task.FromResult(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>GitVersion.Cli.Generator.Tests</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="System.CommandLine" />

<PackageReference Include="Microsoft.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GitVersion.Cli.Generator\GitVersion.Cli.Generator.csproj" />
<ProjectReference Include="..\GitVersion.Common.Command\GitVersion.Common.Command.csproj" />
<ProjectReference Include="..\GitVersion.Core\GitVersion.Core.csproj" />
</ItemGroup>

</Project>
Loading
Loading