public async Task WaitForOutputAsync_Test() { using var process = XProcess.Start("dotnet", "testee.dll -i", baseDir); var found = await process.WaitForOutputAsync(str => str == "20", millsecondsTimeout : 3000); found.IsTrue(); }
public async Task Publish_with_SingleFileEnabled_Test() { var rid = "win-x64"; var driverFileName = "chromedriver.exe"; var executableFileFormat = Format.PE32; var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin"); var publishCommand = new[] { "dotnet", "publish", "-r", rid, "-o", "out", "-c:Release", "-p:PublishChromeDriver=true", "-p:PublishSingleFile=true", "-p:SelfContained=false" }; // IMPORTANT: 2nd time of publishing, sometimes lost driver file in the published folder, so we have to validate it.. for (var i = 0; i < 2; i++) { using var dotnet = await XProcess.Start( filename : publishCommand.First(), arguments : String.Join(' ', publishCommand.Skip(1)), workDir).WaitForExitAsync(); dotnet.ExitCode.Is(0); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsTrue(); DetectFormat(driverFullPath).Is(executableFileFormat); } }
public async Task Output_of_ProjectB_Contains_DriverFile_Test() { var vsAppDir = Environment.GetEnvironmentVariable("VSAPPIDDIR"); if (vsAppDir == null) { Assert.Inconclusive(@"This test requires Visual Studio and the definition of the ""VSAPPDIR"" environment variable to point out the directory where Visual Studio ""devenv.exe"" exists. (ex: VSAPPDIR=C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\)"); } var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "ProjectAB"), item => item.Name is not "obj" and not "bin"); var nuget = Path.Combine(unitTestProjectDir, "..", "buildTools", "nuget.exe"); var devenv = Path.Combine(vsAppDir, "devenv.exe"); await XProcess.Start(nuget, "restore", workDir).ExitCodeIs(0); await XProcess.Start(devenv, "ProjectAB.sln /Build", workDir).ExitCodeIs(0); var outDir = Path.Combine(workDir, "ProjectB", "bin", "Debug", "net472"); var driverFullPath1 = Path.Combine(outDir, "geckodriver"); var driverFullPath2 = Path.Combine(outDir, "geckodriver.exe"); (File.Exists(driverFullPath1) || File.Exists(driverFullPath2)).IsTrue(); }
public async Task WaitForOutputAsync_TimeOuted_Test() { using var process = XProcess.Start("dotnet", "testee.dll -n", baseDir); var found = await process.WaitForOutputAsync(str => false, millsecondsTimeout : 3000); found.IsFalse(); }
public async Task Publish_by_msbuild_Test() { // Given var solutionDir = FileIO.FindContainerDirToAncestor("*.sln"); var srcDir = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp0"); using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin"); for (var i = 0; i < 2; i++) { Console.WriteLine($"{(i == 0 ? "1st" : "2nd")} time publishing..."); // When await XProcess.Start("dotnet", "restore", workDir).WaitForExitAsync(); var dotnetCLI = await XProcess.Start("dotnet", "msbuild -p:Configuration=Debug -p:BlazorEnableCompression=false -p:DeployOnBuild=true -p:PublishUrl=bin/publish", workDir).WaitForExitAsync(); dotnetCLI.ExitCode.Is(0, message: dotnetCLI.StdOutput + dotnetCLI.StdError); // Then // Validate prerendered contents. var wwwrootDir = Path.Combine(workDir, "bin", "publish", "wwwroot"); ValidatePrerenderedContents_of_BlazorWasmApp0(wwwrootDir); } }
public async Task Publish_with_SingleFileEnabled_Test() { var rid = "win-x64"; var driverFileName = "geckodriver.exe"; var executableFileFormat = Format.PE64; using var workDir = this.CreateWorkDir(); var publishCommand = new[] { "dotnet", "publish", "-r", rid, "-o", "out", "-c:Release", "-p:PublishGeckoDriver=true", "-p:PublishSingleFile=true", "-p:SelfContained=false" }; // IMPORTANT: 2nd time of publishing, sometimes lost driver file in the published folder, so we have to validate it.. for (var i = 0; i < 2; i++) { await XProcess.Start( filename : publishCommand.First(), arguments : String.Join(' ', publishCommand.Skip(1)), workDir).ExitCodeIs(0); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsTrue(); DetectFormat(driverFullPath).Is(executableFileFormat); } }
public void Start_when_file_not_found_Test() { Assert.ThrowsAsync <Win32Exception>(async() => { using var process = await XProcess.Start(Path.Combine(".", Guid.NewGuid().ToString("N")) + ".exe").WaitForExitAsync(); }); }
static async Task Main(string[] args) { Console.WriteLine("Start server..."); using var serverProcess = XProcess.Start( "dotnet", "run --project Server --urls=http://127.0.0.1:5010", configure: option => { option.TerminateWhenDisposing = true; }); var serverStarted = false; await foreach (var line in serverProcess.GetOutputAsyncStream()) { Console.WriteLine(line); if (line.Trim().StartsWith("Now listening on:")) { serverStarted = true; break; } } if (serverStarted == false) { return; } Console.WriteLine("Start fetching..."); var httpClient = new HttpClient(); var htmlContent = await httpClient.GetStringAsync("http://127.0.0.1:5010/"); var targetDir = Path.Combine("public", "wwwroot"); Directory.CreateDirectory(targetDir); File.WriteAllText(Path.Combine(targetDir, "root.html"), htmlContent); Console.WriteLine("Complete."); }
public static async ValueTask ExitCodeIs(this XProcess process, int expected) { using (process) { await process.WaitForExitAsync(); process.ExitCode.Is(expected, "message: " + process.Output); } }
public async Task ExitCode_Test() { using var processA = await XProcess.Start("dotnet", "testee.dll", baseDir).WaitForExitAsync(); processA.ExitCode.Is(0); using var processB = await XProcess.Start("dotnet", "testee.dll --exitcode 123", baseDir).WaitForExitAsync(); processB.ExitCode.Is(123); }
public async Task Publish_Test() { // Given var solutionDir = FileIO.FindContainerDirToAncestor("*.sln"); var srcDir = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp0"); using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin"); var expectedHomeTitles = new Dictionary <int, string> { [1] = "Home", [2] = "My Home" }; var expectedEnvNames = new Dictionary <int, string> { [1] = "Prerendering", [2] = "Foo" }; for (var i = 1; i <= 2; i++) { Console.WriteLine($"{(i == 1 ? "1st" : "2nd")} time publishing..."); // When var arg = "publish -c:Debug -p:BlazorEnableCompression=false -o:bin/publish"; // if 2nd time publishing, override the environment name. if (i == 2) { arg += " -p:BlazorWasmPrerenderingEnvironment=" + expectedEnvNames[2]; } var dotnetCLI = await XProcess.Start("dotnet", arg, workDir).WaitForExitAsync(); dotnetCLI.ExitCode.Is(0, message: dotnetCLI.StdOutput + dotnetCLI.StdError); // Then // Validate prerendered contents. var wwwrootDir = Path.Combine(workDir, "bin", "publish", "wwwroot"); ValidatePrerenderedContents_of_BlazorWasmApp0( wwwrootDir, homeTitle: expectedHomeTitles[i], environment: expectedEnvNames[i]); // Validate PWA assets manifest. var indexHtmlBytes = File.ReadAllBytes(Path.Combine(wwwrootDir, "index.html")); using var sha256 = SHA256.Create(); var hash = "sha256-" + Convert.ToBase64String(sha256.ComputeHash(indexHtmlBytes)); var serviceWorkerAssetsJs = File.ReadAllText(Path.Combine(wwwrootDir, "my-assets.js")); serviceWorkerAssetsJs = Regex.Replace(serviceWorkerAssetsJs, @"^self\.assetsManifest\s*=\s*", ""); serviceWorkerAssetsJs = Regex.Replace(serviceWorkerAssetsJs, ";\\s*$", ""); var assetsManifestFile = JsonSerializer.Deserialize <AssetsManifestFile>(serviceWorkerAssetsJs); var assetManifestEntry = assetsManifestFile?.assets?.First(a => a.url == "index.html"); assetManifestEntry.IsNotNull(); assetManifestEntry !.hash.Is(hash); } }
public async Task PublishWithRuntimeIdentifier_NoPublish_Test(string rid, string driverFileName, Format _) { using var workDir = this.CreateWorkDir(); var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out", workDir).WaitForExitAsync(); dotnet.ExitCode.Is(0, message: dotnet.Output); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsFalse(); }
public async Task PublishWithRuntimeIdentifier_with_MSBuildProp_Test(string rid, string driverFileName, Format executableFileFormat) { using var workDir = this.CreateWorkDir(); await XProcess.Start("dotnet", $"publish -r {rid} -o out -p:PublishGeckoDriver=true", workDir) .ExitCodeIs(0); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsTrue(); DetectFormat(driverFullPath).Is(executableFileFormat); }
public async Task PublishWithRuntimeIdentifier_with_DefineConstants_Test(string rid, string driverFileName, Format executableFileFormat) { using var workDir = this.CreateWorkDir(); await XProcess.Start("dotnet", $"publish -r {rid} -o out -p:DefineConstants=_PUBLISH_GECKODRIVER", workDir) .ExitCodeIs(0); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsTrue(); DetectFormat(driverFullPath).Is(executableFileFormat); }
public async Task BuildWithRuntimeIdentifier_Test(string rid, string driverFileName, Format executableFileFormat) { using var workDir = this.CreateWorkDir(); var dotnet = await XProcess.Start("dotnet", $"build -r {rid} -o out", workDir).WaitForExitAsync(); dotnet.ExitCode.Is(0, message: dotnet.Output); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsTrue(); DetectFormat(driverFullPath).Is(executableFileFormat); }
public async Task Publish_with_HTTP500_Test() { // Given var solutionDir = FileIO.FindContainerDirToAncestor("*.sln"); var srcDir = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp1"); using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin"); // When (Set the hoting environment name to "ExceptionTest") var dotnetCLI = await XProcess.Start("dotnet", "publish -c:Release -p:BlazorWasmPrerenderingEnvironment=ExceptionTest -p:BlazorEnableCompression=false --nologo", workDir).WaitForExitAsync(); // Then (Exit code is NOT 0) dotnetCLI.ExitCode.IsNot(0, message: dotnetCLI.Output); }
public async Task PublishWithRuntimeIdentifier_NoPublish_Test(string rid, string driverFileName, Format _) { var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin"); using var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out", workDir).WaitForExitAsync(); dotnet.ExitCode.Is(0); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsFalse(); }
public async Task GetStdOutAsyncStream_Test() { using var process = XProcess.Start("dotnet", "testee.dll --exitcode 234", baseDir); var outputs = new StringBuilder(); await foreach (var output in process.GetStdOutAsyncStream()) { outputs.AppendLine(output); } process.ExitCode.Is(234); var nl = Environment.NewLine; outputs.ToString().Is($"Hello,{nl}Nice to{nl}"); }
public async Task GetStdErrtAsyncStream_Test() { using var process = XProcess.Start("dotnet", "testee.dll", baseDir); var outputs = new StringBuilder(); await foreach (var output in process.GetStdErrAsyncStream()) { outputs.AppendLine(output); } process.ExitCode.Is(0); var nl = Environment.NewLine; outputs.ToString().Is($"everyone.{nl}meet you.{nl}"); }
public async ValueTask <WorkDirectory> PublishAsync() { if (!this.PublishedOnce) { var publishProcess = XProcess.Start( "dotnet", $"publish -c:{this.Configuration} -p:BlazorWasmPrerendering=disable -p:BlazorEnableCompression=false -p:UsingBrowserRuntimeWorkload=false", workingDirectory: this.ProjectDir); await publishProcess.WaitForExitAsync(); publishProcess.ExitCode.Is(0, message: publishProcess.StdOutput + publishProcess.StdError); this.PublishedOnce = true; } return(WorkDirectory.CreateCopyFrom(this.PublishSrcDir, _ => true)); }
public async Task PublishWithRuntimeIdentifier_with_DefineConstants_Test(string rid, string driverFileName, Format executableFileFormat) { var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin"); using var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out -p:DefineConstants=_PUBLISH_CHROMEDRIVER", workDir).WaitForExitAsync(); dotnet.ExitCode.Is(0); var driverFullPath = Path.Combine(workDir, "out", driverFileName); File.Exists(driverFullPath).IsTrue(); DetectFormat(driverFullPath).Is(executableFileFormat); }
public static async ValueTask WaitForOutput(this XProcess process, Func <string, bool> predicate, int millsecondsTimeout) { var bufferedOutput = process.GetAndClearBufferedOutput(); if (predicate(bufferedOutput)) { return; } var cts = new CancellationTokenSource(millsecondsTimeout); await foreach (var output in process.GetOutputAsyncStream().WithCancellation(cts.Token)) { if (predicate(output)) { return; } } }
public async Task Output_of_ProjectB_Contains_DriverFile_Test() { var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "ProjectAB"), item => item.Name is not "obj" and not "bin"); //var devenv = Environment.ExpandEnvironmentVariables(Path.Combine("%DevEnvDir%", "devenv.exe")); var devenv = @"C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe"; using var nugetProcess = await XProcess.Start("nuget", "restore", workDir).WaitForExitAsync(); nugetProcess.ExitCode.Is(0); using var devenvProcess = await XProcess.Start(devenv, "ProjectAB.sln /Build", workDir).WaitForExitAsync(); devenvProcess.ExitCode.Is(0); var outDir = Path.Combine(workDir, "ProjectB", "bin", "Debug", "net472"); var driverFullPath1 = Path.Combine(outDir, "chromedriver"); var driverFullPath2 = Path.Combine(outDir, "chromedriver.exe"); (File.Exists(driverFullPath1) || File.Exists(driverFullPath2)).IsTrue(); }
public async Task AppSettings_Test() { // Given var solutionDir = FileIO.FindContainerDirToAncestor("*.sln"); var srcDir = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp0"); using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin"); File.WriteAllText(Path.Combine(workDir, "wwwroot", "appsettings.json"), @"{""HomeTitle"":""127.0.0.1""}"); // When var dotnetCLI = await XProcess.Start("dotnet", "publish -c:Debug -p:BlazorEnableCompression=false -o:bin/publish", workDir).WaitForExitAsync(); dotnetCLI.ExitCode.Is(0, message: dotnetCLI.StdOutput + dotnetCLI.StdError); // Then // Validate prerendered contents. var wwwrootDir = Path.Combine(workDir, "bin", "publish", "wwwroot"); ValidatePrerenderedContents_of_BlazorWasmApp0(wwwrootDir, homeTitle: "127.0.0.1"); }
public async Task GetAndClearBufferedOutput_Test() { using var process = XProcess.Start("dotnet", "testee.dll -i -o mixboth", baseDir); await process.GetOutputAsyncStream().GetAsyncEnumerator().MoveNextAsync(); await Task.Delay(500); var outputLines1 = process.GetAndClearBufferedOutput().Split('\n'); (outputLines1.Length > 0).IsTrue(); await Task.Delay(500); var outputLines2 = process.Output.Split('\n'); (outputLines2.Length > 0).IsTrue(); var outputLines = outputLines1.Concat(outputLines2).ToArray(); for (var i = 0; i < outputLines.Length; i++) { outputLines[i].Is(i.ToString()); } }
public async Task StdOutput_Test() { using var process = await XProcess.Start("dotnet", "testee.dll", baseDir).WaitForExitAsync(); process.StdOutput.Is("Hello,\nNice to"); }
public void ExitCode_of_running_process_is_null_Test() { using var process = XProcess.Start("dotnet", "testee.dll --infinitecounter", baseDir); process.ExitCode.IsNull(); }
public async Task Output_Test() { using var process = await XProcess.Start("dotnet", "testee.dll", baseDir).WaitForExitAsync(); process.Output.Is("Hello,\neveryone.\nNice to\nmeet you."); }
public async Task StdError_Test() { using var process = await XProcess.Start("dotnet", "testee.dll", baseDir).WaitForExitAsync(); process.StdError.Is("everyone.\nmeet you."); }