Skip to content

Commit 4b025e5

Browse files
committed
refactor
1 parent 41211ab commit 4b025e5

File tree

6 files changed

+56
-35
lines changed

6 files changed

+56
-35
lines changed

src/GitVersionExe.Tests/ArgumentBuilder.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ public ArgumentBuilder(string workingDirectory)
1010
}
1111

1212

13-
public ArgumentBuilder(string workingDirectory, string exec, string execArgs, string projectFile, string projectArgs, string logFile, bool isTeamCity)
13+
public ArgumentBuilder(string workingDirectory, string exec, string execArgs, string projectFile, string projectArgs, string logFile)
1414
{
1515
this.workingDirectory = workingDirectory;
1616
this.exec = exec;
1717
this.execArgs = execArgs;
1818
this.projectFile = projectFile;
1919
this.projectArgs = projectArgs;
2020
this.logFile = logFile;
21-
this.isTeamCity = isTeamCity;
2221
}
2322

24-
public ArgumentBuilder(string workingDirectory, string additionalArguments, bool isTeamCity, string logFile)
23+
public ArgumentBuilder(string workingDirectory, string additionalArguments, string logFile)
2524
{
2625
this.workingDirectory = workingDirectory;
27-
this.isTeamCity = isTeamCity;
2826
this.additionalArguments = additionalArguments;
2927
this.logFile = logFile;
3028
}
@@ -35,9 +33,6 @@ public ArgumentBuilder(string workingDirectory, string additionalArguments, bool
3533

3634
public string LogFile => logFile;
3735

38-
public bool IsTeamCity => isTeamCity;
39-
40-
4136
public override string ToString()
4237
{
4338
var arguments = new StringBuilder();
@@ -78,7 +73,6 @@ public override string ToString()
7873
private readonly string additionalArguments;
7974
private readonly string exec;
8075
private readonly string execArgs;
81-
private readonly bool isTeamCity;
8276
private readonly string logFile;
8377
private readonly string projectArgs;
8478
private readonly string projectFile;

src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void CheckBuildServerVerbosityConsole(string verbosityArg, string expecte
7474
[Test]
7575
public void WorkingDirectoryWithoutGitFolderCrashesWithInformativeMessage()
7676
{
77-
var results = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, null, isTeamCity: false, logToFile: false);
77+
var results = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, null, false);
7878
results.Output.ShouldContain("Can't find the .git directory in");
7979
}
8080

src/GitVersionExe.Tests/GitVersionHelper.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Text;
56
using GitVersion.BuildServers;
67
using GitVersion.Helpers;
@@ -15,34 +16,53 @@ public static ExecutionResults ExecuteIn(string workingDirectory,
1516
string execArgs = null,
1617
string projectFile = null,
1718
string projectArgs = null,
18-
bool isTeamCity = false,
19-
bool logToFile = true)
19+
bool logToFile = true,
20+
params KeyValuePair<string, string>[] environments
21+
)
2022
{
2123
var logFile = logToFile ? Path.Combine(workingDirectory, "log.txt") : null;
22-
var args = new ArgumentBuilder(workingDirectory, exec, execArgs, projectFile, projectArgs, logFile, isTeamCity);
23-
return ExecuteIn(args);
24+
var args = new ArgumentBuilder(workingDirectory, exec, execArgs, projectFile, projectArgs, logFile);
25+
return ExecuteIn(args, environments);
2426
}
2527

26-
public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false, bool logToFile = true)
28+
public static ExecutionResults ExecuteIn(
29+
string workingDirectory,
30+
string arguments,
31+
bool logToFile = true,
32+
params KeyValuePair<string, string>[] environments)
2733
{
2834
var logFile = logToFile ? Path.Combine(workingDirectory, "log.txt") : null;
29-
var args = new ArgumentBuilder(workingDirectory, arguments, isTeamCity, logFile);
30-
return ExecuteIn(args);
35+
var args = new ArgumentBuilder(workingDirectory, arguments, logFile);
36+
return ExecuteIn(args, environments);
3137
}
3238

