@@ -88,8 +88,8 @@ public void GetVersion_JsonCompatibility(string version, string assemblyVersion,
88
88
Assert . NotNull ( options ) ;
89
89
Assert . Equal ( version , options . Version ? . ToString ( ) ) ;
90
90
Assert . Equal ( assemblyVersion , options . AssemblyVersion ? . Version ? . ToString ( ) ) ;
91
- Assert . Equal ( precision , options . AssemblyVersion ? . Precision ) ;
92
- Assert . Equal ( buildNumberOffset , options . BuildNumberOffset ) ;
91
+ Assert . Equal ( precision , options . AssemblyVersion ? . PrecisionOrDefault ) ;
92
+ Assert . Equal ( buildNumberOffset , options . BuildNumberOffsetOrDefault ) ;
93
93
Assert . Equal ( publicReleaseRefSpec , options . PublicReleaseRefSpec ) ;
94
94
}
95
95
@@ -114,17 +114,19 @@ public void SetVersion_GetVersionFromFile(string expectedVersion, string expecte
114
114
}
115
115
116
116
[ Theory ]
117
- [ InlineData ( "2.3" , null , VersionOptions . VersionPrecision . Minor , 0 , @"{""version"":""2.3""}" ) ]
118
- [ InlineData ( "2.3" , "2.2" , VersionOptions . VersionPrecision . Minor , 0 , @"{""version"":""2.3"",""assemblyVersion"":""2.2""}" ) ]
119
- [ InlineData ( "2.3" , "2.2" , VersionOptions . VersionPrecision . Minor , - 1 , @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""buildNumberOffset"":-1}" ) ]
120
- [ InlineData ( "2.3" , "2.2" , VersionOptions . VersionPrecision . Revision , - 1 , @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"",""precision"":""revision""},""buildNumberOffset"":-1}" ) ]
121
- public void SetVersion_WritesSimplestFile ( string version , string assemblyVersion , VersionOptions . VersionPrecision precision , int buildNumberOffset , string expectedJson )
117
+ [ InlineData ( "2.3" , null , VersionOptions . VersionPrecision . Minor , 0 , false , @"{""version"":""2.3""}" ) ]
118
+ [ InlineData ( "2.3" , null , VersionOptions . VersionPrecision . Minor , null , true , @"{""version"":""2.3"",""assemblyVersion"":{""precision"":""minor""},""inherit"":true}" ) ]
119
+ [ InlineData ( "2.3" , "2.2" , VersionOptions . VersionPrecision . Minor , 0 , false , @"{""version"":""2.3"",""assemblyVersion"":""2.2""}" ) ]
120
+ [ InlineData ( "2.3" , "2.2" , VersionOptions . VersionPrecision . Minor , - 1 , false , @"{""version"":""2.3"",""assemblyVersion"":""2.2"",""buildNumberOffset"":-1}" ) ]
121
+ [ InlineData ( "2.3" , "2.2" , VersionOptions . VersionPrecision . Revision , - 1 , false , @"{""version"":""2.3"",""assemblyVersion"":{""version"":""2.2"",""precision"":""revision""},""buildNumberOffset"":-1}" ) ]
122
+ public void SetVersion_WritesSimplestFile ( string version , string assemblyVersion , VersionOptions . VersionPrecision ? precision , int ? buildNumberOffset , bool inherit , string expectedJson )
122
123
{
123
124
var versionOptions = new VersionOptions
124
125
{
125
126
Version = SemanticVersion . Parse ( version ) ,
126
- AssemblyVersion = new VersionOptions . AssemblyVersionOptions ( assemblyVersion != null ? new Version ( assemblyVersion ) : null , precision ) ,
127
+ AssemblyVersion = assemblyVersion != null || precision != null ? new VersionOptions . AssemblyVersionOptions ( assemblyVersion != null ? new Version ( assemblyVersion ) : null , precision ) : null ,
127
128
BuildNumberOffset = buildNumberOffset ,
129
+ Inherit = inherit ,
128
130
} ;
129
131
string pathWritten = VersionFile . SetVersion ( this . RepoPath , versionOptions ) ;
130
132
string actualFileContent = File . ReadAllText ( pathWritten ) ;
@@ -142,7 +144,7 @@ public void SetVersion_WritesSimplestFile(string version, string assemblyVersion
142
144
[ InlineData ( @"{""cloudBuild"":{""setVersionVariables"":true}}" , @"{}" ) ]
143
145
public void JsonMinification ( string full , string minimal )
144
146
{
145
- var settings = VersionOptions . JsonSettings ;
147
+ var settings = VersionOptions . GetJsonSettings ( ) ;
146
148
settings . Formatting = Formatting . None ;
147
149
148
150
// Assert that the two representations are equivalent.
@@ -271,6 +273,130 @@ public void GetVersion_String_MissingFile()
271
273
Assert . Null ( VersionFile . GetVersion ( this . RepoPath ) ) ;
272
274
}
273
275
276
+ [ Fact ]
277
+ public void VersionJson_InheritButNoParentFileFound ( )
278
+ {
279
+ this . InitializeSourceControl ( ) ;
280
+ this . WriteVersionFile (
281
+ new VersionOptions
282
+ {
283
+ Inherit = true ,
284
+ Version = SemanticVersion . Parse ( "14.2" ) ,
285
+ } ) ;
286
+ Assert . Throws < InvalidOperationException > ( ( ) => VersionFile . GetVersion ( this . Repo ) ) ;
287
+ }
288
+
289
+ [ Fact ]
290
+ public void VersionJson_DoNotInheritButNoVersionSpecified ( )
291
+ {
292
+ this . InitializeSourceControl ( ) ;
293
+ Assert . Throws < ArgumentException > ( ( ) => this . WriteVersionFile (
294
+ new VersionOptions
295
+ {
296
+ Inherit = false ,
297
+ } ) ) ;
298
+ }
299
+
300
+ [ Theory ]
301
+ [ InlineData ( false , false ) ]
302
+ [ InlineData ( true , false ) ]
303
+ [ InlineData ( true , true ) ]
304
+ public void VersionJson_Inheritance ( bool commitInSourceControl , bool bareRepo )
305
+ {
306
+ if ( commitInSourceControl )
307
+ {
308
+ this . InitializeSourceControl ( ) ;
309
+ }
310
+
311
+ VersionOptions level1 , level2 , level3 , level2NoInherit , level2InheritButResetVersion ;
312
+ this . WriteVersionFile (
313
+ level1 = new VersionOptions
314
+ {
315
+ Version = SemanticVersion . Parse ( "14.2" ) ,
316
+ AssemblyVersion = new VersionOptions . AssemblyVersionOptions { Precision = VersionOptions . VersionPrecision . Major } ,
317
+ } ) ;
318
+ this . WriteVersionFile (
319
+ level2 = new VersionOptions
320
+ {
321
+ Inherit = true ,
322
+ AssemblyVersion = new VersionOptions . AssemblyVersionOptions { Precision = VersionOptions . VersionPrecision . Minor } ,
323
+ } ,
324
+ "foo" ) ;
325
+ this . WriteVersionFile (
326
+ level3 = new VersionOptions
327
+ {
328
+ Inherit = true ,
329
+ BuildNumberOffset = 1 ,
330
+ } ,
331
+ @"foo\bar" ) ;
332
+ this . WriteVersionFile (
333
+ level2NoInherit = new VersionOptions
334
+ {
335
+ Version = SemanticVersion . Parse ( "10.1" ) ,
336
+ } ,
337
+ @"noInherit" ) ;
338
+ this . WriteVersionFile (
339
+ level2InheritButResetVersion = new VersionOptions
340
+ {
341
+ Inherit = true ,
342
+ Version = SemanticVersion . Parse ( "8.2" ) ,
343
+ } ,
344
+ @"inheritWithVersion" ) ;
345
+
346
+ Repository operatingRepo = this . Repo ;
347
+ if ( bareRepo )
348
+ {
349
+ operatingRepo = new Repository (
350
+ Repository . Clone ( this . RepoPath , CreateDirectoryForNewRepo ( ) , new CloneOptions { IsBare = true } ) ) ;
351
+ }
352
+
353
+ using ( operatingRepo )
354
+ {
355
+ VersionOptions GetOption ( string path ) => commitInSourceControl ? VersionFile . GetVersion ( operatingRepo , path ) : VersionFile . GetVersion ( Path . Combine ( this . RepoPath , path ) ) ;
356
+
357
+ var level1Options = GetOption ( string . Empty ) ;
358
+ Assert . False ( level1Options . Inherit ) ;
359
+
360
+ var level2Options = GetOption ( "foo" ) ;
361
+ Assert . Equal ( level1 . Version . Version . Major , level2Options . Version . Version . Major ) ;
362
+ Assert . Equal ( level1 . Version . Version . Minor , level2Options . Version . Version . Minor ) ;
363
+ Assert . Equal ( level2 . AssemblyVersion . Precision , level2Options . AssemblyVersion . Precision ) ;
364
+ Assert . True ( level2Options . Inherit ) ;
365
+
366
+ var level3Options = GetOption ( @"foo\bar" ) ;
367
+ Assert . Equal ( level1 . Version . Version . Major , level3Options . Version . Version . Major ) ;
368
+ Assert . Equal ( level1 . Version . Version . Minor , level3Options . Version . Version . Minor ) ;
369
+ Assert . Equal ( level2 . AssemblyVersion . Precision , level3Options . AssemblyVersion . Precision ) ;
370
+ Assert . Equal ( level2 . AssemblyVersion . Precision , level3Options . AssemblyVersion . Precision ) ;
371
+ Assert . Equal ( level3 . BuildNumberOffset , level3Options . BuildNumberOffset ) ;
372
+ Assert . True ( level3Options . Inherit ) ;
373
+
374
+ var level2NoInheritOptions = GetOption ( "noInherit" ) ;
375
+ Assert . Equal ( level2NoInherit . Version , level2NoInheritOptions . Version ) ;
376
+ Assert . Equal ( VersionOptions . DefaultVersionPrecision , level2NoInheritOptions . AssemblyVersionOrDefault . PrecisionOrDefault ) ;
377
+ Assert . False ( level2NoInheritOptions . Inherit ) ;
378
+
379
+ var level2InheritButResetVersionOptions = GetOption ( "inheritWithVersion" ) ;
380
+ Assert . Equal ( level2InheritButResetVersion . Version , level2InheritButResetVersionOptions . Version ) ;
381
+ Assert . True ( level2InheritButResetVersionOptions . Inherit ) ;
382
+
383
+ if ( commitInSourceControl )
384
+ {
385
+ int totalCommits = operatingRepo . Head . Commits . Count ( ) ;
386
+
387
+ // The version height should be the same for all those that inherit the version from the base,
388
+ // even though the inheriting files were introduced in successive commits.
389
+ Assert . Equal ( totalCommits , operatingRepo . GetVersionHeight ( ) ) ;
390
+ Assert . Equal ( totalCommits , operatingRepo . GetVersionHeight ( "foo" ) ) ;
391
+ Assert . Equal ( totalCommits , operatingRepo . GetVersionHeight ( @"foo\bar" ) ) ;
392
+
393
+ // These either don't inherit, or inherit but reset versions, so the commits were reset.
394
+ Assert . Equal ( 2 , operatingRepo . GetVersionHeight ( "noInherit" ) ) ;
395
+ Assert . Equal ( 1 , operatingRepo . GetVersionHeight ( "inheritWithVersion" ) ) ;
396
+ }
397
+ }
398
+ }
399
+
274
400
private void AssertPathHasVersion ( Commit commit , string absolutePath , VersionOptions expected )
275
401
{
276
402
var actual = VersionFile . GetVersion ( absolutePath ) ;
0 commit comments