|
3 | 3 | using System.Linq;
|
4 | 4 | using LibGit2Sharp;
|
5 | 5 | using Nerdbank.GitVersioning;
|
| 6 | +using Newtonsoft.Json; |
| 7 | +using Newtonsoft.Json.Linq; |
6 | 8 | using Xunit;
|
7 | 9 | using Xunit.Abstractions;
|
8 | 10 |
|
@@ -401,6 +403,156 @@ public void PrepareRelease_InvalidVersionIncrement()
|
401 | 403 | this.AssertError(() => new ReleaseManager().PrepareRelease(this.RepoPath), ReleasePreparationError.InvalidVersionIncrementSetting);
|
402 | 404 | }
|
403 | 405 |
|
| 406 | + [Fact] |
| 407 | + public void PrepareRelease_TextOutput() |
| 408 | + { |
| 409 | + // create and configure repository |
| 410 | + this.InitializeSourceControl(); |
| 411 | + |
| 412 | + // create version.json |
| 413 | + var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse("1.0") }; |
| 414 | + this.WriteVersionFile(versionOptions); |
| 415 | + |
| 416 | + var stdout = new StringWriter(); |
| 417 | + var releaseManager = new ReleaseManager(stdout); |
| 418 | + releaseManager.PrepareRelease(this.RepoPath); |
| 419 | + |
| 420 | + // by default, text output mode should be used => trying to parse it as JSON should fail |
| 421 | + Assert.ThrowsAny<JsonException>(() => JsonConvert.DeserializeObject(stdout.ToString())); |
| 422 | + } |
| 423 | + |
| 424 | + [Fact] |
| 425 | + public void PrepareRelease_JsonOutput() |
| 426 | + { |
| 427 | + // create and configure repository |
| 428 | + this.InitializeSourceControl(); |
| 429 | + |
| 430 | + // create version.json |
| 431 | + var versionOptions = new VersionOptions() |
| 432 | + { |
| 433 | + Version = SemanticVersion.Parse("1.0"), |
| 434 | + Release = new ReleaseOptions() |
| 435 | + { |
| 436 | + BranchName = "v{version}", |
| 437 | + VersionIncrement = ReleaseVersionIncrement.Minor |
| 438 | + } |
| 439 | + }; |
| 440 | + this.WriteVersionFile(versionOptions); |
| 441 | + |
| 442 | + var currentBranchName = this.Repo.Head.FriendlyName; |
| 443 | + var releaseBranchName = "v1.0"; |
| 444 | + |
| 445 | + // run release preparation |
| 446 | + var stdout = new StringWriter(); |
| 447 | + var releaseManager = new ReleaseManager(stdout); |
| 448 | + releaseManager.PrepareRelease(this.RepoPath, outputMode: ReleaseManager.ReleaseManagerOutputMode.Json); |
| 449 | + |
| 450 | + |
| 451 | + // Expected output: |
| 452 | + // { |
| 453 | + // "CurrentBranch" : { |
| 454 | + // "Name" : "<NAME-OF-CURRENT-BRANCH>", |
| 455 | + // "Commit" : "<HEAD-COMMIT-OF-CURRENT-BRANCH>", |
| 456 | + // "Version" : "<UPDATED-VERSION-ON-CURRENT-BRANCH>", |
| 457 | + // }, |
| 458 | + // "NewBranch" : { |
| 459 | + // "Name" : "<NAME-OF-CREATED-BRANCH>", |
| 460 | + // "Commit" : "<HEAD-COMMIT-OF-CREATED-BRANCH>", |
| 461 | + // "Version" : "<VERSION-ON-CREATED-BRANCH>", |
| 462 | + // } |
| 463 | + // } |
| 464 | + |
| 465 | + var jsonOutput = JObject.Parse(stdout.ToString()); |
| 466 | + |
| 467 | + // check "CurrentBranch" output |
| 468 | + { |
| 469 | + var expectedCommitId = this.Repo.Branches[currentBranchName].Tip.Sha; |
| 470 | + var expectedVersion = VersionFile.GetVersion(this.Repo.Branches[currentBranchName].Tip).Version.ToString(); |
| 471 | + |
| 472 | + var currentBranchOutput = jsonOutput.Property("CurrentBranch")?.Value as JObject; |
| 473 | + Assert.NotNull(currentBranchOutput); |
| 474 | + |
| 475 | + Assert.Equal(currentBranchName, currentBranchOutput.GetValue("Name")?.ToString()); |
| 476 | + Assert.Equal(expectedCommitId, currentBranchOutput.GetValue("Commit")?.ToString()); |
| 477 | + Assert.Equal(expectedVersion, currentBranchOutput.GetValue("Version")?.ToString()); |
| 478 | + |
| 479 | + } |
| 480 | + |
| 481 | + // Check "NewBranch" output |
| 482 | + { |
| 483 | + var expectedCommitId = this.Repo.Branches[releaseBranchName].Tip.Sha; |
| 484 | + var expectedVersion = VersionFile.GetVersion(this.Repo.Branches[releaseBranchName].Tip).Version.ToString(); |
| 485 | + |
| 486 | + var newBranchOutput = jsonOutput.Property("NewBranch")?.Value as JObject; |
| 487 | + Assert.NotNull(newBranchOutput); |
| 488 | + |
| 489 | + Assert.Equal(releaseBranchName, newBranchOutput.GetValue("Name")?.ToString()); |
| 490 | + Assert.Equal(expectedCommitId, newBranchOutput.GetValue("Commit")?.ToString()); |
| 491 | + Assert.Equal(expectedVersion, newBranchOutput.GetValue("Version")?.ToString()); |
| 492 | + } |
| 493 | + } |
| 494 | + |
| 495 | + [Fact] |
| 496 | + public void PrepareRelease_JsonOutputWhenUpdatingReleaseBranch() |
| 497 | + { |
| 498 | + // create and configure repository |
| 499 | + this.InitializeSourceControl(); |
| 500 | + |
| 501 | + // create version.json |
| 502 | + var versionOptions = new VersionOptions() |
| 503 | + { |
| 504 | + Version = SemanticVersion.Parse("1.0"), |
| 505 | + Release = new ReleaseOptions() |
| 506 | + { |
| 507 | + BranchName = "v{version}", |
| 508 | + VersionIncrement = ReleaseVersionIncrement.Minor |
| 509 | + } |
| 510 | + }; |
| 511 | + this.WriteVersionFile(versionOptions); |
| 512 | + var branchName = "v1.0"; |
| 513 | + |
| 514 | + // switch to release branch |
| 515 | + Commands.Checkout(this.Repo, this.Repo.CreateBranch(branchName)); |
| 516 | + |
| 517 | + // run release preparation |
| 518 | + var stdout = new StringWriter(); |
| 519 | + var releaseManager = new ReleaseManager(stdout); |
| 520 | + releaseManager.PrepareRelease(this.RepoPath, outputMode: ReleaseManager.ReleaseManagerOutputMode.Json); |
| 521 | + |
| 522 | + |
| 523 | + // Expected output: |
| 524 | + // { |
| 525 | + // "CurrentBranch" : { |
| 526 | + // "Name" : "<NAME>", |
| 527 | + // "Commit" : "<COMMIT>", |
| 528 | + // "Version" : "<VERSION>", |
| 529 | + // }, |
| 530 | + // "NewBranch" : null |
| 531 | + // } |
| 532 | + |
| 533 | + var jsonOutput = JObject.Parse(stdout.ToString()); |
| 534 | + |
| 535 | + // check "CurrentBranch" output |
| 536 | + { |
| 537 | + var expectedCommitId = this.Repo.Branches[branchName].Tip.Sha; |
| 538 | + var expectedVersion = VersionFile.GetVersion(this.Repo.Branches[branchName].Tip).Version.ToString(); |
| 539 | + |
| 540 | + var currentBranchOutput = jsonOutput.Property("CurrentBranch")?.Value as JObject; |
| 541 | + Assert.NotNull(currentBranchOutput); |
| 542 | + |
| 543 | + Assert.Equal(branchName, currentBranchOutput.GetValue("Name")?.ToString()); |
| 544 | + Assert.Equal(expectedCommitId, currentBranchOutput.GetValue("Commit")?.ToString()); |
| 545 | + Assert.Equal(expectedVersion, currentBranchOutput.GetValue("Version")?.ToString()); |
| 546 | + |
| 547 | + } |
| 548 | + // Check "NewBranch" output |
| 549 | + { |
| 550 | + // no new branch was created, so "NewBranch" should be null |
| 551 | + var newBranchOutput = jsonOutput.Property("NewBranch")?.Value as JObject; |
| 552 | + Assert.Null(newBranchOutput); |
| 553 | + } |
| 554 | + } |
| 555 | + |
404 | 556 | private void AssertError(Action testCode, ReleasePreparationError expectedError)
|
405 | 557 | {
|
406 | 558 | var ex = Assert.Throws<ReleasePreparationException>(testCode);
|
|
0 commit comments