Skip to content

Commit 339c8b7

Browse files
authored
Unify publish/test mvc.testing functionality + switch to helix for mvc functional tests (#29028)
1 parent 1d2bb10 commit 339c8b7

19 files changed

+171
-226
lines changed

src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
</ItemGroup>
4747
</Target>
4848

49+
<Target Name="_GeneratePublishTestManifest" BeforeTargets="Publish" DependsOnTargets="_ResolveMvcTestProjectReferences">
50+
<ItemGroup>
51+
<_PublishManifestProjects Include="%(_ContentRootProjectReferences.FusionName)">
52+
<ContentRoot>~</ContentRoot>
53+
</_PublishManifestProjects>
54+
</ItemGroup>
55+
56+
<GenerateMvcTestManifestTask ManifestPath="$(PublishDir)MvcTestingAppManifest.json" Projects="@(_PublishManifestProjects)" />
57+
</Target>
58+
4959
<Target Name="_MvcCopyDependencyFiles" AfterTargets="Build;_ResolveMvcTestProjectReferences" Condition="'$(TargetFramework)'!=''">
5060
<ItemGroup>
5161
<DepsFilePaths

src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,13 @@ private string GetContentRootFromFile(string file)
190190
{
191191
var data = JsonSerializer.Deserialize<IDictionary<string, string>>(File.ReadAllBytes(file));
192192
var key = typeof(TEntryPoint).Assembly.GetName().FullName;
193-
try
194-
{
195-
return data[key];
196-
} catch
193+
194+
if (!data.TryGetValue(key, out var contentRoot))
197195
{
198-
throw new KeyNotFoundException($"Could not find content root for project '{key}' in test manifest file '{file}'");
196+
throw new KeyNotFoundException($"Could not find content root for project '{key}' in test manifest file '{file}'");
199197
}
200198

199+
return (contentRoot == "~") ? AppContext.BaseDirectory : contentRoot;
201200
}
202201

203202
private string GetContentRootFromAssembly()

src/Mvc/samples/MvcSandbox/MvcSandbox.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
5+
<PreserveCompilationContext>true</PreserveCompilationContext>
56
</PropertyGroup>
67

78
<ItemGroup>

src/Mvc/test/Mvc.FunctionalTests/BasicTests.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ public async Task CanRender_CSharp7Views()
4949
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
5050
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
5151

52-
#if GENERATE_BASELINES
53-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
54-
#else
55-
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
56-
#endif
52+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
5753
}
5854

5955
[Fact]
@@ -73,11 +69,7 @@ public async Task CanRender_ViewComponentWithArgumentsFromController()
7369
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
7470
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
7571

76-
#if GENERATE_BASELINES
77-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
78-
#else
79-
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
80-
#endif
72+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
8173
}
8274

8375
[Theory]
@@ -101,11 +93,7 @@ public async Task CanRender_ViewsWithLayout(string url)
10193
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
10294
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
10395

104-
#if GENERATE_BASELINES
105-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
106-
#else
107-
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
108-
#endif
96+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
10997
}
11098

11199
[Fact]
@@ -125,11 +113,7 @@ public async Task CanRender_SimpleViews()
125113
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
126114
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
127115

128-
#if GENERATE_BASELINES
129-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
130-
#else
131-
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
132-
#endif
116+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
133117
}
134118

135119
[Fact]
@@ -147,11 +131,7 @@ public async Task ViewWithAttributePrefix_RendersWithoutIgnoringPrefix()
147131
// Assert
148132
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
149133

150-
#if GENERATE_BASELINES
151-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
152-
#else
153-
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
154-
#endif
134+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
155135
}
156136

157137
[Fact]

src/Mvc/test/Mvc.FunctionalTests/ErrorPageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public async Task CompilationFailuresFromViewImportsAreListed()
117117
// Arrange
118118
var expectedMessage = "The type or namespace name &#x27;NamespaceDoesNotExist&#x27; could not be found ("
119119
+ "are you missing a using directive or an assembly reference?)";
120-
var expectedCompilationContent = "public class Views_ErrorFromViewImports_Index : "
120+
var expectedCompilationContent = "Views_ErrorFromViewImports_Index : "
121121
+ "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage&lt;dynamic&gt;";
122122
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
123123

src/Mvc/test/Mvc.FunctionalTests/HtmlGenerationTest.cs

