Skip to content

Commit bcd25ed

Browse files
committed
Refactor variable serialization and extend serialization tests
Refactored how GitVersion handles variable serialization by introducing the IVersionVariableSerializer interface. Also, extended the corresponding unit tests to validate this refactoring. This helps in improving code modularity and facilitates easier testing and maintenance.
1 parent 848c4ed commit bcd25ed

File tree

19 files changed

+143
-91
lines changed

19 files changed

+143
-91
lines changed

src/GitVersion.App.Tests/GitVersion.App.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestStream.cs" Link="Helpers\TestStream.cs" />
2626
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestBase.cs" Link="Helpers\TestBase.cs" />
2727
<Compile Include="..\GitVersion.Core.Tests\Helpers\GitVersionCoreTestModule.cs" Link="Helpers\GitVersionCoreTestModule.cs" />
28+
<Compile Include="..\GitVersion.Core.Tests\Extensions\GitVersionVariablesExtensions.cs" Link="Extensions\GitVersionVariablesExtensions.cs" />
2829
</ItemGroup>
2930

3031
</Project>

src/GitVersion.App.Tests/Helpers/ExecutionResults.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Core.Tests;
12
using GitVersion.OutputVariables;
23

34
namespace GitVersion.App.Tests;
@@ -23,7 +24,7 @@ public virtual GitVersionVariables OutputVariables
2324
var jsonEndIndex = Output.IndexOf('}');
2425
var json = Output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);
2526

26-
return VersionVariablesHelper.FromJson(json);
27+
return json.ToGitVersionVariables();
2728
}
2829
}
2930
}

src/GitVersion.App.Tests/Helpers/ProgramFixture.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Core.Tests;
12
using GitVersion.Core.Tests.Helpers;
23
using GitVersion.Extensions;
34
using GitVersion.Logging;
@@ -86,11 +87,11 @@ public GitVersionVariables? OutputVariables
8687
{
8788
if (Output.IsNullOrWhiteSpace()) return null;
8889

89-
var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal);
90-
var jsonEndIndex = Output.IndexOf("}", StringComparison.Ordinal);
90+
var jsonStartIndex = Output.IndexOf('{');
91+
var jsonEndIndex = Output.IndexOf('}');
9192
var json = Output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);
9293

93-
return VersionVariablesHelper.FromJson(json);
94+
return json.ToGitVersionVariables();
9495
}
9596
}
9697
}

src/GitVersion.App.Tests/JsonOutputOnBuildServerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using GitVersion.Agents;
2+
using GitVersion.Core.Tests;
23
using GitVersion.Helpers;
3-
using GitVersion.OutputVariables;
44

55
namespace GitVersion.App.Tests;
66

