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 ;
@@ -18,64 +20,103 @@ public GitPreparerTests()
18
20
const string SpecificBranchName = "feature/foo" ;
19
21
20
22
[ 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 )
26
26
{
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 ;
28
32
29
- using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
33
+ try
30
34
{
31
- fixture . Repository . MakeCommits ( 5 ) ;
32
-
33
- if ( checkConfig )
35
+ using ( var fixture = new EmptyRepositoryFixture ( new Config ( ) ) )
34
36
{
35
- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
36
- }
37
+ var expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( '\\ ' ) . Last ( ) ) ;
37
38
38
- fixture . Repository . CreateBranch ( SpecificBranchName ) ;
39
+ fixture . Repository . MakeCommits ( 5 ) ;
40
+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
39
41
40
- if ( checkConfig )
41
- {
42
- fixture . Repository . Refs . UpdateTarget ( fixture . Repository . Refs . Head , fixture . Repository . Refs [ "refs/heads/" + SpecificBranchName ] ) ;
42
+ fixture . Repository . CreateBranch ( SpecificBranchName ) ;
43
43
44
- fixture . Repository . CreateFileAndCommit ( "GitVersionConfig.yaml" ) ;
44
+ var arguments = new Arguments
45
+ {
46
+ TargetPath = tempDir ,
47
+ TargetUrl = fixture . RepositoryPath
48
+ } ;
45
49
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" ) ) ;
48
52
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
+ }
54
57
55
- if ( ! string . IsNullOrWhiteSpace ( branchName ) )
56
- {
57
- arguments . TargetBranch = branchName ;
58
- }
58
+ var gitPreparer = new GitPreparer ( arguments ) ;
59
+ gitPreparer . InitialiseDynamicRepositoryIfNeeded ( ) ;
60
+ dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
59
61
60
- var gitPreparer = new GitPreparer ( arguments ) ;
61
- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
62
+ gitPreparer . IsDynamicGitRepository . ShouldBe ( true ) ;
63
+ gitPreparer . DynamicGitRepositoryPath . ShouldBe ( expectedDynamicRepoLocation + " \\ .git" ) ;
62
64
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 ;
65
68
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 ;
69
89
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 ) ;
71
98
72
- if ( checkConfig )
99
+ var arguments = new Arguments
73
100
{
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" ) ;
77
110
}
78
111
}
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
+ }
79
120
}
80
121
81
122
[ Test ]
@@ -89,7 +130,7 @@ public void WorksCorrectlyWithLocalRepository()
89
130
} ;
90
131
91
132
var gitPreparer = new GitPreparer ( arguments ) ;
92
- var dynamicRepositoryPath = gitPreparer . Prepare ( ) ;
133
+ var dynamicRepositoryPath = gitPreparer . GetDotGitDirectory ( ) ;
93
134
94
135
dynamicRepositoryPath . ShouldBe ( null ) ;
95
136
gitPreparer . IsDynamicGitRepository . ShouldBe ( false ) ;
0 commit comments