33-
private static ExecutionResults ExecuteIn(ArgumentBuilder arguments)
39+
private static ExecutionResults ExecuteIn(ArgumentBuilder arguments,
40+
params KeyValuePair<string, string>[] environments
41+
)
3442
{
3543
var executable = PathHelper.GetExecutable();
3644
var output = new StringBuilder();
3745

38-
var environmentalVariables =
39-
new[]
46+
var environmentalVariables = new Dictionary<string, string>
47+
{
48+
{ TeamCity.EnvironmentVariableName, null },
49+
{ AppVeyor.EnvironmentVariableName, null },
50+
{ TravisCi.EnvironmentVariableName, null },
51+
{ Jenkins.EnvironmentVariableName, null },
52+
{ AzurePipelines.EnvironmentVariableName, null },
53+
};
54+
55+
foreach (var environment in environments)
56+
{
57+
if (environmentalVariables.ContainsKey(environment.Key))
4058
{
41-
new KeyValuePair<string, string>(TeamCity.EnvironmentVariableName, arguments.IsTeamCity ? "8.0.0" : null),
42-
new KeyValuePair<string, string>(AppVeyor.EnvironmentVariableName, null),
43-
new KeyValuePair<string, string>(TravisCi.EnvironmentVariableName, null),
44-
new KeyValuePair<string, string>(AzurePipelines.EnvironmentVariableName, null),
45-
};
59+
environmentalVariables[environment.Key] = environment.Value;
60+
}
61+
else
62+
{
63+
environmentalVariables.Add(environment.Key, environment.Value);
64+
}
65+
}
4666

4767
var exitCode = -1;
4868

@@ -60,7 +80,7 @@ private static ExecutionResults ExecuteIn(ArgumentBuilder arguments)
6080
executable,
6181
args,
6282
arguments.WorkingDirectory,
63-
environmentalVariables);
83+
environmentalVariables.ToArray());
6484
}
6585
catch (Exception exception)
6686
{

src/GitVersionExe.Tests/JsonOutputOnBuildServer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Collections.Generic;
12
using GitTools.Testing;
3+
using GitVersion.BuildServers;
24
using NUnit.Framework;
35
using Shouldly;
46

@@ -13,7 +15,9 @@ public void BeingOnBuildServerDoesntOverrideOutputJson()
1315
fixture.Repository.MakeATaggedCommit("1.2.3");
1416
fixture.Repository.MakeACommit();
1517

16-
var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json", isTeamCity: true);
18+
var env = new KeyValuePair<string, string>(TeamCity.EnvironmentVariableName, "8.0.0");
19+
20+
var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json", environments: env);
1721

1822
result.ExitCode.ShouldBe(0);
1923
result.Output.ShouldStartWith("{");

src/GitVersionExe.Tests/PullRequestInJenkinsPipelineTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using GitTools.Testing;
4+
using GitVersion.BuildServers;
35
using LibGit2Sharp;
46
using NUnit.Framework;
57
using Shouldly;
@@ -14,7 +16,6 @@ public class PullRequestInJenkinsTest
1416
public void GivenJenkinsPipelineHasDuplicatedOriginVersionIsCalculatedProperly()
1517
{
1618
var pipelineBranch = "BRANCH_NAME";
17-
var pipelineBranchOrig = Environment.GetEnvironmentVariable(pipelineBranch);
1819

1920
using var fixture = new EmptyRepositoryFixture();
2021
var remoteRepositoryPath = PathHelper.GetTempPath();
@@ -43,20 +44,19 @@ public void GivenJenkinsPipelineHasDuplicatedOriginVersionIsCalculatedProperly()
4344
Commands.Checkout(fixture.Repository, mergeCommitSha);
4445
}
4546

46-
// Emulating Jenkins environment variable
47-
Environment.SetEnvironmentVariable(pipelineBranch, "PR-5");
48-
Environment.SetEnvironmentVariable("JENKINS_URL", "url");
47+
var env = new[]
48+
{
49+
new KeyValuePair<string, string>(Jenkins.EnvironmentVariableName, "url"),
50+
new KeyValuePair<string, string>(pipelineBranch, "PR-5")
51+
};
4952

50-
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath);
53+
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, environments: env);
5154

5255
result.ExitCode.ShouldBe(0);
5356
result.OutputVariables.FullSemVer.ShouldBe("1.0.4-PullRequest0005.3");
5457

5558
// Cleanup repository files
5659
DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
57-
58-
Environment.SetEnvironmentVariable(pipelineBranch, pipelineBranchOrig);
59-
Environment.SetEnvironmentVariable("JENKINS_URL", null);
6060
}
6161
}
6262
}

src/GitVersionExe.Tests/PullRequestInTeamCityTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using GitTools.Testing;
4+
using GitVersion.BuildServers;
35
using LibGit2Sharp;
46
using NUnit.Framework;
57
using Shouldly;
@@ -41,7 +43,8 @@ public void GivenARemoteWithATagOnMasterAndAPullRequestWithTwoCommitsAndBuildIsR
4143
Commands.Checkout(fixture.Repository, mergeCommitSha);
4244
}
4345

44-
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, isTeamCity: true);
46+
var env = new KeyValuePair<string, string>(TeamCity.EnvironmentVariableName, "8.0.0");
47+
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, environments: env);
4548

4649
result.ExitCode.ShouldBe(0);
4750
result.OutputVariables.FullSemVer.ShouldBe("1.0.4-PullRequest0005.3");

0 commit comments

Comments
 (0)