From 1dad071f48ab1b6d499155ec25c8f62e9df74a7a Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 6 May 2012 17:18:16 +0100 Subject: [PATCH] Create directory for MS Test results file if it does not exist. Handle MS Test results file not being created. --- .../MSTest.SharpDevelop/MSTestDebugger.cs | 20 +++++++++++++--- .../MSTest.SharpDevelop/MSTestRunner.cs | 24 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/samples/MSTest/MSTest.SharpDevelop/MSTestDebugger.cs b/samples/MSTest/MSTest.SharpDevelop/MSTestDebugger.cs index 0d9021a94a..aa4ad68eee 100644 --- a/samples/MSTest/MSTest.SharpDevelop/MSTestDebugger.cs +++ b/samples/MSTest/MSTest.SharpDevelop/MSTestDebugger.cs @@ -49,6 +49,7 @@ namespace ICSharpCode.MSTest protected override ProcessStartInfo GetProcessStartInfo(SelectedTests selectedTests) { resultsFileName = new MSTestResultsFileName(selectedTests).FileName; + CreateDirectoryForResultsFile(); var mstestApplication = new MSTestApplication(selectedTests, resultsFileName); return mstestApplication.ProcessStartInfo; } @@ -64,6 +65,14 @@ namespace ICSharpCode.MSTest return messageService.AskQuestion(question, caption); } + void CreateDirectoryForResultsFile() + { + string path = Path.GetDirectoryName(resultsFileName); + if (!Directory.Exists(path)) { + Directory.CreateDirectory(path); + } + } + void Start(ProcessStartInfo startInfo) { StartDebugger(startInfo); @@ -89,9 +98,14 @@ namespace ICSharpCode.MSTest { debugger.DebugStopped -= DebugStopped; - var testResults = new MSTestResults(resultsFileName); - var workbench = new UnitTestWorkbench(); - workbench.SafeThreadAsyncCall(() => UpdateTestResults(testResults)); + if (File.Exists(resultsFileName)) { + var testResults = new MSTestResults(resultsFileName); + var workbench = new UnitTestWorkbench(); + workbench.SafeThreadAsyncCall(() => UpdateTestResults(testResults)); + } else { + messageService.ShowFormattedErrorMessage("Unable to find test results file: '{0}'.", resultsFileName); + OnAllTestsFinished(source, e); + } } void UpdateTestResults(MSTestResults testResults) diff --git a/samples/MSTest/MSTest.SharpDevelop/MSTestRunner.cs b/samples/MSTest/MSTest.SharpDevelop/MSTestRunner.cs index 3d0e65a51c..a5fb6be9be 100644 --- a/samples/MSTest/MSTest.SharpDevelop/MSTestRunner.cs +++ b/samples/MSTest/MSTest.SharpDevelop/MSTestRunner.cs @@ -42,9 +42,14 @@ namespace ICSharpCode.MSTest void ProcessRunnerExited(object source, EventArgs e) { // Read all tests. - var testResults = new MSTestResults(resultsFileName); - var workbench = new UnitTestWorkbench(); - workbench.SafeThreadAsyncCall(() => UpdateTestResults(testResults)); + if (FileExists(resultsFileName)) { + var testResults = new MSTestResults(resultsFileName); + var workbench = new UnitTestWorkbench(); + workbench.SafeThreadAsyncCall(() => UpdateTestResults(testResults)); + } else { + messageService.ShowFormattedErrorMessage("Unable to find test results file: '{0}'.", resultsFileName); + OnAllTestsFinished(source, e); + } } void UpdateTestResults(MSTestResults testResults) @@ -70,6 +75,7 @@ namespace ICSharpCode.MSTest protected override ProcessStartInfo GetProcessStartInfo(SelectedTests selectedTests) { resultsFileName = new MSTestResultsFileName(selectedTests).FileName; + CreateDirectoryForResultsFile(); var mstestApplication = new MSTestApplication(selectedTests, resultsFileName); return mstestApplication.ProcessStartInfo; } @@ -78,7 +84,7 @@ namespace ICSharpCode.MSTest { LogCommandLine(processStartInfo); - if (ApplicationFileNameExists(processStartInfo.FileName)) { + if (FileExists(processStartInfo.FileName)) { processRunner.WorkingDirectory = processStartInfo.WorkingDirectory; processRunner.Start(processStartInfo.FileName, processStartInfo.Arguments); } else { @@ -86,7 +92,15 @@ namespace ICSharpCode.MSTest } } - bool ApplicationFileNameExists(string fileName) + void CreateDirectoryForResultsFile() + { + string path = Path.GetDirectoryName(resultsFileName); + if (!Directory.Exists(path)) { + Directory.CreateDirectory(path); + } + } + + bool FileExists(string fileName) { return fileSystem.FileExists(fileName); }