Skip to content
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

Refactor variable serialization and extend serialization tests #3806

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/GitVersion.App.Tests/GitVersion.App.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestStream.cs" Link="Helpers\TestStream.cs" />
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestBase.cs" Link="Helpers\TestBase.cs" />
<Compile Include="..\GitVersion.Core.Tests\Helpers\GitVersionCoreTestModule.cs" Link="Helpers\GitVersionCoreTestModule.cs" />
<Compile Include="..\GitVersion.Core.Tests\Extensions\GitVersionVariablesExtensions.cs" Link="Extensions\GitVersionVariablesExtensions.cs" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion src/GitVersion.App.Tests/Helpers/ExecutionResults.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Core.Tests;
using GitVersion.OutputVariables;

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

return VersionVariablesHelper.FromJson(json);
return json.ToGitVersionVariables();
}
}
}
7 changes: 4 additions & 3 deletions src/GitVersion.App.Tests/Helpers/ProgramFixture.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Core.Tests;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Logging;
Expand Down Expand Up @@ -86,11 +87,11 @@ public GitVersionVariables? OutputVariables
{
if (Output.IsNullOrWhiteSpace()) return null;

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

return VersionVariablesHelper.FromJson(json);
return json.ToGitVersionVariables();
}
}
}
4 changes: 2 additions & 2 deletions src/GitVersion.App.Tests/JsonOutputOnBuildServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GitVersion.Agents;
using GitVersion.Core.Tests;
using GitVersion.Helpers;
using GitVersion.OutputVariables;

namespace GitVersion.App.Tests;

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

var outputVariables = VersionVariablesHelper.FromJson(json);
var outputVariables = json.ToGitVersionVariables();
outputVariables.ShouldNotBeNull();
outputVariables.FullSemVer.ShouldBeEquivalentTo(expectedVersion);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using GitVersion.OutputVariables;

namespace GitVersion.Core.Tests;

