Skip to content

Commit 1a90870

Browse files
committed
minor cleanup
1 parent cc1f5b0 commit 1a90870

30 files changed

+272
-250
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
8080
});
8181

8282
var gitPreparer = sp.GetService<IGitPreparer>();
83+
gitPreparer.Prepare();
84+
8385
var gitVersionCalculator = sp.GetService<IGitVersionCalculator>();
8486

85-
gitPreparer.Prepare();
8687
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
8788

8889
Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
2525

2626
repository ??= fixture.Repository;
2727

28-
var options = Options.Create(new Arguments { OverrideConfig = configuration, TargetPath = repository.GetRepositoryDirectory() });
28+
var options = Options.Create(new Arguments
29+
{
30+
OverrideConfig = configuration,
31+
TargetPath = repository.GetRepositoryDirectory(),
32+
TargetBranch = branch,
33+
CommitId = commitId,
34+
OnlyTrackedBranches = onlyTrackedBranches
35+
});
2936

3037
var sp = ConfigureServices(services =>
3138
{
@@ -53,13 +60,9 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
5360
}
5461
}
5562

56-
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
57-
{
58-
fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, onlyTrackedBranches, targetBranch);
59-
}
60-
61-
public static void AssertFullSemver(this RepositoryFixtureBase fixture, Config configuration, string fullSemver, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
63+
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, Config configuration = null, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
6264
{
65+
configuration ??= new Config();
6366
configuration.Reset();
6467
Console.WriteLine("---------");
6568

src/GitVersionCore.Tests/GitVersionContextBuilder.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,21 @@ private GitVersionContextBuilder AddBranch(string branchName)
7272
return this;
7373
}
7474

75-
public GitVersionContext Build()
75+
public void Build()
7676
{
7777
var repo = repository ?? CreateRepository();
7878
var config = configuration ?? new Config();
7979

8080
config.Reset();
8181

82-
var options = Options.Create(new Arguments { OverrideConfig = config, TargetPath = repo.GetRepositoryDirectory() });
82+
var options = Options.Create(new Arguments { OverrideConfig = config });
8383

8484
ServicesProvider = ConfigureServices(services =>
8585
{
8686
services.AddSingleton(options);
87+
services.AddSingleton(repo);
8788
overrideServices?.Invoke(services);
8889
});
89-
var context = ServicesProvider.GetService<IOptions<GitVersionContext>>().Value;
90-
return context;
9190
}
9291

9392
private static IRepository CreateRepository()

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using System.Collections.Generic;
32
using GitTools.Testing;
43
using GitVersion;
@@ -26,17 +25,18 @@ public void CanInheritVersioningMode(VersioningMode mode)
2625
};
2726
config.Reset();
2827

29-
var mockBranch = new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
28+
var branchName = "master";
29+
var mockBranch = new MockBranch(branchName) { new MockCommit { CommitterEx = Generate.SignatureNow() } };
3030
var mockRepository = new MockRepository
3131
{
32+
Head = mockBranch,
3233
Branches = new MockBranchCollection
3334
{
3435
mockBranch
3536
}
3637
};
3738

38-
var gitVersionContextFactory = GetGitVersionContextFactory(config);
39-
var context = gitVersionContextFactory.Init(mockRepository, mockBranch);
39+
var context = GetGitVersionContext(mockRepository, branchName, config);
4040

4141
context.Configuration.VersioningMode.ShouldBe(mode);
4242
}
@@ -62,22 +62,22 @@ public void CanInheritIncrement(IncrementStrategy increment, IncrementStrategy?
6262
fixture.BranchTo(dummyBranchName);
6363
fixture.MakeACommit();
6464

65-
var gitVersionContextFactory = GetGitVersionContextFactory(config);
66-
var context = gitVersionContextFactory.Init(fixture.Repository, fixture.Repository.Branches[dummyBranchName]);
65+
var context = GetGitVersionContext(fixture.Repository, dummyBranchName, config);
6766

6867
context.Configuration.Increment.ShouldBe(alternateExpected ?? increment);
6968
}
7069

7170
[Test]
7271
public void UsesBranchSpecificConfigOverTopLevelDefaults()
7372
{
73+
var branchName = "develop";
7474
var config = new Config
7575
{
7676
VersioningMode = VersioningMode.ContinuousDelivery,
7777
Branches =
7878
{
7979
{
80-
"develop", new BranchConfig
80+
branchName, new BranchConfig
8181
{
8282
VersioningMode = VersioningMode.ContinuousDeployment,
8383
Tag = "alpha"
@@ -86,18 +86,18 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
8686
}
8787
};
8888
config.Reset();
89-
var develop = new MockBranch("develop") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
89+
var develop = new MockBranch(branchName) { new MockCommit { CommitterEx = Generate.SignatureNow() } };
9090
var mockRepository = new MockRepository
9191
{
92+
Head = develop,
9293
Branches = new MockBranchCollection
9394
{
9495
new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } },
9596
develop
9697
}
9798
};
9899

99-
var gitVersionContextFactory = GetGitVersionContextFactory(config);
100-
var context = gitVersionContextFactory.Init(mockRepository, develop);
100+
var context = GetGitVersionContext(mockRepository, branchName, config);
101101

102102
context.Configuration.Tag.ShouldBe("alpha");
103103
}
@@ -134,15 +134,15 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
134134
{
135135
releaseLatestBranch,
136136
releaseVersionBranch
137-
}
137+
},
138+
Head = releaseLatestBranch
138139
};
139140

