Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
scan gauge_additional_libs, some refactoring, fixes #58
Browse files Browse the repository at this point in the history
  • Loading branch information
sriv committed Mar 21, 2016
1 parent 77d16bc commit fea57c7
Show file tree
Hide file tree
Showing 18 changed files with 387 additions and 46 deletions.
Expand Up @@ -19,22 +19,23 @@
using System.IO;
using System.Reflection;
using Gauge.CSharp.Lib.Attribute;
using Gauge.CSharp.Runner.Wrappers;
using Moq;
using NUnit.Framework;

namespace Gauge.CSharp.Runner.UnitTests
{
[TestFixture]
public class AssemblyScannerTests
public class AssemblyLoaderTests
{
public class TestAssembly : Assembly { }

[Step("Foo text")]
public void DummyStepMethod() { }

private Mock<TestAssembly> mockAssembly;
private Mock<TestAssembly> _mockAssembly;
private MethodInfo _stepMethod;
private AssemblyScanner _assemblyScanner;
private AssemblyLoader _assemblyLoader;
private Mock<IAssemblyWrapper> _mockAssemblyWrapper;

[SetUp]
Expand All @@ -48,15 +49,15 @@ public void Setup()
_mockAssemblyWrapper = new Mock<IAssemblyWrapper>();
var fileWrapper = new Mock<IFileWrapper>();
_stepMethod = thisType.GetMethod("DummyStepMethod");
mockAssembly = new Mock<TestAssembly>();
mockAssembly.Setup(assembly => assembly.GetTypes()).Returns(new[] { thisType });
mockAssembly.Setup(assembly => assembly.GetType(thisType.FullName)).Returns(thisType);
mockAssembly.Setup(assembly => assembly.GetReferencedAssemblies()).Returns(new[] { new AssemblyName("Gauge.CSharp.Lib") });
_mockAssembly = new Mock<TestAssembly>();
_mockAssembly.Setup(assembly => assembly.GetTypes()).Returns(new[] { thisType });
_mockAssembly.Setup(assembly => assembly.GetType(thisType.FullName)).Returns(thisType);
_mockAssembly.Setup(assembly => assembly.GetReferencedAssemblies()).Returns(new[] { new AssemblyName("Gauge.CSharp.Lib") });
fileWrapper.Setup(wrapper => wrapper.Exists(libPath)).Returns(true);
_mockAssemblyWrapper.Setup(wrapper => wrapper.LoadFrom(libPath)).Returns(typeof(Step).Assembly).Verifiable();
_mockAssemblyWrapper.Setup(wrapper => wrapper.ReflectionOnlyLoadFrom(assemblyLocation)).Returns(mockAssembly.Object);
_mockAssemblyWrapper.Setup(wrapper => wrapper.LoadFrom(assemblyLocation)).Returns(mockAssembly.Object);
_assemblyScanner = new AssemblyScanner(_mockAssemblyWrapper.Object, fileWrapper.Object, new[] { assemblyLocation });
_mockAssemblyWrapper.Setup(wrapper => wrapper.ReflectionOnlyLoadFrom(assemblyLocation)).Returns(_mockAssembly.Object);
_mockAssemblyWrapper.Setup(wrapper => wrapper.LoadFrom(assemblyLocation)).Returns(_mockAssembly.Object);
_assemblyLoader = new AssemblyLoader(_mockAssemblyWrapper.Object, fileWrapper.Object, new[] { assemblyLocation });
}

[TearDown]
Expand All @@ -73,7 +74,7 @@ public void ShouldThrowExceptionWhenLibAssemblyNotFound()
var mockAssemblyWrapper = new Mock<IAssemblyWrapper>();
var fileWrapper = new Mock<IFileWrapper>();

Assert.Throws<FileNotFoundException>(() => new AssemblyScanner(mockAssemblyWrapper.Object, fileWrapper.Object, new[] { tmpLocation }));
Assert.Throws<FileNotFoundException>(() => new AssemblyLoader(mockAssemblyWrapper.Object, fileWrapper.Object, new[] { tmpLocation }));
}

