From 9c2860e4d9e5caf514b9a92784b2149eb70637b5 Mon Sep 17 00:00:00 2001 From: Oski Kervinen Date: Mon, 28 May 2018 16:02:34 +0300 Subject: [PATCH 1/2] Fixed: Test input and output files stored in VS directory The fix to the problem with directories with spaces transitioned to moving relative path names for the test input and output files. Sadly, the current working directory of the test process is some wandom Visual Studio directory, which caused problems with access rights sometimes, and is not a great place for them anyway. Fixed by explicitly setting the working directory based on the test executable. --- TestAdapter/ProcessRunner.cs | 13 +++++++------ TestAdapter/TestDiscoverer.cs | 6 +++++- TestAdapter/TestExecutor.cs | 13 ++++++++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/TestAdapter/ProcessRunner.cs b/TestAdapter/ProcessRunner.cs index 2bb0f00..68e8067 100644 --- a/TestAdapter/ProcessRunner.cs +++ b/TestAdapter/ProcessRunner.cs @@ -10,7 +10,7 @@ namespace CatchTestAdapter /// /// Runs external processes. /// - class ProcessRunner + public class ProcessRunner { /// /// Execute a plain external process. @@ -18,7 +18,7 @@ class ProcessRunner /// Path to executable. /// Command line arguments. /// - public static IList RunProcess(string cmd, string args ) + public static IList RunProcess(string cmd, string args, string workingDirectory ) { // Start a new process. var processStartInfo = new ProcessStartInfo( cmd, args ) @@ -27,7 +27,7 @@ public static IList RunProcess(string cmd, string args ) RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, - WorkingDirectory = @"." + WorkingDirectory = workingDirectory }; using ( Process process = Process.Start( processStartInfo ) ) @@ -43,7 +43,7 @@ public static IList RunProcess(string cmd, string args ) /// The executable. /// Command-line parameters. /// - public static IList RunDebugProcess( IFrameworkHandle frameworkHandle, string cmd, string args ) + public static IList RunDebugProcess( IFrameworkHandle frameworkHandle, string cmd, string args, string workingDirectory ) { // We cannot reliably capture the output of a process launched by the framework. // We store the output in a temp file instead. @@ -53,7 +53,7 @@ public static IList RunDebugProcess( IFrameworkHandle frameworkHandle, s // Tell the framework to run the process in a debugger. int pid = frameworkHandle.LaunchProcessWithDebuggerAttached( - cmd, System.Environment.CurrentDirectory, + cmd, workingDirectory, argsWithOutFile, new Dictionary() ); // Wait for exit. @@ -63,7 +63,8 @@ public static IList RunDebugProcess( IFrameworkHandle frameworkHandle, s } // Get the output. - var outputLines = new List( System.IO.File.ReadAllLines( outputFile ) ); + var outputLines = new List( System.IO.File.ReadAllLines( workingDirectory + + System.IO.Path.DirectorySeparatorChar + outputFile ) ); System.IO.File.Delete( outputFile ); return outputLines; diff --git a/TestAdapter/TestDiscoverer.cs b/TestAdapter/TestDiscoverer.cs index 332bdde..416ac9e 100644 --- a/TestAdapter/TestDiscoverer.cs +++ b/TestAdapter/TestDiscoverer.cs @@ -46,7 +46,11 @@ public void DiscoverTests(IEnumerable sources, IDiscoveryContext discove public static IList CreateTestCases( string exeName ) { var testCases = new List(); - var output = ProcessRunner.RunProcess(exeName, "--list-tests --verbosity high"); + + // Use the directory of the executable as the working directory. + string workingDirectory = System.IO.Path.GetDirectoryName( exeName ); + + var output = ProcessRunner.RunProcess(exeName, "--list-tests --verbosity high", workingDirectory); foreach (var test in ParseListing( exeName, output ) ) { diff --git a/TestAdapter/TestExecutor.cs b/TestAdapter/TestExecutor.cs index eec8d3e..f277376 100644 --- a/TestAdapter/TestExecutor.cs +++ b/TestAdapter/TestExecutor.cs @@ -133,9 +133,16 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame // Get a list of all test case names var listOfTestCases = tests.Aggregate("", (acc, test) => acc + test.DisplayName + "\n"); + // Use the directory of the executable as the working directory. + string workingDirectory = System.IO.Path.GetDirectoryName( CatchExe ); + if ( workingDirectory == "" ) + workingDirectory = "."; + // Write them to the input file for Catch runner string caseFile = "test.cases"; - System.IO.File.WriteAllText(caseFile, listOfTestCases); + System.IO.File.WriteAllText( + workingDirectory + System.IO.Path.DirectorySeparatorChar + caseFile, + listOfTestCases); string originalDirectory = Directory.GetCurrentDirectory(); // Execute the tests @@ -144,11 +151,11 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame string arguments = "-r xml --durations yes --input-file=" + caseFile; if ( runContext.IsBeingDebugged ) { - output_text = ProcessRunner.RunDebugProcess( frameworkHandle, CatchExe, arguments ); + output_text = ProcessRunner.RunDebugProcess( frameworkHandle, CatchExe, arguments, workingDirectory ); } else { - output_text = ProcessRunner.RunProcess( CatchExe, arguments ); + output_text = ProcessRunner.RunProcess( CatchExe, arguments, workingDirectory ); } timer.Stop(); From c1b78f0b3801c151aa9f60892923237eb20e4e89 Mon Sep 17 00:00:00 2001 From: Oski Kervinen Date: Mon, 28 May 2018 16:03:49 +0300 Subject: [PATCH 2/2] Mock LaunchProcessWithDebuggerAttached Without this, debugging the adapter tests fails, because it recognizes it is in the debugger and tries to use the debugger attach code path. --- TestAdapterTest/Mocks/MockFrameworkHandle.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TestAdapterTest/Mocks/MockFrameworkHandle.cs b/TestAdapterTest/Mocks/MockFrameworkHandle.cs index 919b9ae..6755f40 100644 --- a/TestAdapterTest/Mocks/MockFrameworkHandle.cs +++ b/TestAdapterTest/Mocks/MockFrameworkHandle.cs @@ -29,7 +29,8 @@ public struct TestMessage { public int LaunchProcessWithDebuggerAttached( string filePath, string workingDirectory, string arguments, IDictionary environmentVariables ) { - throw new NotImplementedException(); + CatchTestAdapter.ProcessRunner.RunProcess( filePath, arguments, workingDirectory ); + return 0; } public void RecordAttachments( IList attachmentSets )