diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs index 3a17bcc276..b110b774d4 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs @@ -51,6 +51,7 @@ public class ProxyOperationManager private bool _testHostLaunched; private int _testHostProcessId; private string? _testHostProcessStdError; + private string? _testHostProcessFileName; /// /// Initializes a new instance of the class. @@ -231,6 +232,7 @@ public virtual bool SetupChannel(IEnumerable sources, string? runSetting sources, envVars, connectionInfo)); + _testHostProcessFileName = testHostStartInfo.FileName; try { // Launch the test host. @@ -512,7 +514,7 @@ private void TestHostManagerHostExited(object? sender, HostProviderEventArgs? e) // If not it reports timeout, if we don't set this before OnClientProcessExit we will // report timeout even though we exited the test host before even attempting the connect. _testHostExited.Set(); - RequestSender.OnClientProcessExit(_testHostProcessStdError); + RequestSender.OnClientProcessExit(BuildCrashErrorContext(_testHostProcessFileName, _testHostProcessStdError)); } private void ThrowOnTestHostExited(IEnumerable sources, bool testHostExited) @@ -521,7 +523,7 @@ private void ThrowOnTestHostExited(IEnumerable sources, bool testHostExi { // We might consider passing standard output here in case standard error is not // available because some errors don't end up in the standard error output. - throw new TestPlatformException(string.Format(CultureInfo.CurrentCulture, CrossPlatEngineResources.TestHostExitedWithError, string.Join("', '", sources), _testHostProcessStdError)); + throw new TestPlatformException(string.Format(CultureInfo.CurrentCulture, CrossPlatEngineResources.TestHostExitedWithError, string.Join("', '", sources), BuildCrashErrorContext(_testHostProcessFileName, _testHostProcessStdError))); } } @@ -551,13 +553,31 @@ private void ThrowExceptionOnConnectionFailure(IEnumerable sources, int } } - // After testhost process launched failed with error. + // After testhost process crashed (stderr present), replace the message with a diagnostic one. if (!StringUtils.IsNullOrWhiteSpace(_testHostProcessStdError)) { - // Testhost failed with error. - errorMsg = string.Format(CultureInfo.CurrentCulture, CrossPlatEngineResources.TestHostExitedWithError, string.Join("', '", sources), _testHostProcessStdError); + errorMsg = string.Format(CultureInfo.CurrentCulture, CrossPlatEngineResources.TestHostExitedWithError, string.Join("', '", sources), BuildCrashErrorContext(_testHostProcessFileName, _testHostProcessStdError)); + } + else if (!StringUtils.IsNullOrWhiteSpace(_testHostProcessFileName)) + { + // Process launched but no stderr — append path as a diagnostic addendum without overwriting the timeout message. + errorMsg += $" {BuildCrashErrorContext(_testHostProcessFileName, null)}"; } throw new TestPlatformException(errorMsg); } + + private static string BuildCrashErrorContext(string? processFileName, string? stdError) + { + var hasPath = !StringUtils.IsNullOrWhiteSpace(processFileName); + var hasError = !StringUtils.IsNullOrWhiteSpace(stdError); + + return (hasPath, hasError) switch + { + (true, true) => $"Process path: {processFileName}{Environment.NewLine}{stdError}", + (true, false) => $"Process path: {processFileName}", + (false, true) => stdError!, + _ => string.Empty, + }; + } } diff --git a/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/ExecutionTests.cs b/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/ExecutionTests.cs index c70fe7796a..4fd4b85bb4 100644 --- a/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/ExecutionTests.cs +++ b/test/Microsoft.TestPlatform.Acceptance.IntegrationTests/ExecutionTests.cs @@ -188,7 +188,7 @@ public void StackOverflowExceptionShouldBeLoggedToConsoleAndDiagLogFile(RunnerIn var errorMessage = "Process is terminated due to StackOverflowException."; if (runnerInfo.IsNetTarget) { - errorMessage = "Test host process crashed : Stack overflow."; + errorMessage = "Stack overflow."; } ExitCodeEquals(1); diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs index 6cb8aa5060..227a73d05f 100644 --- a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs @@ -441,6 +441,153 @@ public void CloseShouldNotThrowIfEndSessionFails() _testOperationManager.Close(); } + [TestMethod] + public void SetupChannelShouldIncludeProcessPathAndStdErrorWhenTestHostExitsWithBoth() + { + var processPath = @"C:\testhost.exe"; + var stdError = "Unhandled exception. System.Exception: Boom"; + + _mockTestHostManager.Setup( + m => m.GetTestHostProcessStartInfo( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .Returns(new TestProcessStartInfo { FileName = processPath }); + + _mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny(), It.IsAny())) + .Callback(() => + { + _mockTestHostManager.Raise(t => t.HostLaunched += null, new HostProviderEventArgs(string.Empty)); + _mockTestHostManager.Raise(t => t.HostExited += null, new HostProviderEventArgs(stdError)); + }) + .Returns(Task.FromResult(true)); + + _mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny(), It.IsAny())).Returns(false); + + var operationManager = new TestableProxyOperationManager(_mockRequestData.Object, _mockRequestSender.Object, _mockTestHostManager.Object); + + var ex = Assert.ThrowsExactly(() => operationManager.SetupChannel(["test.dll"], DefaultRunSettings)); + Assert.Contains("Process path:", ex.Message); + Assert.Contains(processPath, ex.Message); + Assert.Contains(stdError, ex.Message); + } + + [TestMethod] + public void SetupChannelShouldIncludeOnlyStdErrorWhenProcessPathIsNull() + { + var stdError = "Unhandled exception. System.Exception: Boom"; + + _mockTestHostManager.Setup( + m => m.GetTestHostProcessStartInfo( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .Returns(new TestProcessStartInfo { FileName = null }); + + _mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny(), It.IsAny())) + .Callback(() => + { + _mockTestHostManager.Raise(t => t.HostLaunched += null, new HostProviderEventArgs(string.Empty)); + _mockTestHostManager.Raise(t => t.HostExited += null, new HostProviderEventArgs(stdError)); + }) + .Returns(Task.FromResult(true)); + + _mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny(), It.IsAny())).Returns(false); + + var operationManager = new TestableProxyOperationManager(_mockRequestData.Object, _mockRequestSender.Object, _mockTestHostManager.Object); + + var ex = Assert.ThrowsExactly(() => operationManager.SetupChannel(["test.dll"], DefaultRunSettings)); + Assert.Contains(stdError, ex.Message); + Assert.DoesNotContain("Process path:", ex.Message); + } + + [TestMethod] + public void SetupChannelShouldIncludeProcessPathWhenTestHostTimesOutWithNoStdError() + { + var processPath = @"C:\testhost.exe"; + + _mockTestHostManager.Setup( + m => m.GetTestHostProcessStartInfo( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .Returns(new TestProcessStartInfo { FileName = processPath }); + + _mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny(), It.IsAny())) + .Callback(() => _mockTestHostManager.Raise(t => t.HostLaunched += null, new HostProviderEventArgs(string.Empty))) + .Returns(Task.FromResult(true)); + + _mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny(), It.IsAny())).Returns(false); + + var operationManager = new TestableProxyOperationManager(_mockRequestData.Object, _mockRequestSender.Object, _mockTestHostManager.Object); + + var ex = Assert.ThrowsExactly(() => operationManager.SetupChannel(["test.dll"], DefaultRunSettings)); + Assert.Contains($" Process path: {processPath}", ex.Message); + } + + [TestMethod] + public void SetupChannelShouldPassCrashContextToRequestSenderOnTestHostExit() + { + var processPath = @"C:\testhost.exe"; + var stdError = "Fatal error"; + + _mockTestHostManager.Setup( + m => m.GetTestHostProcessStartInfo( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .Returns(new TestProcessStartInfo { FileName = processPath }); + + _mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny(), It.IsAny())) + .Callback(() => + { + _mockTestHostManager.Raise(t => t.HostLaunched += null, new HostProviderEventArgs(string.Empty)); + _mockTestHostManager.Raise(t => t.HostExited += null, new HostProviderEventArgs(stdError)); + }) + .Returns(Task.FromResult(true)); + + _mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny(), It.IsAny())).Returns(false); + + var operationManager = new TestableProxyOperationManager(_mockRequestData.Object, _mockRequestSender.Object, _mockTestHostManager.Object); + + Assert.ThrowsExactly(() => operationManager.SetupChannel(["test.dll"], DefaultRunSettings)); + + // Verify OnClientProcessExit was called with the crash context containing both path and error. + _mockRequestSender.Verify( + rs => rs.OnClientProcessExit(It.Is(s => s.Contains(processPath) && s.Contains(stdError))), + Times.Once); + } + + [TestMethod] + public void SetupChannelShouldIncludeProcessPathInErrorWhenTestHostExitsWithOnlyPath() + { + var processPath = @"C:\testhost.exe"; + + _mockTestHostManager.Setup( + m => m.GetTestHostProcessStartInfo( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .Returns(new TestProcessStartInfo { FileName = processPath }); + + _mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny(), It.IsAny())) + .Callback(() => + { + _mockTestHostManager.Raise(t => t.HostLaunched += null, new HostProviderEventArgs(string.Empty)); + // Host exits with empty stderr (no error output). + _mockTestHostManager.Raise(t => t.HostExited += null, new HostProviderEventArgs(string.Empty)); + }) + .Returns(Task.FromResult(true)); + + _mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny(), It.IsAny())).Returns(false); + + var operationManager = new TestableProxyOperationManager(_mockRequestData.Object, _mockRequestSender.Object, _mockTestHostManager.Object); + + var ex = Assert.ThrowsExactly(() => operationManager.SetupChannel(["test.dll"], DefaultRunSettings)); + // ThrowOnTestHostExited fires first; with no stderr the crash context is just the path. + Assert.Contains(processPath, ex.Message); + } + private void SetupWaitForTestHostExit() { // Raise host exited when end session is called diff --git a/test/Microsoft.TestPlatform.Library.IntegrationTests/TranslationLayerTests/RunTests.cs b/test/Microsoft.TestPlatform.Library.IntegrationTests/TranslationLayerTests/RunTests.cs index 3aae29ea6d..5e394623b2 100644 --- a/test/Microsoft.TestPlatform.Library.IntegrationTests/TranslationLayerTests/RunTests.cs +++ b/test/Microsoft.TestPlatform.Library.IntegrationTests/TranslationLayerTests/RunTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; @@ -186,8 +187,8 @@ public void RunTestsShouldThrowOnStackOverflowException(RunnerInfo runnerInfo) _runEventHandler); var errorMessagePattern = runnerInfo.IsNetFrameworkTarget - ? $"The active test run was aborted. Reason: Test host process crashed : Process is terminated due to StackOverflowException.*" - : $"The active test run was aborted. Reason: Test host process crashed : Stack overflow.*"; + ? $"The active test run was aborted. Reason: Test host process crashed : Process path: *{Environment.NewLine}Process is terminated due to StackOverflowException.*" + : $"The active test run was aborted. Reason: Test host process crashed : Process path: *{Environment.NewLine}Stack overflow.*"; _runEventHandler.Errors.Should().ContainSingle() .Which.Should().Match(errorMessagePattern);