Skip to content

Port template test fixes from release/6.0 #41549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ProjectTemplates/Shared/ProjectFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Templates.Test.Helpers;

public class ProjectFactoryFixture : IDisposable
{
private const string LetterChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private readonly ConcurrentDictionary<string, Project> _projects = new ConcurrentDictionary<string, Project>();

public IMessageSink DiagnosticsMessageSink { get; }
Expand Down Expand Up @@ -42,7 +43,16 @@ public async Task<Project> GetOrCreateProject(string projectKey, ITestOutputHelp
DiagnosticsMessageSink = DiagnosticsMessageSink,
ProjectGuid = Path.GetRandomFileName().Replace(".", string.Empty)
};
project.ProjectName = $"AspNet.{project.ProjectGuid}";
// Replace first character with a random letter if it's a digit to avoid random insertions of '_'
// into template namespace declarations (i.e. make it more stable for testing)
var projectNameSuffix = !char.IsLetter(project.ProjectGuid[0])
? string.Create(project.ProjectGuid.Length, project.ProjectGuid, (suffix, guid) =>
{
guid.AsSpan(1).CopyTo(suffix[1..]);
suffix[0] = GetRandomLetter();
})
: project.ProjectGuid;
project.ProjectName = $"AspNet.{projectNameSuffix}";

var assemblyPath = GetType().Assembly;
var basePath = GetTemplateFolderBasePath(assemblyPath);
Expand All @@ -52,6 +62,8 @@ public async Task<Project> GetOrCreateProject(string projectKey, ITestOutputHelp
output);
}

private static char GetRandomLetter() => LetterChars[Random.Shared.Next(LetterChars.Length - 1)];

private static string GetTemplateFolderBasePath(Assembly assembly) =>
(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HELIX_DIR")))
? assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#endif
using BlazorServerWeb_CSharp.Data;

namespace Company.WebApplication1;
namespace BlazorServerWeb_CSharp;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
using ComponentsWebAssembly_CSharp;
#endif

namespace Company.WebApplication1;
#if (Hosted)
namespace ComponentsWebAssembly_CSharp.Client;
#else
namespace ComponentsWebAssembly_CSharp;
#endif

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using ComponentsWebAssembly_CSharp.Server.Models;
#endif

namespace Company.WebApplication1;
namespace ComponentsWebAssembly_CSharp;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GrpcService_CSharp.Services;

namespace Company.WebApplication1;
namespace GrpcService_CSharp;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Company.Application1;

namespace Company.WebApplication1;
namespace Company.Application1;

public class Program
{
Expand Down
15 changes: 15 additions & 0 deletions src/ProjectTemplates/test/BaselineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public ITestOutputHelper Output
}

// This test should generally not be quarantined as it only is checking that the expected files are on disk
// and that the namespace declarations in the generated .cs files start with the project name
[Theory]
[MemberData(nameof(TemplateBaselines))]
public async Task Template_Produces_The_Right_Set_Of_FilesAsync(string arguments, string[] expectedFiles)
Expand Down Expand Up @@ -100,6 +101,20 @@ public async Task Template_Produces_The_Right_Set_Of_FilesAsync(string arguments
continue;
}
Assert.Contains(relativePath, expectedFiles);

if (relativePath.EndsWith(".cs", StringComparison.Ordinal))
{
var namespaceDeclarationPrefix = "namespace ";
var namespaceDeclaration = File.ReadLines(Path.Combine(Project.TemplateOutputDir, relativePath))
.SingleOrDefault(line => line.StartsWith(namespaceDeclarationPrefix, StringComparison.Ordinal))
?.Substring(namespaceDeclarationPrefix.Length);

// nullable because Program.cs with top-level statements doesn't have a namespace declaration
if (namespaceDeclaration is not null)
{
Assert.StartsWith(Project.ProjectName, namespaceDeclaration, StringComparison.Ordinal);
}
}
}
}

