Skip to content

Commit

Permalink
Refactoring TestAssemblyFinder's usage of FileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
codereflection committed Nov 6, 2011
1 parent 01a3c7f commit 066c8da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
25 changes: 10 additions & 15 deletions src/Giles.Core/Configuration/TestAssemblyFinder.cs
Expand Up @@ -2,21 +2,21 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Giles.Core.IO;
using Giles.Core.Runners;

namespace Giles.Core.Configuration
{
public class TestAssemblyFinder
{
private readonly IFileSystem fileSystem;
private static readonly IFileSystem FileSystem = new FileSystem();

public Func<IFileSystem> GetFileSystem = () => FileSystem;

//Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Giles.Specs", "Giles.Specs\Giles.Specs.csproj", "{8FAE1516-9D4A-4575-83BF-515BC6AF8AC3}"
private static readonly Regex SolutionFileRegex = new Regex(
@"Project\(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC\}""\)" +
@"[^""]*""[^""]*""" +
@"[^""]*""[^""]*""" +
@"[^""]*""([^""]*)""", RegexOptions.Compiled);

private static readonly string[] SupportedAssemblies
Expand All @@ -27,18 +27,13 @@ public class TestAssemblyFinder
"xunit.dll"
};

public TestAssemblyFinder(IFileSystem fileSystem)
{
this.fileSystem = fileSystem;
}

public IEnumerable<string> FindTestAssembliesIn(string solutionFilePath)
{
var projectFiles = GetProjectFilePaths(solutionFilePath);
var testAssemblies = new List<Tuple<int, string>>();
foreach (var file in projectFiles)
{
using (var stream = fileSystem.OpenFile(file, FileMode.Open,
using (var stream = GetFileSystem().OpenFile(file, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
var project = MsBuildProject.Load(stream);
Expand All @@ -61,7 +56,7 @@ public IEnumerable<string> FindTestAssembliesIn(string solutionFilePath)

private IEnumerable<String> GetProjectFilePaths(string solutionFilePath)
{
var solutionContents = fileSystem.ReadAllText(solutionFilePath);
var solutionContents = GetFileSystem().ReadAllText(solutionFilePath);
var solutionFileDir = Path.GetDirectoryName(solutionFilePath);
var match = SolutionFileRegex.Match(solutionContents);
while (match.Success)
Expand All @@ -76,10 +71,10 @@ private IEnumerable<String> GetProjectFilePaths(string solutionFilePath)
private int GetIsTestProjectScore(MsBuildProject project)
{
// TODO: may want to group this information with other info about supported test frameworks
int numberOfReferencedTestAssemblies =
SupportedAssemblies.Where( item => IsTestFrameworkReferenced( project, item ) )
var numberOfReferencedTestAssemblies =
SupportedAssemblies.Where(item => IsTestFrameworkReferenced(project, item))
.Count();
int score = numberOfReferencedTestAssemblies * 10;
var score = numberOfReferencedTestAssemblies * 10;

// no supported test framework, abort
if (score == 0)
Expand All @@ -97,7 +92,7 @@ private int GetIsTestProjectScore(MsBuildProject project)
return score;
}

private bool IsTestFrameworkReferenced(MsBuildProject project, string frameworkAssemblyFilename)
private static bool IsTestFrameworkReferenced(MsBuildProject project, string frameworkAssemblyFilename)
{
return project.GetLocalAssemblyRefs().Any(info =>
info.EndsWith(frameworkAssemblyFilename, StringComparison.OrdinalIgnoreCase));
Expand Down
Expand Up @@ -37,7 +37,10 @@ public class a_solution_with_a_test_project
.Returns(TestResources.Read(
"Giles.Specs.Core.Configuration.Resources.Giles.Specs.csproj"));
testFinder = new TestAssemblyFinder(fileSystem);
testFinder = new TestAssemblyFinder
{
GetFileSystem = () => fileSystem
};
};
}

Expand Down
9 changes: 4 additions & 5 deletions src/Giles/Program.cs
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using CommandLine;
using Giles.Core.Configuration;
using Giles.Core.IO;
using Giles.Core.Utility;
using Giles.Core.Watchers;
using Giles.Options;
Expand Down Expand Up @@ -63,7 +62,7 @@ private static string GetTestAssemblyPath(CLOptions options)
{
var testAssemblyPath = options.TestAssemblyPath != null
? options.TestAssemblyPath.Replace("\"", string.Empty)
: FindTestAssembly(options);
: FindTestAssembly(options.SolutionPath);

if (testAssemblyPath == null)
{
Expand All @@ -76,10 +75,10 @@ private static string GetTestAssemblyPath(CLOptions options)
return testAssemblyPath;
}

private static string FindTestAssembly(CLOptions options)
private static string FindTestAssembly(string solutionPath)
{
var testAssemblyFinder = new TestAssemblyFinder(new FileSystem());
var testAssemblies = testAssemblyFinder.FindTestAssembliesIn(options.SolutionPath);
var testAssemblyFinder = new TestAssemblyFinder();
var testAssemblies = testAssemblyFinder.FindTestAssembliesIn(solutionPath);

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

0 comments on commit 066c8da

Please sign in to comment.