Skip to content
Browse files

Extended the unit tests for ProcessExecutor to 89%. Remaining code is…

… either exception handling (and mainly for edge cases) or handling different OSs (e.g. detect Unix/Mac).
  • Loading branch information...
1 parent 6272dc6 commit 2704265bd3c123593167e5addc35267adee1b5b3 @csut017 csut017 committed Feb 27, 2011
Showing with 143 additions and 2 deletions.
  1. +143 −2 Playground/CruiseControl.Core.Tests/Utilities/ProcessExecutorTests.cs
View
145 Playground/CruiseControl.Core.Tests/Utilities/ProcessExecutorTests.cs
@@ -1,6 +1,7 @@
namespace CruiseControl.Core.Tests.Utilities
{
using System;
+ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
@@ -93,8 +94,7 @@ public void PopulateKillProcessHandlesUnix()
[Test]
public void KillProcessesForProjectKillsAProcess()
{
- var fileSystemMock = new Mock<IFileSystem>(MockBehavior.Strict);
- fileSystemMock.Setup(fs => fs.CheckIfFileExists(It.IsAny<string>())).Returns(true);
+ var fileSystemMock = InitialiseFileSystemMockForExecute();
var info = new ProcessInfo("sleeper");
var executor = new ProcessExecutor(fileSystemMock.Object);
var projectName = "aProject";
@@ -111,10 +111,151 @@ public void KillProcessesForProjectKillsAProcess()
}
[Test]
+ public void ExecuteRunsProcess()
+ {
+ var fileSystemMock = InitialiseFileSystemMockForExecute();
+ var info = new ProcessInfo("sleeper", "1");
+ var executor = new ProcessExecutor(fileSystemMock.Object);
+ var projectName = "aProject";
+ var waitHandle = new ManualResetEvent(false);
+ ProcessResult result = null;
+ var thread = new Thread(
+ () =>
+ {
+ try
+ {
+ result = executor.Execute(info, projectName, "aTask", "C:\\somewhere.txt");
+ }
+ finally
+ {
+ waitHandle.Set();
+ }
+ });
+ thread.Start();
+ waitHandle.WaitOne(TimeSpan.FromSeconds(30));
+ Assert.IsTrue(result.Succeeded);
+ }
+
+ [Test]
+ public void ExecuteTimesOut()
+ {
+ var fileSystemMock = InitialiseFileSystemMockForExecute();
+ var info = new ProcessInfo("sleeper") { TimeOut = TimeSpan.FromSeconds(1) };
+ var executor = new ProcessExecutor(fileSystemMock.Object);
+ var projectName = "aProject";
+ var waitHandle = new ManualResetEvent(false);
+ ProcessResult result = null;
+ var thread = new Thread(
+ () =>
+ {
+ try
+ {
+ result = executor.Execute(info, projectName, "aTask", "C:\\somewhere.txt");
+ }
+ finally
+ {
+ waitHandle.Set();
+ }
+ });
+ thread.Start();
+ waitHandle.WaitOne(TimeSpan.FromSeconds(30));
+ Assert.IsTrue(result.TimedOut);
+ }
+
+ [Test]
+ public void ExecutePassesOnOutput()
+ {
+ var fileSystemMock = InitialiseFileSystemMockForExecute();
+ var info = new ProcessInfo("sleeper", "1");
+ var output = new List<ProcessOutputEventArgs>();
+ var executor = new ProcessExecutor(fileSystemMock.Object);
+ executor.ProcessOutput += (o, e) => output.Add(e);
+ var projectName = "aProject";
+ var waitHandle = new ManualResetEvent(false);
+ ProcessResult result = null;
+ var thread = new Thread(
+ () =>
+ {
+ try
+ {
+ result = executor.Execute(info, projectName, "aTask", "C:\\somewhere.txt");
+ }
+ finally
+ {
+ waitHandle.Set();
+ }
+ });
+ thread.Start();
+ waitHandle.WaitOne(TimeSpan.FromSeconds(30));
+ CollectionAssert.IsNotEmpty(output);
+ }
+
+ [Test]
+ public void ExecuteChangesPriority()
+ {
+ var fileSystemMock = InitialiseFileSystemMockForExecute();
+ var info = new ProcessInfo("sleeper", "1", null, ProcessPriorityClass.BelowNormal);
+ var executor = new ProcessExecutor(fileSystemMock.Object);
+ var projectName = "aProject";
+ var waitHandle = new ManualResetEvent(false);
+ ProcessResult result = null;
+ var thread = new Thread(
+ () =>
+ {
+ try
+ {
+ result = executor.Execute(info, projectName, "aTask", "C:\\somewhere.txt");
+ }
+ finally
+ {
+ waitHandle.Set();
+ }
+ });
+ thread.Start();
+ waitHandle.WaitOne(TimeSpan.FromSeconds(30));
+ }
+
+ [Test]
+ public void ExecuteWritesToStdIn()
+ {
+ var fileSystemMock = InitialiseFileSystemMockForExecute();
+ var info = new ProcessInfo("sleeper", "1") {StandardInputContent = "SomeData"};
+ var executor = new ProcessExecutor(fileSystemMock.Object);
+ var projectName = "aProject";
+ var waitHandle = new ManualResetEvent(false);
+ ProcessResult result = null;
+ var thread = new Thread(
+ () =>
+ {
+ try
+ {
+ result = executor.Execute(info, projectName, "aTask", "C:\\somewhere.txt");
+ }
+ finally
+ {
+ waitHandle.Set();
+ }
+ });
+ thread.Start();
+ waitHandle.WaitOne(TimeSpan.FromSeconds(30));
+ }
+
+ [Test]
public void KillProcessesForProjectHandlesAMissingProject()
{
ProcessExecutor.KillProcessesForProject(null, "DoesNothingExist");
}
#endregion
+
+ #region Helper methods
+ private static Mock<IFileSystem> InitialiseFileSystemMockForExecute()
+ {
+ var fileSystemMock = new Mock<IFileSystem>(MockBehavior.Strict);
+ fileSystemMock.Setup(fs => fs.CheckIfFileExists(It.IsAny<string>())).Returns(true);
+ fileSystemMock.Setup(fs => fs.OpenFileForWrite("C:\\somewhere.txt")).Returns(new MemoryStream());
+ fileSystemMock.Setup(fs => fs.OpenFileForRead("C:\\somewhere.txt")).Returns(new MemoryStream());
+ return fileSystemMock;
+ }
+ #endregion
}
}

0 comments on commit 2704265

Please sign in to comment.
Something went wrong with that request. Please try again.