[Test]
Expand All @@ -85,13 +86,13 @@ public void ShouldGetTargetAssembly()
[Test]
public void ShouldGetAssemblyReferencingGaugeLib()
{
Assert.Contains(mockAssembly.Object, _assemblyScanner.AssembliesReferencingGaugeLib);
Assert.Contains(_mockAssembly.Object, _assemblyLoader.AssembliesReferencingGaugeLib);
}

[Test]
public void ShouldGetMethodsForGaugeAttribute()
{
Assert.Contains(_stepMethod, _assemblyScanner.GetMethods(typeof(Step)));
Assert.Contains(_stepMethod, _assemblyLoader.GetMethods(typeof(Step)));
}
}
}
137 changes: 137 additions & 0 deletions Runner.UnitTests/AssemblyLocaterTests.cs
@@ -0,0 +1,137 @@
// Copyright 2015 ThoughtWorks, Inc.
//
// This file is part of Gauge-CSharp.
//
// Gauge-CSharp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gauge-CSharp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.Linq;
using Gauge.CSharp.Core;
using Gauge.CSharp.Runner.Wrappers;
using Moq;
using NUnit.Framework;

namespace Gauge.CSharp.Runner.UnitTests
{
[TestFixture]
internal class AssemblyLocaterTests
{
private readonly Mock<IDirectoryWrapper> _mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
private readonly Mock<IFileWrapper> _mockFileWrapper = new Mock<IFileWrapper>();

[SetUp]
public void Setup()
{
Environment.SetEnvironmentVariable("GAUGE_PROJECT_ROOT", Directory.GetCurrentDirectory());
}

[TearDown]
public void TearDown()
{
Environment.SetEnvironmentVariable("GAUGE_ADDITIONAL_LIBS", null);
Environment.SetEnvironmentVariable("GAUGE_PROJECT_ROOT", null);
}

[Test]
public void ShouldGetAssembliesFromGaugeBin()
{
var expectedAssemblies = new[] {"fooAssemblyLocation", "barAssemblyLocation"};
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(expectedAssemblies);
var assemblyLocater = new AssemblyLocater(_mockDirectoryWrapper.Object, _mockFileWrapper.Object);

var assemblies = assemblyLocater.GetAllAssemblies();

Assert.AreEqual(expectedAssemblies, assemblies);
}

[Test]
public void ShouldGetAssembliesFromGaugeAdditionalLibsEnvVar()
{
Environment.SetEnvironmentVariable("GAUGE_ADDITIONAL_LIBS", "foo/");
var expectedAssemblies = new[] { "fooAssemblyLocation", "barAssemblyLocation" };
_mockDirectoryWrapper.Setup(wrapper => wrapper.Exists(Path.GetFullPath("foo/"))).Returns(true);
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(Enumerable.Empty<string>());
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Path.GetFullPath("foo/"), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(expectedAssemblies);

var assemblyLocater = new AssemblyLocater(_mockDirectoryWrapper.Object, _mockFileWrapper.Object);

var assemblies = assemblyLocater.GetAllAssemblies();
Assert.AreEqual(expectedAssemblies, assemblies);
}

[Test]
public void ShouldAddAssembliyFromGaugeAdditionalLibFile()
{
Environment.SetEnvironmentVariable("GAUGE_ADDITIONAL_LIBS", "foo.dll");
var expectedAssemblies = new[] { Path.GetFullPath("foo.dll") };
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(Enumerable.Empty<string>());
_mockFileWrapper.Setup(wrapper => wrapper.Exists(Path.GetFullPath("foo.dll"))).Returns(true);
var assemblyLocater = new AssemblyLocater(_mockDirectoryWrapper.Object, _mockFileWrapper.Object);

var assemblies = assemblyLocater.GetAllAssemblies();
Assert.AreEqual(expectedAssemblies, assemblies);
}

[Test]
public void ShouldAddAssembliesFromMultipleLocations()
{
Environment.SetEnvironmentVariable("GAUGE_ADDITIONAL_LIBS", "foo.dll, foo/");
var expectedAssemblies = new[] { Path.GetFullPath("foo.dll"), "fooAssemblyLocation", "barAssemblyLocation" };
_mockDirectoryWrapper.Setup(wrapper => wrapper.Exists(Path.GetFullPath("foo/"))).Returns(true);
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(Enumerable.Empty<string>());
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Path.GetFullPath("foo/"), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(new []{"fooAssemblyLocation", "barAssemblyLocation"});
_mockFileWrapper.Setup(wrapper => wrapper.Exists(Path.GetFullPath("foo.dll"))).Returns(true);
var assemblyLocater = new AssemblyLocater(_mockDirectoryWrapper.Object, _mockFileWrapper.Object);

var assemblies = assemblyLocater.GetAllAssemblies();

Assert.AreEqual(expectedAssemblies, assemblies);
}

[Test]
public void ShoulNotdAddAssembliesFromInvalidDirectory()
{
Environment.SetEnvironmentVariable("GAUGE_ADDITIONAL_LIBS", "foo/");
_mockDirectoryWrapper.Setup(wrapper => wrapper.Exists(Path.GetFullPath("foo/"))).Returns(false);
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(Enumerable.Empty<string>());

var assemblyLocater = new AssemblyLocater(_mockDirectoryWrapper.Object, _mockFileWrapper.Object);

var assemblies = assemblyLocater.GetAllAssemblies();
Assert.IsEmpty(assemblies);
}

[Test]
public void ShouldNotAddAssembliesFromInvalidFile()
{
Environment.SetEnvironmentVariable("GAUGE_ADDITIONAL_LIBS", "foo.dll");
_mockFileWrapper.Setup(wrapper => wrapper.Exists(Path.GetFullPath("foo.dll"))).Returns(false);
_mockDirectoryWrapper.Setup(wrapper => wrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly))
.Returns(Enumerable.Empty<string>());

var assemblyLocater = new AssemblyLocater(_mockDirectoryWrapper.Object, _mockFileWrapper.Object);

var assemblies = assemblyLocater.GetAllAssemblies();
Assert.IsEmpty(assemblies);
}
}
}
6 changes: 5 additions & 1 deletion Runner.UnitTests/Gauge.CSharp.Runner.UnitTests.csproj
Expand Up @@ -75,7 +75,8 @@
<Reference Include="System.Runtime" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyScannerTests.cs" />
<Compile Include="AssemblyLocaterTests.cs" />
<Compile Include="AssemblyLoaderTests.cs" />
<Compile Include="ClassInstanceManagerTests.cs" />
<Compile Include="Converter\StringParamConverterTests.cs" />
<Compile Include="Converter\TableParamConverterTests.cs" />
Expand Down Expand Up @@ -117,6 +118,9 @@
<Name>Gauge.CSharp.Runner</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 2 additions & 2 deletions Runner.UnitTests/HookRegistryTests.cs
Expand Up @@ -38,12 +38,12 @@ public class HookRegistryTests