@@ -61,7 +61,7 @@ public void BeingOnBuildServerWithOutputJsonAndOutputFileDoesNotFail(string outp
6161
var filePath = PathHelper.Combine(fixture.LocalRepositoryFixture.RepositoryPath, fileName);
6262
var json = File.ReadAllText(filePath);
6363

64-
var outputVariables = VersionVariablesHelper.FromJson(json);
64+
var outputVariables = json.ToGitVersionVariables();
6565
outputVariables.ShouldNotBeNull();
6666
outputVariables.FullSemVer.ShouldBeEquivalentTo(expectedVersion);
6767
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using GitVersion.OutputVariables;
2+
3+
namespace GitVersion.Core.Tests;
4+
5+
public static class GitVersionVariablesExtensions
6+
{
7+
public static string ToJson(this GitVersionVariables gitVersionVariables)
8+
{
9+
var serializer = new VersionVariableSerializer(new FileSystem());
10+
return serializer.ToJson(gitVersionVariables);
11+
}
12+
13+
public static GitVersionVariables ToGitVersionVariables(this string json)
14+
{
15+
var serializer = new VersionVariableSerializer(new FileSystem());
16+
return serializer.FromJson(json);
17+
}
18+
}

src/GitVersion.Core.Tests/VersionCalculation/JsonVersionBuilderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using GitVersion.Core.Tests.Helpers;
2-
using GitVersion.OutputVariables;
32
using GitVersion.VersionCalculation;
43
using Microsoft.Extensions.DependencyInjection;
54

src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using GitVersion.Configuration;
22
using GitVersion.Core.Tests.Helpers;
33
using GitVersion.Logging;
4-
using GitVersion.OutputVariables;
54
using GitVersion.VersionCalculation;
65
using Microsoft.Extensions.DependencyInjection;
76

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GitVersion.OutputVariables;
2+
3+
public interface IVersionVariableSerializer
4+
{
5+
GitVersionVariables FromJson(string json);
6+
string ToJson(GitVersionVariables gitVersionVariables);
7+
GitVersionVariables FromFile(string filePath);
8+
void ToFile(GitVersionVariables gitVersionVariables, string filePath);
9+
}

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -492,61 +492,11 @@ GitVersion.OutputVariables.GitVersionVariables.VersionSourceSha.get -> string?
492492
GitVersion.OutputVariables.GitVersionVariables.VersionSourceSha.init -> void
493493
GitVersion.OutputVariables.GitVersionVariables.WeightedPreReleaseNumber.get -> string!
494494
GitVersion.OutputVariables.GitVersionVariables.WeightedPreReleaseNumber.init -> void
495-
GitVersion.OutputVariables.VersionVariablesHelper
496-
GitVersion.OutputVariables.VersionVariablesJsonModel
497-
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemFileVer.get -> string?
498-
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemFileVer.set -> void
499-
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemVer.get -> string?
500-
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemVer.set -> void
501-
GitVersion.OutputVariables.VersionVariablesJsonModel.BranchName.get -> string?
502-
GitVersion.OutputVariables.VersionVariablesJsonModel.BranchName.set -> void
503-
GitVersion.OutputVariables.VersionVariablesJsonModel.BuildMetaData.get -> int?
504-
GitVersion.OutputVariables.VersionVariablesJsonModel.BuildMetaData.set -> void
505-
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitDate.get -> string?
506-
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitDate.set -> void
507-
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitsSinceVersionSource.get -> int?
508-
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitsSinceVersionSource.set -> void
509-
GitVersion.OutputVariables.VersionVariablesJsonModel.EscapedBranchName.get -> string?
510-
GitVersion.OutputVariables.VersionVariablesJsonModel.EscapedBranchName.set -> void
511-
GitVersion.OutputVariables.VersionVariablesJsonModel.FullBuildMetaData.get -> string?
512-
GitVersion.OutputVariables.VersionVariablesJsonModel.FullBuildMetaData.set -> void
513-
GitVersion.OutputVariables.VersionVariablesJsonModel.FullSemVer.get -> string?
514-
GitVersion.OutputVariables.VersionVariablesJsonModel.FullSemVer.set -> void
515-
GitVersion.OutputVariables.VersionVariablesJsonModel.InformationalVersion.get -> string?
516-
GitVersion.OutputVariables.VersionVariablesJsonModel.InformationalVersion.set -> void
517-
GitVersion.OutputVariables.VersionVariablesJsonModel.Major.get -> int?
518-
GitVersion.OutputVariables.VersionVariablesJsonModel.Major.set -> void
519-
GitVersion.OutputVariables.VersionVariablesJsonModel.MajorMinorPatch.get -> string?
520-
GitVersion.OutputVariables.VersionVariablesJsonModel.MajorMinorPatch.set -> void
521-
GitVersion.OutputVariables.VersionVariablesJsonModel.Minor.get -> int?
522-
GitVersion.OutputVariables.VersionVariablesJsonModel.Minor.set -> void
523-
GitVersion.OutputVariables.VersionVariablesJsonModel.Patch.get -> int?
524-
GitVersion.OutputVariables.VersionVariablesJsonModel.Patch.set -> void
525-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabel.get -> string?
526-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabel.set -> void
527-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabelWithDash.get -> string?
528-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabelWithDash.set -> void
529-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseNumber.get -> int?
530-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseNumber.set -> void
531-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTag.get -> string?
532-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTag.set -> void
533-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTagWithDash.get -> string?
534-
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTagWithDash.set -> void
535-
GitVersion.OutputVariables.VersionVariablesJsonModel.SemVer.get -> string?
536-
GitVersion.OutputVariables.VersionVariablesJsonModel.SemVer.set -> void
537-
GitVersion.OutputVariables.VersionVariablesJsonModel.Sha.get -> string?
538-
GitVersion.OutputVariables.VersionVariablesJsonModel.Sha.set -> void
539-
GitVersion.OutputVariables.VersionVariablesJsonModel.ShortSha.get -> string?
540-
GitVersion.OutputVariables.VersionVariablesJsonModel.ShortSha.set -> void
541-
GitVersion.OutputVariables.VersionVariablesJsonModel.UncommittedChanges.get -> int?
542-
GitVersion.OutputVariables.VersionVariablesJsonModel.UncommittedChanges.set -> void
543-
GitVersion.OutputVariables.VersionVariablesJsonModel.VersionSourceSha.get -> string?
544-
GitVersion.OutputVariables.VersionVariablesJsonModel.VersionSourceSha.set -> void
545-
GitVersion.OutputVariables.VersionVariablesJsonModel.VersionVariablesJsonModel() -> void
546-
GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.get -> int?
547-
GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.set -> void
548-
GitVersion.OutputVariables.VersionVariablesJsonStringConverter
549-
GitVersion.OutputVariables.VersionVariablesJsonStringConverter.VersionVariablesJsonStringConverter() -> void
495+
GitVersion.OutputVariables.IVersionVariableSerializer
496+
GitVersion.OutputVariables.IVersionVariableSerializer.FromFile(string! filePath) -> GitVersion.OutputVariables.GitVersionVariables!
497+
GitVersion.OutputVariables.IVersionVariableSerializer.FromJson(string! json) -> GitVersion.OutputVariables.GitVersionVariables!
498+
GitVersion.OutputVariables.IVersionVariableSerializer.ToFile(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables, string! filePath) -> void
499+
GitVersion.OutputVariables.IVersionVariableSerializer.ToJson(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string!
550500
GitVersion.ReferenceName.TryGetSemanticVersion(out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? tagPrefix, GitVersion.SemanticVersionFormat format) -> bool
551501
GitVersion.RefSpecDirection
552502
GitVersion.RefSpecDirection.Fetch = 0 -> GitVersion.RefSpecDirection
@@ -688,7 +638,7 @@ GitVersion.VersionCalculation.BaseVersion.ShouldIncrement.init -> void
688638
GitVersion.VersionCalculation.BaseVersion.Source.get -> string!
689639
GitVersion.VersionCalculation.BaseVersion.Source.init -> void
690640
GitVersion.VersionCalculation.Caching.GitVersionCache
691-
GitVersion.VersionCalculation.Caching.GitVersionCache.GitVersionCache(GitVersion.IFileSystem! fileSystem, GitVersion.Logging.ILog! log, GitVersion.IGitRepositoryInfo! repositoryInfo) -> void
641+
GitVersion.VersionCalculation.Caching.GitVersionCache.GitVersionCache(GitVersion.IFileSystem! fileSystem, GitVersion.OutputVariables.IVersionVariableSerializer! serializer, GitVersion.Logging.ILog! log, GitVersion.IGitRepositoryInfo! repositoryInfo) -> void
692642
GitVersion.VersionCalculation.Caching.GitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> GitVersion.OutputVariables.GitVersionVariables?
693643
GitVersion.VersionCalculation.Caching.GitVersionCache.WriteVariablesToDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey, GitVersion.OutputVariables.GitVersionVariables! versionVariables) -> void
694644
GitVersion.VersionCalculation.Caching.GitVersionCacheKey
@@ -765,10 +715,6 @@ override GitVersion.Agents.LocalBuild.IsDefault.get -> bool
765715
override GitVersion.BranchCommit.Equals(object? obj) -> bool
766716
override GitVersion.BranchCommit.GetHashCode() -> int
767717
override GitVersion.Helpers.LambdaKeyComparer<TSource, TKey>.Compare(TSource? x, TSource? y) -> int
768-
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.CanConvert(System.Type! typeToConvert) -> bool
769-
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.HandleNull.get -> bool
770-
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.Read(ref System.Text.Json.Utf8JsonReader reader, System.Type! typeToConvert, System.Text.Json.JsonSerializerOptions! options) -> string!
771-
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.Write(System.Text.Json.Utf8JsonWriter! writer, string? value, System.Text.Json.JsonSerializerOptions! options) -> void
772718
override GitVersion.ReferenceName.Equals(object? obj) -> bool
773719
override GitVersion.ReferenceName.GetHashCode() -> int
774720
override GitVersion.ReferenceName.ToString() -> string!
@@ -863,10 +809,6 @@ static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! lo
863809
static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! log, GitVersion.Logging.Verbosity verbosity, string! format, params object![]! args) -> void
864810
static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! log, string! format, params object![]! args) -> void
865811
static GitVersion.Logging.LogExtensions.Write(this GitVersion.Logging.ILog! log, GitVersion.Logging.LogLevel level, string! format, params object![]! args) -> void
866-
static GitVersion.OutputVariables.VersionVariablesHelper.FromFile(string! filePath, GitVersion.IFileSystem! fileSystem) -> GitVersion.OutputVariables.GitVersionVariables!
867-
static GitVersion.OutputVariables.VersionVariablesHelper.FromJson(string! json) -> GitVersion.OutputVariables.GitVersionVariables!
868-
static GitVersion.OutputVariables.VersionVariablesHelper.ToFile(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables, string! filePath, GitVersion.IFileSystem! fileSystem) -> void
869-
static GitVersion.OutputVariables.VersionVariablesHelper.ToJson(this GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string!
870812
static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName!
871813
static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName!
872814
static GitVersion.ReferenceName.TryParse(out GitVersion.ReferenceName? value, string! canonicalName) -> bool

src/GitVersion.Core/VersionCalculation/Caching/GitVersionCache.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace GitVersion.VersionCalculation.Caching;
88
public class GitVersionCache : IGitVersionCache
99
{
1010
private readonly IFileSystem fileSystem;
11+
private readonly IVersionVariableSerializer serializer;
1112
private readonly ILog log;
1213
private readonly IGitRepositoryInfo repositoryInfo;
1314

14-
public GitVersionCache(IFileSystem fileSystem, ILog log, IGitRepositoryInfo repositoryInfo)
15+
public GitVersionCache(IFileSystem fileSystem, IVersionVariableSerializer serializer, ILog log, IGitRepositoryInfo repositoryInfo)
1516
{
1617
this.fileSystem = fileSystem.NotNull();
18+
this.serializer = serializer.NotNull();
1719
this.log = log.NotNull();
1820
this.repositoryInfo = repositoryInfo.NotNull();
1921
}
@@ -25,7 +27,7 @@ public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, GitVersionVar
2527
{
2628
try
2729
{
28-
VersionVariablesHelper.ToFile(versionVariables, cacheFileName, this.fileSystem);
30+
serializer.ToFile(versionVariables, cacheFileName);
2931
}
3032
catch (Exception ex)
3133
{
@@ -46,7 +48,7 @@ public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, GitVersionVar
4648
}
4749
try
4850
{
49-
var loadedVariables = VersionVariablesHelper.FromFile(cacheFileName, this.fileSystem);
51+
var loadedVariables = serializer.FromFile(cacheFileName);
5052
return loadedVariables;
5153
}
5254
catch (Exception ex)

0 commit comments

Comments
 (0)