From 435904bc0a858439f53964bb0789d98ab255636b Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 13 Aug 2013 22:20:24 -0700 Subject: [PATCH] better process.start for mono --- NzbDrone.Common.Test/ProcessProviderTests.cs | 11 ++---- NzbDrone.Common/ProcessProvider.cs | 38 +++++++++---------- .../UpdateTests/UpdateServiceFixture.cs | 2 +- NzbDrone.Core/Update/InstallUpdateService.cs | 2 +- NzbDrone.Host/AccessControl/UrlAclAdapter.cs | 2 +- NzbDrone.Host/ApplicationServer.cs | 2 +- NzbDrone.Integration.Test/NzbDroneRunner.cs | 6 +-- NzbDrone.Update.Test/StartNzbDroneService.cs | 6 +-- .../UpdateEngine/DetectApplicationType.cs | 2 +- NzbDrone.Update/UpdateEngine/StartNzbDrone.cs | 2 +- .../UpdateEngine/TerminateNzbDrone.cs | 4 +- NzbDrone/SysTray/SysTrayApp.cs | 2 +- 12 files changed, 34 insertions(+), 45 deletions(-) diff --git a/NzbDrone.Common.Test/ProcessProviderTests.cs b/NzbDrone.Common.Test/ProcessProviderTests.cs index 1978203a3..2b7abb0c8 100644 --- a/NzbDrone.Common.Test/ProcessProviderTests.cs +++ b/NzbDrone.Common.Test/ProcessProviderTests.cs @@ -55,9 +55,7 @@ namespace NzbDrone.Common.Test [Test] public void Should_be_able_to_start_process() { - var startInfo = new ProcessStartInfo(Path.Combine(Directory.GetCurrentDirectory(), DummyApp.DUMMY_PROCCESS_NAME + ".exe")); - - var process = Subject.Start(startInfo); + var process = Subject.Start(Path.Combine(Directory.GetCurrentDirectory(), DummyApp.DUMMY_PROCCESS_NAME + ".exe")); Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should() .BeTrue("excepted one dummy process to be already running"); @@ -71,7 +69,7 @@ namespace NzbDrone.Common.Test [Test] public void Should_be_able_to_execute_process() { - var process = Subject.ShellExecute(Path.Combine(Directory.GetCurrentDirectory(), DummyApp.DUMMY_PROCCESS_NAME + ".exe")); + var process = Subject.Start(Path.Combine(Directory.GetCurrentDirectory(), DummyApp.DUMMY_PROCCESS_NAME + ".exe")); Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should() @@ -95,10 +93,9 @@ namespace NzbDrone.Common.Test dummy2.HasExited.Should().BeTrue(); } - public Process StartDummyProcess() + private Process StartDummyProcess() { - var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe"); - return Subject.Start(startInfo); + return Subject.Start(DummyApp.DUMMY_PROCCESS_NAME + ".exe"); } [Test] diff --git a/NzbDrone.Common/ProcessProvider.cs b/NzbDrone.Common/ProcessProvider.cs index 578f954dc..05af9895d 100644 --- a/NzbDrone.Common/ProcessProvider.cs +++ b/NzbDrone.Common/ProcessProvider.cs @@ -14,22 +14,21 @@ namespace NzbDrone.Common { ProcessInfo GetCurrentProcess(); ProcessInfo GetProcessById(int id); - Process Start(string path); - Process Start(ProcessStartInfo startInfo); + void OpenDefaultBrowser(string url); void WaitForExit(Process process); void SetPriority(int processId, ProcessPriorityClass priority); void KillAll(string processName); bool Exists(string processName); ProcessPriorityClass GetCurrentProcessPriority(); - Process ShellExecute(string path, string args = null, Action onOutputDataReceived = null, Action onErrorDataReceived = null); + Process Start(string path, string args = null, Action onOutputDataReceived = null, Action onErrorDataReceived = null); } public class ProcessProvider : IProcessProvider { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public const string NzbDroneProcessName = "NzbDrone"; - public const string NzbDroneConsoleProcessName = "NzbDrone.Console"; + public const string NZB_DRONE_PROCESS_NAME = "NzbDrone"; + public const string NZB_DRONE_CONSOLE_PROCESS_NAME = "NzbDrone.Console"; private static List GetProcessesByName(string name) { @@ -73,12 +72,22 @@ namespace NzbDrone.Common } - public Process Start(string path) + public void OpenDefaultBrowser(string url) { - return Start(new ProcessStartInfo(path)); + Logger.Info("Opening URL [{0}]", url); + + var process = new Process + { + StartInfo = new ProcessStartInfo(url) + { + UseShellExecute = true + } + }; + + process.Start(); } - public Process ShellExecute(string path, string args = null, Action onOutputDataReceived = null, Action onErrorDataReceived = null) + public Process Start(string path, string args = null, Action onOutputDataReceived = null, Action onErrorDataReceived = null) { if (OsInfo.IsMono && path.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase)) @@ -139,19 +148,6 @@ namespace NzbDrone.Common return process; } - public Process Start(ProcessStartInfo startInfo) - { - Logger.Info("Starting process. [{0}]", startInfo.FileName); - - var process = new Process - { - StartInfo = startInfo - }; - process.Start(); - - return process; - } - public void WaitForExit(Process process) { Logger.Trace("Waiting for process {0} to exit.", process.ProcessName); diff --git a/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs b/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs index 4d36befe2..2a485146c 100644 --- a/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs +++ b/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs @@ -104,7 +104,7 @@ namespace NzbDrone.Core.Test.UpdateTests Subject.Execute(new ApplicationUpdateCommand()); Mocker.GetMock() - .Verify(c => c.ShellExecute(It.IsAny(), "12", null, null), Times.Once()); + .Verify(c => c.Start(It.IsAny(), "12", null, null), Times.Once()); } [Test] diff --git a/NzbDrone.Core/Update/InstallUpdateService.cs b/NzbDrone.Core/Update/InstallUpdateService.cs index 0b25f8717..f9ac9ef55 100644 --- a/NzbDrone.Core/Update/InstallUpdateService.cs +++ b/NzbDrone.Core/Update/InstallUpdateService.cs @@ -72,7 +72,7 @@ namespace NzbDrone.Core.Update _logger.Info("Starting update client {0}", _appFolderInfo.GetUpdateClientExePath()); - var process = _processProvider.ShellExecute(_appFolderInfo.GetUpdateClientExePath(), _processProvider.GetCurrentProcess().Id.ToString()); + var process = _processProvider.Start(_appFolderInfo.GetUpdateClientExePath(), _processProvider.GetCurrentProcess().Id.ToString()); _processProvider.WaitForExit(process); diff --git a/NzbDrone.Host/AccessControl/UrlAclAdapter.cs b/NzbDrone.Host/AccessControl/UrlAclAdapter.cs index 6dc5ff5c4..c7e1480d6 100644 --- a/NzbDrone.Host/AccessControl/UrlAclAdapter.cs +++ b/NzbDrone.Host/AccessControl/UrlAclAdapter.cs @@ -43,7 +43,7 @@ namespace NzbDrone.Host.AccessControl { try { - var process = _processProvider.ShellExecute("netsh.exe", arguments); + var process = _processProvider.Start("netsh.exe", arguments); process.WaitForExit(5000); } catch (Exception ex) diff --git a/NzbDrone.Host/ApplicationServer.cs b/NzbDrone.Host/ApplicationServer.cs index 6e00575d7..b1abc00c5 100644 --- a/NzbDrone.Host/ApplicationServer.cs +++ b/NzbDrone.Host/ApplicationServer.cs @@ -63,7 +63,7 @@ namespace NzbDrone.Host try { _logger.Info("Starting default browser. {0}", _hostController.AppUrl); - _processProvider.Start(_hostController.AppUrl); + _processProvider.OpenDefaultBrowser(_hostController.AppUrl); } catch (Exception e) { diff --git a/NzbDrone.Integration.Test/NzbDroneRunner.cs b/NzbDrone.Integration.Test/NzbDroneRunner.cs index 90e2a0105..5e16a39b1 100644 --- a/NzbDrone.Integration.Test/NzbDroneRunner.cs +++ b/NzbDrone.Integration.Test/NzbDroneRunner.cs @@ -56,14 +56,14 @@ namespace NzbDrone.Integration.Test public void KillAll() { - _processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName); - _processProvider.KillAll(ProcessProvider.NzbDroneProcessName); + _processProvider.KillAll(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME); + _processProvider.KillAll(ProcessProvider.NZB_DRONE_PROCESS_NAME); } private void Start(string outputNzbdroneConsoleExe) { var args = "-nobrowser -data=\"" + AppDate + "\""; - _nzbDroneProcess = _processProvider.ShellExecute(outputNzbdroneConsoleExe, args, OnOutputDataReceived, OnOutputDataReceived); + _nzbDroneProcess = _processProvider.Start(outputNzbdroneConsoleExe, args, OnOutputDataReceived, OnOutputDataReceived); } diff --git a/NzbDrone.Update.Test/StartNzbDroneService.cs b/NzbDrone.Update.Test/StartNzbDroneService.cs index c4fc01ca7..7bd2b047f 100644 --- a/NzbDrone.Update.Test/StartNzbDroneService.cs +++ b/NzbDrone.Update.Test/StartNzbDroneService.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using Moq; using NUnit.Framework; using NzbDrone.Common; @@ -33,10 +32,7 @@ namespace NzbDrone.Update.Test Subject.Start(AppType.Service, targetFolder); - Mocker.GetMock().Verify(c => c.Start(It.Is(s => - s.FileName == "c:\\NzbDrone\\NzbDrone.Console.exe" && - s.Arguments == StartupArguments.NO_BROWSER - )), Times.Once()); + Mocker.GetMock().Verify(c => c.Start("c:\\NzbDrone\\NzbDrone.Console.exe", StartupArguments.NO_BROWSER, null, null), Times.Once()); ExceptionVerification.ExpectedWarns(1); } diff --git a/NzbDrone.Update/UpdateEngine/DetectApplicationType.cs b/NzbDrone.Update/UpdateEngine/DetectApplicationType.cs index afe596041..68a60b004 100644 --- a/NzbDrone.Update/UpdateEngine/DetectApplicationType.cs +++ b/NzbDrone.Update/UpdateEngine/DetectApplicationType.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Update.UpdateEngine return AppType.Service; } - if (_processProvider.Exists(ProcessProvider.NzbDroneConsoleProcessName)) + if (_processProvider.Exists(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME)) { return AppType.Console; } diff --git a/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs b/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs index 4df6c410b..1a419bb7f 100644 --- a/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs +++ b/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs @@ -73,7 +73,7 @@ namespace NzbDrone.Update.UpdateEngine _logger.Info("Starting {0}", fileName); var path = Path.Combine(installationFolder, fileName); - _processProvider.Start(new ProcessStartInfo(path, StartupArguments.NO_BROWSER)); + _processProvider.Start(path, StartupArguments.NO_BROWSER); } } } \ No newline at end of file diff --git a/NzbDrone.Update/UpdateEngine/TerminateNzbDrone.cs b/NzbDrone.Update/UpdateEngine/TerminateNzbDrone.cs index 99e55470b..80ae0e91b 100644 --- a/NzbDrone.Update/UpdateEngine/TerminateNzbDrone.cs +++ b/NzbDrone.Update/UpdateEngine/TerminateNzbDrone.cs @@ -44,8 +44,8 @@ namespace NzbDrone.Update.UpdateEngine _logger.Info("Killing all running processes"); - _processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName); - _processProvider.KillAll(ProcessProvider.NzbDroneProcessName); + _processProvider.KillAll(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME); + _processProvider.KillAll(ProcessProvider.NZB_DRONE_PROCESS_NAME); } } } \ No newline at end of file diff --git a/NzbDrone/SysTray/SysTrayApp.cs b/NzbDrone/SysTray/SysTrayApp.cs index 77dab4f53..595a53f16 100644 --- a/NzbDrone/SysTray/SysTrayApp.cs +++ b/NzbDrone/SysTray/SysTrayApp.cs @@ -84,7 +84,7 @@ namespace NzbDrone.SysTray { try { - _processProvider.Start(_hostController.AppUrl); + _processProvider.OpenDefaultBrowser(_hostController.AppUrl); } catch (Exception) {