Skip to content

Commit

Permalink
Introduce SpecFlowJsonLocator (#2069)
Browse files Browse the repository at this point in the history
* Introduce SpecFlowJsonLocator

* Create interface for SpecFlowJsonLocator

- Adjust callers to use DI

* Adapt tests for DI
  • Loading branch information
epresi committed Jul 27, 2020
1 parent bec8793 commit 9a347a6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using TechTalk.SpecFlow.Analytics;
using TechTalk.SpecFlow.Analytics.AppInsights;
using TechTalk.SpecFlow.Analytics.UserId;
using TechTalk.SpecFlow.Configuration;
using TechTalk.SpecFlow.EnvironmentAccess;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.Generator.Project;

namespace SpecFlow.Tools.MsBuild.Generation
Expand Down Expand Up @@ -45,6 +47,10 @@ public class GenerateFeatureFileCodeBehindTaskContainerBuilder
objectContainer.RegisterTypeAs<AppInsightsEventSerializer, IAppInsightsEventSerializer>();
objectContainer.RegisterTypeAs<HttpClientWrapper, HttpClientWrapper>();
objectContainer.RegisterTypeAs<AnalyticsEventProvider, IAnalyticsEventProvider>();
objectContainer.RegisterTypeAs<ConfigurationLoader, IConfigurationLoader>();
objectContainer.RegisterTypeAs<GeneratorConfigurationProvider, IGeneratorConfigurationProvider>();
objectContainer.RegisterTypeAs<ProjectReader, ISpecFlowProjectReader>();
objectContainer.RegisterTypeAs<SpecFlowJsonLocator, ISpecFlowJsonLocator>();

if (generateFeatureFileCodeBehindTaskConfiguration.OverrideAnalyticsTransmitter is null)
{
Expand Down
2 changes: 2 additions & 0 deletions TechTalk.SpecFlow.Generator/DefaultDependencyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public virtual void RegisterDefaults(ObjectContainer container)
container.RegisterTypeAs<ConfigurationLoader, IConfigurationLoader>();

container.RegisterTypeAs<SpecFlowGherkinParserFactory, IGherkinParserFactory>();

container.RegisterTypeAs<SpecFlowJsonLocator, ISpecFlowJsonLocator>();

RegisterUnitTestGeneratorProviders(container);
}
Expand Down
14 changes: 8 additions & 6 deletions TechTalk.SpecFlow.Generator/Project/MsBuildProjectReader.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using TechTalk.SpecFlow.Configuration;
using TechTalk.SpecFlow.Generator.Configuration;

namespace TechTalk.SpecFlow.Generator.Project
{
public class MSBuildProjectReader : IMSBuildProjectReader
{
private readonly ISpecFlowProjectReader _projectReader;

public MSBuildProjectReader(ISpecFlowProjectReader projectReader)
{
_projectReader = projectReader;
}

public SpecFlowProject LoadSpecFlowProjectFromMsBuild(string projectFilePath, string rootNamespace)
{
var configurationProvider = new GeneratorConfigurationProvider(new ConfigurationLoader());
var projectReader = new ProjectReader(configurationProvider, new ProjectLanguageReader());
return projectReader.ReadSpecFlowProject(projectFilePath, rootNamespace);
return _projectReader.ReadSpecFlowProject(projectFilePath, rootNamespace);
}
}
}
42 changes: 6 additions & 36 deletions TechTalk.SpecFlow/Configuration/ConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ namespace TechTalk.SpecFlow.Configuration
{
public class ConfigurationLoader : IConfigurationLoader
{
private const string JsonConfigurationFileName = "specflow.json";

private readonly AppConfigConfigurationLoader _appConfigConfigurationLoader;
//private readonly ObjectContainer _objectContainer;
private readonly JsonConfigurationLoader _jsonConfigurationLoader;
private readonly ISpecFlowJsonLocator _specFlowJsonLocator;


public ConfigurationLoader()
public ConfigurationLoader(ISpecFlowJsonLocator specFlowJsonLocator)
{
_specFlowJsonLocator = specFlowJsonLocator;
_jsonConfigurationLoader = new JsonConfigurationLoader();
_appConfigConfigurationLoader = new AppConfigConfigurationLoader();
}
Expand Down Expand Up @@ -63,9 +62,7 @@ public bool HasJsonConfig
{
get
{
var specflowJsonFile = GetSpecflowJsonFilePath();


var specflowJsonFile = _specFlowJsonLocator.GetSpecFlowJsonFilePath();
return File.Exists(specflowJsonFile);
}
}
Expand Down Expand Up @@ -164,7 +161,7 @@ private SpecFlowConfiguration LoadAppConfig(SpecFlowConfiguration specFlowConfig

private SpecFlowConfiguration LoadJson(SpecFlowConfiguration specFlowConfiguration)
{
var jsonContent = File.ReadAllText(GetSpecflowJsonFilePath());
var jsonContent = File.ReadAllText(_specFlowJsonLocator.GetSpecFlowJsonFilePath());

return LoadJson(specFlowConfiguration, jsonContent);
}
Expand All @@ -174,33 +171,6 @@ private SpecFlowConfiguration LoadJson(SpecFlowConfiguration specFlowConfigurati
return _jsonConfigurationLoader.LoadJson(specFlowConfiguration, jsonContent);
}

private string GetSpecflowJsonFilePath()
{
var specflowJsonFileInAppDomainBaseDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, JsonConfigurationFileName);

if (File.Exists(specflowJsonFileInAppDomainBaseDirectory))
{
return specflowJsonFileInAppDomainBaseDirectory;
}

var specflowJsonFileTwoDirectoriesUp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", JsonConfigurationFileName);

if (File.Exists(specflowJsonFileTwoDirectoriesUp))
{
return specflowJsonFileTwoDirectoriesUp;
}

var specflowJsonFileInCurrentDirectory = Path.Combine(Environment.CurrentDirectory, JsonConfigurationFileName);

if (File.Exists(specflowJsonFileInCurrentDirectory))
{
return specflowJsonFileInCurrentDirectory;
}




return null;
}

}
}
7 changes: 7 additions & 0 deletions TechTalk.SpecFlow/Configuration/ISpecFlowJsonLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TechTalk.SpecFlow.Configuration
{
public interface ISpecFlowJsonLocator
{
string GetSpecFlowJsonFilePath();
}
}
36 changes: 36 additions & 0 deletions TechTalk.SpecFlow/Configuration/SpecFlowJsonLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.IO;

namespace TechTalk.SpecFlow.Configuration
{
public class SpecFlowJsonLocator : ISpecFlowJsonLocator
{
public const string JsonConfigurationFileName = "specflow.json";

public string GetSpecFlowJsonFilePath()
{
var specflowJsonFileInAppDomainBaseDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, JsonConfigurationFileName);

if (File.Exists(specflowJsonFileInAppDomainBaseDirectory))
{
return specflowJsonFileInAppDomainBaseDirectory;
}

var specflowJsonFileTwoDirectoriesUp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", JsonConfigurationFileName);

if (File.Exists(specflowJsonFileTwoDirectoriesUp))
{
return specflowJsonFileTwoDirectoriesUp;
}

var specflowJsonFileInCurrentDirectory = Path.Combine(Environment.CurrentDirectory, JsonConfigurationFileName);

if (File.Exists(specflowJsonFileInCurrentDirectory))
{
return specflowJsonFileInCurrentDirectory;
}

return null;
}
}
}
2 changes: 2 additions & 0 deletions TechTalk.SpecFlow/Infrastructure/DefaultDependencyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public virtual void RegisterGlobalContainerDefaults(ObjectContainer container)
container.RegisterTypeAs<AppInsightsEventSerializer, IAppInsightsEventSerializer>();
container.RegisterTypeAs<HttpClientWrapper, HttpClientWrapper>();
container.RegisterTypeAs<AnalyticsEventProvider, IAnalyticsEventProvider>();

