Skip to content

Commit 149ef33

Browse files
[release/7.0] Get template error codes from the right processes (#44246)
* Get template error codes from the right processes * Make Project actions self-checking Co-authored-by: Chris R <Tratcher@outlook.com>
1 parent 4ec533e commit 149ef33

File tree

16 files changed

+82
-145
lines changed

16 files changed

+82
-145
lines changed

src/ProjectTemplates/Shared/BlazorTemplateTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ protected async Task<Project> CreateBuildPublishAsync(string auth = null, string
5252
project.TargetFramework = targetFramework;
5353
}
5454

55-
var createResult = await project.RunDotNetNewAsync(ProjectType, auth: auth, args: args, errorOnRestoreError: false);
56-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
55+
await project.RunDotNetNewAsync(ProjectType, auth: auth, args: args, errorOnRestoreError: false);
5756

5857
if (serverProject || auth is null)
5958
{
@@ -78,8 +77,7 @@ protected async Task<Project> CreateBuildPublishAsync(string auth = null, string
7877
targetProject = GetSubProject(project, "Server", $"{project.ProjectName}.Server");
7978
}
8079

81-
var publishResult = await targetProject.RunDotNetPublishAsync(noRestore: false);
82-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", targetProject, publishResult));
80+
await targetProject.RunDotNetPublishAsync(noRestore: false);
8381
}
8482

8583
return project;

src/ProjectTemplates/Shared/Project.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static string ArtifactsLogDir
6161
public ITestOutputHelper Output { get; set; }
6262
public IMessageSink DiagnosticsMessageSink { get; set; }
6363

64-
internal async Task<ProcessResult> RunDotNetNewAsync(
64+
internal async Task RunDotNetNewAsync(
6565
string templateName,
6666
string auth = null,
6767
string language = null,
@@ -126,10 +126,10 @@ internal async Task<ProcessResult> RunDotNetNewAsync(
126126
result.ExitCode = -1;
127127
}
128128

129-
return result;
129+
Assert.True(0 == result.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", this, result));
130130
}
131131

132-
internal async Task<ProcessResult> RunDotNetPublishAsync(IDictionary<string, string> packageOptions = null, string additionalArgs = null, bool noRestore = true)
132+
internal async Task RunDotNetPublishAsync(IDictionary<string, string> packageOptions = null, string additionalArgs = null, bool noRestore = true)
133133
{
134134
Output.WriteLine("Publishing ASP.NET Core application...");
135135

@@ -150,10 +150,11 @@ internal async Task<ProcessResult> RunDotNetPublishAsync(IDictionary<string, str
150150
}
151151

152152
CaptureBinLogOnFailure(execution);
153-
return result;
153+
154+
Assert.True(0 == result.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", this, result));
154155
}
155156

156-
internal async Task<ProcessResult> RunDotNetBuildAsync(IDictionary<string, string> packageOptions = null, string additionalArgs = null, bool errorOnBuildWarning = true)
157+
internal async Task RunDotNetBuildAsync(IDictionary<string, string> packageOptions = null, string additionalArgs = null, bool errorOnBuildWarning = true)
157158
{
158159
Output.WriteLine("Building ASP.NET Core application...");
159160

@@ -172,7 +173,8 @@ internal async Task<ProcessResult> RunDotNetBuildAsync(IDictionary<string, strin
172173
}
173174

174175
CaptureBinLogOnFailure(execution);
175-
return result;
176+
177+
Assert.True(0 == result.ExitCode, ErrorMessages.GetFailedProcessMessage("build", this, result));
176178
}
177179

178180
internal AspNetProcess StartBuiltProjectAsync(bool hasListeningUri = true, ILogger logger = null)
@@ -206,7 +208,7 @@ internal AspNetProcess StartPublishedProjectAsync(bool hasListeningUri = true, b
206208
return new AspNetProcess(DevCert, Output, TemplatePublishDir, projectDll, environment, published: true, hasListeningUri: hasListeningUri, usePublishedAppHost: usePublishedAppHost);
207209
}
208210

209-
internal async Task<ProcessResult> RunDotNetEfCreateMigrationAsync(string migrationName)
211+
internal async Task RunDotNetEfCreateMigrationAsync(string migrationName)
210212
{
211213
var args = $"--verbose --no-build migrations add {migrationName}";
212214

@@ -222,10 +224,11 @@ internal async Task<ProcessResult> RunDotNetEfCreateMigrationAsync(string migrat
222224

223225
using var result = ProcessEx.Run(Output, TemplateOutputDir, command, args);
224226
await result.Exited;
225-
return new ProcessResult(result);
227+
var processResult = new ProcessResult(result);
228+
Assert.True(0 == processResult.ExitCode, ErrorMessages.GetFailedProcessMessage("run EF migrations", this, processResult));
226229
}
227230

228-
internal async Task<ProcessResult> RunDotNetEfUpdateDatabaseAsync()
231+
internal async Task RunDotNetEfUpdateDatabaseAsync()
229232
{
230233
var args = "--verbose --no-build database update";
231234

@@ -241,7 +244,8 @@ internal async Task<ProcessResult> RunDotNetEfUpdateDatabaseAsync()
241244

242245
using var result = ProcessEx.Run(Output, TemplateOutputDir, command, args);
243246
await result.Exited;
244-
return new ProcessResult(result);
247+
var processResult = new ProcessResult(result);
248+
Assert.True(0 == processResult.ExitCode, ErrorMessages.GetFailedProcessMessage("update database", this, processResult));
245249
}
246250

247251
// If this fails, you should generate new migrations via migrations/updateMigrations.cmd
@@ -289,7 +293,7 @@ public void AssertFileExists(string path, bool shouldExist)
289293
}
290294
}
291295

292-
public async Task<Project> VerifyLaunchSettings(string[] expectedLaunchProfileNames)
296+
public async Task VerifyLaunchSettings(string[] expectedLaunchProfileNames)
293297
{
294298
var launchSettingsFiles = Directory.EnumerateFiles(TemplateOutputDir, "launchSettings.json", SearchOption.AllDirectories);
295299

@@ -342,8 +346,6 @@ public async Task<Project> VerifyLaunchSettings(string[] expectedLaunchProfileNa
342346
}
343347
}
344348
}
345-
346-
return this;
347349
}
348350

349351
public string ReadFile(string path)
@@ -352,7 +354,7 @@ public string ReadFile(string path)
352354
return File.ReadAllText(Path.Combine(TemplateOutputDir, path));
353355
}
354356