140-
var gitVersionContextFactory = GetGitVersionContextFactory(config);
141-
var latestContext = gitVersionContextFactory.Init(mockRepository, releaseLatestBranch);
142-
141+
var latestContext = GetGitVersionContext(mockRepository, releaseLatestBranch.CanonicalName, config);
143142
latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None);
144143

145-
var versionContext = gitVersionContextFactory.Init(mockRepository, releaseVersionBranch);
144+
mockRepository.Head = releaseVersionBranch;
145+
var versionContext = GetGitVersionContext(mockRepository, releaseVersionBranch.CanonicalName, config);
146146
versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch);
147147
}
148148

@@ -166,18 +166,35 @@ public void CanFindParentBranchForInheritingIncrementStrategy()
166166
Commands.Checkout(repo.Repository, featureBranch);
167167
repo.Repository.MakeACommit();
168168

169-
var gitVersionContextFactory = GetGitVersionContextFactory(config);
170-
var context = gitVersionContextFactory.Init(repo.Repository, repo.Repository.Head);
169+
var context = GetGitVersionContext(repo.Repository, "develop", config);
171170

172171
context.Configuration.Increment.ShouldBe(IncrementStrategy.Major);
173172
}
174173

175-
private static IGitVersionContextFactory GetGitVersionContextFactory(Config config = null)
174+
private static GitVersionContext GetGitVersionContext(IRepository repository, string branch, Config config = null)
175+
{
176+
config ??= new Config().ApplyDefaults();
177+
var options = Options.Create(new Arguments { OverrideConfig = config, TargetBranch = branch });
178+
179+
var sp = ConfigureServices(services =>
180+
{
181+
services.AddSingleton(options);
182+
services.AddSingleton(repository);
183+
});
184+
185+
return sp.GetService<IOptions<GitVersionContext>>().Value;
186+
}
187+
188+
private static IGitVersionContextFactory GetGitVersionContextFactory(IRepository repository, Config config = null)
176189
{
177190
config ??= new Config().ApplyDefaults();
178191
var options = Options.Create(new Arguments { OverrideConfig = config });
179192

180-
var sp = ConfigureServices(services => { services.AddSingleton(options); });
193+
var sp = ConfigureServices(services =>
194+
{
195+
services.AddSingleton(options);
196+
services.AddSingleton(repository);
197+
});
181198

182199
return sp.GetService<IGitVersionContextFactory>();
183200
}

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void GitPreparerShouldNotFailWhenTargetPathNotInitialized()
6666
{
6767
sp = GetServiceProvider(arguments);
6868

69-
_ = sp.GetService<IGitPreparer>();
69+
sp.GetService<IGitPreparer>();
7070
});
7171
}
7272

@@ -240,9 +240,9 @@ public void CacheFileIsMissing()
240240

241241
var arguments = new Arguments { TargetPath = fixture.RepositoryPath };
242242

243-
var gitVersionCalculator = GetGitVersionCalculator(arguments, log);
244-
245243
fixture.Repository.MakeACommit();
244+
var gitVersionCalculator = GetGitVersionCalculator(arguments, log, fixture.Repository);
245+
246246
gitVersionCalculator.CalculateVersionVariables();
247247

248248
var logsMessages = stringBuilder.ToString();
@@ -290,9 +290,9 @@ public void ConfigChangeInvalidatesCache()
290290

291291
var arguments = new Arguments { TargetPath = fixture.RepositoryPath };
292292

293-
var gitVersionCalculator = GetGitVersionCalculator(arguments);
294-
295293
fixture.Repository.MakeACommit();
294+
295+
var gitVersionCalculator = GetGitVersionCalculator(arguments);
296296
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
297297

298298
versionVariables.AssemblySemVer.ShouldBe("0.1.0.0");
@@ -306,6 +306,8 @@ public void ConfigChangeInvalidatesCache()
306306
var configPath = Path.Combine(fixture.RepositoryPath, DefaultConfigFileLocator.DefaultFileName);
307307
fileSystem.WriteAllText(configPath, "next-version: 5.0");
308308

309+
gitVersionCalculator = GetGitVersionCalculator(arguments, fs: fileSystem);
310+
309311
versionVariables = gitVersionCalculator.CalculateVersionVariables();
310312
versionVariables.AssemblySemVer.ShouldBe("5.0.0.0");
311313
}
@@ -351,9 +353,9 @@ public void NoCacheBypassesCache()
351353

352354
var arguments = new Arguments { TargetPath = fixture.RepositoryPath };
353355

356+
fixture.Repository.MakeACommit();
354357
var gitVersionCalculator = GetGitVersionCalculator(arguments);
355358

356-
fixture.Repository.MakeACommit();
357359
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
358360

