forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildManagerWrapperTest.cs
More file actions
315 lines (257 loc) · 12.7 KB
/
Copy pathBuildManagerWrapperTest.cs
File metadata and controls
315 lines (257 loc) · 12.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Hosting;
using Microsoft.Internal.Web.Utils;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.WebPages.Test
{
public class BuildManagerWrapperTest
{
private const string _precompileConfigFileName = "~/PrecompiledApp.config";
[Fact]
public void CanCreateObjectFactoryReturnsFalseIfExtensionIsNotRegistered()
{
// Arrange
var buildManagerWrapper = CreateWrapperInstance();
// Act
bool supported = buildManagerWrapper.IsPathExtensionSupported("~/styles/index.css");
// Assert
Assert.False(supported);
}
[Fact]
public void CanCreateObjectFactoryReturnsFalseIfVirtualPathIsExtensionless()
{
// Arrange
var buildManagerWrapper = CreateWrapperInstance();
// Act
bool supported = buildManagerWrapper.IsPathExtensionSupported("~/default");
// Assert
Assert.False(supported);
}
[Fact]
public void CanCreateObjectFactoryReturnsFalseIfVirtualPathExtensionIsEmpty()
{
// Arrange
var buildManagerWrapper = CreateWrapperInstance();
// Act
bool supported = buildManagerWrapper.IsPathExtensionSupported("~/default.");
// Assert
Assert.False(supported);
}
[Fact]
public void CanCreateObjectFactoryReturnsTrueIfVirtualPathExtensionIsRegistered()
{
// Arrange
var buildManagerWrapper = CreateWrapperInstance();
// Act
bool supported = buildManagerWrapper.IsPathExtensionSupported("~/default.cshtml");
// Assert
Assert.True(supported);
}
[Fact]
public void CanCreateObjectFactoryPerformsCaseInsenitiveComparison()
{
// Arrange
var buildManagerWrapper = CreateWrapperInstance();
// Act
bool supported = buildManagerWrapper.IsPathExtensionSupported("~/default.CShTml");
// Assert
Assert.True(supported);
}
[Fact]
public void CanCreateObjectFactoryWorksForAllRegisteredExtensions()
{
// Arrange
var buildManagerWrapper = CreateWrapperInstance();
// Act
bool supported = buildManagerWrapper.IsPathExtensionSupported("~/default.vbHtml");
// Assert
Assert.True(supported);
}
[Fact]
public void IsNonPrecompiledAppReturnsFalseIfPrecompiledConfigFileDoesNotExist()
{
// Arrange
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.IsAny<string>())).Returns(false);
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var isPrecompiled = buildManagerWrapper.IsNonUpdatablePrecompiledApp();
// Assert
Assert.False(isPrecompiled);
vpp.Verify();
}
[Fact]
public void IsNonPrecompiledAppReturnsFalseIfPrecompiledConfigFileIsNotValidXml()
{
// Arrange
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
var file = new Mock<VirtualFile>();
vpp.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile("some random text that is clearly not xml!"));
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var isPrecompiled = buildManagerWrapper.IsNonUpdatablePrecompiledApp();
// Assert
Assert.False(isPrecompiled);
vpp.Verify();
}
[Fact]
public void IsNonPrecompiledAppReturnsFalseIfConfigFileDoesNotContainExpectedElements()
{
// Arrange
var fileContent = @"<?xml version=""1.0""?><configuration><appSettings></appSettings></configuration>";
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
vpp.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var isPrecompiled = buildManagerWrapper.IsNonUpdatablePrecompiledApp();
// Assert
Assert.False(isPrecompiled);
vpp.Verify();
}
[Fact]
public void IsNonPrecompiledAppReturnsFalseIfAppIsUpdatable()
{
// Arrange
var fileContent = @"<precompiledApp version=""2"" updatable=""true""/>";
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
vpp.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var isPrecompiled = buildManagerWrapper.IsNonUpdatablePrecompiledApp();
// Assert
Assert.False(isPrecompiled);
vpp.Verify();
}
[Fact]
public void IsNonPrecompiledAppReturnsTrueIfConfigFileIsValidAndIsAppIsNotUpdatable()
{
// Arrange
var fileContent = @"<precompiledApp version=""2"" updatable=""false""/>";
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
vpp.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var isPrecompiled = buildManagerWrapper.IsNonUpdatablePrecompiledApp();
// Assert
Assert.True(isPrecompiled);
vpp.Verify();
}
[Fact]
public void ExistsUsesVppIfSiteIfNotPrecompiled()
{
// Arrange
var virtualPath = "~/default.cshtml";
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(false).Verifiable();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(virtualPath)))).Returns(true).Verifiable();
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var exists = buildManagerWrapper.Exists(virtualPath);
// Assert
vpp.Verify();
Assert.True(exists);
}
[Fact]
public void ExistsUsesVppIfSiteIfSiteIsPrecompiledButUpdateable()
{
// Arrange
var virtualPath = "~/default.cshtml";
var fileContent = @"<precompiledApp version=""2"" updatable=""true""/>";
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
vpp.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(virtualPath)))).Returns(true).Verifiable();
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var exists = buildManagerWrapper.Exists(virtualPath);
// Assert
vpp.Verify();
Assert.True(exists);
}
/// <remarks>
/// This method adds items to HttpRuntime.Cache.
/// </summary>
[Fact]
public void ExistsInPrecompiledReturnsFalseIfExtensionIsUnsupported()
{
// Arrange
var virtualPath = "~/ExistsInPrecompiledReturnsFalseIfExtensionIsUnsupported.jpeg";
var fileContent = @"<precompiledApp version=""2"" updatable=""false""/>";
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
vpp.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
var buildManagerWrapper = new BuildManagerWrapper(vpp.Object, GetVirtualPathUtility());
// Act
var exists = buildManagerWrapper.Exists(virtualPath);
// Assert
vpp.Verify();
Assert.False(exists);
object cachedValue = HttpRuntime.Cache.Get(BuildManagerWrapper.KeyGuid + "_" + virtualPath);
Assert.NotNull(cachedValue);
Assert.False((bool)cachedValue.GetType().GetProperty("Exists").GetValue(cachedValue, null));
}
[Fact]
public void IsNonPrecompiledAppReturnsTrue_VPPRegistrationChanging()
{
// Arrange
string fileContent = @"<precompiledApp version=""2"" updatable=""false""/>";
IVirtualPathUtility pathUtility = GetVirtualPathUtility();
Mock<VirtualPathProvider> mockProvider1 = new Mock<VirtualPathProvider>();
mockProvider1.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
mockProvider1.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
Mock<VirtualPathProvider> mockProvider2 = new Mock<VirtualPathProvider>();
mockProvider2.Setup(c => c.FileExists(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(true).Verifiable();
mockProvider2.Setup(c => c.GetFile(It.Is<string>(p => p.Equals(_precompileConfigFileName)))).Returns(GetFile(fileContent)).Verifiable();
VirtualPathProvider provider = mockProvider1.Object;
// Act; uses one VirtualPathProvider in constructor and the other when IsNonUpdatablePrecompiledApp() is called directly
BuildManagerWrapper buildManagerWrapper = new BuildManagerWrapper(() => provider, pathUtility);
// The moral equivalent of HostingEnvironment.RegisterVirtualPathProvider(provider2.Object)
provider = mockProvider2.Object;
bool isPrecompiled = buildManagerWrapper.IsNonUpdatablePrecompiledApp();
// Assert
Assert.True(isPrecompiled);
mockProvider1.Verify();
mockProvider1.Verify(vpp => vpp.FileExists(It.IsAny<string>()), Times.Once());
mockProvider1.Verify(vpp => vpp.GetFile(It.IsAny<string>()), Times.Once());
mockProvider2.Verify();
mockProvider2.Verify(vpp => vpp.FileExists(It.IsAny<string>()), Times.Once());
mockProvider2.Verify(vpp => vpp.GetFile(It.IsAny<string>()), Times.Once());
}
private static BuildManagerWrapper CreateWrapperInstance(IEnumerable<string> supportedExtensions = null)
{
return new BuildManagerWrapper(new Mock<VirtualPathProvider>().Object, GetVirtualPathUtility()) { SupportedExtensions = supportedExtensions ?? new[] { "cshtml", "vbhtml" } };
}
private static VirtualFile GetFile(string content)
{
var file = new Mock<VirtualFile>("test file");
file.Setup(f => f.Open()).Returns(() => new MemoryStream(Encoding.UTF8.GetBytes(content)));
return file.Object;
}
private static IVirtualPathUtility GetVirtualPathUtility()
{
var utility = new Mock<VirtualPathUtilityBase>();
utility.Setup(c => c.ToAbsolute(It.IsAny<string>())).Returns<string>(c => c);
return utility.Object;
}
}
public abstract class VirtualPathUtilityBase : IVirtualPathUtility
{
public virtual string Combine(string basePath, string relativePath)
{
throw new NotImplementedException();
}
public virtual string ToAbsolute(string virtualPath)
{
throw new NotImplementedException();
}
}
}