better process.start for mono

This commit is contained in:
kay.one 2013-08-13 22:20:24 -07:00
parent aee7019ed2
commit 435904bc0a
12 changed files with 34 additions and 45 deletions

View File

@ -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]

View File

@ -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<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null);
Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> 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<Process> 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<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null)
public Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> 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);

View File

@ -104,7 +104,7 @@ namespace NzbDrone.Core.Test.UpdateTests
Subject.Execute(new ApplicationUpdateCommand());
Mocker.GetMock<IProcessProvider>()
.Verify(c => c.ShellExecute(It.IsAny<string>(), "12", null, null), Times.Once());
.Verify(c => c.Start(It.IsAny<string>(), "12", null, null), Times.Once());
}
[Test]

View File

@ -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);

View File

@ -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)

View File

@ -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)
{

View File

@ -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);
}

View File

@ -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<IProcessProvider>().Verify(c => c.Start(It.Is<ProcessStartInfo>(s =>
s.FileName == "c:\\NzbDrone\\NzbDrone.Console.exe" &&
s.Arguments == StartupArguments.NO_BROWSER
)), Times.Once());
Mocker.GetMock<IProcessProvider>().Verify(c => c.Start("c:\\NzbDrone\\NzbDrone.Console.exe", StartupArguments.NO_BROWSER, null, null), Times.Once());
ExceptionVerification.ExpectedWarns(1);
}

View File

@ -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;
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -84,7 +84,7 @@ namespace NzbDrone.SysTray
{
try
{
_processProvider.Start(_hostController.AppUrl);
_processProvider.OpenDefaultBrowser(_hostController.AppUrl);
}
catch (Exception)
{