@@ -9,16 +9,22 @@ namespace GitVersion.Agents.Tests;
99[ TestFixture ]
1010public class GitLabCiTests : TestBase
1111{
12- private GitLabCi buildServer ;
1312 private IServiceProvider sp ;
13+ private GitLabCi buildServer ;
14+ private IEnvironment environment ;
1415
1516 [ SetUp ]
1617 public void SetUp ( )
1718 {
1819 this . sp = ConfigureServices ( services => services . AddSingleton < GitLabCi > ( ) ) ;
1920 this . buildServer = this . sp . GetRequiredService < GitLabCi > ( ) ;
21+ this . environment = this . sp . GetRequiredService < IEnvironment > ( ) ;
22+ this . environment . SetEnvironmentVariable ( GitLabCi . EnvironmentVariableName , "true" ) ;
2023 }
2124
25+ [ TearDown ]
26+ public void TearDown ( ) => this . environment . SetEnvironmentVariable ( GitLabCi . EnvironmentVariableName , null ) ;
27+
2228 [ Test ]
2329 public void GenerateSetVersionMessageReturnsVersionAsIsAlthoughThisIsNotUsedByJenkins ( )
2430 {
@@ -34,6 +40,55 @@ public void GenerateMessageTest()
3440 generatedParameterMessages [ 0 ] . ShouldBe ( "GitVersion_name=value" ) ;
3541 }
3642
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+
3792 [ Test ]
3893 public void WriteAllVariablesToTheTextWriter ( )
3994 {
0 commit comments