forked from dotnet/sourcelink
/
GetSourceLinkUrlTests.cs
241 lines (203 loc) · 10.5 KB
/
GetSourceLinkUrlTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Build.Tasks.SourceControl;
using TestUtilities;
using Xunit;
using static TestUtilities.KeyValuePairUtils;
namespace Microsoft.SourceLink.AzureDevOpsServer.Git.UnitTests
{
public class GetSourceLinkUrlTests
{
[Fact]
public void EmptyHosts()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("x", KVP("RepositoryUrl", "http://abc.com"), KVP("SourceControl", "git")),
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences(
"ERROR : " + string.Format(CommonResources.AtLeastOneRepositoryHostIsRequired, "SourceLinkAzureDevOpsServerGitHost", "AzureDevOpsServer.Git"), engine.Log);
Assert.False(result);
}
[Fact]
public void BadUrl()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "http://tfs.com/tfs/zzz"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("tfs.com", KVP("VirtualDirectory", "tfs"))
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences(
"ERROR : " + string.Format(CommonResources.ValueOfWithIdentityIsInvalid, "SourceRoot.RepositoryUrl", "/src/", "http://tfs.com/tfs/zzz"), engine.Log);
Assert.False(result);
}
[Theory]
[InlineData("http://contoso.com/tfs2/collection/project/_git/repo", "tfs")]
[InlineData("http://contoso.com/tfs/collection/project/_git/repo", "tfs2")]
[InlineData("http://contoso.com/x/y/collection/project/_git/repo", "x/z")]
[InlineData("http://contoso.com/x/y/collection/project/_git/repo", "x/z/")]
[InlineData("http://contoso.com/x/y/collection/project/_git/repo", "/x/z")]
public void BadUrl_NotMatchingToVirtualDirectory(string url, string virtualDirectory)
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", url), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("contoso.com", KVP("VirtualDirectory", virtualDirectory))
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences(
"ERROR : " + string.Format(CommonResources.ValueOfWithIdentityIsInvalid, "SourceRoot.RepositoryUrl", "/src/", url), engine.Log);
Assert.False(result);
}
[Fact]
public void ImplicitHostNotSupported()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "http://contoso.com:100/tfs/collection/project/_git/repo"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
IsSingleProvider = true,
RepositoryUrl = "http://subdomain.tfs.com:100/tfs/collection/project/_git/repo"
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences(
"ERROR : " + string.Format(CommonResources.AtLeastOneRepositoryHostIsRequired, "SourceLinkAzureDevOpsServerGitHost", "AzureDevOpsServer.Git"), engine.Log);
Assert.False(result);
}
[Theory]
[InlineData("", "")]
[InlineData("", "/")]
[InlineData("/", "")]
[InlineData("/", "/")]
public void BuildSourceLinkUrl(string s1, string s2)
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "http://subdomain.tfs.com:100/collection/project/_git/repo" + s1), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("tfs.com", KVP("VirtualDirectory", "/"), KVP("ContentUrl", "https://domain.com/x/y" + s2)),
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences("", engine.Log);
AssertEx.AreEqual("https://domain.com/x/y/collection/project/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=0123456789abcdefABCDEF000000000000000000&path=/*", task.SourceLinkUrl);
Assert.True(result);
}
[Theory]
[InlineData("", "/")]
[InlineData("/", "/")]
[InlineData("a", "/a/")]
[InlineData("a/", "/a/")]
[InlineData("/a", "/a/")]
[InlineData("/a/", "/a/")]
[InlineData("/A/", "/a/")]
[InlineData("/A/b/C", "/a/B/c/")]
public void DefaultContentUrl(string virtualDirectory, string localPath)
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", $"http://contoso.com{localPath}collection/project/_git/repo"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("contoso.com", KVP("VirtualDirectory", virtualDirectory)),
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences("", engine.Log);
AssertEx.AreEqual($"http://contoso.com{localPath}collection/project/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=0123456789abcdefABCDEF000000000000000000&path=/*", task.SourceLinkUrl);
Assert.True(result);
}
[Fact]
public void VirtualDirectory_Collection()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "https://contoso.com:8080/tfs/collection/_git/repo"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("contoso.com", KVP("VirtualDirectory", "tfs")),
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences("", engine.Log);
AssertEx.AreEqual("https://contoso.com:8080/tfs/collection/repo/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=0123456789abcdefABCDEF000000000000000000&path=/*", task.SourceLinkUrl);
Assert.True(result);
}
[Fact]
public void VirtualDirectory_Collection_Project()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "https://contoso.com:8080/tfs/collection/project/_git/repo"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("contoso.com", KVP("VirtualDirectory", "tfs")),
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences("", engine.Log);
AssertEx.AreEqual("https://contoso.com:8080/tfs/collection/project/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=0123456789abcdefABCDEF000000000000000000&path=/*", task.SourceLinkUrl);
Assert.True(result);
}
[Fact]
public void VirtualDirectory_Collection_Project_Team()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "https://contoso.com:8080/tfs/collection/project/team/_git/repo"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("contoso.com", KVP("VirtualDirectory", "tfs")),
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences("", engine.Log);
AssertEx.AreEqual("https://contoso.com:8080/tfs/collection/project/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=0123456789abcdefABCDEF000000000000000000&path=/*", task.SourceLinkUrl);
Assert.True(result);
}
[Fact]
public void VirtualDirectory_Collection_Project_Team_Option()
{
var engine = new MockEngine();
var task = new GetSourceLinkUrl()
{
BuildEngine = engine,
SourceRoot = new MockItem("/src/", KVP("RepositoryUrl", "https://contoso.com:8080/tfs/collection/project/team/_git/_optimized/repo"), KVP("SourceControl", "git"), KVP("RevisionId", "0123456789abcdefABCDEF000000000000000000")),
Hosts = new[]
{
new MockItem("contoso.com", KVP("VirtualDirectory", "tfs")),
}
};
bool result = task.Execute();
AssertEx.AssertEqualToleratingWhitespaceDifferences("", engine.Log);
AssertEx.AreEqual("https://contoso.com:8080/tfs/collection/project/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=0123456789abcdefABCDEF000000000000000000&path=/*", task.SourceLinkUrl);
Assert.True(result);
}
}
}