|
| 1 | +using GitVersion.BuildAgents; |
| 2 | +using GitVersion.Core.Tests.Helpers; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using NUnit.Framework; |
| 5 | +using Shouldly; |
| 6 | + |
| 7 | +namespace GitVersion.Core.Tests.BuildAgents; |
| 8 | + |
| 9 | +[TestFixture] |
| 10 | +public class BitBucketPipelinesTests : TestBase |
| 11 | +{ |
| 12 | + private IEnvironment environment; |
| 13 | + private BitBucketPipelines buildServer; |
| 14 | + |
| 15 | + [SetUp] |
| 16 | + public void SetEnvironmentVariableForTest() |
| 17 | + { |
| 18 | + var sp = ConfigureServices(services => services.AddSingleton<BitBucketPipelines>()); |
| 19 | + this.environment = sp.GetRequiredService<IEnvironment>(); |
| 20 | + this.buildServer = sp.GetRequiredService<BitBucketPipelines>(); |
| 21 | + |
| 22 | + this.environment.SetEnvironmentVariable("BITBUCKET_WORKSPACE", "MyWorkspace"); |
| 23 | + } |
| 24 | + |
| 25 | + [Test] |
| 26 | + public void CalculateVersionOnMainBranch() |
| 27 | + { |
| 28 | + // Arrange |
| 29 | + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/main"); |
| 30 | + |
| 31 | + var vars = new TestableVersionVariables(fullSemVer: "1.2.3"); |
| 32 | + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); |
| 33 | + |
| 34 | + vsVersion.ShouldBe("1.2.3"); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void CalculateVersionOnDevelopBranch() |
| 39 | + { |
| 40 | + // Arrange |
| 41 | + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/develop"); |
| 42 | + |
| 43 | + var vars = new TestableVersionVariables(fullSemVer: "1.2.3-unstable.4"); |
| 44 | + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); |
| 45 | + |
| 46 | + vsVersion.ShouldBe("1.2.3-unstable.4"); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public void CalculateVersionOnFeatureBranch() |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work"); |
| 54 | + |
| 55 | + var vars = new TestableVersionVariables(fullSemVer: "1.2.3-beta.4"); |
| 56 | + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); |
| 57 | + |
| 58 | + vsVersion.ShouldBe("1.2.3-beta.4"); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public void GetCurrentBranchShouldHandleBranches() |
| 63 | + { |
| 64 | + // Arrange |
| 65 | + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work"); |
| 66 | + |
| 67 | + // Act |
| 68 | + var result = this.buildServer.GetCurrentBranch(false); |
| 69 | + |
| 70 | + // Assert |
| 71 | + result.ShouldBe($"refs/heads/feature/my-work"); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void GetCurrentBranchShouldHandleTags() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null); |
| 79 | + this.environment.SetEnvironmentVariable("BITBUCKET_TAG", "refs/heads/tags/1.2.3"); |
| 80 | + |
| 81 | + // Act |
| 82 | + var result = this.buildServer.GetCurrentBranch(false); |
| 83 | + |
| 84 | + // Assert |
| 85 | + result.ShouldBeNull(); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void GetCurrentBranchShouldHandlePullRequests() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null); |
| 93 | + this.environment.SetEnvironmentVariable("BITBUCKET_TAG", null); |
| 94 | + this.environment.SetEnvironmentVariable("BITBUCKET_PR_ID", "refs/pull/1/merge"); |
| 95 | + |
| 96 | + // Act |
| 97 | + var result = this.buildServer.GetCurrentBranch(false); |
| 98 | + |
| 99 | + // Assert |
| 100 | + result.ShouldBeNull(); |
| 101 | + } |
| 102 | +} |
0 commit comments