container.RegisterTypeAs<SpecFlowJsonLocator, ISpecFlowJsonLocator>();
}

public virtual void RegisterTestThreadContainerDefaults(ObjectContainer testThreadContainer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.IO;
using FluentAssertions;
using Moq;
using TechTalk.SpecFlow.Configuration;
using Xunit;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.Generator.Project;
using TechTalk.SpecFlow.Generator.Helpers;

Expand All @@ -14,8 +17,15 @@ private void Should_parse_csproj_file_correctly(string csprojPath, string langua
{
string directoryName = Path.GetDirectoryName(new Uri(GetType().Assembly.CodeBase).LocalPath);
string projectFilePath = Path.Combine(directoryName, csprojPath);
var specFlowProjectFile = new MSBuildProjectReader().LoadSpecFlowProjectFromMsBuild(projectFilePath, rootNamespace);

var specFlowJsonLocatorMock = new Mock<ISpecFlowJsonLocator>();

var configurationLoader = new ConfigurationLoader(specFlowJsonLocatorMock.Object);
var generatorConfigurationProvider = new GeneratorConfigurationProvider(configurationLoader);
var projectLanguageReader = new ProjectLanguageReader();
var reader = new ProjectReader(generatorConfigurationProvider, projectLanguageReader);

var specFlowProjectFile = reader.ReadSpecFlowProject(projectFilePath, rootNamespace);

specFlowProjectFile.ProjectSettings.DefaultNamespace.Should().Be(rootNamespace);
specFlowProjectFile.ProjectSettings.ProjectName.Should().Be(projectName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using BoDi;
using FluentAssertions;
using Moq;
using Xunit;
using TechTalk.SpecFlow.BindingSkeletons;
using TechTalk.SpecFlow.Configuration;
Expand All @@ -20,8 +21,10 @@ public class AppConfigTests
[Fact]
public void CanLoadConfigFromConfigFile()
{
var specFlowJsonLocatorMock = new Mock<ISpecFlowJsonLocator>();

var runtimeConfiguration = ConfigurationLoader.GetDefault();
var configurationLoader = new ConfigurationLoader();
var configurationLoader = new ConfigurationLoader(specFlowJsonLocatorMock.Object);

runtimeConfiguration = configurationLoader.Load(runtimeConfiguration);
}
Expand Down

0 comments on commit 9a347a6

Please sign in to comment.