Lines changed: 49 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public static TheoryData<string, string> WebPagesData
6565
{ "Environment", null },
6666
// Testing InputTagHelper with File
6767
{ "Input", null },
68-
// Testing the LinkTagHelper
69-
{ "Link", null },
7068
// Test ability to generate nearly identical HTML with MVC tag and HTML helpers.
7169
// Only attribute order should differ.
7270
{ "Order", "/HtmlGeneration_Order/Submit" },
@@ -78,8 +76,6 @@ public static TheoryData<string, string> WebPagesData
7876
{ "ProductList", "/HtmlGeneration_Product" },
7977
{ "ProductListUsingTagHelpers", "/HtmlGeneration_Product" },
8078
{ "ProductListUsingTagHelpersWithNullModel", "/HtmlGeneration_Product" },
81-
// Testing the ScriptTagHelper
82-
{ "Script", null },
8379
};
8480

8581
return data;
@@ -118,26 +114,34 @@ public async Task HtmlGenerationWebSite_GeneratesExpectedResults(string action,
118114
responseContent = responseContent.Trim();
119115
if (antiforgeryPath == null)
120116
{
121-
#if GENERATE_BASELINES
122117
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
123-
#else
124118
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
125-
#endif
126119
}
127120
else
128121
{
129122
var forgeryToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, antiforgeryPath);
130-
#if GENERATE_BASELINES
131-
// Reverse usual substitution and insert a format item into the new file content.
132-
responseContent = responseContent.Replace(forgeryToken, "{0}");
133-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
134-
#else
135-
expectedContent = string.Format(CultureInfo.InvariantCulture, expectedContent, forgeryToken);
136-
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
137-
#endif
123+
124+
if (ResourceFile.GenerateBaselines)
125+
{
126+
// Reverse usual substitution and insert a format item into the new file content.
127+
responseContent = responseContent.Replace(forgeryToken, "{0}");
128+
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
129+
}
130+
else
131+
{
132+
expectedContent = string.Format(CultureInfo.InvariantCulture, expectedContent, forgeryToken);
133+
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
134+
}
138135
}
139136
}
140137

138+
[ConditionalTheory]
139+
[InlineData("Link", null)]
140+
[InlineData("Script", null)]
141+
[SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/10423")]
142+
public Task HtmlGenerationWebSite_GeneratesExpectedResultsNotReadyForHelix(string action, string antiforgeryPath)
143+
=> HtmlGenerationWebSite_GeneratesExpectedResults(action, antiforgeryPath);
144+
141145
[Fact]
142146
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/25206")]
143147
public async Task HtmlGenerationWebSite_GeneratesExpectedResults_WithImageData()
@@ -167,11 +171,8 @@ public async Task HtmlGenerationWebSite_LinkGeneration_With21CompatibilityBehavi
167171
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
168172

169173
responseContent = responseContent.Trim();
170-
#if GENERATE_BASELINES
171-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
172-
#else
173-
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
174-
#endif
174+
175+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
175176
}
176177

177178
public static TheoryData<string, string> EncodedPagesData
@@ -183,11 +184,9 @@ public static TheoryData<string, string> EncodedPagesData
183184
{ "AttributesWithBooleanValues", null },
184185
{ "EditWarehouse", null },
185186
{ "Index", null },
186-
{ "Link", null },
187187
{ "Order", "/HtmlGeneration_Order/Submit" },
188188
{ "OrderUsingHtmlHelpers", "/HtmlGeneration_Order/Submit" },
189189
{ "Product", null },
190-
{ "Script", null },
191190
};
192191
}
193192
}
@@ -214,29 +213,22 @@ public async Task HtmlGenerationWebSite_GenerateEncodedResults(string action, st
214213
responseContent = responseContent.Trim();
215214
if (antiforgeryPath == null)
216215
{
217-
#if GENERATE_BASELINES
218-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
219-
#else
220-
Assert.Equal(
221-
expectedContent.Trim(),
222-
responseContent,
223-
ignoreLineEndingDifferences: true);
224-
#endif
216+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
225217
}
226218
else
227219
{
228-
var forgeryToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, antiforgeryPath);
229-
#if GENERATE_BASELINES
230-
// Reverse usual substitution and insert a format item into the new file content.
231-
responseContent = responseContent.Replace(forgeryToken, "{0}");
232-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
233-
#else
234-
expectedContent = string.Format(CultureInfo.InvariantCulture, expectedContent, forgeryToken);
235-
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
236-
#endif
220+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent, token: AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, antiforgeryPath));
237221
}
238222
}
239223

