1
- using System . IO ;
1
+ using System ;
2
+ using System . IO ;
3
+ using System . Linq ;
2
4
using GitVersion ;
3
5
using LibGit2Sharp ;
4
6
using NUnit . Framework ;
@@ -24,58 +26,119 @@ public GitPreparerTests()
24
26
[ TestCase ( SpecificBranchName , SpecificBranchName , true ) ]
25
27
public void WorksCorrectlyWithRemoteRepository ( string branchName , string expectedBranchName , bool checkConfig )
26
28
{
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 ;
28
34
29
- using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
35
+ try
30
36
{
31
- fixture . Repository . MakeCommits ( 5 ) ;
32
-
33
- if ( checkConfig )
37
+ using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
34
38
{
35
- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
36
- }
39
+ var expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( '\\ ' ) . Last ( ) ) ;
37
40
38
- fixture . Repository . CreateBranch ( SpecificBranchName ) ;
41
+ fixture . Repository . MakeCommits ( 5 ) ;
42
+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
39
43
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
+ }
43
48
44
- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
49
+ fixture . Repository . CreateBranch ( SpecificBranchName ) ;
45
50
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 ] ) ;
48
54
49
- var arguments = new Arguments
50
- {
51
- TargetPath = tempDir ,
52
- TargetUrl = fixture . RepositoryPath
53
- } ;
55
+ fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
54
56
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
+ } ;
59
65
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" ) ) ;
62
68
63
- dynamicRepositoryPath . ShouldBe ( Path . Combine ( tempDir , "_dynamicrepository" , ".git" ) ) ;
64
- gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
69
+ if ( ! string . IsNullOrWhiteSpace ( branchName ) )
70
+ {
71
+ arguments . TargetBranch = branchName ;
72
+ }
65
73
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 ( ) ;
69
77
70
- currentBranch . EndsWith ( expectedBranchName ) . ShouldBe ( true ) ;
78
+ gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
79
+ gitPreparer . DynamicGitRepositoryPath . ShouldBe ( expectedDynamicRepoLocation + "\\ .git" ) ;
71
80
72
- if ( checkConfig )
81
+ using ( var repository = new Repository ( dynamicRepositoryPath ) )
73
82
{
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
+ }
76
92
}
77
93
}
78
94
}
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
+ }
79
142
}
80
143
81
144
[ Test ]
@@ -89,7 +152,7 @@ public void WorksCorrectlyWithLocalRepository()
89
152
} ;
90
153
91
154
var gitPreparer = new GitPreparer ( arguments ) ;
92
- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
155
+ var dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
93
156
94
157
dynamicRepositoryPath . ShouldBe ( null ) ;
95
158
gitPreparer . IsDynamicGitRepository . ShouldBe ( false ) ;
0 commit comments