[release/6.0.1xx] dotnet watch smoke test (#13392)
* add dotnet watch smoke test
This commit is contained in:
parent
b0516d4281
commit
bc714f97e9
3 changed files with 80 additions and 13 deletions
|
@ -79,14 +79,25 @@ internal class DotNetHelper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExecuteCmd(string args, string? workingDirectory = null)
|
public void ExecuteCmd(string args, string? workingDirectory = null, Action<Process>? additionalProcessConfigCallback = null, int expectedExitCode = 0, int millisecondTimeout = -1)
|
||||||
{
|
{
|
||||||
|
Action<Process, string?> configureProcess = (Process process, string? workingDirectory) => {
|
||||||
|
ConfigureProcess(process, workingDirectory);
|
||||||
|
|
||||||
|
if (additionalProcessConfigCallback != null)
|
||||||
|
{
|
||||||
|
additionalProcessConfigCallback(process);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
(Process Process, string StdOut, string StdErr) executeResult = ExecuteHelper.ExecuteProcess(
|
(Process Process, string StdOut, string StdErr) executeResult = ExecuteHelper.ExecuteProcess(
|
||||||
DotNetPath,
|
DotNetPath,
|
||||||
args,
|
args,
|
||||||
OutputHelper,
|
OutputHelper,
|
||||||
configure: (process) => ConfigureProcess(process, workingDirectory));
|
configure: (process) => configureProcess(process, workingDirectory),
|
||||||
ExecuteHelper.ValidateExitCode(executeResult);
|
millisecondTimeout: millisecondTimeout);
|
||||||
|
|
||||||
|
ExecuteHelper.ValidateExitCode(executeResult, expectedExitCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ConfigureProcess(Process process, string? workingDirectory, bool setPath = false)
|
public static void ConfigureProcess(Process process, string? workingDirectory, bool setPath = false)
|
||||||
|
@ -170,18 +181,14 @@ internal class DotNetHelper
|
||||||
|
|
||||||
public void ExecuteRunWeb(string projectName)
|
public void ExecuteRunWeb(string projectName)
|
||||||
{
|
{
|
||||||
(Process Process, string StdOut, string StdErr) executeResult = ExecuteHelper.ExecuteProcess(
|
ExecuteCmd(
|
||||||
DotNetPath,
|
|
||||||
$"run {GetBinLogOption(projectName, "run")}",
|
$"run {GetBinLogOption(projectName, "run")}",
|
||||||
OutputHelper,
|
GetProjectDirectory(projectName),
|
||||||
configure: configureProcess,
|
additionalProcessConfigCallback: processConfigCallback,
|
||||||
millisecondTimeout: 30000);
|
millisecondTimeout: 30000);
|
||||||
ExecuteHelper.ValidateExitCode(executeResult);
|
|
||||||
|
|
||||||
void configureProcess(Process process)
|
void processConfigCallback(Process process)
|
||||||
{
|
{
|
||||||
ConfigureProcess(process, GetProjectDirectory(projectName));
|
|
||||||
|
|
||||||
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
|
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
|
||||||
{
|
{
|
||||||
if (e.Data?.Contains("Application started. Press Ctrl+C to shut down.") ?? false)
|
if (e.Data?.Contains("Application started. Press Ctrl+C to shut down.") ?? false)
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
|
||||||
|
|
||||||
|
public class DotNetWatchTests : SmokeTests
|
||||||
|
{
|
||||||
|
public DotNetWatchTests(ITestOutputHelper outputHelper) : base(outputHelper) { }
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WatchTests()
|
||||||
|
{
|
||||||
|
string projectDirectory = DotNetHelper.ExecuteNew(DotNetTemplate.Console.GetName(), nameof(DotNetWatchTests));
|
||||||
|
bool outputChanged = false;
|
||||||
|
|
||||||
|
// We expect an exit code of 143 (128 + 15, i.e. SIGTERM) because we are killing the process manually
|
||||||
|
DotNetHelper.ExecuteCmd(
|
||||||
|
"watch run",
|
||||||
|
workingDirectory: projectDirectory,
|
||||||
|
additionalProcessConfigCallback: processConfigCallback,
|
||||||
|
expectedExitCode: 143,
|
||||||
|
millisecondTimeout: 30000);
|
||||||
|
|
||||||
|
Assert.True(outputChanged);
|
||||||
|
|
||||||
|
void processConfigCallback(Process process)
|
||||||
|
{
|
||||||
|
const string waitingString = "Waiting for a file to change before restarting dotnet...";
|
||||||
|
const string expectedString = "Hello from dotnet watch!";
|
||||||
|
|
||||||
|
bool fileChanged = false;
|
||||||
|
|
||||||
|
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
|
||||||
|
{
|
||||||
|
if (e.Data?.Contains(waitingString) ?? false)
|
||||||
|
{
|
||||||
|
if (!fileChanged) {
|
||||||
|
OutputHelper.WriteLine("Program started, changing file on disk to trigger restart...");
|
||||||
|
File.WriteAllText(
|
||||||
|
Path.Combine(projectDirectory, "Program.cs"),
|
||||||
|
File.ReadAllText(Path.Combine(projectDirectory, "Program.cs")).Replace("Hello, World!", expectedString));
|
||||||
|
fileChanged = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (e.Data?.Contains(expectedString) ?? false)
|
||||||
|
{
|
||||||
|
outputChanged = true;
|
||||||
|
OutputHelper.WriteLine("Successfully re-ran program after code change.");
|
||||||
|
ExecuteHelper.ExecuteProcessValidateExitCode("kill", $"-s TERM {process.Id}", OutputHelper);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -84,9 +84,9 @@ internal static class ExecuteHelper
|
||||||
return result.StdOut;
|
return result.StdOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ValidateExitCode((Process Process, string StdOut, string StdErr) result)
|
public static void ValidateExitCode((Process Process, string StdOut, string StdErr) result, int expectedExitCode = 0)
|
||||||
{
|
{
|
||||||
if (result.Process.ExitCode != 0)
|
if (result.Process.ExitCode != expectedExitCode)
|
||||||
{
|
{
|
||||||
ProcessStartInfo startInfo = result.Process.StartInfo;
|
ProcessStartInfo startInfo = result.Process.StartInfo;
|
||||||
string msg = $"Failed to execute {startInfo.FileName} {startInfo.Arguments}" +
|
string msg = $"Failed to execute {startInfo.FileName} {startInfo.Arguments}" +
|
||||||
|
|
Loading…
Reference in a new issue