Skip to content

Commit 24486e4

Browse files
committed
refactored ExecCommand and ExecuteCore
1 parent e8462ce commit 24486e4

File tree

16 files changed

+159
-94
lines changed

16 files changed

+159
-94
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using GitVersion;
3+
using GitVersion.Configuration;
34
using NUnit.Framework;
45
using GitVersion.Logging;
56

@@ -61,14 +62,27 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
6162
var root = Path.Combine(workDirectory, name);
6263
var dynamicDirectory = Path.Combine(root, "D"); // dynamic, keeping directory as short as possible
6364
var workingDirectory = Path.Combine(root, "W"); // working, keeping directory as short as possible
65+
var arguments = new Arguments
66+
{
67+
TargetUrl = url,
68+
DynamicRepositoryLocation = dynamicDirectory,
69+
TargetBranch = targetBranch,
70+
NoFetch = false,
71+
TargetPath = workingDirectory,
72+
CommitId = commitId
73+
};
6474

6575
Directory.CreateDirectory(dynamicDirectory);
6676
Directory.CreateDirectory(workingDirectory);
6777

68-
var executeCore = new ExecuteCore(new TestFileSystem(), new TestEnvironment(), new NullLog());
78+
var testFileSystem = new TestFileSystem();
79+
var testEnvironment = new TestEnvironment();
80+
var log = new NullLog();
81+
var configFileLocator = new DefaultConfigFileLocator(testFileSystem, log);
82+
83+
var executeCore = new ExecuteCore(testFileSystem, testEnvironment, log, configFileLocator);
6984

70-
var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch,
71-
false, workingDirectory, commitId);
85+
var versionVariables = executeCore.ExecuteGitVersion(arguments);
7286

7387
Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);
7488
}

src/GitVersionCore.Tests/ExecuteCoreTests.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ public class ExecuteCoreTests : TestBase
2424
private IFileSystem fileSystem;
2525
private IEnvironment environment;
2626
private ILog log;
27+
private IConfigFileLocator configFileLocator;
2728

2829
[SetUp]
2930
public void SetUp()
3031
{
3132
fileSystem = new FileSystem();
3233
environment = new TestEnvironment();
3334
log = new NullLog();
35+
configFileLocator = new DefaultConfigFileLocator(fileSystem, log);
3436
}
3537

