1
1
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2
+ using System ;
2
3
using Microsoft . Build . Tasks . SourceControl ;
3
4
using TestUtilities ;
4
5
using Xunit ;
@@ -8,11 +9,6 @@ namespace Microsoft.SourceLink.Bitbucket.Git.UnitTests
8
9
{
9
10
public class GetSourceLinkUrlTests
10
11
{
11
- private const string ExpectedUrlForCloudEdition =
12
- "https://api.domain.com/x/y/2.0/repositories/a/b/src/0123456789abcdefABCDEF000000000000000000/*" ;
13
- private const string ExpectedUrlForEnterpriseEditionOldVersion = "https://bitbucket.domain.com/projects/a/repos/b/browse/*?at=0123456789abcdefABCDEF000000000000000000&raw" ;
14
- private const string ExpectedUrlForEnterpriseEditionNewVersion = "https://bitbucket.domain.com/projects/a/repos/b/raw/*?at=0123456789abcdefABCDEF000000000000000000" ;
15
-
16
12
[ Fact ]
17
13
public void EmptyHosts ( )
18
14
{
@@ -32,31 +28,79 @@ public void EmptyHosts()
32
28
Assert . False ( result ) ;
33
29
}
34
30
31
+ [ Theory ]
32
+ [ InlineData ( "a/b" , "" , "a" , "b" ) ]
33
+ [ InlineData ( "/a/b" , "" , "a" , "b" ) ]
34
+ [ InlineData ( "/a/b/" , "" , "a" , "b" ) ]
35
+ [ InlineData ( "scm/a" , "" , "scm" , "a" ) ]
36
+ [ InlineData ( "scm/a/b" , "" , "a" , "b" ) ]
37
+ [ InlineData ( "/r/scm/a/b" , "r" , "a" , "b" ) ]
38
+ [ InlineData ( "/r/s/scm/a/b" , "r/s" , "a" , "b" ) ]
39
+ [ InlineData ( "/r/s/a/b" , "r/s" , "a" , "b" ) ]
40
+ [ InlineData ( "/r/s/scm/b" , "r/s" , "scm" , "b" ) ]
41
+ public void TryParseEnterpriseUrl ( string relativeUrl , string expectedBaseUrl , string expectedProjectName , string expectedRepositoryName )
42
+ {
43
+ Assert . True ( GetSourceLinkUrl . TryParseEnterpriseUrl ( relativeUrl , out var baseUrl , out var projectName , out var repositoryName ) ) ;
44
+ Assert . Equal ( expectedBaseUrl , baseUrl ) ;
45
+ Assert . Equal ( expectedProjectName , projectName ) ;
46
+ Assert . Equal ( expectedRepositoryName , repositoryName ) ;
47
+ }
48
+
49
+ [ Theory ]
50
+ [ InlineData ( "" ) ]
51
+ [ InlineData ( "/" ) ]
52
+ [ InlineData ( "x" ) ]
53
+ public void TryParseEnterpriseUrl_Errors ( string relativeUrl )
54
+ {
55
+ Assert . False ( GetSourceLinkUrl . TryParseEnterpriseUrl ( relativeUrl , out _ , out _ , out _ ) ) ;
56
+ }
57
+
35
58
[ Theory ]
36
59
[ InlineData ( "" , "" ) ]
37
60
[ InlineData ( "" , "/" ) ]
38
61
[ InlineData ( "/" , "" ) ]
39
62
[ InlineData ( "/" , "/" ) ]
40
63
public void BuildSourceLinkUrl_BitbucketCloud ( string s1 , string s2 )
41
64
{
42
- var isEnterpriseEditionSetting = KVP ( "EnterpriseEdition" , "false" ) ;
43
65
var engine = new MockEngine ( ) ;
44
66
var task = new GetSourceLinkUrl ( )
45
67
{
46
68
BuildEngine = engine ,
47
69
SourceRoot = new MockItem ( "/src/" , KVP ( "RepositoryUrl" , "http://subdomain.mybitbucket.org:100/a/b" + s1 ) , KVP ( "SourceControl" , "git" ) , KVP ( "RevisionId" , "0123456789abcdefABCDEF000000000000000000" ) ) ,
48
70
Hosts = new [ ]
49
71
{
50
- new MockItem ( "mybitbucket.org" , KVP ( "ContentUrl" , "https://domain.com/x/y" + s2 ) , isEnterpriseEditionSetting ) ,
72
+ new MockItem ( "mybitbucket.org" , KVP ( "ContentUrl" , "https://domain.com/x/y" + s2 ) , KVP ( "EnterpriseEdition" , "false" ) ) ,
51
73
}
52
74
} ;
53
75
54
76
bool result = task . Execute ( ) ;
55
77
AssertEx . AssertEqualToleratingWhitespaceDifferences ( "" , engine . Log ) ;
56
- AssertEx . AreEqual ( ExpectedUrlForCloudEdition , task . SourceLinkUrl ) ;
78
+ AssertEx . AreEqual ( "https://api.domain.com/x/y/2.0/repositories/a/b/src/0123456789abcdefABCDEF000000000000000000/*" , task . SourceLinkUrl ) ;
57
79
Assert . True ( result ) ;
58
80
}
59
81
82
+ [ Fact ]
83
+ public void BuildSourceLinkUrl_BitbucketEnterprise_InvalidUrl ( )
84
+ {
85
+ var engine = new MockEngine ( ) ;
86
+ var task = new GetSourceLinkUrl ( )
87
+ {
88
+ BuildEngine = engine ,
89
+ SourceRoot = new MockItem ( "/src/" , KVP ( "RepositoryUrl" , "http://subdomain.mybitbucket.org/a" ) , KVP ( "SourceControl" , "git" ) , KVP ( "RevisionId" , "0123456789abcdefABCDEF000000000000000000" ) ) ,
90
+ Hosts = new [ ]
91
+ {
92
+ new MockItem ( "mybitbucket.org" , KVP ( "ContentUrl" , "https://domain.com/x/y" ) ) ,
93
+ }
94
+ } ;
95
+
96
+ bool result = task . Execute ( ) ;
97
+
98
+ AssertEx . AssertEqualToleratingWhitespaceDifferences (
99
+ "ERROR : " + string . Format ( CommonResources . ValueOfWithIdentityIsInvalid , "SourceRoot.RepositoryUrl" , "/src/" , "http://subdomain.mybitbucket.org/a" ) , engine . Log ) ;
100
+
101
+ Assert . False ( result ) ;
102
+ }
103
+
60
104
[ Fact ]
61
105
public void BuildSourceLinkUrl_MetadataWithEnterpriseEditionButWithoutVersion_UseNewVersionAsDefauld ( )
62
106
{
@@ -74,7 +118,7 @@ public void BuildSourceLinkUrl_MetadataWithEnterpriseEditionButWithoutVersion_Us
74
118
75
119
bool result = task . Execute ( ) ;
76
120
AssertEx . AssertEqualToleratingWhitespaceDifferences ( "" , engine . Log ) ;
77
- AssertEx . AreEqual ( ExpectedUrlForEnterpriseEditionNewVersion , task . SourceLinkUrl ) ;
121
+ AssertEx . AreEqual ( "https://bitbucket.domain.com/projects/a/repos/b/raw/*?at=0123456789abcdefABCDEF000000000000000000" , task . SourceLinkUrl ) ;
78
122
Assert . True ( result ) ;
79
123
}
80
124
@@ -104,7 +148,7 @@ public void BuildSourceLinkUrl_BitbucketEnterpriseOldVersionSsh(string s1, strin
104
148
105
149
bool result = task . Execute ( ) ;
106
150
AssertEx . AssertEqualToleratingWhitespaceDifferences ( "" , engine . Log ) ;
107
- AssertEx . AreEqual ( ExpectedUrlForEnterpriseEditionOldVersion , task . SourceLinkUrl ) ;
151
+ AssertEx . AreEqual ( "https://bitbucket.domain.com/projects/a/repos/b/browse/*?at=0123456789abcdefABCDEF000000000000000000&raw" , task . SourceLinkUrl ) ;
108
152
Assert . True ( result ) ;
109
153
}
110
154
@@ -125,7 +169,7 @@ public void BuildSourceLinkUrl_BitbucketEnterpriseOldVersionHttps(string s1, str
125
169
var task = new GetSourceLinkUrl ( )
126
170
{
127
171
BuildEngine = engine ,
128
- SourceRoot = new MockItem ( "/src/" , KVP ( "RepositoryUrl" , "http://bitbucket.domain.com:100/scm/a/b" + s1 ) , KVP ( "SourceControl" , "git" ) , KVP ( "RevisionId" , "0123456789abcdefABCDEF000000000000000000" ) ) ,
172
+ SourceRoot = new MockItem ( "/src/" , KVP ( "RepositoryUrl" , "http://bitbucket.domain.com:100/base/ scm/a/b" + s1 ) , KVP ( "SourceControl" , "git" ) , KVP ( "RevisionId" , "0123456789abcdefABCDEF000000000000000000" ) ) ,
129
173
Hosts = new [ ]
130
174
{
131
175
new MockItem ( "domain.com" , KVP ( "ContentUrl" , "https://bitbucket.domain.com" + s2 ) , isEnterpriseEditionSetting , version ) ,
@@ -134,7 +178,7 @@ public void BuildSourceLinkUrl_BitbucketEnterpriseOldVersionHttps(string s1, str
134
178
135
179
bool result = task . Execute ( ) ;
136
180
AssertEx . AssertEqualToleratingWhitespaceDifferences ( "" , engine . Log ) ;
137
- AssertEx . AreEqual ( ExpectedUrlForEnterpriseEditionOldVersion , task . SourceLinkUrl ) ;
181
+ AssertEx . AreEqual ( "https://bitbucket.domain.com/base/projects/a/repos/b/browse/*?at=0123456789abcdefABCDEF000000000000000000&raw" , task . SourceLinkUrl ) ;
138
182
Assert . True ( result ) ;
139
183
}
140
184
@@ -165,7 +209,7 @@ public void BuildSourceLinkUrl_BitbucketEnterpriseNewVersionSsh(string s1, strin
165
209
166
210
bool result = task . Execute ( ) ;
167
211
AssertEx . AssertEqualToleratingWhitespaceDifferences ( "" , engine . Log ) ;
168
- AssertEx . AreEqual ( ExpectedUrlForEnterpriseEditionNewVersion , task . SourceLinkUrl ) ;
212
+ AssertEx . AreEqual ( "https://bitbucket.domain.com/projects/a/repos/b/raw/*?at=0123456789abcdefABCDEF000000000000000000" , task . SourceLinkUrl ) ;
169
213
Assert . True ( result ) ;
170
214
}
171
215
@@ -196,7 +240,7 @@ public void BuildSourceLinkUrl_BitbucketEnterpriseNewVersionHttps(string s1, str
196
240
197
241
bool result = task . Execute ( ) ;
198
242
AssertEx . AssertEqualToleratingWhitespaceDifferences ( "" , engine . Log ) ;
199
- AssertEx . AreEqual ( ExpectedUrlForEnterpriseEditionNewVersion , task . SourceLinkUrl ) ;
243
+ AssertEx . AreEqual ( "https://bitbucket.domain.com/projects/a/repos/b/raw/*?at=0123456789abcdefABCDEF000000000000000000" , task . SourceLinkUrl ) ;
200
244
Assert . True ( result ) ;
201
245
}
202
246
0 commit comments