355-
internal async Task<ProcessEx> RunDotNetNewRawAsync(string arguments)
357+
internal async Task RunDotNetNewRawAsync(string arguments)
356358
{
357359
var result = ProcessEx.Run(
358360
Output,
@@ -362,7 +364,7 @@ internal async Task<ProcessEx> RunDotNetNewRawAsync(string arguments)
362364
$" --debug:disable-sdk-templates --debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"" +
363365
$" -o {TemplateOutputDir}");
364366
await result.Exited;
365-
return result;
367+
Assert.True(result.ExitCode == 0, result.GetFormattedOutput());
366368
}
367369

368370
public void Dispose()

src/ProjectTemplates/test/Templates.Blazor.Tests/BlazorTemplateTest.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ protected async Task<Project> CreateBuildPublishAsync(string auth = null, string
3535
project.TargetFramework = targetFramework;
3636
}
3737

38-
var createResult = await project.RunDotNetNewAsync(ProjectType, auth: auth, args: args);
39-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
38+
await project.RunDotNetNewAsync(ProjectType, auth: auth, args: args);
4039

4140
if (!onlyCreate)
4241
{
@@ -46,15 +45,13 @@ protected async Task<Project> CreateBuildPublishAsync(string auth = null, string
4645
targetProject = GetSubProject(project, "Server", $"{project.ProjectName}.Server");
4746
}
4847

49-
var publishResult = await targetProject.RunDotNetPublishAsync(noRestore: false);
50-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", targetProject, publishResult));
48+
await targetProject.RunDotNetPublishAsync(noRestore: false);
5149

5250
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
5351
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
5452
// later, while the opposite is not true.
5553

56-
var buildResult = await targetProject.RunDotNetBuildAsync();
57-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", targetProject, buildResult));
54+
await targetProject.RunDotNetBuildAsync();
5855
}
5956

