forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitVersionExecutorTests.cs
More file actions
631 lines (484 loc) · 25.4 KB
/
GitVersionExecutorTests.cs
File metadata and controls
631 lines (484 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System.IO.Abstractions;
using GitVersion.Agents;
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Helpers;
using GitVersion.Logging;
using GitVersion.Testing.Extensions;
using GitVersion.VersionCalculation.Caching;
using LibGit2Sharp;
namespace GitVersion.Core.Tests;
[TestFixture]
[Parallelizable(ParallelScope.None)]
public class GitVersionExecutorTests : TestBase
{
private IFileSystem fileSystem;
private ILog log;
private GitVersionCacheProvider gitVersionCacheProvider;
private IServiceProvider sp;
private const string versionCacheFileContent =
"""
{
"AssemblySemFileVer": "4.10.3.0",
"AssemblySemVer": "4.10.3.0",
"BranchName": "feature/test",
"BuildMetaData": null,
"CommitDate": "2015-11-10T00:00:00.000Z",
"CommitsSinceVersionSource": 19,
"EscapedBranchName": "feature-test",
"FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
"FullSemVer": "4.10.3-test.19",
"InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
"Major": 4,
"MajorMinorPatch": "4.10.3",
"Minor": 10,
"Patch": 3,
"PreReleaseLabel": "test",
"PreReleaseLabelWithDash": "-test",
"PreReleaseNumber": 19,
"PreReleaseTag": "test.19",
"PreReleaseTagWithDash": "-test.19",
"SemVer": "4.10.3-test.19",
"Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
"ShortSha": "dd2a29af",
"UncommittedChanges": 0,
"VersionSourceDistance": 19,
"VersionSourceSemVer": "4.10.2",
"VersionSourceSha": "4.10.2",
"WeightedPreReleaseNumber": 19
}
""";
[Test]
public void CacheKeySameAfterReNormalizing()
{
using var fixture = new EmptyRepositoryFixture();
const string targetUrl = "https://github.com/GitTools/GitVersion.git";
const string targetBranch = $"refs/head/{MainBranch}";
var gitVersionOptions = new GitVersionOptions { RepositoryInfo = { TargetUrl = targetUrl, TargetBranch = targetBranch }, WorkingDirectory = fixture.RepositoryPath, Settings = { NoNormalize = false } };
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
sp.DiscoverRepository();
var preparer = this.sp.GetRequiredService<IGitPreparer>();
preparer.Prepare();
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
var cacheKey1 = cacheKeyFactory.Create(null);
preparer.Prepare();
var cacheKey2 = cacheKeyFactory.Create(null);
cacheKey2.Value.ShouldBe(cacheKey1.Value);
}
[Test]
public void GitPreparerShouldNotFailWhenTargetPathNotInitialized()
{
const string targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitVersionOptions = new GitVersionOptions { RepositoryInfo = { TargetUrl = targetUrl }, WorkingDirectory = string.Empty };
Should.NotThrow(() =>
{
this.sp = GetServiceProvider(gitVersionOptions);
this.sp.GetRequiredService<IGitPreparer>();
});
}
[Test]
public void CacheKeyForWorktree()
{
this.fileSystem = new FileSystem();
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
var worktreePath = GetWorktreePath(fixture);
try
{
// create a branch and a new worktree for it
var repo = new Repository(fixture.RepositoryPath);
repo.Worktrees.Add("worktree", worktreePath, false);
const string targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitVersionOptions = new GitVersionOptions { RepositoryInfo = { TargetUrl = targetUrl, TargetBranch = MainBranch }, WorkingDirectory = worktreePath };
this.sp = GetServiceProvider(gitVersionOptions);
sp.DiscoverRepository();
var preparer = this.sp.GetRequiredService<IGitPreparer>();
preparer.Prepare();
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
var cacheKey = cacheKeyFactory.Create(null);
cacheKey.Value.ShouldNotBeEmpty();
}
finally
{
FileSystemHelper.Directory.DeleteDirectory(worktreePath);
}
}
[Test]
public void CacheFileExistsOnDisk()
{
var stringBuilder = new StringBuilder();
var logAppender = new TestLogAppender(Action);
this.log = new Log(logAppender);
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, this.log);
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("0.0.1.0");
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
var cacheKey = cacheKeyFactory.Create(null);
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
this.fileSystem.File.WriteAllText(cacheFileName, versionCacheFileContent);
versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("4.10.3.0");
var logsMessages = stringBuilder.ToString();
logsMessages.ShouldContain("Loading version variables from disk cache file", Case.Insensitive, logsMessages);
return;
void Action(string s) => stringBuilder.AppendLine(s);
}
[Test]
public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDynamicallyCalculatedWithoutSavingInCache()
{
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, this.log);
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("0.0.1.0");
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
var cacheKey = cacheKeyFactory.Create(null);
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
this.fileSystem.File.WriteAllText(cacheFileName, versionCacheFileContent);
var cacheDirectory = this.gitVersionCacheProvider.GetCacheDirectory();
var cacheDirectoryTimestamp = this.fileSystem.GetLastDirectoryWrite(cacheDirectory);
var configuration = GitFlowConfigurationBuilder.New.WithTagPrefixPattern("prefix").Build();
var overrideConfiguration = new ConfigurationHelper(configuration).Dictionary;
gitVersionOptions = new() { WorkingDirectory = fixture.RepositoryPath, ConfigurationInfo = { OverrideConfiguration = overrideConfiguration } };
gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("0.0.1.0");
var cachedDirectoryTimestampAfter = this.fileSystem.GetLastDirectoryWrite(cacheDirectory);
cachedDirectoryTimestampAfter.ShouldBe(cacheDirectoryTimestamp, "Cache was updated when override configuration was set");
}
[Test]
public void CacheFileIsMissing()
{
var stringBuilder = new StringBuilder();
var logAppender = new TestLogAppender(Action);
this.log = new Log(logAppender);
using var fixture = new EmptyRepositoryFixture();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
fixture.Repository.MakeACommit();
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, this.log, fixture.Repository.ToGitRepository());
gitVersionCalculator.CalculateVersionVariables();
var logsMessages = stringBuilder.ToString();
logsMessages.ShouldMatch("(?s).*Cache file.*(?-s) not found.*");
return;
void Action(string s) => stringBuilder.AppendLine(s);
}
[TestCase(ConfigurationFileLocator.DefaultFileName)]
[TestCase(ConfigurationFileLocator.DefaultAlternativeFileName)]
[TestCase(ConfigurationFileLocator.DefaultFileNameDotted)]
[TestCase(ConfigurationFileLocator.DefaultAlternativeFileNameDotted)]
public void ConfigChangeInvalidatesCache(string configFileName)
{
using var fixture = new EmptyRepositoryFixture();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
fixture.Repository.MakeACommit();
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("0.0.1.0");
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
var cacheKey = cacheKeyFactory.Create(null);
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
this.fileSystem.File.WriteAllText(cacheFileName, versionCacheFileContent);
versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("4.10.3.0");
var configPath = FileSystemHelper.Path.Combine(fixture.RepositoryPath, configFileName);
this.fileSystem.File.WriteAllText(configPath, "next-version: 5.0.0");
gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, fs: this.fileSystem);
versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("5.0.0.0");
}
[Test]
public void NoCacheBypassesCache()
{
using var fixture = new EmptyRepositoryFixture();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
fixture.Repository.MakeACommit();
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("0.0.1.0");
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
var cacheKey = cacheKeyFactory.Create(null);
var cacheFileName = this.gitVersionCacheProvider.GetCacheFileName(cacheKey);
this.fileSystem.File.WriteAllText(cacheFileName, versionCacheFileContent);
versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("4.10.3.0");
gitVersionOptions.Settings.NoCache = true;
versionVariables = gitVersionCalculator.CalculateVersionVariables();
versionVariables.AssemblySemVer.ShouldBe("0.0.1.0");
}
[Test]
public void WorkingDirectoryWithoutGit()
{
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = SysEnv.SystemDirectory };
var exception = Assert.Throws<DirectoryNotFoundException>(() =>
{
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
gitVersionCalculator.CalculateVersionVariables();
});
exception?.Message.ShouldContain("Cannot find the .git directory");
}
[Test]
public void WorkingDirectoryWithoutCommits()
{
using var fixture = new EmptyRepositoryFixture();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
var exception = Assert.Throws<GitVersionException>(() =>
{
var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
gitVersionCalculator.CalculateVersionVariables();
});
exception?.Message.ShouldContain("No commits found on the current branch.");
}
[Test]
public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()
{
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
var worktreePath = GetWorktreePath(fixture);
try
{
// create a branch and a new worktree for it
var repo = new Repository(fixture.RepositoryPath);
repo.Worktrees.Add("worktree", worktreePath, false);
const string targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitVersionOptions = new GitVersionOptions { RepositoryInfo = { TargetUrl = targetUrl }, WorkingDirectory = worktreePath };
this.sp = GetServiceProvider(gitVersionOptions);
var repositoryInfo = this.sp.GetRequiredService<IGitRepositoryInfo>();
repositoryInfo.ProjectRootDirectory?.TrimEnd('/', '\\').ShouldBe(worktreePath);
}
finally
{
FileSystemHelper.Directory.DeleteDirectory(worktreePath);
}
}
[Test]
public void GetProjectRootDirectoryNoWorktree()
{
using var fixture = new EmptyRepositoryFixture();
const string targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitVersionOptions = new GitVersionOptions { RepositoryInfo = { TargetUrl = targetUrl }, WorkingDirectory = fixture.RepositoryPath };
this.sp = GetServiceProvider(gitVersionOptions);
var repositoryInfo = this.sp.GetRequiredService<IGitRepositoryInfo>();
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
repositoryInfo.ProjectRootDirectory?.TrimEnd('/', '\\').ShouldBe(expectedPath);
}
[Test]
public void GetDotGitDirectoryNoWorktree()
{
using var fixture = new EmptyRepositoryFixture();
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath };
this.sp = GetServiceProvider(gitVersionOptions);
var repositoryInfo = this.sp.GetRequiredService<IGitRepositoryInfo>();
var expectedPath = FileSystemHelper.Path.Combine(fixture.RepositoryPath, ".git");
repositoryInfo.DotGitDirectory.ShouldBe(expectedPath);
}
[Test]
public void GetDotGitDirectoryWorktree()
{
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
var worktreePath = GetWorktreePath(fixture);
try
{
// create a branch and a new worktree for it
var repo = new Repository(fixture.RepositoryPath);
repo.Worktrees.Add("worktree", worktreePath, false);
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreePath };
this.sp = GetServiceProvider(gitVersionOptions);
var repositoryInfo = this.sp.GetRequiredService<IGitRepositoryInfo>();
var expectedPath = FileSystemHelper.Path.Combine(fixture.RepositoryPath, ".git");
repositoryInfo.DotGitDirectory.ShouldBe(expectedPath);
}
finally
{
FileSystemHelper.Directory.DeleteDirectory(worktreePath);
}
}
[Test]
public void CalculateVersionFromWorktreeHead()
{
// Setup
this.fileSystem = new FileSystem();
using var fixture = new EmptyRepositoryFixture();
var repoDir = fileSystem.DirectoryInfo.New(fixture.RepositoryPath);
var worktreePath = FileSystemHelper.Path.Combine(repoDir.Parent?.FullName, $"{repoDir.Name}-v1");
fixture.Repository.MakeATaggedCommit("v1.0.0");
var branchV1 = fixture.Repository.CreateBranch("support/1.0");
fixture.Repository.MakeATaggedCommit("v2.0.0");
fixture.Repository.Worktrees.Add(branchV1.CanonicalName, "1.0", worktreePath, false);
using var worktreeFixture = new LocalRepositoryFixture(new(worktreePath));
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture.RepositoryPath };
var sut = GetGitVersionCalculator(gitVersionOptions, fs: this.fileSystem);
// Execute
var version = sut.CalculateVersionVariables();
// Verify
version.SemVer.ShouldBe("1.0.0");
version.VersionSourceSemVer.ShouldBe("1.0.0");
var commits = worktreeFixture.Repository.Head.Commits;
version.Sha.ShouldBe(commits.First().Sha);
}
[Test]
public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndNotTagged_ThrowException()
{
// Setup
using var fixture = new RemoteRepositoryFixture();
fixture.LocalRepositoryFixture.Repository.MakeACommit("Init commit");
fixture.LocalRepositoryFixture.Repository.CreateBranch("feature/1.0");
fixture.LocalRepositoryFixture.Checkout("feature/1.0");
var commit = fixture.LocalRepositoryFixture.Repository.MakeACommit("feat: a new commit");
fixture.LocalRepositoryFixture.Repository.CreateBranch("support/1.0");
fixture.LocalRepositoryFixture.Checkout(commit.Sha);
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture.RepositoryPath };
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
sp.DiscoverRepository();
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
// Execute & Verify
var exception = Assert.Throws<WarningException>(() => sut.CalculateVersionVariables());
exception?.Message.ShouldBe("Failed to try and guess branch to use. Move one of the branches along a commit to remove warning");
}
[Test]
public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagged_ReturnSemver()
{
// Setup
using var fixture = new RemoteRepositoryFixture();
fixture.LocalRepositoryFixture.Repository.MakeACommit("Init commit");
fixture.LocalRepositoryFixture.Repository.CreateBranch("feature/1.0");
fixture.LocalRepositoryFixture.Checkout("feature/1.0");
var commit = fixture.LocalRepositoryFixture.Repository.MakeACommit("feat: a new commit");
fixture.LocalRepositoryFixture.Repository.CreateBranch("support/1.0");
fixture.LocalRepositoryFixture.ApplyTag("1.0.1");
fixture.LocalRepositoryFixture.Checkout(commit.Sha);
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture.RepositoryPath };
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
sp.DiscoverRepository();
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
// Execute
var version = sut.CalculateVersionVariables();
// Verify
version.SemVer.ShouldBe("1.0.1");
version.VersionSourceSemVer.ShouldBe("1.0.1");
var commits = worktreeFixture.Repository.Head.Commits;
version.Sha.ShouldBe(commits.First().Sha);
}
[Test]
public void CalculateVersionVariables_ShallowFetch_ThrowException()
{
// Setup
using var fixture = new RemoteRepositoryFixture();
fixture.LocalRepositoryFixture.MakeShallow();
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture.RepositoryPath };
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
sp.DiscoverRepository();
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
// Execute & Verify
var exception = Assert.Throws<WarningException>(() => sut.CalculateVersionVariables());
exception?.Message.ShouldBe("Repository is a shallow clone. Git repositories must contain the full history. See https://gitversion.net/docs/reference/requirements#unshallow for more info.");
}
[Test]
public void CalculateVersionVariables_ShallowFetch_WithAllowShallow_ShouldNotThrowException()
{
// Setup
using var fixture = new RemoteRepositoryFixture();
fixture.LocalRepositoryFixture.MakeShallow();
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
var gitVersionOptions = new GitVersionOptions
{
WorkingDirectory = worktreeFixture.RepositoryPath,
Settings = { AllowShallow = true }
};
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
sp.DiscoverRepository();
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
// Execute
var version = sut.CalculateVersionVariables();
// Verify
version.ShouldNotBeNull();
var commits = worktreeFixture.Repository.Head.Commits;
version.Sha.ShouldBe(commits.First().Sha);
}
[Test]
public void CalculateVersionVariables_WithLimitedCloneDepth_AndAllowShallowTrue_ShouldCalculateVersionCorrectly()
{
// Setup
using var fixture = new RemoteRepositoryFixture();
fixture.LocalRepositoryFixture.MakeShallow();
fixture.LocalRepositoryFixture.Repository.MakeACommit("Initial commit");
fixture.LocalRepositoryFixture.Repository.MakeATaggedCommit("1.0.0");
var latestCommit = fixture.LocalRepositoryFixture.Repository.MakeACommit("+semver:major");
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
var gitVersionOptions = new GitVersionOptions
{
WorkingDirectory = worktreeFixture.RepositoryPath,
Settings = { AllowShallow = true }
};
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
sp.DiscoverRepository();
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
// Execute
var version = sut.CalculateVersionVariables();
// Verify
version.ShouldNotBeNull();
// Verify that the correct commit is used
version.Sha.ShouldBe(latestCommit.Sha);
version.MajorMinorPatch.ShouldBe("2.0.0");
version.VersionSourceSemVer.ShouldBe("1.0.0");
// Verify repository is still recognized as shallow
var repository = this.sp.GetRequiredService<IGitRepository>();
repository.IsShallow.ShouldBeTrue("Repository should still be shallow after version calculation");
}
private string GetWorktreePath(EmptyRepositoryFixture fixture)
{
var worktreePath = FileSystemHelper.Path.Combine(this.fileSystem.Directory.GetParent(fixture.RepositoryPath)?.FullName, Guid.NewGuid().ToString());
return worktreePath;
}
private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog? logger = null, IGitRepository? repository = null, IFileSystem? fs = null)
{
this.sp = GetServiceProvider(gitVersionOptions, logger, repository, fs);
this.fileSystem = this.sp.GetRequiredService<IFileSystem>();
this.log = this.sp.GetRequiredService<ILog>();
this.gitVersionCacheProvider = (GitVersionCacheProvider)this.sp.GetRequiredService<IGitVersionCacheProvider>();
sp.DiscoverRepository();
return this.sp.GetRequiredService<IGitVersionCalculateTool>();
}
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog? log = null, IGitRepository? repository = null, IFileSystem? fileSystem = null, IEnvironment? environment = null) =>
ConfigureServices(services =>
{
services.AddSingleton<IGitVersionContextFactory, GitVersionContextFactory>();
services.AddSingleton(sp =>
{
var contextFactory = sp.GetRequiredService<IGitVersionContextFactory>();
return new Lazy<GitVersionContext>(() => contextFactory.Create());
});
if (log != null) services.AddSingleton(log);
if (fileSystem != null) services.AddSingleton(fileSystem);
if (repository != null) services.AddSingleton(repository);
if (environment != null) services.AddSingleton(environment);
var options = Options.Create(gitVersionOptions);
services.AddSingleton(options);
services.AddSingleton<IGitRepositoryInfo>(sp =>
{
var fs = sp.GetRequiredService<IFileSystem>();
return new GitRepositoryInfo(fs, options);
});
});
}