1- using System . IO ;
1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
24using GitVersion ;
35using LibGit2Sharp ;
46using NUnit . Framework ;
@@ -24,58 +26,119 @@ public GitPreparerTests()
2426 [ TestCase ( SpecificBranchName , SpecificBranchName , true ) ]
2527 public void WorksCorrectlyWithRemoteRepository ( string branchName , string expectedBranchName , bool checkConfig )
2628 {
27- var tempDir = Path . GetTempPath ( ) ;
29+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
30+ var tempPath = Path . GetTempPath ( ) ;
31+ var tempDir = Path . Combine ( tempPath , repoName ) ;
32+ Directory . CreateDirectory ( tempDir ) ;
33+ string dynamicRepositoryPath = null ;
2834
29- using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
35+ try
3036 {
31- fixture . Repository . MakeCommits ( 5 ) ;
32-
33- if ( checkConfig )
37+ using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
3438 {
35- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
36- }
39+ var expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( '\\ ' ) . Last ( ) ) ;
3740
38- fixture . Repository . CreateBranch ( SpecificBranchName ) ;
41+ fixture . Repository . MakeCommits ( 5 ) ;
42+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
3943
40- if ( checkConfig )
41- {
42- fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + SpecificBranchName ] ) ;
44+ if ( checkConfig )
45+ {
46+ fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
47+ }
4348
44- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
49+ fixture . Repository . CreateBranch ( SpecificBranchName ) ;
4550
46- fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + DefaultBranchName ] ) ;
47- }
51+ if ( checkConfig )
52+ {
53+ fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + SpecificBranchName ] ) ;
4854
49- var arguments = new Arguments
50- {
51- TargetPath = tempDir ,
52- TargetUrl = fixture . RepositoryPath
53- } ;
55+ fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
5456
55- if ( ! string . IsNullOrWhiteSpace ( branchName ) )
56- {
57- arguments . TargetBranch = branchName ;
58- }
57+ fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + DefaultBranchName ] ) ;
58+ }
59+
60+ var arguments = new Arguments
61+ {
62+ TargetPath = tempDir ,
63+ TargetUrl = fixture . RepositoryPath
64+ } ;
5965
60- var gitPreparer = new GitPreparer ( arguments ) ;
61- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
66+ // Copy contents into working directory
67+ File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
6268
63- dynamicRepositoryPath . ShouldBe ( Path . Combine ( tempDir , "_dynamicrepository" , ".git" ) ) ;
64- gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
69+ if ( ! string . IsNullOrWhiteSpace ( branchName ) )
70+ {
71+ arguments . TargetBranch = branchName ;
72+ }
6573
66- using ( var repository = new Repository ( dynamicRepositoryPath ) )
67- {
68- var currentBranch = repository . Head . CanonicalName ;
74+ var gitPreparer = new GitPreparer ( arguments ) ;
75+ gitPreparer . InitialiseDynamicRepositoryIfNeeded ( ) ;
76+ dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
6977
70- currentBranch . EndsWith ( expectedBranchName ) . ShouldBe ( true ) ;
78+ gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
79+ gitPreparer . DynamicGitRepositoryPath . ShouldBe ( expectedDynamicRepoLocation + "\\ .git" ) ;
7180
72- if ( checkConfig )
81+ using ( var repository = new Repository ( dynamicRepositoryPath ) )
7382 {
74- var expectedConfigPath = Path . Combine ( dynamicRepositoryPath , "..\\ GitVersionConfig.yaml" ) ;
75- File . Exists ( expectedConfigPath ) . ShouldBe ( true ) ;
83+ var currentBranch = repository . Head . CanonicalName ;
84+
85+ currentBranch . EndsWith ( expectedBranchName ) . ShouldBe ( true ) ;
86+
87+ if ( checkConfig )
88+ {
89+ var expectedConfigPath = Path . Combine ( dynamicRepositoryPath , "..\\ GitVersionConfig.yaml" ) ;
90+ File . Exists ( expectedConfigPath ) . ShouldBe ( true ) ;
91+ }
7692 }
7793 }
7894 }
95+ finally
96+ {
97+ Directory . Delete ( tempDir , true ) ;
98+ if ( dynamicRepositoryPath != null )
99+ DeleteHelper . DeleteGitRepository ( dynamicRepositoryPath ) ;
100+ }
101+ }
102+
103+ [ Test ]
104+ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken ( )
105+ {
106+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
107+ var tempPath = Path . GetTempPath ( ) ;
108+ var tempDir = Path . Combine ( tempPath , repoName ) ;
109+ Directory . CreateDirectory ( tempDir ) ;
110+ string expectedDynamicRepoLocation = null ;
111+
112+ try
113+ {
114+ using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
115+ {
116+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
117+ File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
118+ expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( '\\ ' ) . Last ( ) ) ;
119+ Directory . CreateDirectory ( expectedDynamicRepoLocation ) ;
120+
121+ var arguments = new Arguments
122+ {
123+ TargetPath = tempDir ,
124+ TargetUrl = fixture . RepositoryPath
125+ } ;
126+
127+ var gitPreparer = new GitPreparer ( arguments ) ;
128+ gitPreparer . InitialiseDynamicRepositoryIfNeeded ( ) ;
129+
130+ gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
131+ gitPreparer . DynamicGitRepositoryPath . ShouldBe ( expectedDynamicRepoLocation + "_1\\ .git" ) ;
132+ }
133+ }
134+ finally
135+ {
136+ Directory . Delete ( tempDir , true ) ;
137+ if ( expectedDynamicRepoLocation != null )
138+ Directory . Delete ( expectedDynamicRepoLocation , true ) ;
139+ if ( expectedDynamicRepoLocation != null )
140+ DeleteHelper . DeleteGitRepository ( expectedDynamicRepoLocation + "_1" ) ;
141+ }
79142 }
80143
81144 [ Test ]
@@ -89,7 +152,7 @@ public void WorksCorrectlyWithLocalRepository()
89152 } ;
90153
91154 var gitPreparer = new GitPreparer ( arguments ) ;
92- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
155+ var dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
93156
94157 dynamicRepositoryPath . ShouldBe ( null ) ;
95158 gitPreparer . IsDynamicGitRepository . ShouldBe ( false ) ;
0 commit comments