Skip to content

Commit b928931

Browse files
committed
Run blazor create/build/publish as part of template tests
1 parent 223d6e5 commit b928931

File tree

3 files changed

+402
-0
lines changed

3 files changed

+402
-0
lines changed
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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
70+
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
71+
// later, while the opposite is not true.
72+
73+
var buildResult = await targetProject.RunDotNetBuildAsync();
74+
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", targetProject, buildResult));
75+
}
76+
77+
return project;
78+
}
79+
80+
protected static Project GetSubProject(Project project, string projectDirectory, string projectName)
81+
{
82+
var subProjectDirectory = Path.Combine(project.TemplateOutputDir, projectDirectory);
83+
if (!Directory.Exists(subProjectDirectory))
84+
{
85+
throw new DirectoryNotFoundException($"Directory {subProjectDirectory} was not found.");
86+
}
87+
88+
var subProject = new Project
89+
{
90+
Output = project.Output,
91+
DiagnosticsMessageSink = project.DiagnosticsMessageSink,
92+
ProjectName = projectName,
93+
TemplateOutputDir = subProjectDirectory,
94+
};
95+
96+
return subProject;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)