public static class GitVersionVariablesExtensions
{
public static string ToJson(this GitVersionVariables gitVersionVariables)
{
var serializer = new VersionVariableSerializer(new FileSystem());
return serializer.ToJson(gitVersionVariables);
}

public static GitVersionVariables ToGitVersionVariables(this string json)
{
var serializer = new VersionVariableSerializer(new FileSystem());
return serializer.FromJson(json);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GitVersion.Core.Tests.Helpers;
using GitVersion.OutputVariables;
using GitVersion.VersionCalculation;
using Microsoft.Extensions.DependencyInjection;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Logging;
using GitVersion.OutputVariables;
using GitVersion.VersionCalculation;
using Microsoft.Extensions.DependencyInjection;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GitVersion.OutputVariables;

public interface IVersionVariableSerializer
{
GitVersionVariables FromJson(string json);
string ToJson(GitVersionVariables gitVersionVariables);
GitVersionVariables FromFile(string filePath);
void ToFile(GitVersionVariables gitVersionVariables, string filePath);
}
70 changes: 6 additions & 64 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -492,61 +492,11 @@ GitVersion.OutputVariables.GitVersionVariables.VersionSourceSha.get -> string?
GitVersion.OutputVariables.GitVersionVariables.VersionSourceSha.init -> void
GitVersion.OutputVariables.GitVersionVariables.WeightedPreReleaseNumber.get -> string!
GitVersion.OutputVariables.GitVersionVariables.WeightedPreReleaseNumber.init -> void
GitVersion.OutputVariables.VersionVariablesHelper
GitVersion.OutputVariables.VersionVariablesJsonModel
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemFileVer.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemFileVer.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemVer.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.AssemblySemVer.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.BranchName.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.BranchName.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.BuildMetaData.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.BuildMetaData.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitDate.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitDate.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitsSinceVersionSource.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.CommitsSinceVersionSource.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.EscapedBranchName.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.EscapedBranchName.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.FullBuildMetaData.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.FullBuildMetaData.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.FullSemVer.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.FullSemVer.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.InformationalVersion.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.InformationalVersion.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.Major.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.Major.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.MajorMinorPatch.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.MajorMinorPatch.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.Minor.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.Minor.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.Patch.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.Patch.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabel.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabel.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabelWithDash.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseLabelWithDash.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseNumber.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseNumber.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTag.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTag.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTagWithDash.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.PreReleaseTagWithDash.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.SemVer.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.SemVer.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.Sha.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.Sha.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.ShortSha.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.ShortSha.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.UncommittedChanges.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.UncommittedChanges.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.VersionSourceSha.get -> string?
GitVersion.OutputVariables.VersionVariablesJsonModel.VersionSourceSha.set -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.VersionVariablesJsonModel() -> void
GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.get -> int?
GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.set -> void
GitVersion.OutputVariables.VersionVariablesJsonStringConverter
GitVersion.OutputVariables.VersionVariablesJsonStringConverter.VersionVariablesJsonStringConverter() -> void
GitVersion.OutputVariables.IVersionVariableSerializer
GitVersion.OutputVariables.IVersionVariableSerializer.FromFile(string! filePath) -> GitVersion.OutputVariables.GitVersionVariables!
GitVersion.OutputVariables.IVersionVariableSerializer.FromJson(string! json) -> GitVersion.OutputVariables.GitVersionVariables!
GitVersion.OutputVariables.IVersionVariableSerializer.ToFile(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables, string! filePath) -> void
GitVersion.OutputVariables.IVersionVariableSerializer.ToJson(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string!
GitVersion.ReferenceName.TryGetSemanticVersion(out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? tagPrefix, GitVersion.SemanticVersionFormat format) -> bool
GitVersion.RefSpecDirection
GitVersion.RefSpecDirection.Fetch = 0 -> GitVersion.RefSpecDirection
Expand Down Expand Up @@ -688,7 +638,7 @@ GitVersion.VersionCalculation.BaseVersion.ShouldIncrement.init -> void
GitVersion.VersionCalculation.BaseVersion.Source.get -> string!
GitVersion.VersionCalculation.BaseVersion.Source.init -> void
GitVersion.VersionCalculation.Caching.GitVersionCache
GitVersion.VersionCalculation.Caching.GitVersionCache.GitVersionCache(GitVersion.IFileSystem! fileSystem, GitVersion.Logging.ILog! log, GitVersion.IGitRepositoryInfo! repositoryInfo) -> void
GitVersion.VersionCalculation.Caching.GitVersionCache.GitVersionCache(GitVersion.IFileSystem! fileSystem, GitVersion.OutputVariables.IVersionVariableSerializer! serializer, GitVersion.Logging.ILog! log, GitVersion.IGitRepositoryInfo! repositoryInfo) -> void
GitVersion.VersionCalculation.Caching.GitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> GitVersion.OutputVariables.GitVersionVariables?
GitVersion.VersionCalculation.Caching.GitVersionCache.WriteVariablesToDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey, GitVersion.OutputVariables.GitVersionVariables! versionVariables) -> void
GitVersion.VersionCalculation.Caching.GitVersionCacheKey
Expand Down Expand Up @@ -765,10 +715,6 @@ override GitVersion.Agents.LocalBuild.IsDefault.get -> bool
override GitVersion.BranchCommit.Equals(object? obj) -> bool
override GitVersion.BranchCommit.GetHashCode() -> int
override GitVersion.Helpers.LambdaKeyComparer<TSource, TKey>.Compare(TSource? x, TSource? y) -> int
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.CanConvert(System.Type! typeToConvert) -> bool
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.HandleNull.get -> bool
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.Read(ref System.Text.Json.Utf8JsonReader reader, System.Type! typeToConvert, System.Text.Json.JsonSerializerOptions! options) -> string!
override GitVersion.OutputVariables.VersionVariablesJsonStringConverter.Write(System.Text.Json.Utf8JsonWriter! writer, string? value, System.Text.Json.JsonSerializerOptions! options) -> void
override GitVersion.ReferenceName.Equals(object? obj) -> bool
override GitVersion.ReferenceName.GetHashCode() -> int
override GitVersion.ReferenceName.ToString() -> string!
Expand Down Expand Up @@ -863,10 +809,6 @@ static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! lo
static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! log, GitVersion.Logging.Verbosity verbosity, string! format, params object![]! args) -> void
static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! log, string! format, params object![]! args) -> void
static GitVersion.Logging.LogExtensions.Write(this GitVersion.Logging.ILog! log, GitVersion.Logging.LogLevel level, string! format, params object![]! args) -> void
static GitVersion.OutputVariables.VersionVariablesHelper.FromFile(string! filePath, GitVersion.IFileSystem! fileSystem) -> GitVersion.OutputVariables.GitVersionVariables!
static GitVersion.OutputVariables.VersionVariablesHelper.FromJson(string! json) -> GitVersion.OutputVariables.GitVersionVariables!
static GitVersion.OutputVariables.VersionVariablesHelper.ToFile(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables, string! filePath, GitVersion.IFileSystem! fileSystem) -> void
static GitVersion.OutputVariables.VersionVariablesHelper.ToJson(this GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string!
static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName!
static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName!
static GitVersion.ReferenceName.TryParse(out GitVersion.ReferenceName? value, string! canonicalName) -> bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace GitVersion.VersionCalculation.Caching;
public class GitVersionCache : IGitVersionCache
{
private readonly IFileSystem fileSystem;
private readonly IVersionVariableSerializer serializer;
private readonly ILog log;
private readonly IGitRepositoryInfo repositoryInfo;

public GitVersionCache(IFileSystem fileSystem, ILog log, IGitRepositoryInfo repositoryInfo)
public GitVersionCache(IFileSystem fileSystem, IVersionVariableSerializer serializer, ILog log, IGitRepositoryInfo repositoryInfo)
{
this.fileSystem = fileSystem.NotNull();
this.serializer = serializer.NotNull();
this.log = log.NotNull();
this.repositoryInfo = repositoryInfo.NotNull();
}
Expand All @@ -25,7 +27,7 @@ public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, GitVersionVar
{
try
{
VersionVariablesHelper.ToFile(versionVariables, cacheFileName, this.fileSystem);
serializer.ToFile(versionVariables, cacheFileName);
}
catch (Exception ex)
{
Expand All @@ -46,7 +48,7 @@ public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, GitVersionVar
}
try
{
var loadedVariables = VersionVariablesHelper.FromFile(cacheFileName, this.fileSystem);
var loadedVariables = serializer.FromFile(cacheFileName);
return loadedVariables;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestLogAppender.cs" Link="Helpers\TestLogAppender.cs" />
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestStream.cs" Link="Helpers\TestStream.cs" />
<Compile Include="..\GitVersion.Core.Tests\Extensions\GitToolsTestingExtensions.cs" Link="Extensions\GitToolsTestingExtensions.cs" />
<Compile Include="..\GitVersion.Core.Tests\Extensions\GitVersionVariablesExtensions.cs" Link="Extensions\GitVersionVariablesExtensions.cs" />
</ItemGroup>
</Project>
7 changes: 5 additions & 2 deletions src/GitVersion.MsBuild/GitVersionTaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ internal class GitVersionTaskExecutor : IGitVersionTaskExecutor
{
private readonly IFileSystem fileSystem;
private readonly IGitVersionOutputTool gitVersionOutputTool;
private readonly IVersionVariableSerializer serializer;
private readonly IConfigurationProvider configurationProvider;
private readonly IOptions<GitVersionOptions> options;

public GitVersionTaskExecutor(IFileSystem fileSystem, IGitVersionOutputTool gitVersionOutputTool,
IConfigurationProvider configurationProvider, IOptions<GitVersionOptions> options)
IVersionVariableSerializer serializer, IConfigurationProvider configurationProvider,
IOptions<GitVersionOptions> options)
{
this.fileSystem = fileSystem.NotNull();
this.gitVersionOutputTool = gitVersionOutputTool.NotNull();
this.serializer = serializer.NotNull();
this.configurationProvider = configurationProvider.NotNull();
this.options = options.NotNull();
}
Expand Down Expand Up @@ -104,7 +107,7 @@ public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task)

private GitVersionVariables GitVersionVariables(GitVersionTaskBase task)
{
var versionVariables = VersionVariablesHelper.FromFile(task.VersionFile, this.fileSystem);
var versionVariables = serializer.FromFile(task.VersionFile);
return versionVariables;
}
}
2 changes: 2 additions & 0 deletions src/GitVersion.Output/GitVersionOutputModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using GitVersion.Output.GitVersionInfo;
using GitVersion.Output.OutputGenerator;
using GitVersion.Output.WixUpdater;
using GitVersion.OutputVariables;
using Microsoft.Extensions.DependencyInjection;

namespace GitVersion.Output;
Expand All @@ -12,6 +13,7 @@ public void RegisterTypes(IServiceCollection services)
{
services.AddSingleton<IGitVersionOutputTool, GitVersionOutputTool>();
services.AddSingleton<IOutputGenerator, OutputGenerator.OutputGenerator>();
services.AddSingleton<IVersionVariableSerializer, VersionVariableSerializer>();
services.AddSingleton<IGitVersionInfoGenerator, GitVersionInfoGenerator>();
services.AddSingleton<IWixVersionFileUpdater, WixVersionFileUpdater>();
services.AddSingleton<IAssemblyInfoFileUpdater, AssemblyInfoFileUpdater>();
Expand Down
9 changes: 7 additions & 2 deletions src/GitVersion.Output/OutputGenerator/OutputGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ internal sealed class OutputGenerator : IOutputGenerator
{
private readonly IConsole console;
private readonly IFileSystem fileSystem;
private readonly IVersionVariableSerializer serializer;
private readonly IEnvironment environment;
private readonly IOptions<GitVersionOptions> options;
private readonly ICurrentBuildAgent buildAgent;

public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IFileSystem fileSystem, IEnvironment environment, IOptions<GitVersionOptions> options)
public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IFileSystem fileSystem,
IVersionVariableSerializer serializer, IEnvironment environment, IOptions<GitVersionOptions> options)
{
this.console = console.NotNull();
this.fileSystem = fileSystem.NotNull();
this.serializer = serializer.NotNull();
this.environment = environment;
this.options = options.NotNull();
this.buildAgent = buildAgent.NotNull();
Expand All @@ -36,7 +39,7 @@ public void Execute(GitVersionVariables variables, OutputContext context)
this.buildAgent.WriteIntegration(this.console.WriteLine, variables, context.UpdateBuildNumber ?? true);
}

var json = variables.ToJson();
var json = this.serializer.ToJson(variables);
if (gitVersionOptions.Output.Contains(OutputType.File))
{
var retryOperation = new RetryAction<IOException>();
Expand All @@ -55,6 +58,7 @@ public void Execute(GitVersionVariables variables, OutputContext context)
{
throw new WarningException("Cannot specify both /showvariable and /format");
}

if (gitVersionOptions.ShowVariable is not null)
{
if (!variables.TryGetValue(gitVersionOptions.ShowVariable, out var part))
Expand All @@ -65,6 +69,7 @@ public void Execute(GitVersionVariables variables, OutputContext context)
this.console.WriteLine(part);
return;
}

if (gitVersionOptions.Format is not null)
{
var format = gitVersionOptions.Format;
Expand Down
Loading