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