|
| 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