224+
225+
[ConditionalTheory]
226+
[InlineData("Link", null)]
227+
[InlineData("Script", null)]
228+
[SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/10423")]
229+
public Task HtmlGenerationWebSite_GenerateEncodedResultsNotReadyForHelix(string action, string antiforgeryPath)
230+
=> HtmlGenerationWebSite_GenerateEncodedResults(action, antiforgeryPath);
231+
240232
// Testing how ModelMetadata is handled as ViewDataDictionary instances are created.
241233
[Theory]
242234
[InlineData("AtViewModel")]
@@ -259,11 +251,7 @@ public async Task CheckViewData_GeneratesExpectedResults(string action)
259251
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
260252

261253
responseContent = responseContent.Trim();
262-
#if GENERATE_BASELINES
263-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
264-
#else
265-
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
266-
#endif
254+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent);
267255
}
268256

269257
[Fact]
@@ -293,17 +281,7 @@ public async Task ValidationTagHelpers_GeneratesExpectedSpansAndDivs()
293281
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
294282

295283
responseContent = responseContent.Trim();
296-
var forgeryToken =
297-
AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, "Customer/HtmlGeneration_Customer");
298-
299-
#if GENERATE_BASELINES
300-
// Reverse usual substitution and insert a format item into the new file content.
301-
responseContent = responseContent.Replace(forgeryToken, "{0}");
302-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
303-
#else
304-
expectedContent = string.Format(CultureInfo.InvariantCulture, expectedContent, forgeryToken);
305-
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
306-
#endif
284+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile, expectedContent, responseContent, token: AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, "Customer/HtmlGeneration_Customer"));
307285
}
308286

309287
[Fact]
@@ -396,12 +374,12 @@ public async Task CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewCompo
396374
var response2 = await (await Client.SendAsync(request)).Content.ReadAsStringAsync();
397375

398376
// Assert - 1
399-
#if GENERATE_BASELINES
400-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile1, expected1, response1.Trim());
401-
#else
402-
Assert.Equal(expected1, response1.Trim(), ignoreLineEndingDifferences: true);
403-
Assert.Equal(expected1, response2.Trim(), ignoreLineEndingDifferences: true);
404-
#endif
377+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile1, expected1, response1.Trim());
378+
379+
if (!ResourceFile.GenerateBaselines)
380+
{
381+
Assert.Equal(expected1, response2.Trim(), ignoreLineEndingDifferences: true);
382+
}
405383

406384
// Act - 2
407385
// Verify content gets changed in partials when one of the vary by parameters is changed
@@ -412,12 +390,11 @@ public async Task CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewCompo
412390
var response4 = await (await Client.SendAsync(request)).Content.ReadAsStringAsync();
413391

414392
// Assert - 2
415-
#if GENERATE_BASELINES
416-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile2, expected2, response3.Trim());
417-
#else
418-
Assert.Equal(expected2, response3.Trim(), ignoreLineEndingDifferences: true);
419-
Assert.Equal(expected2, response4.Trim(), ignoreLineEndingDifferences: true);
420-
#endif
393+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile2, expected2, response3.Trim());
394+
if (!ResourceFile.GenerateBaselines)
395+
{
396+
Assert.Equal(expected2, response4.Trim(), ignoreLineEndingDifferences: true);
397+
}
421398

422399
// Act - 3
423400
// Verify content gets changed in a View Component when the Vary-by-header parameters is changed
@@ -428,12 +405,11 @@ public async Task CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewCompo
428405
var response6 = await (await Client.SendAsync(request)).Content.ReadAsStringAsync();
429406

430407
// Assert - 3
431-
#if GENERATE_BASELINES
432-
ResourceFile.UpdateFile(_resourcesAssembly, outputFile3, expected3, response5.Trim());
433-
#else
434-
Assert.Equal(expected3, response5.Trim(), ignoreLineEndingDifferences: true);
435-
Assert.Equal(expected3, response6.Trim(), ignoreLineEndingDifferences: true);
436-
#endif
408+
ResourceFile.UpdateOrVerify(_resourcesAssembly, outputFile3, expected3, response5.Trim());
409+
if (!ResourceFile.GenerateBaselines)
410+
{
411+
Assert.Equal(expected3, response6.Trim(), ignoreLineEndingDifferences: true);
412+
}
437413
}
438414

439415
[Fact]

0 commit comments

Comments
 (0)