6057
return project;

src/ProjectTemplates/test/Templates.Blazor.Tests/BlazorWasmTemplateTest.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,20 @@ private async Task<Project> CreateBuildPublishIndividualAuthProject(bool useLoca
248248
var appSettingsPath = Path.Combine(serverProject.TemplateOutputDir, "appsettings.json");
249249
File.WriteAllText(appSettingsPath, replacedSection);
250250

251-
var publishResult = await serverProject.RunDotNetPublishAsync();
252-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", serverProject, publishResult));
251+
await serverProject.RunDotNetPublishAsync();
253252

254253
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
255254
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
256255
// later, while the opposite is not true.
257256

258-
var buildResult = await serverProject.RunDotNetBuildAsync();
259-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", serverProject, buildResult));
257+
await serverProject.RunDotNetBuildAsync();
260258

261-
var migrationsResult = await serverProject.RunDotNetEfCreateMigrationAsync("blazorwasm");
262-
Assert.True(0 == migrationsResult.ExitCode, ErrorMessages.GetFailedProcessMessage("run EF migrations", serverProject, migrationsResult));
259+
await serverProject.RunDotNetEfCreateMigrationAsync("blazorwasm");
263260
serverProject.AssertEmptyMigration("blazorwasm");
264261

265262
if (useLocalDb)
266263
{
267-
var dbUpdateResult = await serverProject.RunDotNetEfUpdateDatabaseAsync();
268-
Assert.True(0 == dbUpdateResult.ExitCode, ErrorMessages.GetFailedProcessMessage("update database", serverProject, dbUpdateResult));
264+
await serverProject.RunDotNetEfUpdateDatabaseAsync();
269265
}
270266

271267
return project;

src/ProjectTemplates/test/Templates.Blazor.WebAssembly.Auth.Tests/BlazorWasmTemplateAuthTest.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,15 @@ private async Task<Project> CreateBuildPublishIndividualAuthProject(bool useLoca
9595
var appSettingsPath = Path.Combine(serverProject.TemplateOutputDir, "appsettings.json");
9696
File.WriteAllText(appSettingsPath, replacedSection);
9797

98-
var publishResult = await serverProject.RunDotNetPublishAsync();
99-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", serverProject, publishResult));
98+
await serverProject.RunDotNetPublishAsync();
10099

101100
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
102101
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
103102
// later, while the opposite is not true.
104103

105-
var buildResult = await serverProject.RunDotNetBuildAsync();
106-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", serverProject, buildResult));
104+
await serverProject.RunDotNetBuildAsync();
107105

108-
var migrationsResult = await serverProject.RunDotNetEfCreateMigrationAsync("blazorwasm");
109-
Assert.True(0 == migrationsResult.ExitCode, ErrorMessages.GetFailedProcessMessage("run EF migrations", serverProject, migrationsResult));
106+
await serverProject.RunDotNetEfCreateMigrationAsync("blazorwasm");
110107
serverProject.AssertEmptyMigration("blazorwasm");
111108

112109
return project;

src/ProjectTemplates/test/Templates.Mvc.Tests/MvcTemplateTest.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ private async Task MvcTemplateCore(string languageOverride, string[] args = null
5555
{
5656
var project = await ProjectFactory.CreateProject(Output);
5757

58-
var createResult = await project.RunDotNetNewAsync("mvc", language: languageOverride, args: args);
59-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
58+
await project.RunDotNetNewAsync("mvc", language: languageOverride, args: args);
6059

6160
var noHttps = args?.Contains(ArgConstants.NoHttps) ?? false;
6261
var expectedLaunchProfileNames = noHttps
@@ -78,15 +77,13 @@ private async Task MvcTemplateCore(string languageOverride, string[] args = null
7877
return;
7978
}
8079

81-
var publishResult = await project.RunDotNetPublishAsync();
82-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", project, publishResult));
80+
await project.RunDotNetPublishAsync();
8381

8482
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
8583
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
8684
// later, while the opposite is not true.
8785

88-
var buildResult = await project.RunDotNetBuildAsync();
89-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", project, buildResult));
86+
await project.RunDotNetBuildAsync();
9087

9188
IEnumerable<string> menuLinks = new List<string> {
9289
PageUrls.HomeUrl,
@@ -157,8 +154,7 @@ private async Task MvcTemplate_IndividualAuth_Core(bool useLocalDB, bool useProg
157154
: noHttps
158155
? new[] { ArgConstants.NoHttps }
159156
: null;
160-
var createResult = await project.RunDotNetNewAsync("mvc", auth: "Individual", useLocalDB: useLocalDB, args: args);
161-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
157+
await project.RunDotNetNewAsync("mvc", auth: "Individual", useLocalDB: useLocalDB, args: args);
162158

163159
var expectedLaunchProfileNames = noHttps
164160
? new[] { "http", "IIS Express" }
@@ -171,18 +167,15 @@ private async Task MvcTemplate_IndividualAuth_Core(bool useLocalDB, bool useProg
171167
Assert.Contains(".db", projectFileContents);
172168
}
173169

174-
var publishResult = await project.RunDotNetPublishAsync();
175-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", project, publishResult));
170+
await project.RunDotNetPublishAsync();
176171

177172
// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
178173
// The output from publish will go into bin/Release/netcoreappX.Y/publish and won't be affected by calling build
179174
// later, while the opposite is not true.
180175

181-
var buildResult = await project.RunDotNetBuildAsync();
182-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", project, buildResult));
176+
await project.RunDotNetBuildAsync();
183177

184-
var migrationsResult = await project.RunDotNetEfCreateMigrationAsync("mvc");
185-
Assert.True(0 == migrationsResult.ExitCode, ErrorMessages.GetFailedProcessMessage("run EF migrations", project, migrationsResult));
178+
await project.RunDotNetEfCreateMigrationAsync("mvc");
186179
project.AssertEmptyMigration("mvc");
187180

188181
// Note: if any links are updated here, RazorPagesTemplateTest.cs should be updated as well
@@ -284,11 +277,9 @@ public async Task MvcTemplate_SingleFileExe()
284277
var project = await ProjectFactory.CreateProject(Output);
285278
project.RuntimeIdentifier = runtimeIdentifer;
286279

287-
var createResult = await project.RunDotNetNewAsync("mvc");
288-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
280+
await project.RunDotNetNewAsync("mvc");
289281

290-
var publishResult = await project.RunDotNetPublishAsync(additionalArgs: $"/p:PublishSingleFile=true -r {runtimeIdentifer} --self-contained", noRestore: false);
291-
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", project, publishResult));
282+
await project.RunDotNetPublishAsync(additionalArgs: $"/p:PublishSingleFile=true -r {runtimeIdentifer} --self-contained", noRestore: false);
292283

293284
var menuLinks = new[]
294285
{
@@ -361,20 +352,17 @@ private async Task<Project> MvcTemplateBuildsAndPublishes(string auth, string[]
361352
{
362353
var project = await ProjectFactory.CreateProject(Output);
363354

364-
var createResult = await project.RunDotNetNewAsync("mvc", auth: auth, args: args);
365-
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
355+
await project.RunDotNetNewAsync("mvc", auth: auth, args: args);
366356

367357
// Identity Web auth requires https and thus ignores the --no-https option if passed so there should never be an 'http' profile
368358
var expectedLaunchProfileNames = new[] { "https", "IIS Express" };
369359
await project.VerifyLaunchSettings(expectedLaunchProfileNames);
370360

371361
// Verify building in debug works
372-
var buildResult = await project.RunDotNetBuildAsync();
373-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", project, buildResult));
362+
await project.RunDotNetBuildAsync();
374363

375364
// Publish builds in "release" configuration. Running publish should ensure we can compile in release and that we can publish without issues.
376-
buildResult = await project.RunDotNetPublishAsync();
377-
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", project, buildResult));
365+
await project.RunDotNetPublishAsync();
378366

379367
return project;
380368
}

0 commit comments

Comments
 (0)