Skip to content

Commit

Permalink
remove read of csproj/static loading when GAUGE_CUSTOM_BUILD_PATH is …
Browse files Browse the repository at this point in the history
…set (#120)

* do not static load when GAUGE_CUSTOM_BUILD_PATH is set, fixes getgauge/gauge#1739

Signed-off-by: sriv <srikanth.ddit@gmail.com>

* bump up version -> 0.2.0

Signed-off-by: sriv <srikanth.ddit@gmail.com>
  • Loading branch information
sriv authored Sep 24, 2020
1 parent b382f90 commit 4ec516e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Gauge.Dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>netcoreapp3.0</TargetFramework>
<PackageId>Runner.NetCore30</PackageId>
<Authors>The Gauge Team</Authors>
<Version>0.1.9</Version>
<Version>0.2.0</Version>
<Company>ThoughtWorks Inc.</Company>
<Product>Gauge</Product>
<Description>C# runner for Gauge. https://gauge.org</Description>
Expand Down
4 changes: 3 additions & 1 deletion src/GaugeCommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*----------------------------------------------------------------*/


using Gauge.Dotnet.Wrappers;

namespace Gauge.Dotnet
{
public class GaugeCommandFactory
Expand All @@ -18,7 +20,7 @@ public static IGaugeCommand GetExecutor(string phase)
default:
return new StartCommand(() =>
{
var loader = new StaticLoader(new System.Lazy<IAttributesLoader>(() => new AttributesLoader()));
var loader = new StaticLoader(new AttributesLoader(), new DirectoryWrapper());
loader.LoadImplementations();
return new GaugeListener(loader);
},
Expand Down
9 changes: 5 additions & 4 deletions src/RunnerServiceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ public override Task<ImplementationFileGlobPatternResponse> GetGlobPatterns(Empt

public override Task<ImplementationFileListResponse> GetImplementationFiles(Empty request, ServerCallContext context)
{
var response = new ImplementationFileListResponse();
response.ImplementationFilePaths.AddRange(FileHelper.GetImplementationFiles());
return _pool.Execute(DefaultExecutionStream, () => response);

return _pool.Execute(DefaultExecutionStream,() => {
var response = new ImplementationFileListResponse();
response.ImplementationFilePaths.AddRange(FileHelper.GetImplementationFiles());
return response;
});
}

public override Task<StepNameResponse> GetStepName(StepNameRequest request, ServerCallContext context)
Expand Down
26 changes: 20 additions & 6 deletions src/StaticLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
using Gauge.Dotnet.Extensions;
using Gauge.Dotnet.Helpers;
using Gauge.Dotnet.Models;
using Gauge.Dotnet.Wrappers;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Gauge.Dotnet
{
public sealed class StaticLoader : IStaticLoader
{
private readonly Lazy<IAttributesLoader> _attributesLoader;
private readonly IAttributesLoader _attributesLoader;
private readonly IDirectoryWrapper _directoryWrapper;
private readonly IStepRegistry _stepRegistry;


public StaticLoader(Lazy<IAttributesLoader> attributesLoader)
public StaticLoader(IAttributesLoader attributesLoader, IDirectoryWrapper directoryWrapper)
{
_stepRegistry = new StepRegistry();
_attributesLoader = attributesLoader;
_directoryWrapper = directoryWrapper;
}

public IStepRegistry GetStepRegistry()
Expand Down Expand Up @@ -57,7 +60,7 @@ public void RemoveSteps(string file)

private bool IsFileRemoved(string file)
{
var attributes = _attributesLoader.Value.GetRemovedAttributes();
var attributes = _attributesLoader.GetRemovedAttributes();
var removedFiles = FileHelper.GetRemovedDirFiles();

var isFileRemoved =
Expand All @@ -68,14 +71,25 @@ private bool IsFileRemoved(string file)

internal void LoadImplementations()
{
var classFiles = Directory.EnumerateFiles(Utils.GaugeProjectRoot, "*.cs",
if (!string.IsNullOrEmpty(Utils.TryReadEnvValue("GAUGE_CUSTOM_BUILD_PATH")))
{
Logger.Debug("GAUGE_CUSTOM_BUILD_PATH is set, skipping static loading");
return;
}

var classFiles = _directoryWrapper.EnumerateFiles(Utils.GaugeProjectRoot, "*.cs",
SearchOption.AllDirectories).ToList();
var attributes = _attributesLoader.Value.GetRemovedAttributes();
var attributes = _attributesLoader.GetRemovedAttributes();
foreach (var attribute in attributes)
{
classFiles.Remove(Path.Combine(Utils.GaugeProjectRoot, attribute.Value));
}
var removedFiles = FileHelper.GetRemovedDirFiles();
var wantedFiles = classFiles.Except(removedFiles);
foreach (var f in wantedFiles) LoadStepsFromText(File.ReadAllText(f), f);
foreach (var f in wantedFiles)
{
LoadStepsFromText(File.ReadAllText(f), f);
}
}

private void AddStepsToRegistry(string fileName, IEnumerable<MethodDeclarationSyntax> stepMethods)
Expand Down
7 changes: 5 additions & 2 deletions test/Processors/CacheFileProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Xml.Linq;
using Gauge.Dotnet.Processors;
using Gauge.Dotnet.Wrappers;
using Gauge.Messages;
using Moq;
using NUnit.Framework;
Expand All @@ -22,7 +23,8 @@ public void ShouldProcessMessage()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);

var processor = new CacheFileProcessor(loader);
var request = new CacheFileRequest
Expand Down Expand Up @@ -52,7 +54,8 @@ public void ShouldProcessRequestWithDeleteStatus()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string content = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
"{\n" +
Expand Down
7 changes: 5 additions & 2 deletions test/Processors/StepPositionsProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Xml.Linq;
using Gauge.Dotnet.Processors;
using Gauge.Dotnet.Wrappers;
using Gauge.Messages;
using Moq;
using NUnit.Framework;
Expand All @@ -23,7 +24,8 @@ public void ShouldProcessRequest()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string content = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
"{\n" +
Expand Down Expand Up @@ -54,7 +56,8 @@ public void ShouldProcessRequestForAliasSteps()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string content = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
"{\n" +
Expand Down
52 changes: 46 additions & 6 deletions test/StaticLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Xml.Linq;
using Gauge.CSharp.Core;
using Gauge.Dotnet.Wrappers;
using Moq;
using NUnit.Framework;

Expand All @@ -24,7 +25,8 @@ public void ShouldAddAliasesSteps()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string text = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
"{\n" +
Expand All @@ -50,7 +52,8 @@ public void ShouldAddStepsFromGivenContent()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);

const string text = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
Expand All @@ -73,7 +76,8 @@ public void ShouldLoadStepsWithPositoin()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string file1 = @"Foo.cs";

const string text = "using Gauge.CSharp.Lib.Attributes;\n" +
Expand Down Expand Up @@ -121,7 +125,8 @@ public void ShouldNotReloadStepOfRemovedFile()
var list = new List<XAttribute>();
list.AddRange(attributes);
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(list);
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);

const string text = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
Expand All @@ -146,7 +151,8 @@ public void ShouldReloadSteps()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string text = "using Gauge.CSharp.Lib.Attributes;\n" +
"namespace foobar\n" +
"{\n" +
Expand Down Expand Up @@ -188,7 +194,8 @@ public void ShouldRemoveSteps()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Returns(new List<XAttribute>());
var loader = new StaticLoader(new Lazy<IAttributesLoader>(() => mockAttributesLoader.Object));
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);
const string file1 = @"Foo.cs";

const string text = "using Gauge.CSharp.Lib.Attributes;\n" +
Expand Down Expand Up @@ -227,5 +234,38 @@ public void ShouldRemoveSteps()

Assert.False(loader.GetStepRegistry().ContainsStep("hola"));
}

public class LoadImplementationsTest
{
private const string GaugeCustomBuildPathEnv = "GAUGE_CUSTOM_BUILD_PATH";
private string old;

[SetUp]
public void Setup()
{
old = Utils.TryReadEnvValue(GaugeCustomBuildPathEnv);
Environment.SetEnvironmentVariable(GaugeCustomBuildPathEnv, "foo");
}

[Test]
public void ShouldNotLoadWhenCustomBuildPathIsSet()
{
var mockAttributesLoader = new Mock<IAttributesLoader>();
mockAttributesLoader.Setup(x => x.GetRemovedAttributes()).Verifiable();
var mockDirectoryWrapper = new Mock<IDirectoryWrapper>();
mockDirectoryWrapper.Setup(x => x.EnumerateFiles(It.IsAny<string>(), "*.cs", SearchOption.AllDirectories));

var loader = new StaticLoader(mockAttributesLoader.Object, mockDirectoryWrapper.Object);

mockAttributesLoader.Verify(x => x.GetRemovedAttributes(), Times.Never());
Environment.SetEnvironmentVariable("GAUGE_CUSTOM_BUILD_PATH", old);
}

[TearDown]
public void TearDown()
{
Environment.SetEnvironmentVariable(GaugeCustomBuildPathEnv, old);
}
}
}
}

0 comments on commit 4ec516e

Please sign in to comment.