359361
versionVariables.AssemblySemVer.ShouldBe("0.1.0.0");
@@ -373,9 +375,12 @@ public void WorkingDirectoryWithoutGit()
373375
{
374376
var arguments = new Arguments { TargetPath = Environment.SystemDirectory };
375377

376-
var gitVersionCalculator = GetGitVersionCalculator(arguments);
377378

378-
var exception = Assert.Throws<DirectoryNotFoundException>(() => gitVersionCalculator.CalculateVersionVariables());
379+
var exception = Assert.Throws<DirectoryNotFoundException>(() =>
380+
{
381+
var gitVersionCalculator = GetGitVersionCalculator(arguments);
382+
gitVersionCalculator.CalculateVersionVariables();
383+
});
379384
exception.Message.ShouldContain("Can't find the .git directory in");
380385
}
381386

@@ -434,14 +439,16 @@ public void GetProjectRootDirectoryNoWorktree()
434439
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
435440
{
436441
using var fixture = new EmptyRepositoryFixture();
442+
fixture.Repository.MakeACommit();
443+
437444
var arguments = new Arguments
438445
{
439446
TargetPath = fixture.RepositoryPath,
440447
TargetUrl = "https://github.com/GitTools/GitVersion.git",
441448
TargetBranch = "refs/head/master"
442449
};
443450

444-
var gitVersionCalculator = GetGitVersionCalculator(arguments);
451+
var gitVersionCalculator = GetGitVersionCalculator(arguments, repository: fixture.Repository);
445452
gitPreparer.Prepare();
446453
gitVersionCalculator.CalculateVersionVariables();
447454
}
@@ -498,9 +505,9 @@ public void GetDotGitDirectoryWorktree()
498505
}
499506
}
500507

501-
private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog logger = null)
508+
private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
502509
{
503-
sp = GetServiceProvider(arguments, logger);
510+
sp = GetServiceProvider(arguments, logger, repository, fs);
504511

505512
fileSystem = sp.GetService<IFileSystem>();
506513
log = sp.GetService<ILog>();
@@ -510,11 +517,13 @@ private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog
510517
return sp.GetService<IGitVersionCalculator>();
511518
}
512519

513-
private static IServiceProvider GetServiceProvider(Arguments arguments, ILog log = null)
520+
private static IServiceProvider GetServiceProvider(Arguments arguments, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null)
514521
{
515522
return ConfigureServices(services =>
516523
{
517524
if (log != null) services.AddSingleton(log);
525+
if (fileSystem != null) services.AddSingleton(fileSystem);
526+
if (repository != null) services.AddSingleton(repository);
518527
services.AddSingleton(Options.Create(arguments));
519528
});
520529
}

src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenarios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void CanTakeVersionFromReleaseBranch()
1717
fixture.Repository.CreateBranch("release-4.0.123");
1818
fixture.Checkout(commit.Sha);
1919

20-
fixture.AssertFullSemver("4.0.123-beta.1+0", fixture.Repository, commit.Sha, onlyTrackedBranches: false, targetBranch: "release-4.0.123");
20+
fixture.AssertFullSemver("4.0.123-beta.1+0", null, fixture.Repository, commit.Sha, onlyTrackedBranches: false, targetBranch: "release-4.0.123");
2121
}
2222

2323
[Test]

src/GitVersionCore.Tests/IntegrationTests/DevelopScenarios.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void CanChangeDevelopTagViaConfig()
7272
fixture.Repository.MakeATaggedCommit("1.0.0");
7373
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("develop"));
7474
fixture.Repository.MakeACommit();
75-
fixture.AssertFullSemver(config, "1.1.0-alpha.1");
75+
fixture.AssertFullSemver("1.1.0-alpha.1", config);
7676
}
7777

7878
[Test]
@@ -130,7 +130,7 @@ public void CanHandleContinuousDelivery()
130130
fixture.Repository.MakeATaggedCommit("1.0.0");
131131
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("develop"));
132132
fixture.Repository.MakeATaggedCommit("1.1.0-alpha7");
133-
fixture.AssertFullSemver(config, "1.1.0-alpha.7");
133+
fixture.AssertFullSemver("1.1.0-alpha.7", config);
134134
}
135135

136136
[Test]
@@ -240,7 +240,7 @@ public void CommitsSinceVersionSourceShouldNotGoDownUponGitFlowReleaseFinish()
240240
fixture.Repository.Branches.Remove("release/1.2.0");
241241

242242
var expectedFullSemVer = "1.3.0-alpha.9";
243-
fixture.AssertFullSemver(config, expectedFullSemVer);
243+
fixture.AssertFullSemver(expectedFullSemVer, config);
244244
}
245245

246246
[Test]
@@ -272,7 +272,7 @@ public void CommitsSinceVersionSourceShouldNotGoDownUponMergingFeatureOnlyToDeve
272272
fixture.Repository.Branches.Remove("release/1.2.0");
273273

274274
var expectedFullSemVer = "1.3.0-alpha.5";
275-
fixture.AssertFullSemver(config, expectedFullSemVer);
275+
fixture.AssertFullSemver(expectedFullSemVer, config);
276276
}
277277

278278
[Test]

0 commit comments

Comments
 (0)