3638
[Test]
3739
public void CacheKeySameAfterReNormalizing()
3840
{
39-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
41+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
4042

4143
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
4244
{
@@ -58,7 +60,7 @@ public void CacheKeySameAfterReNormalizing()
5860
[Description("LibGit2Sharp fails here when running under Mono")]
5961
public void CacheKeyForWorktree()
6062
{
61-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
63+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
6264

6365
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
6466
{
@@ -124,12 +126,12 @@ public void CacheFileExistsOnDisk()
124126
var logAppender = new TestLogAppender(Action);
125127
log = new Log(logAppender);
126128

127-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
129+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
128130

129131
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
130132
{
131133
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
132-
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
134+
vv = versionAndBranchFinder.ExecuteGitVersion(new Arguments { TargetPath = fixture.RepositoryPath });
133135
vv.AssemblySemVer.ShouldBe("4.10.3.0");
134136
});
135137

@@ -173,7 +175,7 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
173175
CommitDate: 2015-11-10
174176
";
175177

176-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
178+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
177179

178180
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
179181
{
@@ -184,7 +186,7 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
184186

185187
var cacheDirectoryTimestamp = fileSystem.GetLastDirectoryWrite(cacheDirectory);
186188

187-
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null, new Config() { TagPrefix = "prefix" });
189+
vv = versionAndBranchFinder.ExecuteGitVersion(new Arguments { TargetPath = fixture.RepositoryPath , OverrideConfig = new Config { TagPrefix = "prefix" } });
188190

189191
vv.AssemblySemVer.ShouldBe("0.1.0.0");
190192

@@ -204,7 +206,7 @@ public void CacheFileIsMissing()
204206
var logAppender = new TestLogAppender(Action);
205207
log = new Log(logAppender);
206208

207-
var executeCore = new ExecuteCore(fileSystem, environment, log);
209+
var executeCore = new ExecuteCore(fileSystem, environment, log, configFileLocator);
208210

209211
RepositoryScope(executeCore);
210212
var logsMessages = stringBuilder.ToString();
@@ -248,18 +250,19 @@ public void ConfigChangeInvalidatesCache()
248250
CommitDate: 2015-11-10
249251
";
250252

251-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
253+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
252254

253255
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
254256
{
255257
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
256-
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
258+
var arguments = new Arguments { TargetPath = fixture.RepositoryPath };
259+
vv = versionAndBranchFinder.ExecuteGitVersion(arguments);
257260
vv.AssemblySemVer.ShouldBe("4.10.3.0");
258261

259262
var configPath = Path.Combine(fixture.RepositoryPath, "GitVersionConfig.yaml");
260263
fileSystem.WriteAllText(configPath, "next-version: 5.0");
261264

262-
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
265+
vv = versionAndBranchFinder.ExecuteGitVersion(arguments);
263266
vv.AssemblySemVer.ShouldBe("5.0.0.0");
264267
});
265268
}
@@ -300,15 +303,18 @@ public void NoCacheBypassesCache()
300303
CommitDate: 2015-11-10
301304
";
302305

303-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
306+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
304307

305308
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
306309
{
310+
var arguments = new Arguments { TargetPath = fixture.RepositoryPath };
311+
307312
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
308-
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
313+
vv = versionAndBranchFinder.ExecuteGitVersion(arguments);
309314
vv.AssemblySemVer.ShouldBe("4.10.3.0");
310315

311-
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null, noCache: true);
316+
arguments.NoCache = true;
317+
vv = versionAndBranchFinder.ExecuteGitVersion(arguments);
312318
vv.AssemblySemVer.ShouldBe("0.1.0.0");
313319
});
314320
}
@@ -317,11 +323,13 @@ public void NoCacheBypassesCache()
317323
[Test]
318324
public void WorkingDirectoryWithoutGit()
319325
{
320-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
326+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
321327

322328
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
323329
{
324-
var exception = Assert.Throws<DirectoryNotFoundException>(() => versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, Environment.SystemDirectory, null));
330+
var arguments = new Arguments { TargetPath = Environment.SystemDirectory };
331+
332+
var exception = Assert.Throws<DirectoryNotFoundException>(() => versionAndBranchFinder.ExecuteGitVersion(arguments));
325333
exception.Message.ShouldContain("Can't find the .git directory in");
326334
});
327335
}
@@ -331,7 +339,7 @@ public void WorkingDirectoryWithoutGit()
331339
[Description("LibGit2Sharp fails when running under Mono")]
332340
public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
333341
{
334-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
342+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
335343

336344
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
337345
{
@@ -356,7 +364,7 @@ public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
356364
[Test]
357365
public void GetProjectRootDirectory_NoWorktree()
358366
{
359-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
367+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
360368

361369
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
362370
{
@@ -370,18 +378,25 @@ public void GetProjectRootDirectory_NoWorktree()
370378
[Test]
371379
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
372380
{
373-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
381+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
374382

375383
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
376384
{
377-
versionAndBranchFinder.ExecuteGitVersion("https://github.com/GitTools/GitVersion.git", null, new Authentication(), "refs/head/master", false, fixture.RepositoryPath, null);
385+
var arguments = new Arguments
386+
{
387+
TargetPath = fixture.RepositoryPath,
388+
TargetUrl = "https://github.com/GitTools/GitVersion.git",
389+
TargetBranch = "refs/head/master"
390+
};
391+
392+
versionAndBranchFinder.ExecuteGitVersion(arguments);
378393
});
379394
}
380395

381396
[Test]
382397
public void GetDotGitDirectory_NoWorktree()
383398
{
384-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
399+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
385400

386401
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
387402
{
@@ -397,7 +412,7 @@ public void GetDotGitDirectory_NoWorktree()
397412
[Description("LibGit2Sharp fails when running under Mono")]
398413
public void GetDotGitDirectory_Worktree()
399414
{
400-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log);
415+
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
401416

402417
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
403418
{
@@ -428,9 +443,11 @@ private void RepositoryScope(ExecuteCore executeCore, Action<EmptyRepositoryFixt
428443
environment.SetEnvironmentVariable(TravisCI.EnvironmentVariableName, null);
429444
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, null);
430445

446+
431447
using var fixture = new EmptyRepositoryFixture();
448+
var arguments = new Arguments { TargetPath = fixture.RepositoryPath };
432449
fixture.Repository.MakeACommit();
433-
var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
450+
var vv = executeCore.ExecuteGitVersion(arguments);
434451

435452
vv.AssemblySemVer.ShouldBe("0.1.0.0");
436453
vv.FileName.ShouldNotBeNullOrEmpty();

src/GitVersionCore/Arguments.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ namespace GitVersion
77
{
88
public class Arguments
99
{
10-
public Arguments()
11-
{
12-
Authentication = new Authentication();
13-
OverrideConfig = new Config();
14-
Output = OutputType.Json;
15-
UpdateAssemblyInfoFileName = new HashSet<string>();
16-
Verbosity = Verbosity.Normal;
17-
}
18-
1910
public Authentication Authentication;
2011

2112
public Config OverrideConfig;
@@ -36,26 +27,24 @@ public Arguments()
3627
public string LogFilePath;
3728
public string ShowVariable;
3829

39-
public OutputType Output;
40-
4130
public string Proj;
4231
public string ProjArgs;
4332
public string Exec;
4433
public string ExecArgs;
4534

46-
public bool UpdateAssemblyInfo;
47-
public ISet<string> UpdateAssemblyInfoFileName;
48-
public bool EnsureAssemblyInfo;
49-
5035
public bool UpdateWixVersionFile;
5136

5237
public bool ShowConfig;
5338
public bool NoFetch;
5439
public bool NoCache;
5540
public bool NoNormalize;
5641

57-
public Verbosity Verbosity;
42+
public OutputType Output = OutputType.Json;
43+
public Verbosity Verbosity = Verbosity.Normal;
5844

45+
public bool UpdateAssemblyInfo;
46+
public ISet<string> UpdateAssemblyInfoFileName = new HashSet<string>();
47+
public bool EnsureAssemblyInfo;
5948
public void AddAssemblyInfoFileName(string fileName)
6049
{
6150
UpdateAssemblyInfoFileName.Add(fileName);

src/GitVersionCore/IModule.cs renamed to src/GitVersionCore/Common/IModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
22

3-
namespace GitVersion
3+
namespace GitVersion.Common
44
{
55
public interface IModule
66
{

src/GitVersionCore/CoreModule.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public void RegisterTypes(IServiceCollection services)
1111
services.AddSingleton<IFileSystem, FileSystem>();
1212
services.AddSingleton<IEnvironment, Environment>();
1313
services.AddSingleton<ILog, Log>();
14+
15+
services.AddSingleton<IExecuteCore, ExecuteCore>();
1416
}
1517
}
1618
}

src/GitVersionCore/ExecuteCore.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,47 @@
1010

1111
namespace GitVersion
1212
{
13-
public class ExecuteCore
13+
public class ExecuteCore : IExecuteCore
1414
{
1515
private readonly IFileSystem fileSystem;
1616
private readonly IEnvironment environment;
1717
private readonly ILog log;
1818
private readonly IConfigFileLocator configFileLocator;
1919
private readonly GitVersionCache gitVersionCache;
2020

21-
public ExecuteCore(IFileSystem fileSystem, IEnvironment environment, ILog log, IConfigFileLocator configFileLocator = null)
21+
public ExecuteCore(IFileSystem fileSystem, IEnvironment environment, ILog log, IConfigFileLocator configFileLocator)
2222
{
2323
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
2424
this.environment = environment;
2525
this.log = log;
26-
this.configFileLocator = configFileLocator ?? new DefaultConfigFileLocator(fileSystem, log);
26+
this.configFileLocator = configFileLocator;
2727
gitVersionCache = new GitVersionCache(fileSystem, log);
2828
}
2929

30-
public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null, bool noCache = false, bool noNormalize = false)
30+
public VersionVariables ExecuteGitVersion(Arguments arguments)
31+
{
32+
return ExecuteGitVersion(
33+
arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication,
34+
arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath,
35+
arguments.CommitId, arguments.OverrideConfig, arguments.NoCache, arguments.NoNormalize);
36+
}
37+
38+
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication)
39+
{
40+
try
41+
{
42+
versionVariables = ExecuteGitVersion(null, null, authentication, null, noFetch, directory, null);
43+
return true;
44+
}
45+
catch (Exception ex)
46+
{
47+
log.Warning("Could not determine assembly version: " + ex);
48+
versionVariables = null;
49+
return false;
50+
}
51+
}
52+
53+
private VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null, bool noCache = false, bool noNormalize = false)
3154
{
3255
BuildServerList.Init(environment, log);
3356

@@ -85,21 +108,6 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
85108
return versionVariables;
86109
}
87110

88-
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication)
89-
{
90-
try
91-
{
92-
versionVariables = ExecuteGitVersion(null, null, authentication, null, noFetch, directory, null);
93-
return true;
94-
}
95-
catch (Exception ex)
96-
{
97-
log.Warning("Could not determine assembly version: " + ex);
98-
versionVariables = null;
99-
return false;
100-
}
101-
}
102-
103111
private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository)
104112
{
105113
if (buildServer == null)

src/GitVersionCore/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using GitVersion.Common;
2+
13
namespace GitVersion.Extensions
24
{
35
using Microsoft.Extensions.DependencyInjection;

src/GitVersionCore/IExecuteCore.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using GitVersion.OutputVariables;
2+
3+
namespace GitVersion
4+
{
5+
public interface IExecuteCore
6+
{
7+
VersionVariables ExecuteGitVersion(Arguments arguments);
8+
bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication);
9+
}
10+
}

0 commit comments

Comments
 (0)