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

Add tests for the cli Source Generator #4617

merged 25 commits into from
Jul 10, 2025

Conversation

arturcic
Copy link
Member

@arturcic arturcic commented Jul 9, 2025

This pull request introduces several changes across multiple files to improve code organization, enhance functionality, and update dependencies. Key changes include updates to .editorconfig for consistency, enhancements to the CLI generator and its tests, dependency updates in Directory.Packages.props, and refactoring of the CalculateCommand implementation.

Configuration Updates:

  • .editorconfig: Added Microsoft .NET-specific properties and corrected severity levels for diagnostics to lowercase for consistency. [1] [2] [3]

CLI Enhancements:

Dependency Updates:

Code Refactoring:

New Features:

arturcic added 23 commits July 9, 2025 18:36
Introduced a new test project, `GitVersion.Cli.Generator.Tests`, to support unit testing for `GitVersion.Cli.Generator`. Updated solution and package dependency files to include necessary references and dependencies. This ensures better test coverage and maintains consistency across projects.
updates dependency versions

updates Microsoft.Extensions.Logging.Abstractions to 9.0.6 and Polly to 8.6.1

this brings the packages up to date for compatibility and potentially includes bug fixes or performance improvements.
Renamed `CommandsImplModule` to `CommandsModule` for clarity and updated related references. Adjusted the accessibility of `GeneratedHeader` and added required namespaces. Enhanced syntax consistency and replaced array construction with shorthand for better readability.
…ommandImplGenerator with sample source code`

This commit introduces unit tests for the `CommandImplGenerator` to validate its output. The tests include a sample command source and verify the correctness of the generated implementations, such as `TestCommandImpl`, `CommandsModule`, and `RootCommandImpl`. It ensures compatibility with required dependencies and adheres to .NET 9.0 standards.
Renamed `CommandImplGenerator` to `SystemCommandlineGenerator` and introduced an abstract `CommandBaseGenerator` for better extensibility. Moved source generation logic to the new `SystemCommandlineGenerator` and updated related tests accordingly.
Adds a global usings file to the generated code.

This reduces the amount of code needed in the generated files,
making them more readable and maintainable.
Set the output-file option as required in tests to ensure proper parsing. Added nullable context enforcement and enabled implicit usings in source generator tests for better code safety and modern language compatibility.
Replaced the nullable annotation check with the IsRequired property on propertySymbol for determining if a field is required. This improves code clarity and aligns with more explicit APIs.
Moved namespace-related constants from multiple files into the `Content` class for better centralization and consistency. Updated references across the project to use the newly centralized constants, simplifying maintainability. Added `InternalsVisibleTo` in the project file to support unit tests.
Introduce `CliModule` for dependency registration, implementing an `IGitVersionApp` interface to decouple app logic. Refactor `Program.cs` to use the new interface and modularize service initialization, improving maintainability and scalability.
Relocates the CliModule to improve project structure and manage dependencies.
It also adds logging at the service registration level.
implements a new test command with an input file option.
creates a run configuration for the test command in the IDE.
Migrates from a custom `IContainerRegistrar` and `IContainer` to the standard `IServiceCollection` and `ServiceProvider` from Microsoft.Extensions.DependencyInjection.
This simplifies dependency injection and improves compatibility with the .NET ecosystem.
Migrates to Microsoft.Extensions.Logging for logging abstraction.

This change removes the custom logging infrastructure and replaces it with the standard Microsoft logging. This provides better integration with other .NET libraries and frameworks and allows for more flexible configuration and management of logging.
Refactors constructors to use readonly backing fields for injected dependencies.

Adds .editorconfig settings for .NET code style and updates existing settings.
Changes ILogger injection to ILogger<T> in command classes.
This aligns with best practices for dependency injection and improves logging context.
Ensures correct command registration in the IoC container by including the namespace when the command type namespace differs from the main command namespace.
Also, adjust the tests to reflect this change.
Renames the `Content` class to `SystemCommandlineContent` for clarity and consistency.

This change improves code organization and readability by providing a more descriptive name for the class that contains content related to system command line generation.
Introduces a CLI application infrastructure, including the implementation of `ICliApp` and related dependencies.
This provides a structured approach for running CLI commands.

Additionally, the command and root command implementations are adjusted to accommodate the new infrastructure.
Adds the extensions namespace to the code generator, ensuring it is correctly included in generated files.

This change ensures consistency and proper referencing of extension methods within the generated code.
Extends the command-line interface generator to support additional options for commands.

This enhancement allows specifying options with their descriptions, aliases, and required status, improving flexibility.
Qualifies the commands module using the extension namespace.

This ensures the code generator correctly references
extension methods within the generated files.
Integrates `LoggingEnricher` for enhanced logging configuration and ensures its proper registration in IoC. Updates tests and code generation to support this change.
Updates the `RegisterTypes` method signature across `IGitVersionModule` implementations and usage. This simplifies service registration by enabling method chaining in IoC configuration.
@arturcic arturcic changed the title Feature/new cli tests Add tests for the cli Source Generator Jul 9, 2025
Refactors `GitVersionApp` to implement `ICliApp` and updates dependency injection to register `GitVersionApp` as `ICliApp`. Adjusts `RunAsync` to include  `CancellationToken`, ensuring proper cancellation support.
@arturcic arturcic force-pushed the feature/new-cli-tests branch 2 times, most recently from 01bfcd4 to 99fc7de Compare July 10, 2025 08:55
Splits the format workflow into separate format and build workflows for the new CLI project.
This improves the workflow organization and allows for more targeted builds and tests.
@arturcic arturcic force-pushed the feature/new-cli-tests branch from 99fc7de to 95ec97d Compare July 10, 2025 09:45
@arturcic arturcic marked this pull request as ready for review July 10, 2025 10:20
@arturcic arturcic merged commit c729f29 into main Jul 10, 2025
271 of 274 checks passed
@arturcic arturcic deleted the feature/new-cli-tests branch July 10, 2025 10:21
Copy link
Contributor

mergify bot commented Jul 10, 2025

Thank you @arturcic for your contribution!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants