From f57510ed12c04117825c0f2a6add89432b6bd3eb Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 01:39:01 +0000 Subject: [PATCH] breaking: move the GitVersion providers to GitVersion 7 Pin GitVersion.Tool to 7.0.0 and update the providers for its breaking changes: - Run `dotnet gitversion --output json`; v7 no longer accepts the `/output json` syntax. - Read the pre-release from `PreReleaseLabel`; v7 renamed `PreReleaseTag` to `PreReleaseLabel`, and the old `PreReleaseLabel` (the label without the number) to `PreReleaseLabelName`. - Include the tool version in the GitVersion output cache key, so output cached by GitVersion 6 (where `PreReleaseLabel` means something else) is never reused. Migrate this repository's GitVersion.yml to the v7 `calculation:` / `output:` layout and the renamed `{PreReleaseLabelWithDash}` variable. The resulting versions are unchanged for main, patch, develop, feature, pull request, other and tagged builds. Add Invex.Atom.Module.GitVersion.Tests covering the pinned version, the v7 arguments and variables, and cache invalidation. BREAKING CHANGE: builds using IGitVersion now run GitVersion 7, which rejects the GitVersion 6 configuration layout. Migrate GitVersion.yml with `dotnet gitversion config migrate --in-place` and rename `PreReleaseTag*` variables in format strings to `PreReleaseLabel*`. Closes #412 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NmBdNrhc5dVcJV7JXypNEJ --- .devops/workflows/Test_Devops_Build.yml | 6 + .github/workflows/Build.yml | 78 ++++++ GitVersion.yml | 237 +++++++++--------- Invex.Atom.slnx | 1 + _atom/Targets/ITestTargets.cs | 1 + docs/modules/git-version.md | 9 +- .../Invex.Atom.Module.GitVersion.csproj | 4 + .../Providers/GitVersionBuildIdProvider.cs | 2 +- .../GitVersionBuildVersionProvider.cs | 13 +- .../Providers/GitVersionCache.cs | 30 ++- .../Providers/GitVersionTool.cs | 7 +- .../GitVersionProviderTests.cs | 177 +++++++++++++ .../Invex.Atom.Module.GitVersion.Tests.csproj | 55 ++++ .../_usings.cs | 19 ++ 14 files changed, 505 insertions(+), 134 deletions(-) create mode 100644 tests/Invex.Atom.Module.GitVersion.Tests/GitVersionProviderTests.cs create mode 100644 tests/Invex.Atom.Module.GitVersion.Tests/Invex.Atom.Module.GitVersion.Tests.csproj create mode 100644 tests/Invex.Atom.Module.GitVersion.Tests/_usings.cs diff --git a/.devops/workflows/Test_Devops_Build.yml b/.devops/workflows/Test_Devops_Build.yml index 3a83c02d..44555d5e 100644 --- a/.devops/workflows/Test_Devops_Build.yml +++ b/.devops/workflows/Test_Devops_Build.yml @@ -251,6 +251,12 @@ jobs: artifactName: Invex.Atom.Module.GitHubWorkflows.Tests-$(job-runs-on)-$(test-framework) targetPath: $(Build.BinariesDirectory)/Invex.Atom.Module.GitHubWorkflows.Tests + - task: PublishPipelineArtifact@1 + displayName: Invex.Atom.Module.GitVersion.Tests + inputs: + artifactName: Invex.Atom.Module.GitVersion.Tests-$(job-runs-on)-$(test-framework) + targetPath: $(Build.BinariesDirectory)/Invex.Atom.Module.GitVersion.Tests + - task: PublishPipelineArtifact@1 displayName: Invex.Atom.Workflows.Tests inputs: diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index b3f65fb4..99e0fe1f 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -241,6 +241,12 @@ jobs: name: 'Invex.Atom.Module.GitHubWorkflows.Tests-${{ matrix.job-runs-on }}-${{ matrix.test-framework }}' path: '${{ github.workspace }}/.github/publish/Invex.Atom.Module.GitHubWorkflows.Tests' + - name: Store Invex.Atom.Module.GitVersion.Tests + uses: actions/upload-artifact@v7 + with: + name: 'Invex.Atom.Module.GitVersion.Tests-${{ matrix.job-runs-on }}-${{ matrix.test-framework }}' + path: '${{ github.workspace }}/.github/publish/Invex.Atom.Module.GitVersion.Tests' + - name: Store Invex.Atom.Workflows.Tests uses: actions/upload-artifact@v7 with: @@ -1112,6 +1118,78 @@ jobs: name: Invex.Atom.Module.GitHubWorkflows.Tests-macos-latest-net11.0 path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitHubWorkflows.Tests' + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-windows-latest-net10.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-windows-latest-net11.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-windows-11-arm-net10.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-windows-11-arm-net11.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-ubuntu-latest-net10.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-ubuntu-latest-net11.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-ubuntu-24.04-arm-net10.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-ubuntu-24.04-arm-net11.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-macos-15-intel-net10.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-macos-15-intel-net11.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-macos-latest-net10.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + + - name: Retrieve Invex.Atom.Module.GitVersion.Tests + uses: actions/download-artifact@v8 + with: + name: Invex.Atom.Module.GitVersion.Tests-macos-latest-net11.0 + path: '${{ github.workspace }}/.github/artifacts/Invex.Atom.Module.GitVersion.Tests' + - name: Retrieve Invex.Atom.Workflows.Tests uses: actions/download-artifact@v8 with: diff --git a/GitVersion.yml b/GitVersion.yml index 2b776700..753c1d6d 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1,113 +1,124 @@ -assembly-informational-format: '{Major}.{Minor}.{Patch}{PreReleaseTagWithDash}' -assembly-file-versioning-format: '{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber ?? 0}' -mode: ContinuousDelivery -tag-prefix: '[vV]?' - -major-version-bump-message: '^(breaking|major)\(?[A-Za-z0-9\-]*\)?:' -minor-version-bump-message: '^(feat|feature|minor)\(?[A-Za-z0-9\-]*\)?:' -patch-version-bump-message: '^(fix|patch)\(?[A-Za-z0-9\-]*\)?:' -no-bump-message: '^(semver-none|semver-skip)' - -# Tagged releases get the highest band -tag-pre-release-weight: 6000 - -branches: - - # Prevent default GitFlow configs from matching - feature: - regex: ^$ - release: - regex: ^$ - hotfix: - regex: ^$ - support: - regex: ^$ - - ##################################### - # RELEASE BRANCHES - ##################################### - - patch: - label: 'rc' - increment: Patch - prevent-increment: - of-merged-branch: true - when-current-commit-tagged: true - track-merge-target: false - track-merge-message: true - regex: ^patch[/-] - is-release-branch: true - is-main-branch: false - pre-release-weight: 5000 # 5,000–5,999 - - main: - label: 'rc' - increment: Minor - prevent-increment: - of-merged-branch: true - track-merge-target: false - track-merge-message: true - regex: ^master$|^main$ - is-release-branch: false - is-main-branch: true - pre-release-weight: 4000 # 4,000–4,999 - - ##################################### - # MAINLINE BRANCHES - ##################################### - - develop: - label: 'preview' - increment: Minor - prevent-increment: - when-current-commit-tagged: false - track-merge-target: true - track-merge-message: true - regex: ^dev(elop)?(ment)?$ - is-release-branch: false - is-main-branch: false - pre-release-weight: 3000 # 3,000–3,999 - - ##################################### - # FEATURE BRANCHES - ##################################### - - beta-minor: - label: beta.{BranchType}-{BranchName} - increment: Minor - prevent-increment: - when-current-commit-tagged: false - track-merge-target: true - track-merge-message: true - regex: ^(?chore|feat|feature|features|fix|improvement|refactor)[/-](?.+) - is-release-branch: false - is-main-branch: false - pre-release-weight: 2000 # 2,000–2,999 - - ##################################### - # OTHER - ##################################### - - alpha: - label: alpha.{BranchName} - increment: None - prevent-increment: - when-current-commit-tagged: false - track-merge-target: true - track-merge-message: true - regex: (?.+) - is-release-branch: false - is-main-branch: false - pre-release-weight: 1000 # 1,000–1,999 - - pull-request: - label: 'pr' - increment: Inherit - prevent-increment: - of-merged-branch: true - when-current-commit-tagged: false - track-merge-message: true - regex: ^(pull|pull\-requests|pr)[/-] - is-release-branch: false - is-main-branch: false - pre-release-weight: 0 # 0–999 +calculation: + mode: ContinuousDelivery + tag-prefix: '[vV]?' + + major-version-bump-message: '^(breaking|major)\(?[A-Za-z0-9\-]*\)?:' + minor-version-bump-message: '^(feat|feature|minor)\(?[A-Za-z0-9\-]*\)?:' + patch-version-bump-message: '^(fix|patch)\(?[A-Za-z0-9\-]*\)?:' + no-bump-message: '^(semver-none|semver-skip)' + + branches: + + # Prevent default GitFlow configs from matching + feature: + regex: ^$ + release: + regex: ^$ + hotfix: + regex: ^$ + support: + regex: ^$ + + ##################################### + # RELEASE BRANCHES + ##################################### + + patch: + label: 'rc' + increment: Patch + prevent-increment: + of-merged-branch: true + when-current-commit-tagged: true + track-merge-target: false + track-merge-message: true + regex: ^patch[/-] + is-release-branch: true + is-main-branch: false + + main: + label: 'rc' + increment: Minor + prevent-increment: + of-merged-branch: true + track-merge-target: false + track-merge-message: true + regex: ^master$|^main$ + is-release-branch: false + is-main-branch: true + + ##################################### + # MAINLINE BRANCHES + ##################################### + + develop: + label: 'preview' + increment: Minor + prevent-increment: + when-current-commit-tagged: false + track-merge-target: true + track-merge-message: true + regex: ^dev(elop)?(ment)?$ + is-release-branch: false + is-main-branch: false + + ##################################### + # FEATURE BRANCHES + ##################################### + + beta-minor: + label: beta.{BranchType}-{BranchName} + increment: Minor + prevent-increment: + when-current-commit-tagged: false + track-merge-target: true + track-merge-message: true + regex: ^(?chore|feat|feature|features|fix|improvement|refactor)[/-](?.+) + is-release-branch: false + is-main-branch: false + + ##################################### + # OTHER + ##################################### + + alpha: + label: alpha.{BranchName} + increment: None + prevent-increment: + when-current-commit-tagged: false + track-merge-target: true + track-merge-message: true + regex: (?.+) + is-release-branch: false + is-main-branch: false + + pull-request: + label: 'pr' + increment: Inherit + prevent-increment: + of-merged-branch: true + when-current-commit-tagged: false + track-merge-message: true + regex: ^(pull|pull\-requests|pr)[/-] + is-release-branch: false + is-main-branch: false + +output: + assembly-informational-format: '{Major}.{Minor}.{Patch}{PreReleaseLabelWithDash}' + assembly-file-versioning-format: '{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber ?? 0}' + + # Tagged releases get the highest band + tag-pre-release-weight: 6000 + + branches: + patch: + pre-release-weight: 5000 # 5,000–5,999 + main: + pre-release-weight: 4000 # 4,000–4,999 + develop: + pre-release-weight: 3000 # 3,000–3,999 + beta-minor: + pre-release-weight: 2000 # 2,000–2,999 + alpha: + pre-release-weight: 1000 # 1,000–1,999 + pull-request: + pre-release-weight: 0 # 0–999 diff --git a/Invex.Atom.slnx b/Invex.Atom.slnx index acc69742..fa4487a4 100644 --- a/Invex.Atom.slnx +++ b/Invex.Atom.slnx @@ -37,6 +37,7 @@ + diff --git a/_atom/Targets/ITestTargets.cs b/_atom/Targets/ITestTargets.cs index 35743896..69d8f376 100644 --- a/_atom/Targets/ITestTargets.cs +++ b/_atom/Targets/ITestTargets.cs @@ -12,6 +12,7 @@ internal interface ITestTargets : IDotnetTestHelper Projects.Invex_Atom_Module_Dotnet_Tests.Name, Projects.Invex_Atom_Module_DevOpsWorkflows_Tests.Name, Projects.Invex_Atom_Module_GitHubWorkflows_Tests.Name, + Projects.Invex_Atom_Module_GitVersion_Tests.Name, Projects.Invex_Atom_Workflows_Tests.Name, Projects.Invex_Atom_Tool_Tests.Name, ]; diff --git a/docs/modules/git-version.md b/docs/modules/git-version.md index 2c63875a..f02431cf 100644 --- a/docs/modules/git-version.md +++ b/docs/modules/git-version.md @@ -47,13 +47,20 @@ from GitVersion output. ## Prerequisites -The providers install and run a pinned version of the `GitVersion.Tool` global .NET tool (currently `6.8.2`), so the +The providers install and run a pinned version of the `GitVersion.Tool` global .NET tool (currently `7.0.0`), so the build doesn't break when a new GitVersion major version changes its command-line arguments or output. If a different version is installed globally, it is replaced with the pinned version the first time GitVersion is needed. Configure it via `GitVersion.yml` in your repository root, using the configuration format of the pinned GitVersion version. +> [!IMPORTANT] +> GitVersion 7 nests settings under `calculation:` and `output:` and rejects the flat GitVersion 6 layout. It also +> renames the pre-release variables: `PreReleaseTag` is now `PreReleaseLabel`, and the old `PreReleaseLabel` is now +> `PreReleaseLabelName`. Migrate a GitVersion 6 configuration with `dotnet gitversion config migrate --in-place`, then +> update any format strings that still use the old names, such as `{PreReleaseTagWithDash}` (now +> `{PreReleaseLabelWithDash}`). The migration drops comments from the file. + ## See also - [Build Info](../core-concepts/build-info.md): the providers GitVersion replaces. diff --git a/src/Invex.Atom.Module.GitVersion/Invex.Atom.Module.GitVersion.csproj b/src/Invex.Atom.Module.GitVersion/Invex.Atom.Module.GitVersion.csproj index 18de83d2..e259d7b8 100644 --- a/src/Invex.Atom.Module.GitVersion/Invex.Atom.Module.GitVersion.csproj +++ b/src/Invex.Atom.Module.GitVersion/Invex.Atom.Module.GitVersion.csproj @@ -29,6 +29,10 @@ + + + + diff --git a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildIdProvider.cs b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildIdProvider.cs index e5e6fe60..bad69d8b 100644 --- a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildIdProvider.cs +++ b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildIdProvider.cs @@ -54,7 +54,7 @@ public string BuildId { dotnetToolInstallHelper.InstallTool(GitVersionTool.PackageId, GitVersionTool.Version); - var gitVersionResult = processRunner.Run(new("dotnet", "gitversion /output json") + var gitVersionResult = processRunner.Run(new("dotnet", GitVersionTool.OutputJsonArguments) { InvocationLogLevel = LogLevel.Debug, }); diff --git a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildVersionProvider.cs b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildVersionProvider.cs index 79fa6561..91cabb8e 100644 --- a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildVersionProvider.cs +++ b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionBuildVersionProvider.cs @@ -20,7 +20,8 @@ ILogger logger private readonly Lock _lock = new(); /// - /// Gets the semantic version of the build, derived from GitVersion's output. + /// Gets the semantic version of the build, derived from GitVersion's Major, Minor, Patch + /// and PreReleaseLabel output variables. /// /// /// Thrown if the version information cannot be determined from GitVersion's output. @@ -52,7 +53,7 @@ public SemVer Version { dotnetToolInstallHelper.InstallTool(GitVersionTool.PackageId, GitVersionTool.Version); - var gitVersionResult = processRunner.Run(new("dotnet", "gitversion /output json") + var gitVersionResult = processRunner.Run(new("dotnet", GitVersionTool.OutputJsonArguments) { InvocationLogLevel = LogLevel.Debug, }); @@ -78,13 +79,13 @@ public SemVer Version .GetProperty("Patch") .GetUInt32(); - var preReleaseTagProp = jsonOutput + var preReleaseLabelProp = jsonOutput .Value - .GetProperty("PreReleaseTag") + .GetProperty("PreReleaseLabel") .GetString()!; - return field = preReleaseTagProp is { Length: > 0 } - ? SemVer.Parse($"{majorProp}.{minorProp}.{patchProp}-{preReleaseTagProp}") + return field = preReleaseLabelProp is { Length: > 0 } + ? SemVer.Parse($"{majorProp}.{minorProp}.{patchProp}-{preReleaseLabelProp}") : SemVer.Parse($"{majorProp}.{minorProp}.{patchProp}"); } } diff --git a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionCache.cs b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionCache.cs index e3c6a1cd..a953d710 100644 --- a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionCache.cs +++ b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionCache.cs @@ -1,22 +1,24 @@ namespace Invex.Atom.Module.GitVersion.Providers; /// -/// Reads and writes a single-file cache of GitVersion output, keyed by the current Git commit hash. +/// Reads and writes a single-file cache of GitVersion output, keyed by the current Git commit hash and the +/// pinned . /// /// /// The cache is a single file in the build project's obj directory (so it is git-ignored and removed -/// by dotnet clean). The first line stores the Git hash the output was generated for, and the -/// remainder stores the raw GitVersion JSON. When the current hash no longer matches, the cache is ignored -/// and overwritten - just like the tool's restore/build caches, which each track a single hash file rather -/// than one file per key. +/// by dotnet clean). The first line stores the GitVersion tool version and Git hash the output was +/// generated for, and the remainder stores the raw GitVersion JSON. When either no longer matches, the cache +/// is ignored and overwritten - just like the tool's restore/build caches, which each track a single hash file +/// rather than one file per key. The tool version is part of the key because GitVersion's output variables +/// can change meaning between versions, so output cached by another version must never be reused. /// internal static class GitVersionCache { private const string CacheFileName = ".gitversioncache"; /// - /// Returns the cached GitVersion output if it was generated for ; otherwise - /// null. + /// Returns the cached GitVersion output if it was generated for by the pinned + /// ; otherwise null. /// internal static JsonElement? TryRead(IRootedFileSystem fileSystem, string gitHash, ILogger logger) { @@ -33,11 +35,11 @@ internal static class GitVersionCache if (newlineIndex < 0) return null; - var cachedHash = content[..newlineIndex] + var cachedKey = content[..newlineIndex] .Trim(); - // The cached output belongs to a different commit - treat it as a miss. - if (!string.Equals(cachedHash, gitHash, StringComparison.OrdinalIgnoreCase)) + // The cached output belongs to a different commit or GitVersion version - treat it as a miss. + if (!string.Equals(cachedKey, GetCacheKey(gitHash), StringComparison.OrdinalIgnoreCase)) return null; return JsonSerializer.Deserialize(content[(newlineIndex + 1)..], JsonElementContext.Default.JsonElement); @@ -60,14 +62,18 @@ internal static class GitVersionCache } /// - /// Persists the GitVersion output for , overwriting any previous entry. + /// Persists the GitVersion output for and the pinned + /// , overwriting any previous entry. /// internal static void Write(IRootedFileSystem fileSystem, string gitHash, JsonElement output) { fileSystem.Directory.CreateDirectory(ResolveObjDirectory(fileSystem)); - fileSystem.File.WriteAllText(GetCacheFile(fileSystem), $"{gitHash}\n{output.GetRawText()}"); + fileSystem.File.WriteAllText(GetCacheFile(fileSystem), $"{GetCacheKey(gitHash)}\n{output.GetRawText()}"); } + private static string GetCacheKey(string gitHash) => + $"{GitVersionTool.Version}:{gitHash}"; + private static RootedPath GetCacheFile(IRootedFileSystem fileSystem) => ResolveObjDirectory(fileSystem) / CacheFileName; diff --git a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionTool.cs b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionTool.cs index f06b5f7b..ebac1abc 100644 --- a/src/Invex.Atom.Module.GitVersion/Providers/GitVersionTool.cs +++ b/src/Invex.Atom.Module.GitVersion/Providers/GitVersionTool.cs @@ -18,5 +18,10 @@ internal static class GitVersionTool /// /// The exact version of that the providers install and run. /// - internal const string Version = "6.8.2"; + internal const string Version = "7.0.0"; + + /// + /// The arguments passed to dotnet to run GitVersion and write its variables to standard output as JSON. + /// + internal const string OutputJsonArguments = "gitversion --output json"; } diff --git a/tests/Invex.Atom.Module.GitVersion.Tests/GitVersionProviderTests.cs b/tests/Invex.Atom.Module.GitVersion.Tests/GitVersionProviderTests.cs new file mode 100644 index 00000000..3ba92674 --- /dev/null +++ b/tests/Invex.Atom.Module.GitVersion.Tests/GitVersionProviderTests.cs @@ -0,0 +1,177 @@ +namespace Invex.Atom.Module.GitVersion.Tests; + +[TestFixture] +internal sealed class GitVersionProviderTests +{ + [SetUp] + public void SetUp() + { + _host = CreateTestHost(); + _fileSystem = _host.Services.GetRequiredService(); + _toolInstallHelper = A.Fake(); + _processRunner = A.Fake(); + _buildOptionService = A.Fake(); + _gitVersionRuns = []; + + A + .CallTo(() => _buildOptionService.Options) + .Returns([ + new GitVersionProvideBuildIdOption + { + Enabled = true, + }, + new GitVersionProvideBuildVersionOption + { + Enabled = true, + }, + ]); + + A + .CallTo(() => _processRunner.Run(A._)) + .ReturnsLazily((ProcessRunOptions options) => + { + if (options.Name is "git") + return new(options, 0, GitHash, string.Empty); + + _gitVersionRuns.Add(options); + + return new(options, 0, V7Output, string.Empty); + }); + } + + [TearDown] + public void TearDown() => + _host.Dispose(); + + private const string GitHash = "0123456789abcdef0123456789abcdef01234567"; + + private const string V7Output = """ + { + "Major": 1, + "Minor": 2, + "Patch": 3, + "FullSemVer": "1.2.3-beta.feat-thing.4", + "PreReleaseLabel": "beta.feat-thing.4", + "PreReleaseLabelName": "beta.feat-thing", + "PreReleaseNumber": 4 + } + """; + + private IHost _host = null!; + private IRootedFileSystem _fileSystem = null!; + private IDotnetToolInstallHelper _toolInstallHelper = null!; + private IProcessRunner _processRunner = null!; + private IBuildOptionService _buildOptionService = null!; + private List _gitVersionRuns = null!; + + [Test] + public void Version_UsesGitVersion7PreReleaseLabel() + { + // Act + var version = CreateVersionProvider() + .Version; + + // Assert + version.ShouldBe(SemVer.Parse("1.2.3-beta.feat-thing.4")); + } + + [Test] + public void BuildId_UsesFullSemVer() + { + // Act + var buildId = CreateBuildIdProvider() + .BuildId; + + // Assert + buildId.ShouldBe("1.2.3-beta.feat-thing.4"); + } + + [Test] + public void Version_InstallsPinnedGitVersionTool_AndRunsItWithGitVersion7Arguments() + { + // Act + _ = CreateVersionProvider() + .Version; + + // Assert + A + .CallTo(() => _toolInstallHelper.InstallTool("GitVersion.Tool", "7.0.0", A._, A._)) + .MustHaveHappenedOnceExactly(); + + var run = _gitVersionRuns.ShouldHaveSingleItem(); + run.Name.ShouldBe("dotnet"); + + string + .Join(' ', run.Args) + .ShouldBe("gitversion --output json"); + } + + [Test] + public void Version_ReusesOutputCachedForTheSameCommitAndToolVersion() + { + // Arrange + _ = CreateBuildIdProvider() + .BuildId; + + // Act + var version = CreateVersionProvider() + .Version; + + // Assert + version.ShouldBe(SemVer.Parse("1.2.3-beta.feat-thing.4")); + _gitVersionRuns.Count.ShouldBe(1); + } + + [Test] + public void Version_IgnoresOutputCachedWithoutTheToolVersion() + { + // Arrange - a cache entry in the pre-7 format, keyed by the Git hash alone, holding GitVersion 6 output + // in which 'PreReleaseLabel' is the label without the pre-release number. + var objDirectory = _fileSystem.AtomRootDirectory / "obj"; + _fileSystem.Directory.CreateDirectory(objDirectory); + + _fileSystem.File.WriteAllText(objDirectory / ".gitversioncache", + $$""" + {{GitHash}} + { "Major": 1, "Minor": 2, "Patch": 3, "FullSemVer": "1.2.3-beta.feat-thing.4", "PreReleaseTag": "beta.feat-thing.4", "PreReleaseLabel": "beta.feat-thing" } + """); + + // Act + var version = CreateVersionProvider() + .Version; + + // Assert + version.ShouldBe(SemVer.Parse("1.2.3-beta.feat-thing.4")); + _gitVersionRuns.Count.ShouldBe(1); + } + + private GitVersionBuildVersionProvider CreateVersionProvider() => + new(_toolInstallHelper, + _processRunner, + _buildOptionService, + _fileSystem, + NullLogger.Instance); + + private GitVersionBuildIdProvider CreateBuildIdProvider() => + new(_toolInstallHelper, + _processRunner, + _buildOptionService, + _fileSystem, + NullLogger.Instance); + + [UsedImplicitly] + private sealed class TestBuildDefinition(IServiceProvider services) : BuildDefinition(services), IConfigureHost + { + public override IReadOnlyDictionary TargetDefinitions => new Dictionary(); + + public override IReadOnlyDictionary ParamDefinitions => + new Dictionary(); + + public static void ConfigureHostBuilder(IHostApplicationBuilder builder) { } + + public static void ConfigureHost(IHost host) { } + + public override object AccessParam(string paramName) => + throw new NotImplementedException(); + } +} diff --git a/tests/Invex.Atom.Module.GitVersion.Tests/Invex.Atom.Module.GitVersion.Tests.csproj b/tests/Invex.Atom.Module.GitVersion.Tests/Invex.Atom.Module.GitVersion.Tests.csproj new file mode 100644 index 00000000..43caa6f8 --- /dev/null +++ b/tests/Invex.Atom.Module.GitVersion.Tests/Invex.Atom.Module.GitVersion.Tests.csproj @@ -0,0 +1,55 @@ + + + + $(NoWarn);CS1591; + net10.0;net11.0 + false + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/tests/Invex.Atom.Module.GitVersion.Tests/_usings.cs b/tests/Invex.Atom.Module.GitVersion.Tests/_usings.cs new file mode 100644 index 00000000..f395c783 --- /dev/null +++ b/tests/Invex.Atom.Module.GitVersion.Tests/_usings.cs @@ -0,0 +1,19 @@ +global using FakeItEasy; +global using Invex.Atom.Build.BuildOptions; +global using Invex.Atom.Build.Definition; +global using Invex.Atom.Build.FileSystem; +global using Invex.Atom.Build.Hosting; +global using Invex.Atom.Build.Params; +global using Invex.Atom.Module.Dotnet.Helpers; +global using Invex.Atom.Module.GitVersion.Options; +global using Invex.Atom.Module.GitVersion.Providers; +global using Invex.FileSystem; +global using Invex.Process; +global using Invex.SemanticVersion; +global using JetBrains.Annotations; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Hosting; +global using Microsoft.Extensions.Logging.Abstractions; +global using NUnit.Framework; +global using Shouldly; +global using static Invex.Atom.TestUtils.TestUtils;