/
VersionFileTests.cs
614 lines (532 loc) · 27.1 KB
/
VersionFileTests.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibGit2Sharp;
using Nerdbank.GitVersioning;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;
using Version = System.Version;
[Trait("Engine", "Managed")]
public class VersionFileManagedTests : VersionFileTests
{
public VersionFileManagedTests(ITestOutputHelper logger)
: base(logger)
{
}
protected override GitContext CreateGitContext(string path, string committish = null)
=> GitContext.Create(path, committish, writable: false);
}
[Trait("Engine", "LibGit2")]
public class VersionFileLibGit2Tests : VersionFileTests
{
public VersionFileLibGit2Tests(ITestOutputHelper logger)
: base(logger)
{
}
protected override GitContext CreateGitContext(string path, string committish = null)
=> GitContext.Create(path, committish, writable: true);
}
public abstract class VersionFileTests : RepoTestBase
{
private string versionTxtPath;
private string versionJsonPath;
public VersionFileTests(ITestOutputHelper logger)
: base(logger)
{
this.versionTxtPath = Path.Combine(this.RepoPath, VersionFile.TxtFileName);
this.versionJsonPath = Path.Combine(this.RepoPath, VersionFile.JsonFileName);
}
[Fact]
public void IsVersionDefined_Commit()
{
this.InitializeSourceControl();
this.AddCommits();
Assert.False(this.Context.VersionFile.IsVersionDefined());
this.WriteVersionFile();
// Verify that we can find the version.txt file in the most recent commit,
// But not in the initial commit.
using var tipContext = this.CreateGitContext(this.RepoPath, this.LibGit2Repository.Head.Commits.First().Sha);
using var initialContext = this.CreateGitContext(this.RepoPath, this.LibGit2Repository.Head.Commits.Last().Sha);
Assert.True(tipContext.VersionFile.IsVersionDefined());
Assert.False(initialContext.VersionFile.IsVersionDefined());
}
[Fact]
public void IsVersionDefined_String_ConsiderAncestorFolders()
{
// Construct a repo where versions are defined like this:
/* root <- 1.0
a (inherits 1.0)
b <- 1.1
c (inherits 1.1)
*/
this.Context.VersionFile.SetVersion(this.RepoPath, new Version(1, 0));
string subDirA = Path.Combine(this.RepoPath, "a");
string subDirAB = Path.Combine(subDirA, "b");
string subDirABC = Path.Combine(subDirAB, "c");
Directory.CreateDirectory(subDirABC);
this.Context.VersionFile.SetVersion(subDirAB, new Version(1, 1));
using var subDirABCContext = this.CreateGitContext(subDirABC);
using var subDirABContext = this.CreateGitContext(subDirAB);
using var subDirAContext = this.CreateGitContext(subDirA);
Assert.True(subDirABCContext.VersionFile.IsVersionDefined());
Assert.True(subDirABContext.VersionFile.IsVersionDefined());
Assert.True(subDirAContext.VersionFile.IsVersionDefined());
Assert.True(this.Context.VersionFile.IsVersionDefined());
}
[Theory]
[InlineData("2.3", null, null, 0, null, @"{""version"":""2.3""}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, 0, null, @"{""version"":""2.3"",""assemblyVersion"":""2.2""}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, new[] { "refs/heads/master" }, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""versionHeightOffset"":-1,""publicReleaseRefSpec"":[""refs/heads/master""]}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, new[] { "refs/heads/master" }, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""buildNumberOffset"":-1,""publicReleaseRefSpec"":[""refs/heads/master""]}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, 0, null, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2""}}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Revision, 0, null, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"", ""precision"":""revision""}}")]
public void GetVersion_JsonCompatibility(string version, string assemblyVersion, object precision, int versionHeightOffset, string[] publicReleaseRefSpec, string json)
{
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), json);
var options = this.Context.VersionFile.GetVersion();
Assert.NotNull(options);
Assert.Equal(version, options.Version?.ToString());
Assert.Equal(assemblyVersion, options.AssemblyVersion?.Version?.ToString());
Assert.Equal(precision, options.AssemblyVersion?.PrecisionOrDefault);
Assert.Equal(versionHeightOffset, options.VersionHeightOffsetOrDefault);
Assert.Equal(publicReleaseRefSpec, options.PublicReleaseRefSpec);
}
[Theory]
[InlineData("2.3", "")]
[InlineData("2.3", null)]
[InlineData("2.3", "-beta")]
[InlineData("2.3.0", "")]
[InlineData("2.3.0", "-rc")]
public void SetVersion_GetVersionFromFile(string expectedVersion, string expectedPrerelease)
{
string pathWritten = this.Context.VersionFile.SetVersion(this.RepoPath, new Version(expectedVersion), expectedPrerelease);
Assert.Equal(Path.Combine(this.RepoPath, VersionFile.JsonFileName), pathWritten);
string actualFileContent = File.ReadAllText(pathWritten);
this.Logger.WriteLine(actualFileContent);
VersionOptions actualVersion = this.Context.VersionFile.GetVersion();
Assert.Equal(new Version(expectedVersion), actualVersion.Version.Version);
Assert.Equal(expectedPrerelease ?? string.Empty, actualVersion.Version.Prerelease);
}
[Theory]
[InlineData("2.3", null, VersionOptions.VersionPrecision.Minor, 0, false, @"{""version"":""2.3""}")]
[InlineData("2.3", null, VersionOptions.VersionPrecision.Minor, null, true, @"{""version"":""2.3"",""assemblyVersion"":{""precision"":""minor""},""inherit"":true}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, 0, false, @"{""version"":""2.3"",""assemblyVersion"":""2.2""}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Minor, -1, false, @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""versionHeightOffset"":-1}")]
[InlineData("2.3", "2.2", VersionOptions.VersionPrecision.Revision, -1, false, @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"",""precision"":""revision""},""versionHeightOffset"":-1}")]
public void SetVersion_WritesSimplestFile(string version, string assemblyVersion, VersionOptions.VersionPrecision? precision, int? versionHeightOffset, bool inherit, string expectedJson)
{
var versionOptions = new VersionOptions
{
Version = SemanticVersion.Parse(version),
AssemblyVersion = assemblyVersion is not null || precision is not null ? new VersionOptions.AssemblyVersionOptions(assemblyVersion is not null ? new Version(assemblyVersion) : null, precision) : null,
VersionHeightOffset = versionHeightOffset,
Inherit = inherit,
};
string pathWritten = this.Context.VersionFile.SetVersion(this.RepoPath, versionOptions, includeSchemaProperty: false);
string actualFileContent = File.ReadAllText(pathWritten);
this.Logger.WriteLine(actualFileContent);
string normalizedFileContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(actualFileContent));
Assert.Equal(expectedJson, normalizedFileContent);
}
[Fact]
public void SetVersion_PathFilters_OutsideGitRepo()
{
var versionOptions = new VersionOptions
{
Version = SemanticVersion.Parse("1.2"),
PathFilters = new[]
{
new FilterPath("./foo", ""),
}
};
this.Context.VersionFile.SetVersion(this.RepoPath, versionOptions);
}
[Fact]
public void SetVersion_PathFilters_DifferentRelativePaths()
{
this.InitializeSourceControl();
var versionOptions = new VersionOptions
{
Version = SemanticVersion.Parse("1.2"),
PathFilters = new[]
{
new FilterPath("./foo", "bar"),
new FilterPath("/absolute", "bar"),
}
};
var expected = versionOptions.PathFilters.Select(x => x.RepoRelativePath).ToList();
var projectDirectory = Path.Combine(this.RepoPath, "quux");
this.Context.VersionFile.SetVersion(projectDirectory, versionOptions);
using var projectContext = this.CreateGitContext(projectDirectory);
var actualVersionOptions = projectContext.VersionFile.GetVersion();
var actual = actualVersionOptions.PathFilters.Select(x => x.RepoRelativePath).ToList();
Assert.Equal(expected, actual);
}
[Fact]
public void SetVersion_PathFilters_InheritRelativePaths()
{
this.InitializeSourceControl();
var rootVersionOptions = new VersionOptions
{
Version = SemanticVersion.Parse("1.2"),
PathFilters = new[]
{
new FilterPath("./root-file.txt", ""),
new FilterPath("/absolute", ""),
}
};
this.Context.VersionFile.SetVersion(this.RepoPath, rootVersionOptions);
var versionOptions = new VersionOptions
{
Version = SemanticVersion.Parse("1.2"),
Inherit = true
};
var projectDirectory = Path.Combine(this.RepoPath, "quux");
this.Context.VersionFile.SetVersion(projectDirectory, versionOptions);
var expected = rootVersionOptions.PathFilters.Select(x => x.RepoRelativePath).ToList();
using var projectContext = this.CreateGitContext(projectDirectory);
var actualVersionOptions = projectContext.VersionFile.GetVersion();
var actual = actualVersionOptions.PathFilters.Select(x => x.RepoRelativePath).ToList();
Assert.Equal(expected, actual);
}
[Fact]
public void SetVersion_PathFilters_InheritOverride()
{
this.InitializeSourceControl();
var rootVersionOptions = new VersionOptions
{
Version = SemanticVersion.Parse("1.2"),
PathFilters = new[]
{
new FilterPath("./root-file.txt", ""),
new FilterPath("/absolute", ""),
}
};
this.Context.VersionFile.SetVersion(this.RepoPath, rootVersionOptions);
var versionOptions = new VersionOptions
{
Version = SemanticVersion.Parse("1.2"),
Inherit = true,
PathFilters = new[]
{
new FilterPath("./project-file.txt", "quux"),
new FilterPath("/absolute", "quux"),
}
};
var projectDirectory = Path.Combine(this.RepoPath, "quux");
this.Context.VersionFile.SetVersion(projectDirectory, versionOptions);
var expected = versionOptions.PathFilters.Select(x => x.RepoRelativePath).ToList();
using var projectContext = this.CreateGitContext(projectDirectory);
var actualVersionOptions = projectContext.VersionFile.GetVersion();
var actual = actualVersionOptions.PathFilters.Select(x => x.RepoRelativePath).ToList();
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(@"{""cloudBuild"":{""buildNumber"":{""enabled"":false,""includeCommitId"":{""when"":""nonPublicReleaseOnly"",""where"":""buildMetadata""}}}}", @"{}")]
[InlineData(@"{""cloudBuild"":{""buildNumber"":{""enabled"":true,""includeCommitId"":{""when"":""nonPublicReleaseOnly"",""where"":""buildMetadata""}}}}", @"{""cloudBuild"":{""buildNumber"":{""enabled"":true}}}")]
[InlineData(@"{""cloudBuild"":{""buildNumber"":{""enabled"":true,""includeCommitId"":{""when"":""always"",""where"":""buildMetadata""}}}}", @"{""cloudBuild"":{""buildNumber"":{""enabled"":true,""includeCommitId"":{""when"":""always""}}}}")]
[InlineData(@"{""cloudBuild"":{""buildNumber"":{""enabled"":true,""includeCommitId"":{""when"":""nonPublicReleaseOnly"",""where"":""fourthVersionComponent""}}}}", @"{""cloudBuild"":{""buildNumber"":{""enabled"":true,""includeCommitId"":{""where"":""fourthVersionComponent""}}}}")]
[InlineData(@"{""cloudBuild"":{""setVersionVariables"":true}}", @"{}")]
[InlineData(@"{""cloudBuild"":{""setAllVariables"":false}}", @"{}")]
[InlineData(@"{""release"":{""increment"":""minor""}}", @"{}")]
[InlineData(@"{""release"":{""branchName"":""v{version}""}}", @"{}")]
[InlineData(@"{""release"":{""firstUnstableTag"":""alpha""}}", @"{}")]
[InlineData(@"{""release"":{""gitCommitIdPrefix"":""g""}}", @"{}")]
[InlineData(@"{""release"":{""firstUnstableTag"":""tag""}}", @"{""release"":{""firstUnstableTag"":""tag""}}")]
[InlineData(@"{""release"":{""branchName"":""v{version}"",""versionIncrement"":""minor"",""firstUnstableTag"":""alpha""}}", @"{}")]
[InlineData(@"{""release"":{""versionIncrement"":""major""}}", @"{""release"":{""versionIncrement"":""major""}}")]
[InlineData(@"{""release"":{""branchName"":""someName""}}", @"{""release"":{""branchName"":""someName""}}")]
[InlineData(@"{""release"":{""branchName"":""someName"",""versionIncrement"":""major""}}", @"{""release"":{""branchName"":""someName"",""versionIncrement"":""major""}}")]
[InlineData(@"{""release"":{""branchName"":""someName"",""versionIncrement"":""major"",""firstUnstableTag"":""alpha""}}", @"{""release"":{""branchName"":""someName"",""versionIncrement"":""major""}}")]
[InlineData(@"{""release"":{""branchName"":""someName"",""versionIncrement"":""major"",""firstUnstableTag"":""pre""}}", @"{""release"":{""branchName"":""someName"",""versionIncrement"":""major"",""firstUnstableTag"":""pre""}}")]
public void JsonMinification(string full, string minimal)
{
var settings = VersionOptions.GetJsonSettings();
settings.Formatting = Formatting.None;
// Assert that the two representations are equivalent.
var fullVersion = JsonConvert.DeserializeObject<VersionOptions>(full, settings);
var minimalVersion = JsonConvert.DeserializeObject<VersionOptions>(minimal, settings);
Assert.Equal(fullVersion, minimalVersion);
string fullVersionSerialized = JsonConvert.SerializeObject(fullVersion, settings);
Assert.Equal(minimal, fullVersionSerialized);
}
[Fact]
public void GetVersion_CanReadSpecConformantJsonFile()
{
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), "{ version: \"1.2-pre\" }");
VersionOptions actualVersion = this.Context.VersionFile.GetVersion();
Assert.NotNull(actualVersion);
Assert.Equal(new Version(1, 2), actualVersion.Version.Version);
Assert.Equal("-pre", actualVersion.Version.Prerelease);
}
[Fact]
public void GetVersion_CanReadSpecConformantTxtFile_SingleLine()
{
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.TxtFileName), "1.2-pre");
VersionOptions actualVersion = this.Context.VersionFile.GetVersion();
Assert.NotNull(actualVersion);
Assert.Equal(new Version(1, 2), actualVersion.Version.Version);
Assert.Equal("-pre", actualVersion.Version.Prerelease);
}
[Fact]
public void GetVersion_CanReadSpecConformantTxtFile_MultiLine()
{
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.TxtFileName), "1.2\n-pre");
VersionOptions actualVersion = this.Context.VersionFile.GetVersion();
Assert.NotNull(actualVersion);
Assert.Equal(new Version(1, 2), actualVersion.Version.Version);
Assert.Equal("-pre", actualVersion.Version.Prerelease);
}
[Fact]
public void GetVersion_CanReadSpecConformantTxtFile_MultiLineNoHyphen()
{
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.TxtFileName), "1.2\npre");
VersionOptions actualVersion = this.Context.VersionFile.GetVersion();
Assert.NotNull(actualVersion);
Assert.Equal(new Version(1, 2), actualVersion.Version.Version);
Assert.Equal("-pre", actualVersion.Version.Prerelease);
}
[Fact]
public void GetVersion_Commit()
{
Assert.Null(this.Context.VersionFile.GetVersion());
Assert.False(this.Context.VersionFile.IsVersionDefined());
this.InitializeSourceControl();
this.WriteVersionFile();
VersionOptions fromCommit = this.Context.VersionFile.GetVersion();
VersionOptions fromFile = this.Context.VersionFile.GetVersion();
Assert.NotNull(fromCommit);
Assert.Equal(fromFile, fromCommit);
}
[Fact]
public void GetVersion_String_FindsNearestFileInAncestorDirectories()
{
// Construct a repo where versions are defined like this:
/* root <- 1.0
a (inherits 1.0)
b <- 1.1
c (inherits 1.1)
*/
var rootVersionSpec = new VersionOptions { Version = SemanticVersion.Parse("1.0") };
var subdirVersionSpec = new VersionOptions { Version = SemanticVersion.Parse("1.1") };
this.Context.VersionFile.SetVersion(this.RepoPath, rootVersionSpec);
string subDirA = Path.Combine(this.RepoPath, "a");
string subDirAB = Path.Combine(subDirA, "b");
string subDirABC = Path.Combine(subDirAB, "c");
Directory.CreateDirectory(subDirABC);
this.Context.VersionFile.SetVersion(subDirAB, new Version(1, 1));
this.InitializeSourceControl();
var commit = this.LibGit2Repository.Head.Commits.First().Sha;
this.AssertPathHasVersion(commit, subDirABC, subdirVersionSpec);
this.AssertPathHasVersion(commit, subDirAB, subdirVersionSpec);
this.AssertPathHasVersion(commit, subDirA, rootVersionSpec);
this.AssertPathHasVersion(commit, this.RepoPath, rootVersionSpec);
}
[Fact]
public void GetVersion_String_FindsNearestFileInAncestorDirectories_WithAssemblyVersion()
{
// Construct a repo where versions are defined like this:
/* root <- 14.0
a (inherits 14.0)
b <- 11.0
c (inherits 11.0)
*/
var rootVersionSpec = new VersionOptions
{
Version = SemanticVersion.Parse("14.1"),
AssemblyVersion = new VersionOptions.AssemblyVersionOptions(new Version(14, 0)),
};
var subdirVersionSpec = new VersionOptions { Version = SemanticVersion.Parse("11.0") };
this.Context.VersionFile.SetVersion(this.RepoPath, rootVersionSpec);
string subDirA = Path.Combine(this.RepoPath, "a");
string subDirAB = Path.Combine(subDirA, "b");
string subDirABC = Path.Combine(subDirAB, "c");
Directory.CreateDirectory(subDirABC);
this.Context.VersionFile.SetVersion(subDirAB, subdirVersionSpec);
this.InitializeSourceControl();
var commit = this.LibGit2Repository.Head.Commits.First().Sha;
this.AssertPathHasVersion(commit, subDirABC, subdirVersionSpec);
this.AssertPathHasVersion(commit, subDirAB, subdirVersionSpec);
this.AssertPathHasVersion(commit, subDirA, rootVersionSpec);
this.AssertPathHasVersion(commit, this.RepoPath, rootVersionSpec);
}
[Fact]
public void GetVersion_ReadReleaseSettings_VersionIncrement()
{
var json = @"{ ""version"" : ""1.2"", ""release"" : { ""versionIncrement"" : ""major"" } }";
var path = Path.Combine(this.RepoPath, "version.json");
File.WriteAllText(path, json);
var versionOptions = this.Context.VersionFile.GetVersion();
Assert.NotNull(versionOptions.Release);
Assert.NotNull(versionOptions.Release.VersionIncrement);
Assert.Equal(VersionOptions.ReleaseVersionIncrement.Major, versionOptions.Release.VersionIncrement);
}
[Fact]
public void GetVersion_ReadReleaseSettings_FirstUnstableTag()
{
var json = @"{ ""version"" : ""1.2"", ""release"" : { ""firstUnstableTag"" : ""preview"" } }";
var path = Path.Combine(this.RepoPath, "version.json");
File.WriteAllText(path, json);
var versionOptions = this.Context.VersionFile.GetVersion();
Assert.NotNull(versionOptions.Release);
Assert.NotNull(versionOptions.Release.FirstUnstableTag);
Assert.Equal("preview", versionOptions.Release.FirstUnstableTag);
}
[Fact]
public void GetVersion_ReadReleaseSettings_BranchName()
{
var json = @"{ ""version"" : ""1.2"", ""release"" : { ""branchName"" : ""someValue{version}"" } }";
var path = Path.Combine(this.RepoPath, "version.json");
File.WriteAllText(path, json);
var versionOptions = this.Context.VersionFile.GetVersion();
Assert.NotNull(versionOptions.Release);
Assert.NotNull(versionOptions.Release.BranchName);
Assert.Equal("someValue{version}", versionOptions.Release.BranchName);
}
[Fact]
public void GetVersion_ReadPathFilters()
{
this.InitializeSourceControl();
var json = @"{ ""version"" : ""1.2"", ""pathFilters"" : [ "":/root.txt"", ""./hello"" ] }";
var path = Path.Combine(this.RepoPath, "version.json");
File.WriteAllText(path, json);
var repoRelativeBaseDirectory = ".";
var versionOptions = this.Context.VersionFile.GetVersion();
Assert.NotNull(versionOptions.PathFilters);
Assert.Equal(new[] { "/root.txt", "./hello" }, versionOptions.PathFilters.Select(fp => fp.ToPathSpec(repoRelativeBaseDirectory)));
}
[Fact]
public void GetVersion_WithPathFiltersOutsideOfGitRepo()
{
var json = @"{ ""version"" : ""1.2"", ""pathFilters"" : [ ""."" ] }";
var path = Path.Combine(this.RepoPath, "version.json");
File.WriteAllText(path, json);
this.Context.VersionFile.GetVersion();
}
[Fact]
public void GetVersion_String_MissingFile()
{
Assert.Null(this.Context.VersionFile.GetVersion());
}
[Fact]
public void VersionJson_InheritButNoParentFileFound()
{
this.InitializeSourceControl();
this.WriteVersionFile(
new VersionOptions
{
Inherit = true,
Version = SemanticVersion.Parse("14.2"),
});
Assert.Throws<InvalidOperationException>(() => this.Context.VersionFile.GetVersion());
}
[Fact]
public void VersionJson_DoNotInheritButNoVersionSpecified()
{
this.InitializeSourceControl();
Assert.Throws<ArgumentException>(() => this.WriteVersionFile(
new VersionOptions
{
Inherit = false,
}));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void VersionJson_Inheritance(bool commitInSourceControl)
{
if (commitInSourceControl)
{
this.InitializeSourceControl();
}
VersionOptions level1, level2, level3, level2NoInherit, level2InheritButResetVersion;
this.WriteVersionFile(
level1 = new VersionOptions
{
Version = SemanticVersion.Parse("14.2"),
AssemblyVersion = new VersionOptions.AssemblyVersionOptions { Precision = VersionOptions.VersionPrecision.Major },
});
this.WriteVersionFile(
level2 = new VersionOptions
{
Inherit = true,
AssemblyVersion = new VersionOptions.AssemblyVersionOptions { Precision = VersionOptions.VersionPrecision.Minor },
},
"foo");
this.WriteVersionFile(
level3 = new VersionOptions
{
Inherit = true,
VersionHeightOffset = 1,
},
"foo/bar");
this.WriteVersionFile(
level2NoInherit = new VersionOptions
{
Version = SemanticVersion.Parse("10.1"),
},
"noInherit");
this.WriteVersionFile(
level2InheritButResetVersion = new VersionOptions
{
Inherit = true,
Version = SemanticVersion.Parse("8.2"),
},
"inheritWithVersion");
VersionOptions GetOption(string path)
{
using var context = this.CreateGitContext(Path.Combine(this.RepoPath, path));
return context.VersionFile.GetVersion();
}
var level1Options = GetOption(string.Empty);
Assert.False(level1Options.Inherit);
var level2Options = GetOption("foo");
Assert.Equal(level1.Version.Version.Major, level2Options.Version.Version.Major);
Assert.Equal(level1.Version.Version.Minor, level2Options.Version.Version.Minor);
Assert.Equal(level2.AssemblyVersion.Precision, level2Options.AssemblyVersion.Precision);
Assert.True(level2Options.Inherit);
var level3Options = GetOption("foo/bar");
Assert.Equal(level1.Version.Version.Major, level3Options.Version.Version.Major);
Assert.Equal(level1.Version.Version.Minor, level3Options.Version.Version.Minor);
Assert.Equal(level2.AssemblyVersion.Precision, level3Options.AssemblyVersion.Precision);
Assert.Equal(level2.AssemblyVersion.Precision, level3Options.AssemblyVersion.Precision);
Assert.Equal(level3.VersionHeightOffset, level3Options.VersionHeightOffset);
Assert.True(level3Options.Inherit);
var level2NoInheritOptions = GetOption("noInherit");
Assert.Equal(level2NoInherit.Version, level2NoInheritOptions.Version);
Assert.Equal(VersionOptions.DefaultVersionPrecision, level2NoInheritOptions.AssemblyVersionOrDefault.PrecisionOrDefault);
Assert.False(level2NoInheritOptions.Inherit);
var level2InheritButResetVersionOptions = GetOption("inheritWithVersion");
Assert.Equal(level2InheritButResetVersion.Version, level2InheritButResetVersionOptions.Version);
Assert.True(level2InheritButResetVersionOptions.Inherit);
if (commitInSourceControl)
{
int totalCommits = this.LibGit2Repository.Head.Commits.Count();
// The version height should be the same for all those that inherit the version from the base,
// even though the inheriting files were introduced in successive commits.
Assert.Equal(totalCommits, this.GetVersionHeight());
Assert.Equal(totalCommits, this.GetVersionHeight("foo"));
Assert.Equal(totalCommits, this.GetVersionHeight("foo/bar"));
// These either don't inherit, or inherit but reset versions, so the commits were reset.
Assert.Equal(2, this.GetVersionHeight("noInherit"));
Assert.Equal(1, this.GetVersionHeight("inheritWithVersion"));
}
}
[Fact]
public void GetVersion_ProducesAbsolutePath()
{
this.InitializeSourceControl();
this.WriteVersionFile();
Assert.NotNull(this.Context.VersionFile.GetVersion(out string actualDirectory));
Assert.True(Path.IsPathRooted(actualDirectory));
}
private void AssertPathHasVersion(string committish, string absolutePath, VersionOptions expected)
{
var actual = this.GetVersionOptions(absolutePath, committish);
Assert.Equal(expected, this.GetVersionOptions(absolutePath, committish));
}
}