public ISandbox SandBox;
private HookRegistry _hookRegistry;
private Mock<IAssemblyScanner> _mockAssemblyScanner;
private Mock<IAssemblyLoader> _mockAssemblyScanner;

[SetUp]
public void Setup()
{
_mockAssemblyScanner = new Mock<IAssemblyScanner>();
_mockAssemblyScanner = new Mock<IAssemblyLoader>();
_mockAssemblyScanner.Setup(scanner => scanner.GetTargetLibAssembly()).Returns(typeof (Step).Assembly);
var types = new[]
{
Expand Down
15 changes: 9 additions & 6 deletions Runner/AssemblyScanner.cs → Runner/AssemblyLoader.cs
Expand Up @@ -24,33 +24,36 @@
using System.Linq;
using System.Reflection;
using Gauge.CSharp.Core;
using Gauge.CSharp.Runner.Wrappers;

namespace Gauge.CSharp.Runner
{
public class AssemblyScanner : IAssemblyScanner
public class AssemblyLoader : IAssemblyLoader
{
private static readonly string GaugeLibAssembleName = typeof(Step).Assembly.GetName().Name;

private static readonly Logger Logger = LogManager.GetLogger("AssemblyScanner");
private static readonly Logger Logger = LogManager.GetLogger("AssemblyLoader");

public List<Assembly> AssembliesReferencingGaugeLib = new List<Assembly>();
public List<Type> ScreengrabberTypes = new List<Type>();
public List<Assembly> AssembliesReferencingGaugeLib { get; private set; }
public List<Type> ScreengrabberTypes { get; private set; }
private Assembly _targetLibAssembly;
private readonly IAssemblyWrapper _assemblyWrapper;
private readonly IFileWrapper _fileWrapper;

public AssemblyScanner(IAssemblyWrapper assemblyWrapper, IFileWrapper fileWrapper, IEnumerable<string> assemblyLocations)
public AssemblyLoader(IAssemblyWrapper assemblyWrapper, IFileWrapper fileWrapper, IEnumerable<string> assemblyLocations)
{
_assemblyWrapper = assemblyWrapper;
_fileWrapper = fileWrapper;
LoadTargetLibAssembly();
AssembliesReferencingGaugeLib= new List<Assembly>();
ScreengrabberTypes = new List<Type>();
foreach (var location in assemblyLocations)
{
ScanAndLoad(location);
}
}

public AssemblyScanner(IEnumerable<string> assemblyLocations)
public AssemblyLoader(IEnumerable<string> assemblyLocations)
: this(new AssemblyWrapper(), new FileWrapper(), assemblyLocations)
{
}
Expand Down
80 changes: 80 additions & 0 deletions Runner/AssemblyLocater.cs
@@ -0,0 +1,80 @@
// Copyright 2015 ThoughtWorks, Inc.
//
// This file is part of Gauge-CSharp.
//
// Gauge-CSharp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gauge-CSharp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Gauge.CSharp.Core;
using Gauge.CSharp.Runner.Wrappers;
using NLog;

namespace Gauge.CSharp.Runner
{
public class AssemblyLocater : IAssemblyLocater
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IDirectoryWrapper _directoryWrapper;
private readonly IFileWrapper _fileWrapper;

public AssemblyLocater(IDirectoryWrapper directoryWrapper, IFileWrapper fileWrapper)
{
_directoryWrapper = directoryWrapper;
_fileWrapper = fileWrapper;
}

public IEnumerable<string> GetAllAssemblies()
{
var assemblies = _directoryWrapper.EnumerateFiles(Utils.GetGaugeBinDir(), "*.dll", SearchOption.TopDirectoryOnly).ToList();
var gaugeAdditionalLibsPath = Environment.GetEnvironmentVariable("gauge_additional_libs");
if (string.IsNullOrEmpty(gaugeAdditionalLibsPath))
return assemblies;

var additionalLibPaths = gaugeAdditionalLibsPath.Split(',').Select(s => Path.GetFullPath(s.Trim()));
foreach (var libPath in additionalLibPaths)
{
if (Path.HasExtension(libPath))
{
AddFile(libPath, assemblies);
continue;
}
AddFilesFromDirectory(libPath, assemblies);
}
return assemblies;
}

private void AddFilesFromDirectory(string path, List<string> assemblies)
{
if (!_directoryWrapper.Exists(path))
{
Logger.Warn("Path does not exist: {0}", path);
return;
}
assemblies.AddRange(_directoryWrapper.EnumerateFiles(path, "*.dll", SearchOption.TopDirectoryOnly));
}

private void AddFile(string path, List<string> assemblies)
{
if (!_fileWrapper.Exists(path))
{
Logger.Warn("Path does not exist: {0}", path);
return;
}
assemblies.Add(path);
}
}
}
17 changes: 17 additions & 0 deletions Runner/DefaultScreenGrabber.cs
@@ -1,3 +1,20 @@
// Copyright 2015 ThoughtWorks, Inc.
//
// This file is part of Gauge-CSharp.
//
// Gauge-CSharp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gauge-CSharp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>.

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
Expand Down

0 comments on commit fea57c7

Please sign in to comment.