@@ -9,16 +9,22 @@ namespace GitVersion.Agents.Tests;
9
9
[ TestFixture ]
10
10
public class GitLabCiTests : TestBase
11
11
{
12
- private GitLabCi buildServer ;
13
12
private IServiceProvider sp ;
13
+ private GitLabCi buildServer ;
14
+ private IEnvironment environment ;
14
15
15
16
[ SetUp ]
16
17
public void SetUp ( )
17
18
{
18
19
this . sp = ConfigureServices ( services => services . AddSingleton < GitLabCi > ( ) ) ;
19
20
this . buildServer = this . sp . GetRequiredService < GitLabCi > ( ) ;
21
+ this . environment = this . sp . GetRequiredService < IEnvironment > ( ) ;
22
+ this . environment . SetEnvironmentVariable ( GitLabCi . EnvironmentVariableName , "true" ) ;
20
23
}
21
24
25
+ [ TearDown ]
26
+ public void TearDown ( ) => this . environment . SetEnvironmentVariable ( GitLabCi . EnvironmentVariableName , null ) ;
27
+
22
28
[ Test ]
23
29
public void GenerateSetVersionMessageReturnsVersionAsIsAlthoughThisIsNotUsedByJenkins ( )
24
30
{
@@ -34,6 +40,55 @@ public void GenerateMessageTest()
34
40
generatedParameterMessages [ 0 ] . ShouldBe ( "GitVersion_name=value" ) ;
35
41
}
36
42
43
+ [ TestCase ( "main" , "main" ) ]
44
+ [ TestCase ( "dev" , "dev" ) ]
45
+ [ TestCase ( "development" , "development" ) ]
46
+ [ TestCase ( "my_cool_feature" , "my_cool_feature" ) ]
47
+ [ TestCase ( "#3-change_projectname" , "#3-change_projectname" ) ]
48
+ public void GetCurrentBranchShouldHandleBranches ( string branchName , string expectedResult )
49
+ {
50
+ this . environment . SetEnvironmentVariable ( "CI_COMMIT_REF_NAME" , branchName ) ;
51
+
52
+ var result = this . buildServer . GetCurrentBranch ( false ) ;
53
+
54
+ result . ShouldBe ( expectedResult ) ;
55
+ }
56
+
57
+ [ TestCase ( "main" , "" , "main" ) ]
58
+ [ TestCase ( "v1.0.0" , "v1.0.0" , null ) ]
59
+ [ TestCase ( "development" , "" , "development" ) ]
60
+ [ TestCase ( "v1.2.1" , "v1.2.1" , null ) ]
61
+ public void GetCurrentBranchShouldHandleTags ( string branchName , string commitTag , string ? expectedResult )
62
+ {
63
+ this . environment . SetEnvironmentVariable ( "CI_COMMIT_REF_NAME" , branchName ) ;
64
+ this . environment . SetEnvironmentVariable ( "CI_COMMIT_TAG" , commitTag ) ; // only set in pipelines for tags
65
+
66
+ var result = this . buildServer . GetCurrentBranch ( false ) ;
67
+
68
+ if ( ! string . IsNullOrEmpty ( expectedResult ) )
69
+ {
70
+ result . ShouldBe ( expectedResult ) ;
71
+ }
72
+ else
73
+ {
74
+ result . ShouldBeNull ( ) ;
75
+ }
76
+ }
77
+
78
+ [ TestCase ( "main" , "main" ) ]
79
+ [ TestCase ( "dev" , "dev" ) ]
80
+ [ TestCase ( "development" , "development" ) ]
81
+ [ TestCase ( "my_cool_feature" , "my_cool_feature" ) ]
82
+ [ TestCase ( "#3-change_projectname" , "#3-change_projectname" ) ]
83
+ public void GetCurrentBranchShouldHandlePullRequests ( string branchName , string expectedResult )
84
+ {
85
+ this . environment . SetEnvironmentVariable ( "CI_COMMIT_REF_NAME" , branchName ) ;
86
+
87
+ var result = this . buildServer . GetCurrentBranch ( false ) ;
88
+
89
+ result . ShouldBe ( expectedResult ) ;
90
+ }
91
+
37
92
[ Test ]
38
93
public void WriteAllVariablesToTheTextWriter ( )
39
94
{
0 commit comments