|
| 1 | +using GitVersion.BuildAgents; |
| 2 | +using GitVersion.Core.Tests.Helpers; |
| 3 | +using GitVersion.Helpers; |
| 4 | +using GitVersion.VersionCalculation; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using NUnit.Framework; |
| 7 | +using Shouldly; |
| 8 | + |
| 9 | +namespace GitVersion.Core.Tests.BuildAgents; |
| 10 | + |
| 11 | +[TestFixture] |
| 12 | +public class BitBucketPipelinesTests : TestBase |
| 13 | +{ |
| 14 | + private IEnvironment environment; |
| 15 | + private BitBucketPipelines buildServer; |
| 16 | + private IServiceProvider sp; |
| 17 | + |
| 18 | + [SetUp] |
| 19 | + public void SetEnvironmentVariableForTest() |
| 20 | + { |
| 21 | + this.sp = ConfigureServices(services => services.AddSingleton<BitBucketPipelines>()); |
| 22 | + this.environment = sp.GetRequiredService<IEnvironment>(); |
| 23 | + this.buildServer = sp.GetRequiredService<BitBucketPipelines>(); |
| 24 | + |
| 25 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, "MyWorkspace"); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void CanNotApplyToCurrentContextWhenEnvironmentVariableNotSet() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, ""); |
| 34 | + |
| 35 | + // Act |
| 36 | + var result = this.buildServer.CanApplyToCurrentContext(); |
| 37 | + |
| 38 | + // Assert |
| 39 | + result.ShouldBeFalse(); |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void CalculateVersionOnMainBranch() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/main"); |
| 47 | + |
| 48 | + var vars = new TestableVersionVariables(fullSemVer: "1.2.3"); |
| 49 | + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); |
| 50 | + |
| 51 | + vsVersion.ShouldBe("1.2.3"); |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public void CalculateVersionOnDevelopBranch() |
| 56 | + { |
| 57 | + // Arrange |
| 58 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/develop"); |
| 59 | + |
| 60 | + var vars = new TestableVersionVariables(fullSemVer: "1.2.3-unstable.4"); |
| 61 | + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); |
| 62 | + |
| 63 | + vsVersion.ShouldBe("1.2.3-unstable.4"); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void CalculateVersionOnFeatureBranch() |
| 68 | + { |
| 69 | + // Arrange |
| 70 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/feature/my-work"); |
| 71 | + |
| 72 | + var vars = new TestableVersionVariables(fullSemVer: "1.2.3-beta.4"); |
| 73 | + var vsVersion = this.buildServer.GenerateSetVersionMessage(vars); |
| 74 | + |
| 75 | + vsVersion.ShouldBe("1.2.3-beta.4"); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void GetCurrentBranchShouldHandleBranches() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/feature/my-work"); |
| 83 | + |
| 84 | + // Act |
| 85 | + var result = this.buildServer.GetCurrentBranch(false); |
| 86 | + |
| 87 | + // Assert |
| 88 | + result.ShouldBe($"refs/heads/feature/my-work"); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public void GetCurrentBranchShouldHandleTags() |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, null); |
| 96 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.TagEnvironmentVariableName, "refs/heads/tags/1.2.3"); |
| 97 | + |
| 98 | + // Act |
| 99 | + var result = this.buildServer.GetCurrentBranch(false); |
| 100 | + |
| 101 | + // Assert |
| 102 | + result.ShouldBeNull(); |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void GetCurrentBranchShouldHandlePullRequests() |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, null); |
| 110 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.TagEnvironmentVariableName, null); |
| 111 | + this.environment.SetEnvironmentVariable(BitBucketPipelines.PullRequestEnvironmentVariableName, "refs/pull/1/merge"); |
| 112 | + |
| 113 | + // Act |
| 114 | + var result = this.buildServer.GetCurrentBranch(false); |
| 115 | + |
| 116 | + // Assert |
| 117 | + result.ShouldBeNull(); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + [Test] |
| 122 | + public void WriteAllVariablesToTheTextWriter() |
| 123 | + { |
| 124 | + var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| 125 | + assemblyLocation.ShouldNotBeNull(); |
| 126 | + var f = PathHelper.Combine(assemblyLocation, "gitversion.properties"); |
| 127 | + |
| 128 | + try |
| 129 | + { |
| 130 | + AssertVariablesAreWrittenToFile(f); |
| 131 | + } |
| 132 | + finally |
| 133 | + { |
| 134 | + File.Delete(f); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void AssertVariablesAreWrittenToFile(string file) |
| 139 | + { |
| 140 | + var writes = new List<string?>(); |
| 141 | + var semanticVersion = new SemanticVersion |
| 142 | + { |
| 143 | + Major = 1, |
| 144 | + Minor = 2, |
| 145 | + Patch = 3, |
| 146 | + PreReleaseTag = "beta1", |
| 147 | + BuildMetaData = "5" |
| 148 | + }; |
| 149 | + |
| 150 | + semanticVersion.BuildMetaData.CommitDate = new DateTimeOffset(2022, 4, 6, 16, 10, 59, TimeSpan.FromHours(10)); |
| 151 | + semanticVersion.BuildMetaData.Sha = "f28807e615e9f06aec8a33c87780374e0c1f6fb8"; |
| 152 | + |
| 153 | + var config = new TestEffectiveConfiguration(); |
| 154 | + var variableProvider = this.sp.GetRequiredService<IVariableProvider>(); |
| 155 | + |
| 156 | + var variables = variableProvider.GetVariablesFor(semanticVersion, config, false); |
| 157 | + |
| 158 | + this.buildServer.WithPropertyFile(file); |
| 159 | + |
| 160 | + this.buildServer.WriteIntegration(writes.Add, variables); |
| 161 | + |
| 162 | + writes[1].ShouldBe("1.2.3-beta.1+5"); |
| 163 | + |
| 164 | + File.Exists(file).ShouldBe(true); |
| 165 | + |
| 166 | + var props = File.ReadAllText(file); |
| 167 | + |
| 168 | + props.ShouldContain("export GITVERSION_MAJOR=1"); |
| 169 | + props.ShouldContain("export GITVERSION_MINOR=2"); |
| 170 | + props.ShouldContain("export GITVERSION_SHA=f28807e615e9f06aec8a33c87780374e0c1f6fb8"); |
| 171 | + props.ShouldContain("export GITVERSION_COMMITDATE=2022-04-06"); |
| 172 | + } |
| 173 | +} |
0 commit comments