forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringFormatWithExtensionTests.cs
More file actions
371 lines (331 loc) · 13.8 KB
/
Copy pathStringFormatWithExtensionTests.cs
File metadata and controls
371 lines (331 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System.Globalization;
using GitVersion.Formatting;
namespace GitVersion.Tests;
[TestFixture]
public class StringFormatWithExtensionTests
{
private TestEnvironment environment = null!;
[SetUp]
public void Setup() => this.environment = new TestEnvironment();
[Test]
public void FormatWithNoTokens()
{
var propertyObject = new { };
const string expected = "Some String without tokens";
var actual = expected.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithSingleSimpleToken()
{
var propertyObject = new { SomeProperty = "SomeValue" };
const string target = "{SomeProperty}";
const string expected = "SomeValue";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMultipleTokensAndVerbatimText()
{
var propertyObject = new { SomeProperty = "AValue", AnotherProperty = "Other Value" };
const string target = "{SomeProperty} some text {AnotherProperty}";
const string expected = "AValue some text Other Value";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithEnvVarToken()
{
this.environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
var propertyObject = new { };
const string target = "{env:GIT_VERSION_TEST_VAR}";
const string expected = "Env Var Value";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithEnvVarTokenWithFallback()
{
this.environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
var propertyObject = new { };
const string target = "{env:GIT_VERSION_TEST_VAR ?? \"fallback\"}";
const string expected = "Env Var Value";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithUnsetEnvVarToken_WithFallback()
{
this.environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null);
var propertyObject = new { };
const string target = "{env:GIT_VERSION_UNSET_TEST_VAR ?? \"fallback\"}";
const string expected = "fallback";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithUnsetEnvVarToken_WithoutFallback()
{
this.environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null);
var propertyObject = new { };
const string target = "{env:GIT_VERSION_UNSET_TEST_VAR}";
Assert.Throws<ArgumentException>(() => target.FormatWith(propertyObject, this.environment));
}
[Test]
public void FormatWithMultipleEnvVars()
{
this.environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_1", "Val-1");
this.environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR_2", "Val-2");
var propertyObject = new { };
const string target = "{env:GIT_VERSION_TEST_VAR_1} and {env:GIT_VERSION_TEST_VAR_2}";
const string expected = "Val-1 and Val-2";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMultipleEnvChars()
{
var propertyObject = new { };
const string target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ?? \"fallback\"}";
Assert.Throws<ArgumentException>(() => target.FormatWith(propertyObject, this.environment));
}
[Test]
public void FormatWithMultipleFallbackChars()
{
var propertyObject = new { };
const string target = " and {env:DUMMY_VAR ??? \"fallback\"}";
Assert.Throws<FormatException>(() => target.FormatWith(propertyObject, this.environment));
}
[Test]
public void FormatWithSingleFallbackChar()
{
this.environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "DummyVal");
var propertyObject = new { };
const string target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR??\"fallback\"}";
Assert.Throws<ArgumentException>(() => target.FormatWith(propertyObject, this.environment));
}
[Test]
public void FormatWithNullPropagationWithMultipleSpaces()
{
var propertyObject = new { SomeProperty = "Some Value" };
const string target = "{SomeProperty} and {env:DUMMY_ENV_VAR ?? \"fallback\"}";
const string expected = "Some Value and fallback";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMissingPropertyAndEnvFallback()
{
this.environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "Dummy-Value");
var propertyObject = new { };
const string target = "{SomeProperty ?? env:DUMMY_ENV_VAR}";
const string expected = "Dummy-Value";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMissingEnvAndPropertyFallback()
{
var propertyObject = new { SomeProperty = "Some Value" };
const string target = "{env:DUMMY_ENV_VAR ?? SomeProperty}";
const string expected = "Some Value";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMultiplePropertiesAndNoFallback()
{
var propertyObject = new { };
const string target = "{SomeProperty ?? SomeOtherProperty ?? MissingProp}";
Assert.Throws<ArgumentException>(() => target.FormatWith(propertyObject, this.environment));
}
[Test]
public void FormatWithMultiplePropertiesAndQuotedFallback()
{
var propertyObject = new { };
const string target = "{SomeProperty ?? SomeOtherProperty ?? \"fallback\"}";
const string expected = "fallback";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMultipleAvailablePropertiesAndFallback()
{
var propertyObject = new { SomeOtherProperty = "Some-Value" };
const string target = "{SomeProperty ?? SomeOtherProperty ?? \"fallback\"}";
const string expected = "Some-Value";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithMissingPropertiesAndIntegerFallback()
{
var propertyObject = new { };
const string target = "{SomeProperty ?? SomeOtherProperty ?? 47}";
const string expected = "47";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void FormatWithPropertyAndEnvAndFormatters()
{
this.environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "DummyVal");
var propertyObject = new { SomeProperty = "TheValue" };
const string target = "{SomeProperty:l} and {env:DUMMY_ENV_VAR:l}";
const string expected = "thevalue and dummyval";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[TestCase("{env:VARIABLE ?? \"\"}", null, null, "")]
[TestCase("{env:MISSING ?? env:VARIABLE}", "Var", null, "Var")]
[TestCase("{Property ?? \"\"}", null, null, "")]
[TestCase("{Property ?? 47}", null, null, "47")]
[TestCase("{Property ?? env:VARIABLE}", null, "Branch", "Branch")]
[TestCase("{Property ?? env:VARIABLE ?? \"\"}", null, null, "")]
[TestCase("{Property ?? env:VARIABLE ?? 42}", null, null, "42")]
public void FormatWith_EnvVarAndPropertyAndFallback_DoesNotThrow(string input, string? envVar, string? property, string expected)
{
if (envVar != null)
{
this.environment.SetEnvironmentVariable("VARIABLE", envVar);
}
object propertyObject = property != null
? new { Property = property }
: new { };
var actual = input.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
[TestCase("{env:VARIABLE}")]
[TestCase("{Property}")]
[TestCase("{Property ?? env:VARIABLE}")]
[TestCase("{Property ?? Property}")]
public void FormatWith_MissingEnvVarOrPropertyAndNoFallback_Throws(string input)
{
object propertyObject = new { };
Assert.Throws<ArgumentException>(() => input.FormatWith(propertyObject, this.environment));
}
[Test]
public void FormatEnvVar_WithFallback_QuotedAndEmpty()
{
this.environment.SetEnvironmentVariable("ENV_VAR", null);
var propertyObject = new { };
const string target = "{env:ENV_VAR ?? \"\"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(""));
}
[Test]
public void FormatProperty_String()
{
var propertyObject = new { Property = "Value" };
const string target = "{Property}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("Value"));
}
[Test]
public void FormatProperty_Integer()
{
var propertyObject = new { Property = 42 };
const string target = "{Property}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("42"));
}
[Test]
public void FormatProperty_NullObject()
{
var propertyObject = new { Property = (object?)null };
const string target = "{Property}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(""));
}
[Test]
public void FormatProperty_NullInteger()
{
var propertyObject = new { Property = (int?)null };
const string target = "{Property}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(""));
}
[Test]
public void FormatProperty_String_WithFallback()
{
var propertyObject = new { Property = "Value" };
const string target = "{Property ?? \"fallback\"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("Value"));
}
[Test]
public void FormatProperty_Integer_WithFallback()
{
var propertyObject = new { Property = 42 };
const string target = "{Property ?? \"fallback\"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("42"));
}
[Test]
public void FormatProperty_NullObject_WithFallback()
{
var propertyObject = new { Property = (object?)null };
const string target = "{Property ?? \"fallback\"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("fallback"));
}
[Test]
public void FormatProperty_NullInteger_WithFallback()
{
var propertyObject = new { Property = (int?)null };
const string target = "{Property ?? 43}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("43"));
}
[Test]
public void FormatProperty_NullObject_WithFallback_Quoted()
{
var propertyObject = new { Property = (object?)null };
const string target = "{Property ?? \"literal\"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo("literal"));
}
[Test]
public void FormatProperty_NullObject_WithFallback_QuotedAndPadded()
{
var propertyObject = new { Property = (object?)null };
const string target = "{Property ?? \" fallback \"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(" fallback "));
}
[Test]
public void FormatProperty_NullObject_WithFallback_QuotedAndEmpty()
{
var propertyObject = new { Property = (object?)null };
const string target = "{Property ?? \"\"}";
var actual = target.FormatWith(propertyObject, this.environment);
Assert.That(actual, Is.EqualTo(""));
}
[Test]
public void FormatAssemblyInformationalVersionWithSemanticVersionCustomFormattedVersionSourceDistance()
{
var semanticVersion = new SemanticVersion
{
Major = 1,
Minor = 2,
Patch = 3,
PreReleaseTag = new SemanticVersionPreReleaseTag(string.Empty, 9, true),
BuildMetaData = new SemanticVersionBuildMetaData("Branch.main")
{
Branch = "main",
VersionSourceSha = "versionSourceSha",
Sha = "commitSha",
ShortSha = "commitShortSha",
VersionSourceDistance = 42,
CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z", CultureInfo.InvariantCulture)
}
};
const string expected = "1.2.3-0042";
var target = "{Major}.{Minor}.{Patch}-{VersionSourceDistance:0000}";
var actual = target.FormatWith(semanticVersion, this.environment);
Assert.That(actual, Is.EqualTo(expected));
target = "{Major}.{Minor}.{Patch}-{CommitsSinceVersionSource:0000}";
actual = target.FormatWith(semanticVersion, this.environment);
Assert.That(actual, Is.EqualTo(expected));
}
}