Expand Down
100 changes: 100 additions & 0 deletions src/ProjectTemplates/test/template-baselines.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,19 @@
],
"AuthOption": "IndividualB2C"
},
"IndividualB2CProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -au IndividualB2C --use-program-main --aad-b2c-instance https://login.microsoftonline.com/tfp/ --domain fake-b2c-domain.onmicrosoft.com --client-id 64f31f76-2750-49e4-aab9-f5de105b5172 -ssp B2C_1_SiUpIn",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Controllers/WeatherForecastController.cs",
"Properties/launchSettings.json"
],
"AuthOption": "IndividualB2C"
},
"SingleOrg": {
"Template": "webapi",
"Arguments": "new webapi -au SingleOrg --aad-instance https://login.microsoftonline.com/ --domain fake-aad-domain.onmicrosoft.com --client-id db33c356-12cc-4953-9167-00ad56c2e8b2 --tenant-id 7e511586-66ec-4108-bc9c-a68dee0dc2aa",
Expand All @@ -554,6 +567,19 @@
],
"AuthOption": "SingleOrg"
},
"SingleOrgProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -au SingleOrg --use-program-main --aad-instance https://login.microsoftonline.com/ --domain fake-aad-domain.onmicrosoft.com --client-id db33c356-12cc-4953-9167-00ad56c2e8b2 --tenant-id 7e511586-66ec-4108-bc9c-a68dee0dc2aa",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Controllers/WeatherForecastController.cs",
"Properties/launchSettings.json"
],
"AuthOption": "SingleOrg"
},
"None": {
"Template": "webapi",
"Arguments": "new webapi -au None",
Expand All @@ -567,6 +593,19 @@
],
"AuthOption": "None"
},
"NoneProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -au None --use-program-main",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Controllers/WeatherForecastController.cs",
"Properties/launchSettings.json"
],
"AuthOption": "None"
},
"MinimalIndividualB2C": {
"Template": "webapi",
"Arguments": "new webapi -minimal -au IndividualB2C --aad-b2c-instance https://login.microsoftonline.com/tfp/ --domain fake-b2c-domain.onmicrosoft.com --client-id 64f31f76-2750-49e4-aab9-f5de105b5172 -ssp B2C_1_SiUpIn",
Expand All @@ -578,6 +617,18 @@
],
"AuthOption": "IndividualB2C"
},
"MinimalIndividualB2CProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -minimal --use-program-main -au IndividualB2C --aad-b2c-instance https://login.microsoftonline.com/tfp/ --domain fake-b2c-domain.onmicrosoft.com --client-id 64f31f76-2750-49e4-aab9-f5de105b5172 -ssp B2C_1_SiUpIn",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Properties/launchSettings.json"
],
"AuthOption": "IndividualB2C"
},
"MinimalSingleOrg": {
"Template": "webapi",
"Arguments": "new webapi -minimal -au SingleOrg --aad-instance https://login.microsoftonline.com/ --domain fake-aad-domain.onmicrosoft.com --client-id db33c356-12cc-4953-9167-00ad56c2e8b2 --tenant-id 7e511586-66ec-4108-bc9c-a68dee0dc2aa",
Expand All @@ -589,6 +640,18 @@
],
"AuthOption": "SingleOrg"
},
"MinimalSingleOrgProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -minimal --use-program-main -au SingleOrg --aad-instance https://login.microsoftonline.com/ --domain fake-aad-domain.onmicrosoft.com --client-id db33c356-12cc-4953-9167-00ad56c2e8b2 --tenant-id 7e511586-66ec-4108-bc9c-a68dee0dc2aa",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Properties/launchSettings.json"
],
"AuthOption": "SingleOrg"
},
"Minimal": {
"Template": "webapi",
"Arguments": "new webapi -minimal -au None",
Expand All @@ -600,6 +663,18 @@
],
"AuthOption": "None"
},
"MinimalProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -minimal --use-program-main -au None",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Properties/launchSettings.json"
],
"AuthOption": "None"
},
"Windows": {
"Template": "webapi",
"Arguments": "new webapi -au Windows",
Expand All @@ -613,6 +688,19 @@
],
"AuthOption": "Windows"
},
"WindowsProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -au Windows --use-program-main",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Controllers/WeatherForecastController.cs",
"Properties/launchSettings.json"
],
"AuthOption": "Windows"
},
"MinimalWindows": {
"Template": "webapi",
"Arguments": "new webapi -minimal -au Windows",
Expand All @@ -624,6 +712,18 @@
],
"AuthOption": "Windows"
},
"MinimalWindowsProgramMain": {
"Template": "webapi",
"Arguments": "new webapi -minimal --use-program-main -au Windows",
"Files": [
"appsettings.Development.json",
"appsettings.json",
"Program.cs",
"WeatherForecast.cs",
"Properties/launchSettings.json"
],
"AuthOption": "Windows"
},
"FSharp": {
"Template": "webapi",
"Arguments": "new webapi --language F#",
Expand Down