/
ReleaseManagerTests.cs
648 lines (547 loc) · 27.8 KB
/
ReleaseManagerTests.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
using System;
using System.IO;
using System.Linq;
using LibGit2Sharp;
using Nerdbank.GitVersioning;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;
using ReleaseOptions = Nerdbank.GitVersioning.VersionOptions.ReleaseOptions;
using ReleasePreparationError = Nerdbank.GitVersioning.ReleaseManager.ReleasePreparationError;
using ReleasePreparationException = Nerdbank.GitVersioning.ReleaseManager.ReleasePreparationException;
using ReleaseVersionIncrement = Nerdbank.GitVersioning.VersionOptions.ReleaseVersionIncrement;
using Version = System.Version;
[Trait("Engine", "Managed")]
public class ReleaseManagerManagedTests : ReleaseManagerTests
{
public ReleaseManagerManagedTests(ITestOutputHelper logger)
: base(logger)
{
}
protected override GitContext CreateGitContext(string path, string committish = null)
=> GitContext.Create(path, committish, writable: false);
}
[Trait("Engine", "LibGit2")]
public class ReleaseManagerLibGit2Tests : ReleaseManagerTests
{
public ReleaseManagerLibGit2Tests(ITestOutputHelper logger)
: base(logger)
{
}
protected override GitContext CreateGitContext(string path, string committish = null)
=> GitContext.Create(path, committish, writable: true);
}
public abstract class ReleaseManagerTests : RepoTestBase
{
public ReleaseManagerTests(ITestOutputHelper logger)
: base(logger)
{
}
[Fact]
public void PrepareRelease_NoGitRepo()
{
// running PrepareRelease should result in an error
// because the repo directory is not a git repository
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.NoGitRepo);
}
[Fact]
public void PrepareRelease_DirtyWorkingDirecotory()
{
this.InitializeSourceControl();
// create a file
File.WriteAllText(Path.Combine(this.RepoPath, "file1.txt"), "");
// running PrepareRelease should result in an error
// because there is a new file not under version control
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.UncommittedChanges);
}
[Fact]
public void PrepareRelease_DirtyIndex()
{
this.InitializeSourceControl();
// create a file and stage it
var filePath = Path.Combine(this.RepoPath, "file1.txt");
File.WriteAllText(filePath, "");
Commands.Stage(this.LibGit2Repository, filePath);
// running PrepareRelease should result in an error
// because there are uncommitted changes
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.UncommittedChanges);
}
[Fact]
public void PrepareRelease_NoVersionFile()
{
this.InitializeSourceControl();
// running PrepareRelease should result in an error
// because there is no version.json
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.NoVersionFile);
}
[Fact]
public void PrepareRelease_InvalidBranchNameSetting()
{
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.2-pre"),
Release = new ReleaseOptions()
{
BranchName = "nameWithoutPlaceholder",
}
};
this.WriteVersionFile(versionOptions);
// running PrepareRelease should result in an error
// because the branchName does not have a placeholder for the version
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.InvalidBranchNameSetting);
}
[Fact]
public void PrepareRelease_ReleaseBranchAlreadyExists()
{
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.2-pre"),
Release = new ReleaseOptions()
{
BranchName = "release/v{version}",
}
};
this.WriteVersionFile(versionOptions);
this.LibGit2Repository.CreateBranch("release/v1.2");
// running PrepareRelease should result in an error
// because the release branch already exists
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.BranchAlreadyExists);
}
[Theory]
// base test cases
[InlineData("1.2-beta", null, null, "v1.2", "1.2")]
[InlineData("1.2-beta.{height}", null, null, "v1.2", "1.2")]
[InlineData("1.2-beta", null, "rc", "v1.2", "1.2-rc")]
[InlineData("1.2-beta.{height}", null, "rc", "v1.2", "1.2-rc.{height}")]
// modify release.branchName
[InlineData("1.2-beta", "v{version}release", null, "v1.2release", "1.2")]
[InlineData("1.2-beta.{height}", "v{version}release", null, "v1.2release", "1.2")]
[InlineData("1.2-beta", "v{version}release", "rc", "v1.2release", "1.2-rc")]
[InlineData("1.2-beta.{height}", "v{version}release", "rc", "v1.2release", "1.2-rc.{height}")]
public void PrepareRelease_ReleaseBranch(string initialVersion, string releaseOptionsBranchName, string releaseUnstableTag, string releaseBranchName, string resultingVersion)
{
releaseOptionsBranchName = releaseOptionsBranchName ?? new ReleaseOptions().BranchNameOrDefault;
// create and configure repository
this.InitializeSourceControl();
var initialVersionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse(initialVersion),
Release = new ReleaseOptions()
{
BranchName = releaseOptionsBranchName
}
};
var expectedVersionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse(resultingVersion),
Release = new ReleaseOptions()
{
BranchName = releaseOptionsBranchName
}
};
// create version.json
this.WriteVersionFile(initialVersionOptions);
// switch to release branch
var branchName = releaseBranchName;
Commands.Checkout(this.LibGit2Repository, this.LibGit2Repository.CreateBranch(branchName));
var tipBeforePrepareRelease = this.LibGit2Repository.Head.Tip;
// run PrepareRelease
var releaseManager = new ReleaseManager();
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag);
// Check if a commit was created
{
var updateVersionCommit = this.LibGit2Repository.Head.Tip;
Assert.NotEqual(tipBeforePrepareRelease.Id, updateVersionCommit.Id);
Assert.Single(updateVersionCommit.Parents);
Assert.Equal(updateVersionCommit.Parents.Single().Id, tipBeforePrepareRelease.Id);
}
// check version on release branch
{
var actualVersionOptions = this.GetVersionOptions(committish: this.LibGit2Repository.Branches[branchName].Tip.Sha);
Assert.Equal(expectedVersionOptions, actualVersionOptions);
}
}
[Theory]
[InlineData("1.2", "rc", "release/v1.2")]
[InlineData("1.2+metadata", "rc", "release/v1.2")]
public void PrepeareRelease_ReleaseBranchWithVersionDecrement(string initialVersion, string releaseUnstableTag, string branchName)
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse(initialVersion) };
this.WriteVersionFile(versionOptions);
// switch to release branch
Commands.Checkout(this.LibGit2Repository, this.LibGit2Repository.CreateBranch(branchName));
// running PrepareRelease should result in an error
// because we're trying to add a prerelease tag to a version without prerelease tag
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag), ReleasePreparationError.VersionDecrement);
}
[Theory]
// base test cases
[InlineData("1.2-beta", null, null, null, null, null, null, "v1.2", "1.2", "1.3-alpha")]
[InlineData("1.2-beta", null, null, null, "rc", null, null, "v1.2", "1.2-rc", "1.3-alpha")]
[InlineData("1.2-beta.{height}", null, null, null, null, null, null, "v1.2", "1.2", "1.3-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, null, null, "rc", null, null, "v1.2", "1.2-rc.{height}", "1.3-alpha.{height}")]
// modify release.branchName
[InlineData("1.2-beta", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2release", "1.2", "1.3-alpha")]
[InlineData("1.2-beta", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2release", "1.2-rc", "1.3-alpha")]
[InlineData("1.2-beta.{height}", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2release", "1.2", "1.3-alpha.{height}")]
[InlineData("1.2-beta.{height}", "v{version}release", ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2release", "1.2-rc.{height}", "1.3-alpha.{height}")]
// modify release.versionIncrement: "Major"
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Major, "alpha", "rc", null, null, "v1.2", "1.2-rc", "2.0-alpha")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Major, "alpha", "rc", null, null, "v1.2", "1.2-rc.{height}", "2.0-alpha.{height}")]
// modify release.versionIncrement: "Build"
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Build, "alpha", null, null, null, "v1.2.3", "1.2.3", "1.2.4-alpha")]
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Build, "alpha", "rc", null, null, "v1.2.3", "1.2.3-rc", "1.2.4-alpha")]
[InlineData("1.2.3-beta.{height}", null, ReleaseVersionIncrement.Build, "alpha", null, null, null, "v1.2.3", "1.2.3", "1.2.4-alpha.{height}")]
[InlineData("1.2.3-beta.{height}", null, ReleaseVersionIncrement.Build, "alpha", "rc", null, null, "v1.2.3", "1.2.3-rc.{height}", "1.2.4-alpha.{height}")]
// modify release.firstUnstableTag
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, "preview", null, null, null, "v1.2", "1.2", "1.3-preview")]
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, "preview", "rc", null, null, "v1.2", "1.2-rc", "1.3-preview")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Minor, "preview", null, null, null, "v1.2", "1.2", "1.3-preview.{height}")]
[InlineData("1.2-beta.{height}", null, ReleaseVersionIncrement.Minor, "preview", "rc", null, null, "v1.2", "1.2-rc.{height}", "1.3-preview.{height}")]
// include build metadata in version
[InlineData("1.2-beta+metadata", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2+metadata", "1.3-alpha+metadata")]
[InlineData("1.2-beta+metadata", null, ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2", "1.2-rc+metadata", "1.3-alpha+metadata")]
[InlineData("1.2-beta.{height}+metadata", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2+metadata", "1.3-alpha.{height}+metadata")]
[InlineData("1.2-beta.{height}+metadata", null, ReleaseVersionIncrement.Minor, "alpha", "rc", null, null, "v1.2", "1.2-rc.{height}+metadata", "1.3-alpha.{height}+metadata")]
// versions without prerelease tags
[InlineData("1.2", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2", "1.3-alpha")]
[InlineData("1.2", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha")]
// explicitly set next version
[InlineData("1.2-beta", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5-alpha")]
[InlineData("1.2-beta.{height}", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5-alpha.{height}")]
[InlineData("1.2-beta.{height}", null, null, "pre", null, "4.5.6", null, "v1.2", "1.2", "4.5.6-pre.{height}")]
// explicitly set version increment overriding the setting from ReleaseOptions
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, null, null, null, ReleaseVersionIncrement.Major, "v1.2", "1.2", "2.0-alpha")]
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Minor, null, null, null, ReleaseVersionIncrement.Build, "v1.2.3", "1.2.3", "1.2.4-alpha")]
public void PrepareRelease_Master(
// data for initial setup (version and release options configured in version.json)
string initialVersion,
string releaseOptionsBranchName,
ReleaseVersionIncrement? releaseOptionsVersionIncrement,
string releaseOptionsFirstUnstableTag,
// arguments passed to PrepareRelease()
string releaseUnstableTag,
string nextVersion,
ReleaseVersionIncrement? parameterVersionIncrement,
// expected versions and branch name after running PrepareRelease()
string expectedBranchName,
string resultingReleaseVersion,
string resultingMainVersion)
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var initialVersionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse(initialVersion),
Release = new ReleaseOptions()
{
VersionIncrement = releaseOptionsVersionIncrement,
BranchName = releaseOptionsBranchName,
FirstUnstableTag = releaseOptionsFirstUnstableTag
}
};
this.WriteVersionFile(initialVersionOptions);
var expectedVersionOptionsReleaseBranch = new VersionOptions()
{
Version = SemanticVersion.Parse(resultingReleaseVersion),
Release = new ReleaseOptions()
{
VersionIncrement = releaseOptionsVersionIncrement,
BranchName = releaseOptionsBranchName,
FirstUnstableTag = releaseOptionsFirstUnstableTag
}
};
var expectedVersionOptionsCurrentBrach = new VersionOptions()
{
Version = SemanticVersion.Parse(resultingMainVersion),
Release = new ReleaseOptions()
{
VersionIncrement = releaseOptionsVersionIncrement,
BranchName = releaseOptionsBranchName,
FirstUnstableTag = releaseOptionsFirstUnstableTag
}
};
var initialBranchName = this.LibGit2Repository.Head.FriendlyName;
var tipBeforePrepareRelease = this.LibGit2Repository.Head.Tip;
// prepare release
var releaseManager = new ReleaseManager();
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion is null ? null : Version.Parse(nextVersion)), parameterVersionIncrement);
// check if a branch was created
Assert.Contains(this.LibGit2Repository.Branches, branch => branch.FriendlyName == expectedBranchName);
// PrepareRelease should switch back to the initial branch
Assert.Equal(initialBranchName, this.LibGit2Repository.Head.FriendlyName);
// check if release branch contains a new commit
// parent of new commit must be the commit before preparing the release
var releaseBranch = this.LibGit2Repository.Branches[expectedBranchName];
{
// If the original branch had no -prerelease tag, the release branch has no commit to author.
if (string.IsNullOrEmpty(initialVersionOptions.Version.Prerelease))
{
Assert.Equal(releaseBranch.Tip.Id, tipBeforePrepareRelease.Id);
}
else
{
Assert.NotEqual(releaseBranch.Tip.Id, tipBeforePrepareRelease.Id);
Assert.Equal(releaseBranch.Tip.Parents.Single().Id, tipBeforePrepareRelease.Id);
}
}
if (string.IsNullOrEmpty(initialVersionOptions.Version.Prerelease))
{
// Verify that one commit was authored.
var incrementCommit = this.LibGit2Repository.Head.Tip;
Assert.Single(incrementCommit.Parents);
Assert.Equal(tipBeforePrepareRelease.Id, incrementCommit.Parents.Single().Id);
}
else
{
// check if current branch contains new commits
// - one commit that updates the version (parent must be the commit before preparing the release)
// - one commit merging the release branch back to master and resolving the conflict
var mergeCommit = this.LibGit2Repository.Head.Tip;
Assert.Equal(2, mergeCommit.Parents.Count());
Assert.Equal(releaseBranch.Tip.Id, mergeCommit.Parents.Skip(1).First().Id);
var updateVersionCommit = mergeCommit.Parents.First();
Assert.Single(updateVersionCommit.Parents);
Assert.Equal(tipBeforePrepareRelease.Id, updateVersionCommit.Parents.First().Id);
}
// check version on release branch
{
var releaseBranchVersion = this.GetVersionOptions(committish: releaseBranch.Tip.Sha);
Assert.Equal(expectedVersionOptionsReleaseBranch, releaseBranchVersion);
}
// check version on master branch
{
var currentBranchVersion = this.GetVersionOptions(committish: this.LibGit2Repository.Head.Tip.Sha);
Assert.Equal(expectedVersionOptionsCurrentBrach, currentBranchVersion);
}
}
[Theory]
[InlineData("1.2", "rc", null)]
[InlineData("1.2+metadata", "rc", null)]
[InlineData("1.2+metadata", null, "0.9")]
public void PrepareRelease_MasterWithVersionDecrement(string initialVersion, string releaseUnstableTag, string nextVersion)
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse(initialVersion) };
this.WriteVersionFile(versionOptions);
// running PrepareRelease should result in an error
// because we're setting the version on master to a lower version
this.AssertError(
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion is null ? null : Version.Parse(nextVersion))),
ReleasePreparationError.VersionDecrement);
}
[Theory]
[InlineData("1.2", "1.2")]
public void PrepareRelease_MasterWithoutVersionIncrement(string initialVersion, string nextVersion)
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse(initialVersion) };
this.WriteVersionFile(versionOptions);
// running PrepareRelease should result in an error
// because we're trying to set master to the version it already has
this.AssertError(
() => new ReleaseManager().PrepareRelease(this.RepoPath, null, (nextVersion is null ? null : Version.Parse(nextVersion))),
ReleasePreparationError.NoVersionIncrement);
}
[Fact]
public void PrepareRelease_DetachedHead()
{
this.InitializeSourceControl();
this.WriteVersionFile("1.0", "-alpha");
Commands.Checkout(this.LibGit2Repository, this.LibGit2Repository.Head.Commits.First());
var ex = Assert.Throws<ReleasePreparationException>(() => new ReleaseManager().PrepareRelease(this.RepoPath));
Assert.Equal(ReleasePreparationError.DetachedHead, ex.Error);
}
[Fact]
public void PrepareRelease_InvalidVersionIncrement()
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.2"),
Release = new ReleaseOptions() { VersionIncrement = ReleaseVersionIncrement.Build }
};
this.WriteVersionFile(versionOptions);
// running PrepareRelease should result in an error
// because a 2-segment version is incompatibale with a increment setting of "build"
this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.InvalidVersionIncrementSetting);
}
[Fact]
public void PrepareRelease_TextOutput()
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse("1.0") };
this.WriteVersionFile(versionOptions);
var stdout = new StringWriter();
var releaseManager = new ReleaseManager(stdout);
releaseManager.PrepareRelease(this.RepoPath);
// by default, text output mode should be used => trying to parse it as JSON should fail
Assert.ThrowsAny<JsonException>(() => JsonConvert.DeserializeObject(stdout.ToString()));
}
[Fact]
public void PrepareRelease_JsonOutput()
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.0"),
Release = new ReleaseOptions()
{
BranchName = "v{version}",
VersionIncrement = ReleaseVersionIncrement.Minor
}
};
this.WriteVersionFile(versionOptions);
var currentBranchName = this.LibGit2Repository.Head.FriendlyName;
var releaseBranchName = "v1.0";
// run release preparation
var stdout = new StringWriter();
var releaseManager = new ReleaseManager(stdout);
releaseManager.PrepareRelease(this.RepoPath, outputMode: ReleaseManager.ReleaseManagerOutputMode.Json);
// Expected output:
// {
// "CurrentBranch" : {
// "Name" : "<NAME-OF-CURRENT-BRANCH>",
// "Commit" : "<HEAD-COMMIT-OF-CURRENT-BRANCH>",
// "Version" : "<UPDATED-VERSION-ON-CURRENT-BRANCH>",
// },
// "NewBranch" : {
// "Name" : "<NAME-OF-CREATED-BRANCH>",
// "Commit" : "<HEAD-COMMIT-OF-CREATED-BRANCH>",
// "Version" : "<VERSION-ON-CREATED-BRANCH>",
// }
// }
var jsonOutput = JObject.Parse(stdout.ToString());
// check "CurrentBranch" output
{
var expectedCommitId = this.LibGit2Repository.Branches[currentBranchName].Tip.Sha;
var expectedVersion = this.GetVersionOptions(committish: this.LibGit2Repository.Branches[currentBranchName].Tip.Sha).Version.ToString();
var currentBranchOutput = jsonOutput.Property("CurrentBranch")?.Value as JObject;
Assert.NotNull(currentBranchOutput);
Assert.Equal(currentBranchName, currentBranchOutput.GetValue("Name")?.ToString());
Assert.Equal(expectedCommitId, currentBranchOutput.GetValue("Commit")?.ToString());
Assert.Equal(expectedVersion, currentBranchOutput.GetValue("Version")?.ToString());
}
// Check "NewBranch" output
{
var expectedCommitId = this.LibGit2Repository.Branches[releaseBranchName].Tip.Sha;
var expectedVersion = this.GetVersionOptions(committish: this.LibGit2Repository.Branches[releaseBranchName].Tip.Sha).Version.ToString();
var newBranchOutput = jsonOutput.Property("NewBranch")?.Value as JObject;
Assert.NotNull(newBranchOutput);
Assert.Equal(releaseBranchName, newBranchOutput.GetValue("Name")?.ToString());
Assert.Equal(expectedCommitId, newBranchOutput.GetValue("Commit")?.ToString());
Assert.Equal(expectedVersion, newBranchOutput.GetValue("Version")?.ToString());
}
}
[Fact]
public void PrepareRelease_JsonOutputWhenUpdatingReleaseBranch()
{
// create and configure repository
this.InitializeSourceControl();
// create version.json
var versionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.0"),
Release = new ReleaseOptions()
{
BranchName = "v{version}",
VersionIncrement = ReleaseVersionIncrement.Minor
}
};
this.WriteVersionFile(versionOptions);
var branchName = "v1.0";
// switch to release branch
Commands.Checkout(this.LibGit2Repository, this.LibGit2Repository.CreateBranch(branchName));
// run release preparation
var stdout = new StringWriter();
var releaseManager = new ReleaseManager(stdout);
releaseManager.PrepareRelease(this.RepoPath, outputMode: ReleaseManager.ReleaseManagerOutputMode.Json);
// Expected output:
// {
// "CurrentBranch" : {
// "Name" : "<NAME>",
// "Commit" : "<COMMIT>",
// "Version" : "<VERSION>",
// },
// "NewBranch" : null
// }
var jsonOutput = JObject.Parse(stdout.ToString());
// check "CurrentBranch" output
{
var expectedCommitId = this.LibGit2Repository.Branches[branchName].Tip.Sha;
var expectedVersion = this.GetVersionOptions(committish: this.LibGit2Repository.Branches[branchName].Tip.Sha).Version.ToString();
var currentBranchOutput = jsonOutput.Property("CurrentBranch")?.Value as JObject;
Assert.NotNull(currentBranchOutput);
Assert.Equal(branchName, currentBranchOutput.GetValue("Name")?.ToString());
Assert.Equal(expectedCommitId, currentBranchOutput.GetValue("Commit")?.ToString());
Assert.Equal(expectedVersion, currentBranchOutput.GetValue("Version")?.ToString());
}
// Check "NewBranch" output
{
// no new branch was created, so "NewBranch" should be null
var newBranchOutput = jsonOutput.Property("NewBranch")?.Value as JObject;
Assert.Null(newBranchOutput);
}
}
[Fact]
public void PrepareRelease_ResetsVersionHeightOffset()
{
// create and configure repository
this.InitializeSourceControl();
var initialVersionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.0-beta"),
VersionHeightOffset = 5,
};
var expectedReleaseVersionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.0"),
VersionHeightOffset = 5,
};
var expectedMainVersionOptions = new VersionOptions()
{
Version = SemanticVersion.Parse("1.1-alpha"),
};
// create version.json
this.WriteVersionFile(initialVersionOptions);
var tipBeforePrepareRelease = this.LibGit2Repository.Head.Tip;
var releaseManager = new ReleaseManager();
releaseManager.PrepareRelease(this.RepoPath);
this.SetContextToHead();
var newVersion = this.Context.VersionFile.GetVersion();
Assert.Equal(expectedMainVersionOptions, newVersion);
VersionOptions releaseVersion = this.GetVersionOptions(committish: this.LibGit2Repository.Branches["v1.0"].Tip.Sha);
Assert.Equal(expectedReleaseVersionOptions, releaseVersion);
}
protected override void InitializeSourceControl(bool withInitialCommit = true)
{
base.InitializeSourceControl(withInitialCommit);
this.Ignore_git2_UntrackedFile();
}
private void AssertError(Action testCode, ReleasePreparationError expectedError)
{
var ex = Assert.Throws<ReleasePreparationException>(testCode);
Assert.Equal(expectedError, ex.Error);
}
}