1- using System . IO ;
1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
24using GitVersion ;
35using LibGit2Sharp ;
46using NUnit . Framework ;
@@ -18,64 +20,103 @@ public GitPreparerTests()
1820 const string SpecificBranchName = "feature/foo" ;
1921
2022 [ Test ]
21- [ TestCase ( null , DefaultBranchName , false ) ]
22- [ TestCase ( SpecificBranchName , SpecificBranchName , false ) ]
23- [ TestCase ( null , DefaultBranchName , true ) ]
24- [ TestCase ( SpecificBranchName , SpecificBranchName , true ) ]
25- public void WorksCorrectlyWithRemoteRepository ( string branchName , string expectedBranchName , bool checkConfig )
23+ [ TestCase ( null , DefaultBranchName ) ]
24+ [ TestCase ( SpecificBranchName , SpecificBranchName ) ]
25+ public void WorksCorrectlyWithRemoteRepository ( string branchName , string expectedBranchName )
2626 {
27- var tempDir = Path . GetTempPath ( ) ;
27+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
28+ var tempPath = Path . GetTempPath ( ) ;
29+ var tempDir = Path . Combine ( tempPath , repoName ) ;
30+ Directory . CreateDirectory ( tempDir ) ;
31+ string dynamicRepositoryPath = null ;
2832
29- using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
33+ try
3034 {
31- fixture . Repository . MakeCommits ( 5 ) ;
32-
33- if ( checkConfig )
35+ using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
3436 {
35- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
36- }
37+ var expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( '\\ ' ) . Last ( ) ) ;
3738
38- fixture . Repository . CreateBranch ( SpecificBranchName ) ;
39+ fixture . Repository . MakeCommits ( 5 ) ;
40+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
3941
40- if ( checkConfig )
41- {
42- fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + SpecificBranchName ] ) ;
42+ fixture . Repository . CreateBranch ( SpecificBranchName ) ;
4343
44- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
44+ var arguments = new Arguments
45+ {
46+ TargetPath = tempDir ,
47+ TargetUrl = fixture . RepositoryPath
48+ } ;
4549
46- fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + DefaultBranchName ] ) ;
47- }
50+ // Copy contents into working directory
51+ File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
4852
49- var arguments = new Arguments
50- {
51- TargetPath = tempDir ,
52- TargetUrl = fixture . RepositoryPath
53- } ;
53+ if ( ! string . IsNullOrWhiteSpace ( branchName ) )
54+ {
55+ arguments . TargetBranch = branchName ;
56+ }
5457
55- if ( ! string . IsNullOrWhiteSpace ( branchName ) )
56- {
57- arguments . TargetBranch = branchName ;
58- }
58+ var gitPreparer = new GitPreparer ( arguments ) ;
59+ gitPreparer . InitialiseDynamicRepositoryIfNeeded ( ) ;
60+ dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
5961
60- var gitPreparer = new GitPreparer ( arguments ) ;
61- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
62+ gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
63+ gitPreparer . DynamicGitRepositoryPath . ShouldBe ( expectedDynamicRepoLocation + " \\ .git" ) ;
6264
63- dynamicRepositoryPath . ShouldBe ( Path . Combine ( tempDir , "_dynamicrepository" , ".git" ) ) ;
64- gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
65+ using ( var repository = new Repository ( dynamicRepositoryPath ) )
66+ {
67+ var currentBranch = repository . Head . CanonicalName ;
6568
66- using ( var repository = new Repository ( dynamicRepositoryPath ) )
67- {
68- var currentBranch = repository . Head . CanonicalName ;
69+ currentBranch . EndsWith ( expectedBranchName ) . ShouldBe ( true ) ;
70+ }
71+ }
72+ }
73+ finally
74+ {
75+ Directory . Delete ( tempDir , true ) ;
76+ if ( dynamicRepositoryPath != null )
77+ DeleteHelper . DeleteGitRepository ( dynamicRepositoryPath ) ;
78+ }
79+ }
80+
81+ [ Test ]
82+ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken ( )
83+ {
84+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
85+ var tempPath = Path . GetTempPath ( ) ;
86+ var tempDir = Path . Combine ( tempPath , repoName ) ;
87+ Directory . CreateDirectory ( tempDir ) ;
88+ string expectedDynamicRepoLocation = null ;
6989
70- currentBranch . EndsWith ( expectedBranchName ) . ShouldBe ( true ) ;
90+ try
91+ {
92+ using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
93+ {
94+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
95+ File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
96+ expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( '\\ ' ) . Last ( ) ) ;
97+ Directory . CreateDirectory ( expectedDynamicRepoLocation ) ;
7198
72- if ( checkConfig )
99+ var arguments = new Arguments
73100 {
74- var expectedConfigPath = Path . Combine ( dynamicRepositoryPath , "..\\ GitVersionConfig.yaml" ) ;
75- File . Exists ( expectedConfigPath ) . ShouldBe ( true ) ;
76- }
101+ TargetPath = tempDir ,
102+ TargetUrl = fixture . RepositoryPath
103+ } ;
104+
105+ var gitPreparer = new GitPreparer ( arguments ) ;
106+ gitPreparer . InitialiseDynamicRepositoryIfNeeded ( ) ;
107+
108+ gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
109+ gitPreparer . DynamicGitRepositoryPath . ShouldBe ( expectedDynamicRepoLocation + "_1\\ .git" ) ;
77110 }
78111 }
112+ finally
113+ {
114+ Directory . Delete ( tempDir , true ) ;
115+ if ( expectedDynamicRepoLocation != null )
116+ Directory . Delete ( expectedDynamicRepoLocation , true ) ;
117+ if ( expectedDynamicRepoLocation != null )
118+ DeleteHelper . DeleteGitRepository ( expectedDynamicRepoLocation + "_1" ) ;
119+ }
79120 }
80121
81122 [ Test ]
@@ -89,7 +130,7 @@ public void WorksCorrectlyWithLocalRepository()
89130 } ;
90131
91132 var gitPreparer = new GitPreparer ( arguments ) ;
92- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
133+ var dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
93134
94135 dynamicRepositoryPath . ShouldBe ( null ) ;
95136 gitPreparer . IsDynamicGitRepository . ShouldBe ( false ) ;
0 commit comments