From 101dea6a51398d663dc148a95b212119b425417c Mon Sep 17 00:00:00 2001 From: Artur Stolear Date: Thu, 12 Mar 2020 10:16:08 +0200 Subject: [PATCH] added test for the output with build server and json when running in build server --- src/GitVersionExe.Tests/ExecutionResults.cs | 6 ++- .../JsonOutputOnBuildServer.cs | 27 ----------- .../JsonOutputOnBuildServerTest.cs | 45 +++++++++++++++++++ src/GitVersionExe/ArgumentParser.cs | 2 +- 4 files changed, 51 insertions(+), 29 deletions(-) delete mode 100644 src/GitVersionExe.Tests/JsonOutputOnBuildServer.cs create mode 100644 src/GitVersionExe.Tests/JsonOutputOnBuildServerTest.cs diff --git a/src/GitVersionExe.Tests/ExecutionResults.cs b/src/GitVersionExe.Tests/ExecutionResults.cs index 7772d1d7f9..0dbeb357cb 100644 --- a/src/GitVersionExe.Tests/ExecutionResults.cs +++ b/src/GitVersionExe.Tests/ExecutionResults.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Newtonsoft.Json; using GitVersion.OutputVariables; @@ -21,7 +22,10 @@ public virtual VersionVariables OutputVariables { get { - var outputVariables = JsonConvert.DeserializeObject>(Output); + var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal); + var json = Output.Substring(jsonStartIndex); + + var outputVariables = JsonConvert.DeserializeObject>(json); return VersionVariables.FromDictionary(outputVariables); } } diff --git a/src/GitVersionExe.Tests/JsonOutputOnBuildServer.cs b/src/GitVersionExe.Tests/JsonOutputOnBuildServer.cs deleted file mode 100644 index 71e028cdd4..0000000000 --- a/src/GitVersionExe.Tests/JsonOutputOnBuildServer.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Collections.Generic; -using GitTools.Testing; -using GitVersion.BuildServers; -using NUnit.Framework; -using Shouldly; - -namespace GitVersionExe.Tests -{ - public class JsonOutputOnBuildServer - { - [Test] - public void BeingOnBuildServerDoesntOverrideOutputJson() - { - using var fixture = new RemoteRepositoryFixture(); - fixture.Repository.MakeATaggedCommit("1.2.3"); - fixture.Repository.MakeACommit(); - - var env = new KeyValuePair(TeamCity.EnvironmentVariableName, "8.0.0"); - - var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json", environments: env); - - result.ExitCode.ShouldBe(0); - result.Output.ShouldStartWith("{"); - result.Output.TrimEnd().ShouldEndWith("}"); - } - } -} diff --git a/src/GitVersionExe.Tests/JsonOutputOnBuildServerTest.cs b/src/GitVersionExe.Tests/JsonOutputOnBuildServerTest.cs new file mode 100644 index 0000000000..80de5f8d65 --- /dev/null +++ b/src/GitVersionExe.Tests/JsonOutputOnBuildServerTest.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; +using GitTools.Testing; +using GitVersion.BuildServers; +using NUnit.Framework; +using Shouldly; + +namespace GitVersionExe.Tests +{ + public class JsonOutputOnBuildServerTest + { + [Test] + public void BeingOnBuildServerDoesntOverrideOutputJson() + { + using var fixture = new RemoteRepositoryFixture(); + fixture.Repository.MakeATaggedCommit("1.2.3"); + fixture.Repository.MakeACommit(); + + var env = new KeyValuePair(TeamCity.EnvironmentVariableName, "8.0.0"); + + var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json", environments: env); + + result.ExitCode.ShouldBe(0); + result.Output.ShouldStartWith("{"); + result.Output.TrimEnd().ShouldEndWith("}"); + } + + [Test] + public void BeingOnBuildServerWithOutputJsonDoesNotFail() + { + using var fixture = new RemoteRepositoryFixture(); + fixture.Repository.MakeATaggedCommit("1.2.3"); + fixture.Repository.MakeACommit(); + + var env = new KeyValuePair(TeamCity.EnvironmentVariableName, "8.0.0"); + + var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json /output buildserver", environments: env); + + result.ExitCode.ShouldBe(0); + const string version = "0.1.0+4"; + result.Output.ShouldContain($"##teamcity[buildNumber '{version}']"); + result.OutputVariables.ShouldNotBeNull(); + result.OutputVariables.FullSemVer.ShouldBeEquivalentTo(version); + } + } +} diff --git a/src/GitVersionExe/ArgumentParser.cs b/src/GitVersionExe/ArgumentParser.cs index eac366f12a..c404558a1a 100644 --- a/src/GitVersionExe/ArgumentParser.cs +++ b/src/GitVersionExe/ArgumentParser.cs @@ -279,7 +279,7 @@ public Arguments ParseArguments(string[] commandLineArguments) { if (!Enum.TryParse(v, true, out OutputType outputType)) { - throw new WarningException($"Value '{value}' cannot be parsed as output type, please use 'json' or 'buildserver'"); + throw new WarningException($"Value '{v}' cannot be parsed as output type, please use 'json' or 'buildserver'"); } arguments.Output.Add(outputType);