@@ -68,6 +68,126 @@ public void ShouldOutputFormatWithEnvironmentVariablesTests(string format, strin
68
68
output . ShouldBeEquivalentTo ( expectedValue ) ;
69
69
}
70
70
71
+ [ TestCase ( "Major" , "1" ) ]
72
+ [ TestCase ( "MajorMinorPatch" , "1.1.0" ) ]
73
+ [ TestCase ( "SemVer" , "1.1.0-foo.1" ) ]
74
+ [ TestCase ( "PreReleaseTagWithDash" , "-foo.1" ) ]
75
+ [ TestCase ( "AssemblySemFileVer" , "1.1.0.0" ) ]
76
+ [ TestCase ( "BranchName" , "feature/foo" ) ]
77
+ [ TestCase ( "FullSemVer" , "1.1.0-foo.1+1" ) ]
78
+ public void ShouldOutputDotEnvEntries ( string variableName , string expectedValue )
79
+ {
80
+ var fixture = CreateTestRepository ( ) ;
81
+
82
+ var consoleBuilder = new StringBuilder ( ) ;
83
+ IConsole consoleAdapter = new TestConsoleAdapter ( consoleBuilder ) ;
84
+
85
+ var sp = ConfigureServices ( services =>
86
+ {
87
+ var options = Options . Create ( new GitVersionOptions { WorkingDirectory = fixture . RepositoryPath , RepositoryInfo = { TargetBranch = fixture . Repository . Head . CanonicalName } , Output = { OutputType . DotEnv } } ) ;
88
+ var repository = fixture . Repository . ToGitRepository ( ) ;
89
+
90
+ services . AddSingleton ( options ) ;
91
+ services . AddSingleton ( repository ) ;
92
+ services . AddSingleton ( consoleAdapter ) ;
93
+ } ) ;
94
+
95
+ var versionVariables = sp . GetRequiredService < IGitVersionCalculateTool > ( ) . CalculateVersionVariables ( ) ;
96
+ var outputGenerator = sp . GetRequiredService < IOutputGenerator > ( ) ;
97
+
98
+ outputGenerator . Execute ( versionVariables , new ( ) ) ;
99
+ var output = consoleBuilder . ToString ( ) ;
100
+ output . ShouldContain ( "GitVersion_" + variableName + "=" + expectedValue + "\n " ) ;
101
+ }
102
+
103
+ [ TestCase ]
104
+ public void ShouldOutputAllCalculatedVariablesAsDotEnvEntries ( )
105
+ {
106
+ var fixture = CreateTestRepository ( ) ;
107
+
108
+ var consoleBuilder = new StringBuilder ( ) ;
109
+ IConsole consoleAdapter = new TestConsoleAdapter ( consoleBuilder ) ;
110
+
111
+ var sp = ConfigureServices ( services =>
112
+ {
113
+ var options = Options . Create ( new GitVersionOptions { WorkingDirectory = fixture . RepositoryPath , RepositoryInfo = { TargetBranch = fixture . Repository . Head . CanonicalName } , Output = { OutputType . DotEnv } } ) ;
114
+ var repository = fixture . Repository . ToGitRepository ( ) ;
115
+
116
+ services . AddSingleton ( options ) ;
117
+ services . AddSingleton ( repository ) ;
118
+ services . AddSingleton ( consoleAdapter ) ;
119
+ } ) ;
120
+
121
+ var versionVariables = sp . GetRequiredService < IGitVersionCalculateTool > ( ) . CalculateVersionVariables ( ) ;
122
+ var outputGenerator = sp . GetRequiredService < IOutputGenerator > ( ) ;
123
+
124
+ outputGenerator . Execute ( versionVariables , new ( ) ) ;
125
+ var output = consoleBuilder . ToString ( ) ;
126
+ var totalOutputLines = output . Split ( "\n " ) . Length - 1 ; // ignore last item that also ends with \n
127
+ Assert . That ( totalOutputLines , Is . EqualTo ( versionVariables . Count ( ) ) ) ;
128
+ }
129
+
130
+ [ TestCase ( "Major" , "0" ) ]
131
+ [ TestCase ( "MajorMinorPatch" , "0.0.1" ) ]
132
+ [ TestCase ( "SemVer" , "0.0.1-1" ) ]
133
+ [ TestCase ( "BuildMetaData" , "''" ) ]
134
+ [ TestCase ( "AssemblySemVer" , "0.0.1.0" ) ]
135
+ [ TestCase ( "PreReleaseTagWithDash" , "-1" ) ]
136
+ [ TestCase ( "BranchName" , "main" ) ]
137
+ [ TestCase ( "PreReleaseLabel" , "''" ) ]
138
+ [ TestCase ( "PreReleaseLabelWithDash" , "''" ) ]
139
+ public void ShouldOutputAllDotEnvEntriesEvenForMinimalRepositories ( string variableName , string expectedValue )
140
+ {
141
+ var fixture = CreateMinimalTestRepository ( ) ;
142
+
143
+ var consoleBuilder = new StringBuilder ( ) ;
144
+ IConsole consoleAdapter = new TestConsoleAdapter ( consoleBuilder ) ;
145
+
146
+ var sp = ConfigureServices ( services =>
147
+ {
148
+ var options = Options . Create ( new GitVersionOptions { WorkingDirectory = fixture . RepositoryPath , RepositoryInfo = { TargetBranch = fixture . Repository . Head . CanonicalName } , Output = { OutputType . DotEnv } } ) ;
149
+ var repository = fixture . Repository . ToGitRepository ( ) ;
150
+
151
+ services . AddSingleton ( options ) ;
152
+ services . AddSingleton ( repository ) ;
153
+ services . AddSingleton ( consoleAdapter ) ;
154
+ } ) ;
155
+
156
+ var versionVariables = sp . GetRequiredService < IGitVersionCalculateTool > ( ) . CalculateVersionVariables ( ) ;
157
+ var outputGenerator = sp . GetRequiredService < IOutputGenerator > ( ) ;
158
+
159
+ outputGenerator . Execute ( versionVariables , new ( ) ) ;
160
+ var output = consoleBuilder . ToString ( ) ;
161
+ output . ShouldContain ( "GitVersion_" + variableName + "=" + expectedValue + "\n " ) ;
162
+ }
163
+
164
+ [ TestCase ]
165
+ public void ShouldOutputAllCalculatedVariablesAsDotEnvEntriesEvenForMinimalRepositories ( )
166
+ {
167
+ var fixture = CreateMinimalTestRepository ( ) ;
168
+
169
+ var consoleBuilder = new StringBuilder ( ) ;
170
+ IConsole consoleAdapter = new TestConsoleAdapter ( consoleBuilder ) ;
171
+
172
+ var sp = ConfigureServices ( services =>
173
+ {
174
+ var options = Options . Create ( new GitVersionOptions { WorkingDirectory = fixture . RepositoryPath , RepositoryInfo = { TargetBranch = fixture . Repository . Head . CanonicalName } , Output = { OutputType . DotEnv } } ) ;
175
+ var repository = fixture . Repository . ToGitRepository ( ) ;
176
+
177
+ services . AddSingleton ( options ) ;
178
+ services . AddSingleton ( repository ) ;
179
+ services . AddSingleton ( consoleAdapter ) ;
180
+ } ) ;
181
+
182
+ var versionVariables = sp . GetRequiredService < IGitVersionCalculateTool > ( ) . CalculateVersionVariables ( ) ;
183
+ var outputGenerator = sp . GetRequiredService < IOutputGenerator > ( ) ;
184
+
185
+ outputGenerator . Execute ( versionVariables , new ( ) ) ;
186
+ var output = consoleBuilder . ToString ( ) ;
187
+ var totalOutputLines = output . Split ( "\n " ) . Length - 1 ; // ignore last item that also ends with \n
188
+ Assert . That ( totalOutputLines , Is . EqualTo ( versionVariables . Count ( ) ) ) ;
189
+ }
190
+
71
191
private static EmptyRepositoryFixture CreateTestRepository ( )
72
192
{
73
193
var fixture = new EmptyRepositoryFixture ( ) ;
@@ -80,4 +200,11 @@ private static EmptyRepositoryFixture CreateTestRepository()
80
200
_ = fixture . Repository . MakeACommit ( ) ;
81
201
return fixture ;
82
202
}
203
+
204
+ private static EmptyRepositoryFixture CreateMinimalTestRepository ( )
205
+ {
206
+ var fixture = new EmptyRepositoryFixture ( ) ;
207
+ _ = fixture . Repository . MakeACommit ( ) ;
208
+ return fixture ;
209
+ }
83
210
}
0 commit comments