Skip to content

Commit

Permalink
Refactoring CommandProcessExecutor to a functional design. No more in…
Browse files Browse the repository at this point in the history
…terface and it now uses static methods.
  • Loading branch information
codereflection committed May 18, 2011
1 parent d195cf0 commit 00ef04f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 35 deletions.
7 changes: 0 additions & 7 deletions src/Giles.Core/Configuration/GilesConfig.cs
Expand Up @@ -27,14 +27,7 @@ public long BuildDelay
}
}

public ICommandProcessExecutor Executor { get; set; }

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion

private void NotifyPropertyChanged(string info)
{
if (PropertyChanged == null) return;
Expand Down
2 changes: 0 additions & 2 deletions src/Giles.Core/Configuration/GilesConfigFactory.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Giles.Core.Runners;
using Giles.Core.UI;

namespace Giles.Core.Configuration
Expand All @@ -27,7 +26,6 @@ public GilesConfig Build()
config.SolutionPath = solutionPath;

config.UserDisplay = new List<IUserDisplay> {new ConsoleUserDisplay(), new GrowlUserDisplay()};
config.Executor = new CommandProcessExecutor();
return config;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Giles.Core/Runners/BuildRunner.cs
Expand Up @@ -25,7 +25,7 @@ public bool Run()
config.UserDisplay.Each(display => display.DisplayMessage("Building..."));

watch.Start();
var result = config.Executor.Execute(settings.MsBuild, config.SolutionPath);
var result = CommandProcessExecutor.Execute(settings.MsBuild, config.SolutionPath);
watch.Stop();

var message = FormatBuildMessages(watch, result);
Expand Down
28 changes: 19 additions & 9 deletions src/Giles.Core/Runners/CommandProcessExecutor.cs
@@ -1,16 +1,26 @@
using System;
using System.Diagnostics;
using System.IO;

namespace Giles.Core.Runners
{
public interface ICommandProcessExecutor
public static class CommandProcessExecutor
{
ExecutionResult Execute(string executable, string arguments);
}
static CommandProcessExecutor()
{
Execute = (fileName, arguements) => RunExecutable(fileName, arguements);
}

public class CommandProcessExecutor : ICommandProcessExecutor
{
public Process SetupProcess(string fileName, string arguments)
/// <summary>
/// Starts the executable passed with the command line arguments
/// </summary>
/// <param name="fileName">Executable to run</param>
/// <param name="arguments">Arguements to pass to the executable</param>
/// <returns>ExecutionResult</returns>
public static Func<string, string, ExecutionResult> Execute;


public static Process SetupProcess(string fileName, string arguments)
{
return new Process
{
Expand All @@ -24,10 +34,10 @@ public Process SetupProcess(string fileName, string arguments)
};
}

public ExecutionResult Execute(string executable, string arguments)
private static ExecutionResult RunExecutable(string fileName, string arguments)
{
var output = string.Empty;
var process = SetupProcess(executable, arguments);
var process = SetupProcess(fileName, arguments);
process.Start();
output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Expand All @@ -38,7 +48,7 @@ public ExecutionResult Execute(string executable, string arguments)
{
ExitCode = exitCode,
Output = output,
Runner = new FileInfo(executable).Name.ToUpper().Replace(".EXE", string.Empty)
Runner = new FileInfo(fileName).Name.ToUpper().Replace(".EXE", string.Empty)
};
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Giles.Core/Runners/TestRunner.cs
Expand Up @@ -40,8 +40,7 @@ void ExecuteRunner(KeyValuePair<string, RunnerAssembly> x)
if (x.Value.Options.Count > 0)
args += " " + x.Value.Options.Aggregate((working, next) => working + next);


var result = config.Executor.Execute(x.Value.Path, args);
var result = CommandProcessExecutor.Execute(x.Value.Path, args);

config.UserDisplay.Each(display => display.DisplayResult(result));
}
Expand Down
4 changes: 0 additions & 4 deletions src/Giles.Specs/Core/Configuration/GilesConfigFactorySpecs.cs
Expand Up @@ -57,10 +57,6 @@ public class when_building : a_giles_config

It should_configure_the_console_user_display = () =>
config.UserDisplay.Count().ShouldBeGreaterThan(0);

It should_configure_a_command_process_executor = () =>
config.Executor.ShouldNotBeNull();

}

}
4 changes: 1 addition & 3 deletions src/Giles.Specs/Core/Runners/CommandProcessExecutorSpecs.cs
Expand Up @@ -6,19 +6,17 @@ namespace Giles.Specs.Core.Runners
{
public class when_setting_up_a_process
{
static CommandProcessExecutor runner;
static Process result;
static string fileName;
static string arguments;

Establish context = () =>
{
runner = new CommandProcessExecutor();
fileName = "test.exe";
arguments = "/runtest";
};
Because of = () =>
result = runner.SetupProcess(fileName, arguments);
result = CommandProcessExecutor.SetupProcess(fileName, arguments);

It should_have_a_result = () =>
result.ShouldNotBeNull();
Expand Down
22 changes: 15 additions & 7 deletions src/Giles.Specs/Core/Runners/TestRunnerSpecs.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Giles.Core.Configuration;
using Giles.Core.Runners;
Expand All @@ -13,7 +14,6 @@ public class with_a_test_runner
protected static string solutionFolder;
protected static string testAssemblyPath;
protected static GilesConfig config;
static ICommandProcessExecutor executor;
static Dictionary<string, RunnerAssembly> runners;

Establish context = () =>
Expand All @@ -22,14 +22,11 @@ public class with_a_test_runner
solutionPath = @"c:\solutionFolder\mySolution.sln";
testAssemblyPath = @"c:\solutionFolder\testProject\bin\debug\testAssembly.dll";
runners = new Dictionary<string, RunnerAssembly>();
runners.Add("foo", new RunnerAssembly{Enabled = true, Options = new List<string>{"bar"}, Path = "baz"});
executor = Substitute.For<ICommandProcessExecutor>();
executor.Execute(Arg.Any<string>(),Arg.Any<string>()).Returns(new ExecutionResult{ExitCode = 0, Output = "poo"});
runners.Add("foo", new RunnerAssembly { Enabled = true, Options = new List<string> { "bar" }, Path = "baz" });
config = new GilesConfig()
{
BuildDelay = 1,
Executor = executor,
BuildDelay = 1,
ProjectRoot = solutionFolder,
SolutionPath = solutionPath,
TestAssemblyPath = testAssemblyPath,
Expand All @@ -41,10 +38,21 @@ public class with_a_test_runner

public class when_running_a_test_runner : with_a_test_runner
{
static bool executeReceived;

Establish context = () =>
CommandProcessExecutor.Execute = (filename, args) => ExecuteReceiver(filename, args);

static ExecutionResult ExecuteReceiver(string filename, string args)
{
executeReceived = true;
return new ExecutionResult { ExitCode = 0 };
}

Because of = () =>
runner.Run();

It should_execute_the_runner = () =>
config.Executor.Received().Execute(Arg.Any<string>(), Arg.Any<string>());
executeReceived.ShouldBeTrue();
}
}

0 comments on commit 00ef04f

Please sign in to comment.