Skip to content

Commit

Permalink
Refactoring GilesConfigFactory into GilesConfigBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
codereflection committed Nov 6, 2011
1 parent 066c8da commit 2cf86a5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 58 deletions.
1 change: 0 additions & 1 deletion src/Giles.Core/Configuration/GilesConfig.cs
Expand Up @@ -11,7 +11,6 @@ public class GilesConfig : INotifyPropertyChanged
private long buildDelay = 500;
public string TestAssemblyPath { get; set; }
public string SolutionPath { get; set; }
public string ProjectRoot { get; set; }

public long BuildDelay
{
Expand Down
@@ -1,25 +1,19 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Giles.Core.UI;
using Giles.Core.UI;

namespace Giles.Core.Configuration
{
public class GilesConfigFactory
public class GilesConfigBuilder
{
readonly GilesConfig config;
readonly GilesConfig config = new GilesConfig();
readonly string solutionPath;
readonly string testAssemblyPath;

public GilesConfigFactory(GilesConfig config, string solutionPath, string testAssemblyPath)
public GilesConfigBuilder(string solutionPath, string testAssemblyPath)
{
this.config = config;
this.solutionPath = solutionPath;
this.testAssemblyPath = testAssemblyPath;
}

public Func<string, Assembly> LoadAssembly;

public GilesConfig Build()
{
config.TestAssemblyPath = testAssemblyPath;
Expand Down
2 changes: 1 addition & 1 deletion src/Giles.Core/Giles.Core.csproj
Expand Up @@ -58,7 +58,7 @@
<Compile Include="AppDomains\GilesAppDomainManager.cs" />
<Compile Include="AppDomains\GilesAppDomainRunner.cs" />
<Compile Include="Configuration\GilesConfig.cs" />
<Compile Include="Configuration\GilesConfigFactory.cs" />
<Compile Include="Configuration\GilesConfigBuilder.cs" />
<Compile Include="Configuration\MsBuildProject.cs" />
<Compile Include="Configuration\RunnerAssembly.cs" />
<Compile Include="Configuration\Settings.cs" />
Expand Down
15 changes: 5 additions & 10 deletions src/Giles.Specs/Core/Configuration/GilesConfigFactorySpecs.cs
@@ -1,18 +1,16 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Giles.Core.Configuration;
using Giles.Core.IO;
using Machine.Specifications;
using Machine.Specifications.Utility;
using NSubstitute;

namespace Giles.Specs.Core.Configuration
{
[Subject(typeof(GilesConfigBuilder))]
public class a_giles_config
{
protected static GilesConfigFactory factory;
protected static GilesConfigBuilder builder;
protected static GilesConfig config;
protected static IFileSystem fileSystem;
protected static string solutionPath;
Expand All @@ -33,18 +31,15 @@ public class a_giles_config
fileSystem.GetDirectoryName(solutionPath).Returns(solutionFolder);
projectRoot = @"c:\solutionRoot";
config = new GilesConfig();
factory = new GilesConfigFactory(config, solutionPath, testAssemblyPath)
{
LoadAssembly = filename => null
};
builder = new GilesConfigBuilder(solutionPath, testAssemblyPath);
};
}

[Subject(typeof(GilesConfigBuilder))]
public class when_building : a_giles_config
{
Because of = () =>
factory.Build();
config = builder.Build();

It built_the_correct_config_test_runners = () =>
config.TestRunners.All(x => x.Value.Path == testRunnerExe).ShouldBeTrue();
Expand Down
37 changes: 20 additions & 17 deletions src/Giles.Specs/Core/Runners/TestRunnerSpecs.cs
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using Giles.Core.Configuration;
using Giles.Core.Runners;
using Machine.Specifications;
using NSubstitute;

namespace Giles.Specs.Core.Runners
{
Expand All @@ -17,23 +15,28 @@ public class with_a_test_runner
static Dictionary<string, RunnerAssembly> runners;

Establish context = () =>
{
solutionFolder = @"c:\solutionFolder";
solutionPath = @"c:\solutionFolder\mySolution.sln";
testAssemblyPath = @"c:\solutionFolder\testProject\bin\debug\testAssembly.dll";
runners = new Dictionary<string, RunnerAssembly>
{
{
"foo",
new RunnerAssembly
{Enabled = true, Options = new List<string> {"bar"}, Path = "baz"}
}
};
config = new GilesConfig()
{
solutionFolder = @"c:\solutionFolder";
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" });
config = new GilesConfig()
{
BuildDelay = 1,
ProjectRoot = solutionFolder,
SolutionPath = solutionPath,
TestAssemblyPath = testAssemblyPath,
TestRunners = runners
};
runner = new TestRunner(config);
BuildDelay = 1,
SolutionPath = solutionPath,
TestAssemblyPath = testAssemblyPath,
TestRunners = runners
};
runner = new TestRunner(config);
};
}

public class when_running_a_test_runner : with_a_test_runner
Expand Down
31 changes: 14 additions & 17 deletions src/Giles/Program.cs
Expand Up @@ -41,12 +41,12 @@ class Program {

static string GetGilesFunnyLine()
{
var assemblyName = Assembly.GetExecutingAssembly().GetName();
var name = Assembly.GetExecutingAssembly().GetName();
return string.Format(@"Grr, argh... v{0}.{1}.{2}.{3}",
assemblyName.Version.Major,
assemblyName.Version.Minor,
assemblyName.Version.Revision,
assemblyName.Version.Build);
name.Version.Major,
name.Version.Minor,
name.Version.Revision,
name.Version.Build);
}

static void SetupSourceWatcher(CLOptions options) {
Expand All @@ -55,35 +55,33 @@ static string GetGilesFunnyLine()

GetSourceWatcher(solutionPath, testAssemblyPath);

// HACK: Only *.cs files? Really?
sourceWatcher.Watch(solutionPath, @"*.cs");
}

private static string GetTestAssemblyPath(CLOptions options)
{
var testAssemblyPath = options.TestAssemblyPath != null
var path = options.TestAssemblyPath != null
? options.TestAssemblyPath.Replace("\"", string.Empty)
: FindTestAssembly(options.SolutionPath);

if (testAssemblyPath == null)
if (path == null)
{
Console.Error.Write(options.GetUsage());
Console.Error.WriteLine("No test assemblies detected. Please specify"
+ " the TestAssemblyPath command line option.");
Console.Error.WriteLine();
Environment.Exit(1);
}
return testAssemblyPath;
return path;
}

private static string FindTestAssembly(string solutionPath)
{
var testAssemblyFinder = new TestAssemblyFinder();
var testAssemblies = testAssemblyFinder.FindTestAssembliesIn(solutionPath);
var finder = new TestAssemblyFinder();
var assemblies = finder.FindTestAssembliesIn(solutionPath);

if (testAssemblies.Count() == 0)
return null;

return testAssemblies.First();
return assemblies.Count() == 0 ? null : assemblies.First();
}

static void GetSourceWatcher(string solutionPath, string testAssemblyPath) {
Expand All @@ -95,8 +93,8 @@ private static string FindTestAssembly(string solutionPath)
static StandardKernel SetupGilesKernelAndConfig(string solutionPath, string testAssemblyPath) {
var kernel = new StandardKernel(new SlayerModule(solutionPath, testAssemblyPath));

var configFactory = kernel.Get<GilesConfigFactory>();
config = configFactory.Build();
var factory = kernel.Get<GilesConfigBuilder>();
config = factory.Build();
return kernel;
}

Expand Down Expand Up @@ -192,7 +190,6 @@ static void DisplayErrors()
Console.WriteLine("\nCurrent Configuration");
Console.WriteLine(" Build Delay: " + config.BuildDelay);
Console.WriteLine(" Solution: " + config.SolutionPath);
Console.WriteLine(" Project Root: " + config.ProjectRoot);
Console.WriteLine(" Test Assembly: " + config.TestAssemblyPath);
config.TestRunners.Each(r => Console.WriteLine(" " + r.Key + " Has been enabled"));
Console.WriteLine();
Expand Down
3 changes: 1 addition & 2 deletions src/Giles/SlayerModule.cs
Expand Up @@ -24,8 +24,7 @@ public override void Load()
Bind<ITestRunner>().To<TestRunner>();
Bind<IFileWatcherFactory>().To<FileWatcherFactory>();
Bind<SourceWatcher>().ToSelf().InSingletonScope();
Bind<GilesConfig>().ToSelf().InSingletonScope();
Bind<GilesConfigFactory>().ToSelf()
Bind<GilesConfigBuilder>().ToSelf()
.WithConstructorArgument("solutionPath", solutionPath)
.WithConstructorArgument("testAssemblyPath", testAssemblyPath);
}
Expand Down

0 comments on commit 2cf86a5

Please sign in to comment.