Skip to content

Commit

Permalink
Refactored Environment Info away from EnvironmentWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
clrudolphi committed Sep 4, 2024
1 parent eb3e257 commit 364e466
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IObjectContainer BuildRootContainer(
objectContainer.RegisterTypeAs<FileService, IFileService>();
objectContainer.RegisterTypeAs<DirectoryService, IDirectoryService>();
objectContainer.RegisterTypeAs<EnvironmentWrapper, IEnvironmentWrapper>();

objectContainer.RegisterTypeAs<EnvironmentInfoProvider, IEnvironmentInfoProvider>();
objectContainer.RegisterTypeAs<EnvironmentReqnrollTelemetryChecker, IEnvironmentReqnrollTelemetryChecker>();
objectContainer.RegisterTypeAs<AnalyticsTransmitter, IAnalyticsTransmitter>();
objectContainer.RegisterTypeAs<HttpClientAnalyticsTransmitterSink, IAnalyticsTransmitterSink>();
Expand Down
24 changes: 12 additions & 12 deletions Reqnroll/Analytics/AnalyticsEventProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ 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;
}

public ReqnrollProjectCompilingEvent CreateProjectCompilingEvent(string msbuildVersion, string assemblyName, string targetFrameworks, string targetFramework, string projectGuid)
{
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(
Expand All @@ -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(
Expand Down
69 changes: 36 additions & 33 deletions Reqnroll/CucumberMesssages/CucumberMessageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEnvironmentWrapper>();
var featureContainer = featureStartedEvent.FeatureContext.FeatureContainer;
var environmentInfoProvider = featureContainer.Resolve<IEnvironmentInfoProvider>();
var environmentWrapper = featureContainer.Resolve<IEnvironmentWrapper>();

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
{
Expand All @@ -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],
Expand All @@ -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);
Expand All @@ -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
}
}
97 changes: 97 additions & 0 deletions Reqnroll/EnvironmentAccess/EnvironmentInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> buildServerTypes
= new Dictionary<string, string> {
{ "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<string>)
return buildServerType.Value;
}
return null;
}

public bool IsRunningInDockerContainer()
{
return environmentWrapper.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") is ISuccess<string>;
}

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;
}

}
}
77 changes: 0 additions & 77 deletions Reqnroll/EnvironmentAccess/EnvironmentWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> buildServerTypes
= new Dictionary<string, string> {
{ "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<string>)
return buildServerType.Value;
}
return null;
}

public bool IsRunningInDockerContainer()
{
return GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") is ISuccess<string>;
}

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;
}

}
}
11 changes: 11 additions & 0 deletions Reqnroll/EnvironmentAccess/IEnvironmentInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Reqnroll.EnvironmentAccess
{
public interface IEnvironmentInfoProvider
{
string GetOSPlatform();
string GetBuildServerName();
bool IsRunningInDockerContainer();
string GetReqnrollVersion();
string GetNetCoreVersion();
}
}
10 changes: 0 additions & 10 deletions Reqnroll/EnvironmentAccess/IEnvironmentWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions Reqnroll/Infrastructure/DefaultDependencyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public virtual void RegisterGlobalContainerDefaults(ObjectContainer container)
container.RegisterTypeAs<ObsoleteStepHandler, IObsoleteStepHandler>();

container.RegisterTypeAs<EnvironmentWrapper, IEnvironmentWrapper>();
container.RegisterTypeAs<EnvironmentInfoProvider, IEnvironmentInfoProvider>();
container.RegisterTypeAs<BinaryFileAccessor, IBinaryFileAccessor>();
container.RegisterTypeAs<TestPendingMessageFactory, ITestPendingMessageFactory>();
container.RegisterTypeAs<TestUndefinedMessageFactory, ITestUndefinedMessageFactory>();
Expand Down
Loading

0 comments on commit 364e466

Please sign in to comment.