Skip to content

Commit 5b6362f

Browse files
Enable test logging for ProjectTemplate tests (#26507)
* Enable test logging for ProjectTemplate tests * Add logging to templates * some updates * nullref
1 parent 503ab6d commit 5b6362f

23 files changed

+360
-460
lines changed

src/Components/test/E2ETest/Infrastructure/AssemblyInfo.AssemblyFixtures.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.E2ETesting;
5+
using Microsoft.AspNetCore.Testing;
56
using Xunit;
67

7-
[assembly: TestFramework("Microsoft.AspNetCore.E2ETesting.XunitTestFrameworkWithAssemblyFixture", "Microsoft.AspNetCore.Components.E2ETests")]
88
[assembly: AssemblyFixture(typeof(SeleniumStandaloneServer))]
99
[assembly: AssemblyFixture(typeof(SauceConnectServer))]

src/Components/test/E2ETest/Microsoft.AspNetCore.Components.E2ETests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
<OutputPath />
2121

22-
<GenerateLoggingTestingAssemblyAttributes>false</GenerateLoggingTestingAssemblyAttributes>
23-
2422
<!-- This project references the shared framework transitively. Prevent restore errors by setting this flag. -->
2523
<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
2624
</PropertyGroup>

src/ProjectTemplates/BlazorTemplates.Tests/AssemblyInfo.AssemblyFixtures.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.E2ETesting;
5+
using Microsoft.AspNetCore.Testing;
56
using Templates.Test.Helpers;
6-
using Xunit;
77

8-
[assembly: TestFramework("Microsoft.AspNetCore.E2ETesting.XunitTestFrameworkWithAssemblyFixture", "BlazorTemplates.Tests")]
9-
10-
[assembly: Microsoft.AspNetCore.E2ETesting.AssemblyFixture(typeof(ProjectFactoryFixture))]
11-
[assembly: Microsoft.AspNetCore.E2ETesting.AssemblyFixture(typeof(SeleniumStandaloneServer))]
8+
[assembly: AssemblyFixture(typeof(ProjectFactoryFixture))]
9+
[assembly: AssemblyFixture(typeof(SeleniumStandaloneServer))]
1210

src/ProjectTemplates/BlazorTemplates.Tests/BlazorTemplates.Tests.csproj

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<!-- Shared testing infrastructure for running E2E tests using selenium -->
44
<Import Project="$(SharedSourceRoot)E2ETesting\E2ETesting.props" />
@@ -22,7 +22,6 @@
2222
<TestTemplateCreationFolder>TestTemplates\</TestTemplateCreationFolder>
2323
<TestPackageRestorePath>$([MSBuild]::EnsureTrailingSlash('$(RepoRoot)'))obj\template-restore\</TestPackageRestorePath>
2424
<TestTemplateTestsProps>TemplateTests.props</TestTemplateTestsProps>
25-
<GenerateLoggingTestingAssemblyAttributes>false</GenerateLoggingTestingAssemblyAttributes>
2625
<TestDependsOnAspNetRuntime>true</TestDependsOnAspNetRuntime>
2726
</PropertyGroup>
2827

@@ -43,11 +42,6 @@
4342
<Reference Include="Microsoft.NET.Sdk.Razor" ReferenceOutputAssembly="false" SkipGetTargetFrameworkProperties="true" />
4443
</ItemGroup>
4544

46-
<PropertyGroup>
47-
<PreserveExistingLogsInOutput Condition="'$(PreserveExistingLogsInOutput)' == '' AND '$(ContinuousIntegrationBuild)' == 'true'">true</PreserveExistingLogsInOutput>
48-
<PreserveExistingLogsInOutput Condition="'$(PreserveExistingLogsInOutput)' == ''">false</PreserveExistingLogsInOutput>
49-
</PropertyGroup>
50-
5145
<ItemGroup>
5246
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
5347
<_Parameter1>DotNetEfFullPath</_Parameter1>
@@ -65,11 +59,6 @@
6559
<_Parameter1>ContinuousIntegrationBuild</_Parameter1>
6660
<_Parameter2>true</_Parameter2>
6761
</AssemblyAttribute>
68-
<AssemblyAttribute Include="Microsoft.AspNetCore.Testing.TestFrameworkFileLoggerAttribute">
69-
<_Parameter1>$(PreserveExistingLogsInOutput)</_Parameter1>
70-
<_Parameter2>$(TargetFramework)</_Parameter2>
71-
<_Parameter3></_Parameter3>
72-
</AssemblyAttribute>
7362
</ItemGroup>
7463

7564
<Target Name="PrepareForTest" BeforeTargets="GetAssemblyAttributes" Condition="$(DesignTimeBuild) != true">

src/ProjectTemplates/Shared/ProjectFactoryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async Task<Project> GetOrCreateProject(string projectKey, ITestOutputHelp
5555
}
5656

5757
private static string GetTemplateFolderBasePath(Assembly assembly) =>
58-
(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HELIX_DIR")))
58+
(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HELIX_DIR")))
5959
? assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
6060
.Single(a => a.Key == "TestTemplateCreationFolder")
6161
.Value
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.Logging;
5+
using Xunit.Abstractions;
6+
7+
namespace Templates.Test.Helpers
8+
{
9+
internal class TestOutputLogger : ITestOutputHelper
10+
{
11+
private readonly ILogger _logger;
12+
13+
public TestOutputLogger(ILogger logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
public void WriteLine(string message)
19+
{
20+
_logger.LogInformation(message);
21+
}
22+
23+
public void WriteLine(string format, params object[] args)
24+
{
25+
_logger.LogInformation(string.Format(format, args));
26+
}
27+
}
28+
}

src/ProjectTemplates/test/AssemblyInfo.AssemblyFixtures.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using Microsoft.AspNetCore.E2ETesting;
55
using Microsoft.AspNetCore.Testing;
66
using Templates.Test.Helpers;
7-
using Xunit;
87

9-
[assembly: TestFramework("Microsoft.AspNetCore.E2ETesting.XunitTestFrameworkWithAssemblyFixture", "ProjectTemplates.Tests")]
10-
11-
[assembly: Microsoft.AspNetCore.E2ETesting.AssemblyFixture(typeof(ProjectFactoryFixture))]
12-
[assembly: Microsoft.AspNetCore.E2ETesting.AssemblyFixture(typeof(SeleniumStandaloneServer))]
8+
[assembly: AssemblyFixture(typeof(ProjectFactoryFixture))]
9+
[assembly: AssemblyFixture(typeof(SeleniumStandaloneServer))]

src/ProjectTemplates/test/BaselineTest.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Templates.Test
1717
{
18-
public class BaselineTest
18+
public class BaselineTest : LoggedTest
1919
{
2020
private static readonly Regex TemplateNameRegex = new Regex(
2121
"new (?<template>[a-zA-Z]+)",
@@ -32,10 +32,9 @@ public class BaselineTest
3232
RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline,
3333
TimeSpan.FromSeconds(1));
3434

35-
public BaselineTest(ProjectFactoryFixture projectFactory, ITestOutputHelper output)
35+
public BaselineTest(ProjectFactoryFixture projectFactory)
3636
{
3737
ProjectFactory = projectFactory;
38-
Output = output;
3938
}
4039

4140
public Project Project { get; set; }
@@ -67,7 +66,18 @@ public static TheoryData<string, string[]> TemplateBaselines
6766
}
6867

6968
public ProjectFactoryFixture ProjectFactory { get; }
70-
public ITestOutputHelper Output { get; }
69+
private ITestOutputHelper _output;
70+
public ITestOutputHelper Output
71+
{
72+
get
73+
{
74+
if (_output == null)
75+
{
76+
_output = new TestOutputLogger(Logger);
77+
}
78+
return _output;
79+
}
80+
}
7181

7282
[Theory]
7383
[MemberData(nameof(TemplateBaselines))]

src/ProjectTemplates/test/ByteOrderMarkTest.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@
55
using System.IO;
66
using System.Linq;
77
using System.Text;
8+
using Microsoft.AspNetCore.Testing;
9+
using Templates.Test.Helpers;
810
using Xunit;
911
using Xunit.Abstractions;
1012

1113
namespace Templates.Test
1214
{
13-
public class ByteOrderMarkTest
15+
public class ByteOrderMarkTest : LoggedTest
1416
{
15-
private readonly ITestOutputHelper _output;
17+
private ITestOutputHelper _output;
18+
public ITestOutputHelper Output
19+
{
20+
get
21+
{
22+
if (_output == null)
23+
{
24+
_output = new TestOutputLogger(Logger);
25+
}
26+
return _output;
27+
}
28+
}
1629

17-
public ByteOrderMarkTest(ITestOutputHelper output)
30+
public ByteOrderMarkTest()
1831
{
19-
_output = output;
2032
}
2133

2234
[Theory]
@@ -43,13 +55,13 @@ public void JSAndJSONInAllTemplates_ShouldNotContainBOM(string projectName)
4355
// Check for UTF8 BOM 0xEF,0xBB,0xBF
4456
if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
4557
{
46-
_output.WriteLine($"File {filePath} has UTF-8 BOM characters.");
58+
Output.WriteLine($"File {filePath} has UTF-8 BOM characters.");
4759
filesWithBOMCharactersPresent = true;
4860
}
4961
// Check for UTF16 BOM 0xFF, 0xFE
5062
if (bytes[0] == 0xFF && bytes[1] == 0xFE)
5163
{
52-
_output.WriteLine($"File {filePath} has UTF-16 BOM characters.");
64+
Output.WriteLine($"File {filePath} has UTF-16 BOM characters.");
5365
filesWithBOMCharactersPresent = true;
5466
}
5567
}
@@ -83,7 +95,7 @@ public void RazorFilesInWebProjects_ShouldContainBOM(string projectName)
8395
var expectedBytes = Encoding.UTF8.GetPreamble();
8496
if (bytes[0] != expectedBytes[0] || bytes[1] != expectedBytes[1] || bytes[2] != expectedBytes[2])
8597
{
86-
_output.WriteLine($"File {filePath} does not have UTF-8 BOM characters.");
98+
Output.WriteLine($"File {filePath} does not have UTF-8 BOM characters.");
8799
nonBOMFilesPresent = true;
88100
}
89101
}

src/ProjectTemplates/test/EmptyWebTemplateTest.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,27 @@
99

1010
namespace Templates.Test
1111
{
12-
public class EmptyWebTemplateTest
12+
public class EmptyWebTemplateTest : LoggedTest
1313
{
14-
public EmptyWebTemplateTest(ProjectFactoryFixture projectFactory, ITestOutputHelper output)
14+
public EmptyWebTemplateTest(ProjectFactoryFixture projectFactory)
1515
{
1616
ProjectFactory = projectFactory;
17-
Output = output;
1817
}
1918

20-
public Project Project { get; set; }
21-
2219
public ProjectFactoryFixture ProjectFactory { get; }
2320

24-
public ITestOutputHelper Output { get; }
21+
private ITestOutputHelper _output;
22+
public ITestOutputHelper Output
23+
{
24+
get
25+
{
26+
if (_output == null)
27+
{
28+
_output = new TestOutputLogger(Logger);
29+
}
30+
return _output;
31+
}
32+
}
2533

2634
[ConditionalFact]
2735
[SkipOnHelix("Cert failures", Queues = "OSX.1014.Amd64;OSX.1014.Amd64.Open")]
@@ -38,41 +46,41 @@ public async Task EmptyWebTemplateFSharp()
3846

3947
private async Task EmtpyTemplateCore(string languageOverride)
4048
{
41-
Project = await ProjectFactory.GetOrCreateProject("empty" + (languageOverride == "F#" ? "fsharp" : "csharp"), Output);
49+
var project = await ProjectFactory.GetOrCreateProject("empty" + (languageOverride == "F#" ? "fsharp" : "csharp"), Output);
4250

43-
var createResult = await Project.RunDotNetNewAsync("web", language: languageOverride);
44-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", Project, createResult));
51+
var createResult = await project.RunDotNetNewAsync("web", language: languageOverride);
52+
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
4553

4654
// Avoid the F# compiler. See https://github.com/dotnet/aspnetcore/issues/14022
4755
if (languageOverride != null)
4856
{
4957
return;
5058
}
5159

52-
var publishResult = await Project.RunDotNetPublishAsync();
53-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", Project, publishResult));
60+
var publishResult = await project.RunDotNetPublishAsync();
61+
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", project, publishResult));
5462

5563
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
5664
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
5765
// later, while the opposite is not true.
5866

59-
var buildResult = await Project.RunDotNetBuildAsync();
60-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", Project, buildResult));
67+
var buildResult = await project.RunDotNetBuildAsync();
68+
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", project, buildResult));
6169

62-
using (var aspNetProcess = Project.StartBuiltProjectAsync())
70+
using (var aspNetProcess = project.StartBuiltProjectAsync())
6371
{
6472
Assert.False(
6573
aspNetProcess.Process.HasExited,
66-
ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", Project, aspNetProcess.Process));
74+
ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));
6775

6876
await aspNetProcess.AssertOk("/");
6977
}
7078

71-
using (var aspNetProcess = Project.StartPublishedProjectAsync())
79+
using (var aspNetProcess = project.StartPublishedProjectAsync())
7280
{
7381
Assert.False(
7482
aspNetProcess.Process.HasExited,
75-
ErrorMessages.GetFailedProcessMessageOrEmpty("Run published project", Project, aspNetProcess.Process));
83+
ErrorMessages.GetFailedProcessMessageOrEmpty("Run published project", project, aspNetProcess.Process));
7684

7785
await aspNetProcess.AssertOk("/");
7886
}

0 commit comments

Comments
 (0)