Skip to content

Commit dcae2a6

Browse files
authored
Run blazor create/build/publish as part of template tests (#34275)
1 parent ecf27de commit dcae2a6

File tree

5 files changed

+395
-8
lines changed

5 files changed

+395
-8
lines changed

src/ProjectTemplates/BlazorTemplates.Tests/BlazorTemplateTest.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ protected async Task<Project> CreateBuildPublishAsync(string projectName, string
112112

113113
var publishResult = await targetProject.RunDotNetPublishAsync(noRestore: false);
114114
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", targetProject, publishResult));
115-
116-
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
117-
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
118-
// later, while the opposite is not true.
119-
120-
var buildResult = await targetProject.RunDotNetBuildAsync();
121-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", targetProject, buildResult));
122115
}
123116

124117
return project;

src/ProjectTemplates/Shared/Project.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private void CaptureBinLogOnFailure(ProcessEx result)
443443
var sourceFile = Path.Combine(TemplateOutputDir, "msbuild.binlog");
444444
Assert.True(File.Exists(sourceFile), $"Log for '{ProjectName}' not found in '{sourceFile}'.");
445445
var destination = Path.Combine(ArtifactsLogDir, ProjectName + ".binlog");
446-
File.Move(sourceFile, destination);
446+
File.Move(sourceFile, destination, overwrite: true); // binlog will exist on retries
447447
}
448448
}
449449

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Testing;
10+
using Templates.Test.Helpers;
11+
using Xunit;
12+
using Xunit.Abstractions;
13+
using Xunit.Sdk;
14+
15+
namespace Templates.Test
16+
{
17+
public class BlazorServerTemplateTest : BlazorTemplateTest
18+
{
19+
public BlazorServerTemplateTest(ProjectFactoryFixture projectFactory)
20+
: base(projectFactory)
21+
{
22+
}
23+
24+
public override string ProjectType { get; } = "blazorserver";
25+
26+
[Fact]
27+
public Task BlazorServerTemplateWorks_NoAuth() => CreateBuildPublishAsync("blazorservernoauth");
28+
29+
[Theory]
30+
[InlineData(true)]
31+
[InlineData(false)]
32+
[SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/30825", Queues = "All.OSX")]
33+
public Task BlazorServerTemplateWorks_IndividualAuth(bool useLocalDB) => CreateBuildPublishAsync("blazorserverindividual" + (useLocalDB ? "uld" : ""));
34+
35+
[Theory]
36+
[InlineData("IndividualB2C", null)]
37+
[InlineData("IndividualB2C", new string[] { "--called-api-url \"https://graph.microsoft.com\"", "--called-api-scopes user.readwrite" })]
38+
[InlineData("SingleOrg", null)]
39+
[InlineData("SingleOrg", new string[] { "--called-api-url \"https://graph.microsoft.com\"", "--called-api-scopes user.readwrite" })]
40+
[InlineData("SingleOrg", new string[] { "--calls-graph" })]
41+
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/30882")]
42+
public Task BlazorServerTemplate_IdentityWeb_BuildAndPublish(string auth, string[] args)
43+
=> CreateBuildPublishAsync("blazorserveridweb" + Guid.NewGuid().ToString().Substring(0, 10).ToLowerInvariant(), auth, args);
44+
45+
}
46+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 System;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Reflection;
8+
using System.Runtime.InteropServices;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Testing;
11+
using Microsoft.Extensions.Configuration;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.Extensions.Logging.Testing;
14+
using Templates.Test.Helpers;
15+
using Xunit;
16+
using Xunit.Abstractions;
17+
18+
namespace Templates.Test
19+
{
20+
public abstract class BlazorTemplateTest : LoggedTest
21+
{
22+
public BlazorTemplateTest(ProjectFactoryFixture projectFactory)
23+
{
24+
ProjectFactory = projectFactory;
25+
}
26+
27+
public ProjectFactoryFixture ProjectFactory { get; set; }
28+
29+
private ITestOutputHelper _output;
30+
public ITestOutputHelper Output
31+
{
32+
get
33+
{
34+
if (_output == null)
35+
{
36+
_output = new TestOutputLogger(Logger);
37+
}
38+
return _output;
39+
}
40+
}
41+
42+
public abstract string ProjectType { get; }
43+
44+
protected async Task<Project> CreateBuildPublishAsync(string projectName, string auth = null, string[] args = null, string targetFramework = null, bool serverProject = false, bool onlyCreate = false)
45+
{
46+
// Additional arguments are needed. See: https://github.com/dotnet/aspnetcore/issues/24278
47+
Environment.SetEnvironmentVariable("EnableDefaultScopedCssItems", "true");
48+
49+
var project = await ProjectFactory.GetOrCreateProject(projectName, Output);
50+
if (targetFramework != null)
51+
{
52+
project.TargetFramework = targetFramework;
53+
}
54+
55+
var createResult = await project.RunDotNetNewAsync(ProjectType, auth: auth, args: args);
56+
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
57+
58+
if (!onlyCreate)
59+
{
60+
var targetProject = project;
61+
if (serverProject)
62+
{
63+
targetProject = GetSubProject(project, "Server", $"{project.ProjectName}.Server");
64+
}
65+
66+
var publishResult = await targetProject.RunDotNetPublishAsync(noRestore: false);
67+
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", targetProject, publishResult));
68+
}
69+
70+
return project;
71+
}
72+
73+
protected static Project GetSubProject(Project project, string projectDirectory, string projectName)
74+
{
75+
var subProjectDirectory = Path.Combine(project.TemplateOutputDir, projectDirectory);
76+
if (!Directory.Exists(subProjectDirectory))
77+
{
78+
throw new DirectoryNotFoundException($"Directory {subProjectDirectory} was not found.");
79+
}
80+
81+
var subProject = new Project
82+
{
83+
Output = project.Output,
84+
DiagnosticsMessageSink = project.DiagnosticsMessageSink,
85+
ProjectName = projectName,
86+
TemplateOutputDir = subProjectDirectory,
87+
};
88+
89+
return subProject;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)