@@ -218,12 +218,133 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA
218218 fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . FullSemVer ) , "1.0.1-1" ) ) ;
219219 }
220220
221- private static void AddGenerateGitVersionInformationTask ( ProjectCreator project , string targetToRun , string taskName ,
221+ [ TestCaseSource ( nameof ( Languages ) ) ]
222+ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUseProjectNamespaceIsSpecifiedAndRootNamespaceIsSet ( string language )
223+ {
224+ const string taskName = nameof ( GenerateGitVersionInformation ) ;
225+ const string outputProperty = nameof ( GenerateGitVersionInformation . GitVersionInformationFilePath ) ;
226+ var randDir = Guid . NewGuid ( ) . ToString ( "N" ) ;
227+
228+ var extension = FileHelper . GetFileExtension ( language ) ;
229+ using var result = ExecuteMsBuildExe ( project =>
230+ {
231+ var intermediateOutputPath = Path . Combine ( "$(MSBuildProjectDirectory)" , randDir ) ;
232+ AddGenerateGitVersionInformationTask ( project , taskName , taskName , outputProperty , language , intermediateOutputPath ) . Property ( "UseProjectNamespaceForGitVersionInformation" , "True" ) . Property ( "RootNamespace" , "Test.Root" ) ;
233+ } , language ) ;
234+
235+ result . ProjectPath . ShouldNotBeNullOrWhiteSpace ( ) ;
236+ result . MsBuild . Count . ShouldBeGreaterThan ( 0 ) ;
237+ result . MsBuild . OverallSuccess . ShouldBe ( true ) ;
238+ result . MsBuild . ShouldAllBe ( x => x . Succeeded ) ;
239+ result . Output . ShouldNotBeNullOrWhiteSpace ( ) ;
240+
241+ var generatedFilePath = PathHelper . Combine ( Path . GetDirectoryName ( result . ProjectPath ) , randDir , $ "GitVersionInformation.g.{ extension } ") ;
242+ result . Output . ShouldContain ( $ "{ outputProperty } : { generatedFilePath } ") ;
243+
244+ var fileContent = File . ReadAllText ( generatedFilePath ) ;
245+ TestContext . WriteLine ( fileContent ) ;
246+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Major ) , "1" ) ) ;
247+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Minor ) , "2" ) ) ;
248+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Patch ) , "4" ) ) ;
249+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . MajorMinorPatch ) , "1.2.4" ) ) ;
250+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . FullSemVer ) , "1.2.4-1" ) ) ;
251+ fileContent . ShouldContain ( "namespace Test.Root" , Case . Insensitive ) ;
252+
253+ }
254+
255+ [ TestCaseSource ( nameof ( Languages ) ) ]
256+ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUseProjectNamespaceIsSpecifiedAndRootNamespaceIsNotSet ( string language )
257+ {
258+ const string taskName = nameof ( GenerateGitVersionInformation ) ;
259+ const string outputProperty = nameof ( GenerateGitVersionInformation . GitVersionInformationFilePath ) ;
260+ var randDir = Guid . NewGuid ( ) . ToString ( "N" ) ;
261+
262+ var extension = FileHelper . GetFileExtension ( language ) ;
263+ using var result = ExecuteMsBuildExeInAzurePipeline ( project =>
264+ {
265+ var intermediateOutputPath = Path . Combine ( "$(MSBuildProjectDirectory)" , randDir ) ;
266+ AddGenerateGitVersionInformationTask ( project , taskName , taskName , outputProperty , language , intermediateOutputPath ) . Property ( "UseProjectNamespaceForGitVersionInformation" , "True" ) ;
267+ } , language ) ;
268+
269+ result . ProjectPath . ShouldNotBeNullOrWhiteSpace ( ) ;
270+ result . MsBuild . Count . ShouldBeGreaterThan ( 0 ) ;
271+ result . MsBuild . OverallSuccess . ShouldBe ( true ) ;
272+ result . MsBuild . ShouldAllBe ( x => x . Succeeded ) ;
273+ result . Output . ShouldNotBeNullOrWhiteSpace ( ) ;
274+
275+ var generatedFilePath = PathHelper . Combine ( Path . GetDirectoryName ( result . ProjectPath ) , randDir , $ "GitVersionInformation.g.{ extension } ") ;
276+ result . Output . ShouldContain ( $ "{ outputProperty } : { generatedFilePath } ") ;
277+
278+ var fileContent = File . ReadAllText ( generatedFilePath ) ;
279+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Major ) , "1" ) ) ;
280+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Minor ) , "0" ) ) ;
281+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Patch ) , "1" ) ) ;
282+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . MajorMinorPatch ) , "1.0.1" ) ) ;
283+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . FullSemVer ) , "1.0.1-1" ) ) ;
284+ fileContent . ShouldContain ( "namespace App" , Case . Insensitive ) ;
285+ }
286+
287+ [ TestCaseSource ( nameof ( Languages ) ) ]
288+ public void GenerateGitVersionInformationTaskShouldCreateFileWithUseProjectNamespaceSetAndRootNamespaceUnSet ( string language )
289+ {
290+ var extension = FileHelper . GetFileExtension ( language ) ;
291+ var task = new GenerateGitVersionInformation
292+ {
293+ Language = language ,
294+ UseProjectNamespaceForGitVersionInformation = "true" ,
295+ ProjectFile = "App.Project.csproj" ,
296+ } ;
297+ using var result = ExecuteMsBuildTask ( task ) ;
298+
299+ result . Success . ShouldBe ( true ) ;
300+ result . Errors . ShouldBe ( 0 ) ;
301+ result . Task . GitVersionInformationFilePath . ShouldNotBeNull ( ) ;
302+ result . Task . GitVersionInformationFilePath . ShouldMatch ( $@ "GitVersionInformation.*\.g\.{ extension } ") ;
303+
304+ var fileContent = File . ReadAllText ( result . Task . GitVersionInformationFilePath ) ;
305+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Major ) , "1" ) ) ;
306+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Minor ) , "2" ) ) ;
307+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Patch ) , "4" ) ) ;
308+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . MajorMinorPatch ) , "1.2.4" ) ) ;
309+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . FullSemVer ) , "1.2.4-1" ) ) ;
310+ fileContent . ShouldContain ( "namespace App.Project" , Case . Insensitive ) ;
311+ }
312+
313+ [ TestCaseSource ( nameof ( Languages ) ) ]
314+ public void GenerateGitVersionInformationTaskShouldCreateFileWithUseProjectNamespaceSetAndRootNamespaceIsSet ( string language )
315+ {
316+
317+ var extension = FileHelper . GetFileExtension ( language ) ;
318+ var task = new GenerateGitVersionInformation
319+ {
320+ Language = language ,
321+ UseProjectNamespaceForGitVersionInformation = "true" ,
322+ ProjectFile = "App.Project.csproj" ,
323+ RootNamespace = "App.Project.RootNamespace" ,
324+ } ;
325+ using var result = ExecuteMsBuildTask ( task ) ;
326+
327+ result . Success . ShouldBe ( true ) ;
328+ result . Errors . ShouldBe ( 0 ) ;
329+ result . Task . GitVersionInformationFilePath . ShouldNotBeNull ( ) ;
330+ result . Task . GitVersionInformationFilePath . ShouldMatch ( $@ "GitVersionInformation.*\.g\.{ extension } ") ;
331+
332+ var fileContent = File . ReadAllText ( result . Task . GitVersionInformationFilePath ) ;
333+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Major ) , "1" ) ) ;
334+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Minor ) , "2" ) ) ;
335+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . Patch ) , "4" ) ) ;
336+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . MajorMinorPatch ) , "1.2.4" ) ) ;
337+ fileContent . ShouldMatch ( string . Format ( regexPattern , nameof ( GitVersionVariables . FullSemVer ) , "1.2.4-1" ) ) ;
338+
339+ fileContent . ShouldContain ( "namespace App.Project.RootNamespace" ) ;
340+ }
341+
342+ private static ProjectCreator AddGenerateGitVersionInformationTask ( ProjectCreator project , string targetToRun , string taskName ,
222343 string outputProperty , string language ,
223344 string intermediateOutputPath = "$(MSBuildProjectDirectory)" )
224345 {
225346 var assemblyFileLocation = typeof ( GitVersionTaskBase ) . Assembly . Location ;
226- project . UsingTaskAssemblyFile ( taskName , assemblyFileLocation )
347+ return project . UsingTaskAssemblyFile ( taskName , assemblyFileLocation )
227348 . Property ( "ManagePackageVersionsCentrally" , "false" )
228349 . Property ( "GenerateAssemblyInfo" , "false" )
229350 . Property ( "Language" , language )
@@ -235,6 +356,8 @@ private static void AddGenerateGitVersionInformationTask(ProjectCreator project,
235356 { "ProjectFile" , "$(MSBuildProjectFullPath)" } ,
236357 { "Language" , "$(Language)" } ,
237358 { "IntermediateOutputPath" , intermediateOutputPath } ,
359+ { "UseProjectNamespaceForGitVersionInformation" , "$(UseProjectNamespaceForGitVersionInformation)" } ,
360+ { "RootNamespace" , "$(RootNamespace)" } ,
238361 } )
239362 . TaskOutputProperty ( outputProperty , outputProperty )
240363 . ItemGroup ( )
0 commit comments