Fixed issue with SPA templates generating invalid NPM package names (#9725)

Added unit test to check NPM package names matches regex
This commit is contained in:
Neil Bostrom 2019-04-29 22:42:25 +01:00 committed by Ryan Brandenburg
parent 53acfe63df
commit d9ad99933c
4 changed files with 16 additions and 4 deletions

View File

@ -1,5 +1,5 @@
{
"name": "Company.WebApplication1",
"name": "company.webapplication1",
"version": "0.0.0",
"scripts": {
"ng": "ng",

View File

@ -1,5 +1,5 @@
{
"name": "Company.WebApplication1",
"name": "company.webapplication1",
"version": "0.1.0",
"private": true,
"dependencies": {

View File

@ -1,5 +1,5 @@
{
"name": "Company.WebApplication1",
"name": "company.webapplication1",
"version": "0.1.0",
"private": true,
"dependencies": {

View File

@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.E2ETesting;
using Newtonsoft.Json.Linq;
@ -51,7 +52,7 @@ namespace Templates.Test.SpaTemplateTest
// multiple NPM installs run concurrently which otherwise causes errors when
// tests run in parallel.
var clientAppSubdirPath = Path.Combine(Project.TemplateOutputDir, "ClientApp");
Assert.True(File.Exists(Path.Combine(clientAppSubdirPath, "package.json")), "Missing a package.json");
ValidatePackageJson(clientAppSubdirPath);
var projectFileContents = ReadFile(Project.TemplateOutputDir, $"{Project.ProjectName}.csproj");
if (usesAuth && !useLocalDb)
@ -137,6 +138,17 @@ namespace Templates.Test.SpaTemplateTest
}
}
private void ValidatePackageJson(string clientAppSubdirPath)
{
Assert.True(File.Exists(Path.Combine(clientAppSubdirPath, "package.json")), "Missing a package.json");
var packageJson = JObject.Parse(ReadFile(clientAppSubdirPath, "package.json"));
// NPM package names must match ^(?:@[a-z0-9-~][a-z0-9-._~]*/)?[a-z0-9-~][a-z0-9-._~]*$
var packageName = (string)packageJson["name"];
Regex regex = new Regex("^(?:@[a-z0-9-~][a-z0-9-._~]*/)?[a-z0-9-~][a-z0-9-._~]*$");
Assert.True(regex.IsMatch(packageName), "package.json name is invalid format");
}
private static async Task WarmUpServer(AspNetProcess aspNetProcess)
{
var attempt = 0;