diff --git a/Reqnroll.Tools.MsBuild.Generation/GenerateFeatureFileCodeBehindTaskContainerBuilder.cs b/Reqnroll.Tools.MsBuild.Generation/GenerateFeatureFileCodeBehindTaskContainerBuilder.cs index fafbf4afa..5ea8ba55d 100644 --- a/Reqnroll.Tools.MsBuild.Generation/GenerateFeatureFileCodeBehindTaskContainerBuilder.cs +++ b/Reqnroll.Tools.MsBuild.Generation/GenerateFeatureFileCodeBehindTaskContainerBuilder.cs @@ -40,7 +40,7 @@ public IObjectContainer BuildRootContainer( objectContainer.RegisterTypeAs(); objectContainer.RegisterTypeAs(); objectContainer.RegisterTypeAs(); - + objectContainer.RegisterTypeAs(); objectContainer.RegisterTypeAs(); objectContainer.RegisterTypeAs(); objectContainer.RegisterTypeAs(); diff --git a/Reqnroll/Analytics/AnalyticsEventProvider.cs b/Reqnroll/Analytics/AnalyticsEventProvider.cs index 301835a22..6c1f8dc07 100644 --- a/Reqnroll/Analytics/AnalyticsEventProvider.cs +++ b/Reqnroll/Analytics/AnalyticsEventProvider.cs @@ -13,13 +13,13 @@ namespace Reqnroll.Analytics public class AnalyticsEventProvider : IAnalyticsEventProvider { private readonly IUserUniqueIdStore _userUniqueIdStore; - private readonly IEnvironmentWrapper _environmentWrapper; + private readonly IEnvironmentInfoProvider _environmentInfoProvider; private readonly string _unitTestProvider; - public AnalyticsEventProvider(IUserUniqueIdStore userUniqueIdStore, UnitTestProviderConfiguration unitTestProviderConfiguration, IEnvironmentWrapper environmentWrapper) + public AnalyticsEventProvider(IUserUniqueIdStore userUniqueIdStore, UnitTestProviderConfiguration unitTestProviderConfiguration, IEnvironmentInfoProvider environmentInfoProvider) { _userUniqueIdStore = userUniqueIdStore; - _environmentWrapper = environmentWrapper; + _environmentInfoProvider = environmentInfoProvider; _unitTestProvider = unitTestProviderConfiguration.UnitTestProvider; } @@ -27,11 +27,11 @@ public ReqnrollProjectCompilingEvent CreateProjectCompilingEvent(string msbuildV { string userId = _userUniqueIdStore.GetUserId(); string unitTestProvider = _unitTestProvider; - string reqnrollVersion = _environmentWrapper.GetReqnrollVersion(); - string buildServerName = _environmentWrapper.GetBuildServerName(); - bool isDockerContainer = _environmentWrapper.IsRunningInDockerContainer(); + string reqnrollVersion = _environmentInfoProvider.GetReqnrollVersion(); + string buildServerName = _environmentInfoProvider.GetBuildServerName(); + bool isDockerContainer = _environmentInfoProvider.IsRunningInDockerContainer(); string hashedAssemblyName = ToSha256(assemblyName); - string platform = _environmentWrapper.GetOSPlatform(); + string platform = _environmentInfoProvider.GetOSPlatform(); string platformDescription = RuntimeInformation.OSDescription; var compiledEvent = new ReqnrollProjectCompilingEvent( @@ -56,13 +56,13 @@ public ReqnrollProjectRunningEvent CreateProjectRunningEvent(string testAssembly { string userId = _userUniqueIdStore.GetUserId(); string unitTestProvider = _unitTestProvider; - string reqnrollVersion = _environmentWrapper.GetReqnrollVersion(); - string targetFramework = _environmentWrapper.GetNetCoreVersion() ?? RuntimeInformation.FrameworkDescription; - bool isDockerContainer = _environmentWrapper.IsRunningInDockerContainer(); - string buildServerName = _environmentWrapper.GetBuildServerName(); + string reqnrollVersion = _environmentInfoProvider.GetReqnrollVersion(); + string targetFramework = _environmentInfoProvider.GetNetCoreVersion() ?? RuntimeInformation.FrameworkDescription; + bool isDockerContainer = _environmentInfoProvider.IsRunningInDockerContainer(); + string buildServerName = _environmentInfoProvider.GetBuildServerName(); string hashedAssemblyName = ToSha256(testAssemblyName); - string platform = _environmentWrapper.GetOSPlatform(); + string platform = _environmentInfoProvider.GetOSPlatform(); string platformDescription = RuntimeInformation.OSDescription; var runningEvent = new ReqnrollProjectRunningEvent( diff --git a/Reqnroll/CucumberMesssages/CucumberMessageFactory.cs b/Reqnroll/CucumberMesssages/CucumberMessageFactory.cs index 94699675c..3660b1364 100644 --- a/Reqnroll/CucumberMesssages/CucumberMessageFactory.cs +++ b/Reqnroll/CucumberMesssages/CucumberMessageFactory.cs @@ -254,40 +254,17 @@ private static TestStepResultStatus ToTestStepResultStatus(ScenarioExecutionStat }; } - - #region utility methods - public static string CanonicalizeStepDefinitionPattern(IStepDefinitionBinding stepDefinition) - { - string signature = GenerateSignature(stepDefinition); - - return $"{stepDefinition.SourceExpression}({signature})"; - } - - public static string CanonicalizeHookBinding(IHookBinding hookBinding) - { - string signature = GenerateSignature(hookBinding); - return $"{hookBinding.Method.Type.Name}.{hookBinding.Method.Name}({signature})"; - } - - private static string GenerateSignature(IBinding stepDefinition) - { - return stepDefinition.Method != null ? String.Join(",", stepDefinition.Method.Parameters.Select(p => p.Type.Name)) : ""; - } - public static string Base64EncodeFile(string filePath) - { - byte[] fileBytes = File.ReadAllBytes(filePath); - return Convert.ToBase64String(fileBytes); - } - public static Envelope ToMeta(FeatureStartedEvent featureStartedEvent) { - var environmentWrapper = featureStartedEvent.FeatureContext.FeatureContainer.Resolve(); + var featureContainer = featureStartedEvent.FeatureContext.FeatureContainer; + var environmentInfoProvider = featureContainer.Resolve(); + var environmentWrapper = featureContainer.Resolve(); - var implementation = new Product("Reqnroll", environmentWrapper.GetReqnrollVersion()); - string targetFramework = environmentWrapper.GetNetCoreVersion() ?? RuntimeInformation.FrameworkDescription; + var implementation = new Product("Reqnroll", environmentInfoProvider.GetReqnrollVersion()); + string targetFramework = environmentInfoProvider.GetNetCoreVersion() ?? RuntimeInformation.FrameworkDescription; var runTime = new Product("dotNet", targetFramework); - var os = new Product(environmentWrapper.GetOSPlatform(), RuntimeInformation.OSDescription); + var os = new Product(environmentInfoProvider.GetOSPlatform(), RuntimeInformation.OSDescription); var cpu = RuntimeInformation.ProcessArchitecture switch { @@ -298,9 +275,9 @@ public static Envelope ToMeta(FeatureStartedEvent featureStartedEvent) _ => new Product(null, null), }; - var ci_name = environmentWrapper.GetBuildServerName(); + var ci_name = environmentInfoProvider.GetBuildServerName(); - var ci = ToCi(ci_name, environmentWrapper); + var ci = ToCi(ci_name, environmentInfoProvider, environmentWrapper); return Envelope.Create(new Meta( (Cucumber.Messages.ProtocolVersion.Version).Split('+')[0], @@ -311,12 +288,12 @@ public static Envelope ToMeta(FeatureStartedEvent featureStartedEvent) ci)); } - private static Ci ToCi(string ci_name, IEnvironmentWrapper environmentWrapper) + private static Ci ToCi(string ci_name, IEnvironmentInfoProvider environmentInfoProvider, IEnvironmentWrapper environmentWrapper) { //TODO: Find a way to abstract how various CI systems convey links to builds and build numbers. // Until then, these will be hard coded as null if (String.IsNullOrEmpty(ci_name)) return null; - + var git = ToGit(environmentWrapper); return new Ci(ci_name, null, null, git); @@ -340,6 +317,32 @@ private static Git ToGit(IEnvironmentWrapper environmentWrapper) ); return git; } + + #region utility methods + public static string CanonicalizeStepDefinitionPattern(IStepDefinitionBinding stepDefinition) + { + string signature = GenerateSignature(stepDefinition); + + return $"{stepDefinition.SourceExpression}({signature})"; + } + + public static string CanonicalizeHookBinding(IHookBinding hookBinding) + { + string signature = GenerateSignature(hookBinding); + return $"{hookBinding.Method.Type.Name}.{hookBinding.Method.Name}({signature})"; + } + + private static string GenerateSignature(IBinding stepDefinition) + { + return stepDefinition.Method != null ? String.Join(",", stepDefinition.Method.Parameters.Select(p => p.Type.Name)) : ""; + } + public static string Base64EncodeFile(string filePath) + { + byte[] fileBytes = File.ReadAllBytes(filePath); + return Convert.ToBase64String(fileBytes); + } + + #endregion } } \ No newline at end of file diff --git a/Reqnroll/EnvironmentAccess/EnvironmentInfoProvider.cs b/Reqnroll/EnvironmentAccess/EnvironmentInfoProvider.cs new file mode 100644 index 000000000..6a9a1cc50 --- /dev/null +++ b/Reqnroll/EnvironmentAccess/EnvironmentInfoProvider.cs @@ -0,0 +1,97 @@ +using Reqnroll.CommonModels; +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Text; + +namespace Reqnroll.EnvironmentAccess +{ + public class EnvironmentInfoProvider : IEnvironmentInfoProvider + { + private readonly IEnvironmentWrapper environmentWrapper; + + public EnvironmentInfoProvider(IEnvironmentWrapper environmentWrapper) + { + this.environmentWrapper = environmentWrapper; + } + + public string GetOSPlatform() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return "Windows"; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return "Linux"; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return "OSX"; + } + + throw new InvalidOperationException("Platform cannot be identified"); + } + + private readonly Dictionary buildServerTypes + = new Dictionary { + { "TF_BUILD","Azure Pipelines"}, + { "TEAMCITY_VERSION","TeamCity"}, + { "JENKINS_HOME","Jenkins"}, + { "GITHUB_ACTIONS","GitHub Actions"}, + { "GITLAB_CI","GitLab CI/CD"}, + { "CODEBUILD_BUILD_ID","AWS CodeBuild"}, + { "TRAVIS","Travis CI"}, + { "APPVEYOR","AppVeyor"}, + { "BITBUCKET_BUILD_NUMBER", "Bitbucket Pipelines" }, + { "bamboo_agentId", "Atlassian Bamboo" }, + { "CIRCLECI", "CircleCI" }, + { "GO_PIPELINE_NAME", "GoCD" }, + { "BUDDY", "Buddy" }, + { "NEVERCODE", "Nevercode" }, + { "SEMAPHORE", "SEMAPHORE" }, + { "BROWSERSTACK_USERNAME", "BrowserStack" }, + { "CF_BUILD_ID", "Codefresh" }, + { "TentacleVersion", "Octopus Deploy" }, + + { "CI_NAME", "CodeShip" } + }; + + public string GetBuildServerName() + { + foreach (var buildServerType in buildServerTypes) + { + var envVariable = environmentWrapper.GetEnvironmentVariable(buildServerType.Key); + if (envVariable is ISuccess) + return buildServerType.Value; + } + return null; + } + + public bool IsRunningInDockerContainer() + { + return environmentWrapper.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") is ISuccess; + } + + public string GetReqnrollVersion() + { + return VersionInfo.AssemblyInformationalVersion; + } + public string GetNetCoreVersion() + { + var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly; + var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); + int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App"); + if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2) + { + return assemblyPath[netCoreAppIndex + 1]; + } + + return null; + } + + } +} diff --git a/Reqnroll/EnvironmentAccess/EnvironmentWrapper.cs b/Reqnroll/EnvironmentAccess/EnvironmentWrapper.cs index 6bfd79e74..40d9f0ef3 100644 --- a/Reqnroll/EnvironmentAccess/EnvironmentWrapper.cs +++ b/Reqnroll/EnvironmentAccess/EnvironmentWrapper.cs @@ -40,82 +40,5 @@ public void SetEnvironmentVariable(string name, string value) public string GetCurrentDirectory() => Environment.CurrentDirectory; - public string GetOSPlatform() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return "Windows"; - } - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - return "Linux"; - } - - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - return "OSX"; - } - - throw new InvalidOperationException("Platform cannot be identified"); - } - - private readonly Dictionary buildServerTypes - = new Dictionary { - { "TF_BUILD","Azure Pipelines"}, - { "TEAMCITY_VERSION","TeamCity"}, - { "JENKINS_HOME","Jenkins"}, - { "GITHUB_ACTIONS","GitHub Actions"}, - { "GITLAB_CI","GitLab CI/CD"}, - { "CODEBUILD_BUILD_ID","AWS CodeBuild"}, - { "TRAVIS","Travis CI"}, - { "APPVEYOR","AppVeyor"}, - { "BITBUCKET_BUILD_NUMBER", "Bitbucket Pipelines" }, - { "bamboo_agentId", "Atlassian Bamboo" }, - { "CIRCLECI", "CircleCI" }, - { "GO_PIPELINE_NAME", "GoCD" }, - { "BUDDY", "Buddy" }, - { "NEVERCODE", "Nevercode" }, - { "SEMAPHORE", "SEMAPHORE" }, - { "BROWSERSTACK_USERNAME", "BrowserStack" }, - { "CF_BUILD_ID", "Codefresh" }, - { "TentacleVersion", "Octopus Deploy" }, - - { "CI_NAME", "CodeShip" } - }; - - public string GetBuildServerName() - { - foreach (var buildServerType in buildServerTypes) - { - var envVariable = GetEnvironmentVariable(buildServerType.Key); - if (envVariable is ISuccess) - return buildServerType.Value; - } - return null; - } - - public bool IsRunningInDockerContainer() - { - return GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") is ISuccess; - } - - public string GetReqnrollVersion() - { - return VersionInfo.AssemblyInformationalVersion; - } - public string GetNetCoreVersion() - { - var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly; - var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); - int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App"); - if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2) - { - return assemblyPath[netCoreAppIndex + 1]; - } - - return null; - } - } } diff --git a/Reqnroll/EnvironmentAccess/IEnvironmentInfoProvider.cs b/Reqnroll/EnvironmentAccess/IEnvironmentInfoProvider.cs new file mode 100644 index 000000000..562372ec4 --- /dev/null +++ b/Reqnroll/EnvironmentAccess/IEnvironmentInfoProvider.cs @@ -0,0 +1,11 @@ +namespace Reqnroll.EnvironmentAccess +{ + public interface IEnvironmentInfoProvider + { + string GetOSPlatform(); + string GetBuildServerName(); + bool IsRunningInDockerContainer(); + string GetReqnrollVersion(); + string GetNetCoreVersion(); + } +} \ No newline at end of file diff --git a/Reqnroll/EnvironmentAccess/IEnvironmentWrapper.cs b/Reqnroll/EnvironmentAccess/IEnvironmentWrapper.cs index d6edfd6bc..5e88ba8bc 100644 --- a/Reqnroll/EnvironmentAccess/IEnvironmentWrapper.cs +++ b/Reqnroll/EnvironmentAccess/IEnvironmentWrapper.cs @@ -13,15 +13,5 @@ public interface IEnvironmentWrapper void SetEnvironmentVariable(string name, string value); string GetCurrentDirectory(); - - string GetOSPlatform(); - - string GetBuildServerName(); - - bool IsRunningInDockerContainer(); - - string GetReqnrollVersion(); - - string GetNetCoreVersion(); } } diff --git a/Reqnroll/Infrastructure/DefaultDependencyProvider.cs b/Reqnroll/Infrastructure/DefaultDependencyProvider.cs index dc005ee67..d75a85f69 100644 --- a/Reqnroll/Infrastructure/DefaultDependencyProvider.cs +++ b/Reqnroll/Infrastructure/DefaultDependencyProvider.cs @@ -71,6 +71,7 @@ public virtual void RegisterGlobalContainerDefaults(ObjectContainer container) container.RegisterTypeAs(); container.RegisterTypeAs(); + container.RegisterTypeAs(); container.RegisterTypeAs(); container.RegisterTypeAs(); container.RegisterTypeAs(); diff --git a/Tests/Reqnroll.RuntimeTests/Analytics/AnalyticsEventProviderTests.cs b/Tests/Reqnroll.RuntimeTests/Analytics/AnalyticsEventProviderTests.cs index 0d28c52c0..33e26f385 100644 --- a/Tests/Reqnroll.RuntimeTests/Analytics/AnalyticsEventProviderTests.cs +++ b/Tests/Reqnroll.RuntimeTests/Analytics/AnalyticsEventProviderTests.cs @@ -20,7 +20,9 @@ public void Should_return_the_build_server_name_in_Compiling_Event() { var userUniqueIdStoreMock = new Mock(); var environmentMock = new Mock(); - var sut = new AnalyticsEventProvider(userUniqueIdStoreMock.Object, new UnitTestProvider.UnitTestProviderConfiguration(), environmentMock.Object); + var environmentInfoMock = new Mock(environmentMock.Object); + + var sut = new AnalyticsEventProvider(userUniqueIdStoreMock.Object, new UnitTestProvider.UnitTestProviderConfiguration(), environmentInfoMock.Object); environmentMock .Setup(m => m.GetEnvironmentVariable("TF_BUILD")) @@ -36,7 +38,9 @@ public void Should_return_the_build_server_name_in_Running_Event() { var userUniqueIdStoreMock = new Mock(); var environmentMock = new Mock(); - var sut = new AnalyticsEventProvider(userUniqueIdStoreMock.Object, new UnitTestProvider.UnitTestProviderConfiguration(), environmentMock.Object); + var environmentInfoMock = new Mock(environmentMock.Object); + + var sut = new AnalyticsEventProvider(userUniqueIdStoreMock.Object, new UnitTestProvider.UnitTestProviderConfiguration(), environmentInfoMock.Object); environmentMock .Setup(m => m.GetEnvironmentVariable("TEAMCITY_VERSION")) @@ -52,7 +56,9 @@ public void Should_return_null_for_the_build_server_name_when_not_detected() { var userUniqueIdStoreMock = new Mock(); var environmentMock = new Mock(); - var sut = new AnalyticsEventProvider(userUniqueIdStoreMock.Object, new UnitTestProvider.UnitTestProviderConfiguration(), environmentMock.Object); + var environmentInfoMock = new Mock(environmentMock.Object); + + var sut = new AnalyticsEventProvider(userUniqueIdStoreMock.Object, new UnitTestProvider.UnitTestProviderConfiguration(), environmentInfoMock.Object); var compilingEvent = sut.CreateProjectRunningEvent(null);