diff --git a/Source/Codecov.Tests/Codecov.Tests.csproj b/Source/Codecov.Tests/Codecov.Tests.csproj index 03415fd..ecf960c 100644 --- a/Source/Codecov.Tests/Codecov.Tests.csproj +++ b/Source/Codecov.Tests/Codecov.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/Source/Codecov.Tests/Coverage/EnviornmentVariables/EnviornmentVariablesTests.cs b/Source/Codecov.Tests/Coverage/EnviornmentVariables/EnviornmentVariablesTests.cs index 6d25cbe..4cc562c 100644 --- a/Source/Codecov.Tests/Coverage/EnviornmentVariables/EnviornmentVariablesTests.cs +++ b/Source/Codecov.Tests/Coverage/EnviornmentVariables/EnviornmentVariablesTests.cs @@ -19,7 +19,7 @@ public void GetEnviornmentVariables_Should_Never_Be_Null() var enviornmentVariables = new Codecov.Coverage.EnviornmentVariables.EnviornmentVariables(options, continuousIntegrationServer); // When - var getEnviornmentVariables = enviornmentVariables.GetEnviornmentVariables; + var getEnviornmentVariables = enviornmentVariables.UserEnvironmentVariables; // Then getEnviornmentVariables.Should().NotBeNull(); @@ -35,7 +35,7 @@ public void Should_Include_CODECOV_ENV() Environment.SetEnvironmentVariable("CODECOV_ENV", "foo"); // When - var getEnviornmentVariables = enviornmentVariables.GetEnviornmentVariables; + var getEnviornmentVariables = enviornmentVariables.UserEnvironmentVariables; // Then getEnviornmentVariables["CODECOV_ENV"].Should().Be("foo"); @@ -47,13 +47,13 @@ public void Should_Include_EnviornmentVariables_From_ContiniousIntegrationServer // Given var options = Substitute.For(); var continuousIntegrationServer = Substitute.For(); - continuousIntegrationServer.GetEnviornmentVariables.Returns(new Dictionary { { "foo", "bar" }, { "fizz", "bizz" } }); + continuousIntegrationServer.UserEnvironmentVariables.Returns(new Dictionary { { "foo", "bar" }, { "fizz", "bizz" } }); var enviornmentVariables = new Codecov.Coverage.EnviornmentVariables.EnviornmentVariables(options, continuousIntegrationServer); Environment.SetEnvironmentVariable("foo", null); Environment.SetEnvironmentVariable("fizz", null); // When - var getEnviornmentVariables = enviornmentVariables.GetEnviornmentVariables; + var getEnviornmentVariables = enviornmentVariables.UserEnvironmentVariables; // Then getEnviornmentVariables["foo"].Should().Be("bar"); @@ -72,7 +72,7 @@ public void Should_Include_EnviornmentVariables_From_Options() Environment.SetEnvironmentVariable("fizz", "bizz"); // When - var getEnviornmentVariables = enviornmentVariables.GetEnviornmentVariables; + var getEnviornmentVariables = enviornmentVariables.UserEnvironmentVariables; // Then getEnviornmentVariables["foo"].Should().Be("bar"); diff --git a/Source/Codecov.Tests/Coverage/Report/ReportTests.cs b/Source/Codecov.Tests/Coverage/Report/ReportTests.cs index 2c9058b..a82fbef 100644 --- a/Source/Codecov.Tests/Coverage/Report/ReportTests.cs +++ b/Source/Codecov.Tests/Coverage/Report/ReportTests.cs @@ -20,13 +20,13 @@ public void If_DisableNetwork_Is_True_The_Source_Code_Should_Be_Empty() { // Given var enviornmentVariables = Substitute.For(); - enviornmentVariables.GetEnviornmentVariables.Returns(new Dictionary { { "foo", "bar" }, { "fizz", "bizz" } }); + enviornmentVariables.UserEnvironmentVariables.Returns(new Dictionary { { "foo", "bar" }, { "fizz", "bizz" } }); var options = Substitute.For(); options.DisableNetwork = true; var sourceCode = Substitute.For(); sourceCode.GetAllButCodecovIgnored.Returns(new[] { - Path.Combine(_systemDrive, "foo", "Class.cs"), - Path.Combine(_systemDrive, "foo", "Interface", "IClass.cs") + Path.Combine(_systemDrive, "foo", "Class.cs"), + Path.Combine(_systemDrive, "foo", "Interface", "IClass.cs") }); sourceCode.Directory.Returns(Path.Combine(_systemDrive, "foo")); var coverage = Substitute.For(); @@ -45,13 +45,13 @@ public void Should_Generate_A_Report() { // Given var enviornmentVariables = Substitute.For(); - enviornmentVariables.GetEnviornmentVariables.Returns(new Dictionary { { "foo", "bar" }, { "fizz", "bizz" } }); + enviornmentVariables.UserEnvironmentVariables.Returns(new Dictionary { { "foo", "bar" }, { "fizz", "bizz" } }); var options = Substitute.For(); options.DisableNetwork = false; var sourceCode = Substitute.For(); sourceCode.GetAllButCodecovIgnored.Returns(new[] { - Path.Combine(_systemDrive, "foo", "Class.cs"), - Path.Combine(_systemDrive, "foo", "Interface", "IClass.cs") + Path.Combine(_systemDrive, "foo", "Class.cs"), + Path.Combine(_systemDrive, "foo", "Interface", "IClass.cs") }); sourceCode.Directory.Returns(Path.Combine(_systemDrive, "foo")); var coverage = Substitute.For(); diff --git a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AppVeyorTests.cs b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AppVeyorTests.cs index 8d43f8f..2b6989b 100644 --- a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AppVeyorTests.cs +++ b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AppVeyorTests.cs @@ -1,18 +1,18 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Codecov.Services.ContinuousIntegrationServers; using FluentAssertions; +using Moq; using Xunit; namespace Codecov.Tests.Services.ContiniousIntegrationServers { - public class AppVeyorTests : IDisposable + public class AppVeyorTests { public static IEnumerable Build_Url_Empty_Data { get { - var possibleDomains = new[]{ null, string.Empty, "https://ci.appveyor.com", "http://localhost:5234" }; + var possibleDomains = new[] { null, string.Empty, "https://ci.appveyor.com", "http://localhost:5234" }; var possibleDummies = new[] { null, string.Empty, "foo", "bar" }; foreach (var domain in possibleDomains) @@ -38,8 +38,9 @@ public static IEnumerable Build_Url_Empty_Data public void Branch_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_REPO_BRANCH", null); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH")).Returns(string.Empty); + var appVeyor = av.Object; // When var branch = appVeyor.Branch; @@ -52,8 +53,9 @@ public void Branch_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exi public void Branch_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_REPO_BRANCH", "develop"); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH")).Returns("develop"); + var appVeyor = av.Object; // When var branch = appVeyor.Branch; @@ -66,8 +68,9 @@ public void Branch_Should_Be_Set_When_Enviornment_Variable_Exits() public void Build_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_JOB_ID", null); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns(string.Empty); + var appVeyor = av.Object; // When var build = appVeyor.Build; @@ -80,8 +83,9 @@ public void Build_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exit public void Build_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_JOB_ID", "Job 123"); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns("Job 123"); + var appVeyor = av.Object; // When var build = appVeyor.Build; @@ -94,8 +98,9 @@ public void Build_Should_Be_Set_When_Enviornment_Variable_Exits() public void Commit_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_REPO_COMMIT", null); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_COMMIT")).Returns(string.Empty); + var appVeyor = av.Object; // When var commit = appVeyor.Commit; @@ -108,8 +113,9 @@ public void Commit_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exi public void Commit_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_REPO_COMMIT", "123"); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_COMMIT")).Returns("123"); + var appVeyor = av.Object; // When var commit = appVeyor.Commit; @@ -122,9 +128,10 @@ public void Commit_Should_Be_Set_When_Enviornment_Variable_Exits() public void Detecter_Should_Be_False_When_AppVeyor_Enviornment_Variable_Or_Ci_Enviornment_Variable_Does_Not_Exit_And_Both_Are_Not_Equal_To_True(string appveyorData, string ciData) { // Given - Environment.SetEnvironmentVariable("APPVEYOR", appveyorData); - Environment.SetEnvironmentVariable("CI", ciData); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR")).Returns(appveyorData); + av.Setup(s => s.GetEnvironmentVariable("CI")).Returns(ciData); + var appVeyor = av.Object; // When var detecter = appVeyor.Detecter; @@ -139,9 +146,10 @@ public void Detecter_Should_Be_False_When_AppVeyor_Enviornment_Variable_Or_Ci_En public void Detecter_Should_Be_True_When_AppVeyor_Enviornment_Variable_And_Ci_Enviornment_Variable_Exist_And_Both_Are_Equal_To_True(string appveyorData, string ciData) { // Given - Environment.SetEnvironmentVariable("APPVEYOR", appveyorData); - Environment.SetEnvironmentVariable("CI", ciData); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR")).Returns(appveyorData); + av.Setup(s => s.GetEnvironmentVariable("CI")).Returns(ciData); + var appVeyor = av.Object; // When var detecter = appVeyor.Detecter; @@ -154,12 +162,12 @@ public void Detecter_Should_Be_True_When_AppVeyor_Enviornment_Variable_And_Ci_En public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exist(string appveyorUrl, string accountData, string slugData, string jobId) { // Given - Environment.SetEnvironmentVariable("APPVEYOR_URL", appveyorUrl); - Environment.SetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME", accountData); - Environment.SetEnvironmentVariable("APPVEYOR_PROJECT_SLUG", slugData); - Environment.SetEnvironmentVariable("APPVEYOR_JOB_ID", jobId); - - var appveyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_URL")).Returns(appveyorUrl); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME")).Returns(accountData); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG")).Returns(slugData); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns(jobId); + var appveyor = av.Object; // When var buildUrl = appveyor.BuildUrl; @@ -172,12 +180,12 @@ public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Ex public void BuildUrl_Should_Not_Empty_String_When_Environment_Variable_Exists() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_URL", "https://ci.appveyor.com"); - Environment.SetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME", "foo"); - Environment.SetEnvironmentVariable("APPVEYOR_PROJECT_SLUG", "bar"); - Environment.SetEnvironmentVariable("APPVEYOR_JOB_ID", "xyz"); - - var appveyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_URL")).Returns("https://ci.appveyor.com"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME")).Returns("foo"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG")).Returns("bar"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns("xyz"); + var appveyor = av.Object; // When var buildUrl = appveyor.BuildUrl; @@ -190,12 +198,12 @@ public void BuildUrl_Should_Not_Empty_String_When_Environment_Variable_Exists() public void BuildUrl_Should_Be_Empty_When_Appveyor_Url_Is_Invalid_Domain(string urlData) { // Given - Environment.SetEnvironmentVariable("APPVEYOR_URL", urlData); - Environment.SetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME", "foo"); - Environment.SetEnvironmentVariable("APPVEYOR_PROJECT_SLUG", "bar"); - Environment.SetEnvironmentVariable("APPVEYOR_JOB_ID", "xyz"); - - var appveyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_URL")).Returns(urlData); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME")).Returns("foo"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG")).Returns("bar"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns("xyz"); + var appveyor = av.Object; // When var buildUrl = appveyor.BuildUrl; @@ -208,11 +216,11 @@ public void BuildUrl_Should_Be_Empty_When_Appveyor_Url_Is_Invalid_Domain(string public void Job_Should_Be_Empty_String_When_Enviornment_Variables_Do_Not_Exit(string accountData, string slugData, string versionData) { // Given - Environment.SetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME", accountData); - Environment.SetEnvironmentVariable("APPVEYOR_PROJECT_SLUG", slugData); - Environment.SetEnvironmentVariable("APPVEYOR_BUILD_VERSION", versionData); - - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME")).Returns(accountData); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG")).Returns(slugData); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_BUILD_VERSION")).Returns(versionData); + var appVeyor = av.Object; // When var job = appVeyor.Job; @@ -225,10 +233,11 @@ public void Job_Should_Be_Empty_String_When_Enviornment_Variables_Do_Not_Exit(st public void Job_Should_Not_Be_Empty_String_When_Enviornment_Variables_Exit() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME", "foo"); - Environment.SetEnvironmentVariable("APPVEYOR_PROJECT_SLUG", "bar"); - Environment.SetEnvironmentVariable("APPVEYOR_BUILD_VERSION", "bang"); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME")).Returns("foo"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG")).Returns("bar"); + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_BUILD_VERSION")).Returns("bang"); + var appVeyor = av.Object; // When var job = appVeyor.Job; @@ -241,8 +250,9 @@ public void Job_Should_Not_Be_Empty_String_When_Enviornment_Variables_Exit() public void Pr_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER", null); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER")).Returns(string.Empty); + var appVeyor = av.Object; // When var pr = appVeyor.Pr; @@ -255,8 +265,9 @@ public void Pr_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() public void Pr_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER", "123"); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER")).Returns("123"); + var appVeyor = av.Object; // When var pr = appVeyor.Pr; @@ -269,8 +280,9 @@ public void Pr_Should_Be_Set_When_Enviornment_Variable_Exits() public void Slug_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_REPO_NAME", null); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_NAME")).Returns(string.Empty); + var appVeyor = av.Object; // When var slug = appVeyor.Slug; @@ -283,8 +295,9 @@ public void Slug_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits public void Slug_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("APPVEYOR_REPO_NAME", "foo/bar"); - var appVeyor = new AppVeyor(); + var av = new Mock() { CallBase = true }; + av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_NAME")).Returns("foo/bar"); + var appVeyor = av.Object; // When var slug = appVeyor.Slug; @@ -292,30 +305,5 @@ public void Slug_Should_Be_Set_When_Enviornment_Variable_Exits() // Then slug.Should().Be("foo/bar"); } - - public void Dispose() - { - // We will remove all environment variables that could have been set during unit test - var envVariable = new[] - { - "CI", - "APPVEYOR_REPO_BRANCH", - "APPVEYOR_JOB_ID", - "APPVEYOR_REPO_COMMIT", - "APPVEYOR", - "APPVEYOR_ACCOUNT_NAME", - "APPVEYOR_PROJECT_SLUG", - "APPVEYOR_BUILD_VERSION", - "APPVEYOR_PULL_REQUEST_NUMBER", - "APPVEYOR_REPO_NAME", - "APPVEYOR_URL", - "CODECOV_SLUG" // We use this travis fork tests - }; - - foreach (var variable in envVariable) - { - Environment.SetEnvironmentVariable(variable, null); - } - } } } diff --git a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AzurePipelinesTests.cs b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AzurePipelinesTests.cs index 2e94815..985267b 100644 --- a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AzurePipelinesTests.cs +++ b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/AzurePipelinesTests.cs @@ -1,7 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Codecov.Services.ContinuousIntegrationServers; using FluentAssertions; +using Moq; using Xunit; namespace Codecov.Tests.Services.ContiniousIntegrationServers @@ -12,7 +12,7 @@ public static IEnumerable Build_Url_Empty_Data { get { - var possibleDomains = new[]{ null, string.Empty, "https://dev.azure.com/", "http://localhost:5234/" }; + var possibleDomains = new[] { null, string.Empty, "https://dev.azure.com/", "http://localhost:5234/" }; var possibleDummies = new[] { null, string.Empty, "foo", "bar" }; foreach (var domain in possibleDomains) @@ -35,9 +35,10 @@ public static IEnumerable Build_Url_Empty_Data public void Branch_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH", null); - Environment.SetEnvironmentVariable("BUILD_SOURCEBRANCHNAME", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH")).Returns(string.Empty); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_SOURCEBRANCHNAME")).Returns(string.Empty); + var pipelines = azure.Object; // When var branch = pipelines.Branch; @@ -50,9 +51,10 @@ public void Branch_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exi public void Branch_Should_Be_Set_When_PR_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH", "develop"); - Environment.SetEnvironmentVariable("BUILD_SOURCEBRANCHNAME", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH")).Returns("develop"); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_SOURCEBRANCHNAME")).Returns(string.Empty); + var pipelines = azure.Object; // When var branch = pipelines.Branch; @@ -65,9 +67,10 @@ public void Branch_Should_Be_Set_When_PR_Enviornment_Variable_Exits() public void Branch_Should_Be_Set_When_Branch_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH", null); - Environment.SetEnvironmentVariable("BUILD_SOURCEBRANCHNAME", "develop"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH")).Returns(string.Empty); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_SOURCEBRANCHNAME")).Returns("develop"); + var pipelines = azure.Object; // When var branch = pipelines.Branch; @@ -80,9 +83,10 @@ public void Branch_Should_Be_Set_When_Branch_Enviornment_Variable_Exits() public void Branch_Should_Prefer_Pull_Request() { // Given - Environment.SetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH", "pr"); - Environment.SetEnvironmentVariable("BUILD_SOURCEBRANCHNAME", "master"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH")).Returns("pr"); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_SOURCEBRANCHNAME")).Returns("master"); + var pipelines = azure.Object; // When var branch = pipelines.Branch; @@ -95,8 +99,9 @@ public void Branch_Should_Prefer_Pull_Request() public void Build_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("BUILD_BUILDNUMBER", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDNUMBER")).Returns(string.Empty); + var pipelines = azure.Object; // When var build = pipelines.Build; @@ -109,8 +114,9 @@ public void Build_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exit public void Build_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("BUILD_BUILDNUMBER", "123"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDNUMBER")).Returns("123"); + var pipelines = azure.Object; // When var build = pipelines.Build; @@ -123,8 +129,9 @@ public void Build_Should_Be_Set_When_Enviornment_Variable_Exits() public void Commit_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("BUILD_SOURCEVERSION", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_SOURCEVERSION")).Returns(string.Empty); + var pipelines = azure.Object; // When var commit = pipelines.Commit; @@ -137,8 +144,9 @@ public void Commit_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exi public void Commit_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("BUILD_SOURCEVERSION", "123"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_SOURCEVERSION")).Returns("123"); + var pipelines = azure.Object; // When var commit = pipelines.Commit; @@ -151,8 +159,9 @@ public void Commit_Should_Be_Set_When_Enviornment_Variable_Exits() public void Detecter_Should_Be_False_When_TfBuild_Enviornment_Variable_Does_Not_Exit(string pipelinesData) { // Given - Environment.SetEnvironmentVariable("TF_BUILD", pipelinesData); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("TF_BUILD")).Returns(pipelinesData); + var pipelines = azure.Object; // When var detecter = pipelines.Detecter; @@ -165,11 +174,11 @@ public void Detecter_Should_Be_False_When_TfBuild_Enviornment_Variable_Does_Not_ public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exist(string serverUrl, string project, string build) { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI", serverUrl); - Environment.SetEnvironmentVariable("SYSTEM_TEAMPROJECT", project); - Environment.SetEnvironmentVariable("BUILD_BUILDID", build); - - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")).Returns(serverUrl); + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMPROJECT")).Returns(project); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDID")).Returns(build); + var pipelines = azure.Object; // When var buildUrl = pipelines.BuildUrl; @@ -182,11 +191,11 @@ public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Ex public void BuildUrl_Should_Not_Empty_String_When_Environment_Variable_Exists() { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI", "https://dev.azure.com/"); - Environment.SetEnvironmentVariable("SYSTEM_TEAMPROJECT", "project"); - Environment.SetEnvironmentVariable("BUILD_BUILDID", "build"); - - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")).Returns("https://dev.azure.com/"); + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMPROJECT")).Returns("project"); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDID")).Returns("build"); + var pipelines = azure.Object; // When var buildUrl = pipelines.BuildUrl; @@ -199,11 +208,11 @@ public void BuildUrl_Should_Not_Empty_String_When_Environment_Variable_Exists() public void BuildUrl_Should_Be_Empty_When_Appveyor_Url_Is_Invalid_Domain(string urlData) { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI", urlData); - Environment.SetEnvironmentVariable("SYSTEM_TEAMPROJECT", "project"); - Environment.SetEnvironmentVariable("BUILD_BUILDID", "build"); - - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")).Returns(urlData); + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMPROJECT")).Returns("project"); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDID")).Returns("build"); + var pipelines = azure.Object; // When var buildUrl = pipelines.BuildUrl; @@ -212,14 +221,14 @@ public void BuildUrl_Should_Be_Empty_When_Appveyor_Url_Is_Invalid_Domain(string buildUrl.Should().BeEmpty(); } - [Theory, InlineData(null, null), InlineData("", ""), InlineData("foo", "")] + [Theory, InlineData("", ""), InlineData("foo", "")] public void Job_Should_Be_Empty_String_When_Enviornment_Variables_Do_Not_Exit(string slugData, string versionData) { // Given - Environment.SetEnvironmentVariable("BUILD_REPOSITORY_NAME", slugData); - Environment.SetEnvironmentVariable("BUILD_BUILDID", versionData); - - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_REPOSITORY_NAME")).Returns(slugData); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDID")).Returns(versionData); + var pipelines = azure.Object; // When var job = pipelines.Job; @@ -232,9 +241,10 @@ public void Job_Should_Be_Empty_String_When_Enviornment_Variables_Do_Not_Exit(st public void Job_Should_Not_Be_Empty_String_When_Enviornment_Variables_Exit() { // Given - Environment.SetEnvironmentVariable("BUILD_REPOSITORY_NAME", "foo/bar"); - Environment.SetEnvironmentVariable("BUILD_BUILDID", "bang"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_REPOSITORY_NAME")).Returns("foo/bar"); + azure.Setup(s => s.GetEnvironmentVariable("BUILD_BUILDID")).Returns("bang"); + var pipelines = azure.Object; // When var job = pipelines.Job; @@ -247,8 +257,9 @@ public void Job_Should_Not_Be_Empty_String_When_Enviornment_Variables_Exit() public void Pr_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER")).Returns(string.Empty); + var pipelines = azure.Object; // When var pr = pipelines.Pr; @@ -261,8 +272,9 @@ public void Pr_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() public void Pr_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER", "123"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER")).Returns("123"); + var pipelines = azure.Object; // When var pr = pipelines.Pr; @@ -275,8 +287,9 @@ public void Pr_Should_Be_Set_When_Enviornment_Variable_Exits() public void Project_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMPROJECT", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMPROJECT")).Returns(string.Empty); + var pipelines = azure.Object; // When var project = pipelines.Project; @@ -289,8 +302,9 @@ public void Project_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Ex public void Project_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMPROJECT", "123"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMPROJECT")).Returns("123"); + var pipelines = azure.Object; // When var project = pipelines.Project; @@ -303,8 +317,9 @@ public void Project_Should_Be_Set_When_Enviornment_Variable_Exits() public void ServerUri_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")).Returns(string.Empty); + var pipelines = azure.Object; // When var serverUri = pipelines.ServerUri; @@ -317,8 +332,9 @@ public void ServerUri_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_ public void ServerUri_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI", "123"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")).Returns("123"); + var pipelines = azure.Object; // When var serverUri = pipelines.ServerUri; @@ -344,8 +360,9 @@ public void Service_Should_Be_Set_To_AzurePipelines() public void Slug_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("BUILD_REPOSITORY_NAME", null); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_REPOSITORY_NAME")).Returns(string.Empty); + var pipelines = azure.Object; // When var slug = pipelines.Slug; @@ -358,8 +375,9 @@ public void Slug_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits public void Slug_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("BUILD_REPOSITORY_NAME", "foo/bar"); - var pipelines = new AzurePipelines(); + var azure = new Mock() { CallBase = true }; + azure.Setup(s => s.GetEnvironmentVariable("BUILD_REPOSITORY_NAME")).Returns("foo/bar"); + var pipelines = azure.Object; // When var slug = pipelines.Slug; diff --git a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/ContiniousIntegrationServerTests.cs b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/ContiniousIntegrationServerTests.cs index 6930064..be93f09 100644 --- a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/ContiniousIntegrationServerTests.cs +++ b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/ContiniousIntegrationServerTests.cs @@ -1,6 +1,6 @@ -using System; -using Codecov.Services.ContinuousIntegrationServers; +using Codecov.Services.ContinuousIntegrationServers; using FluentAssertions; +using Moq; using Xunit; namespace Codecov.Tests.Services.ContiniousIntegrationServers @@ -24,11 +24,14 @@ public void Branch_Should_Be_Empty_String() public void Build_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("CI_BUILD_ID", null); - var continuousIntegrationServer = new ContinuousIntegrationServer(); + var cis = new Mock + { + CallBase = true + }; + cis.Setup(s => s.GetEnvironmentVariable("CI_BUILD_ID")).Returns(string.Empty); // When - var build = continuousIntegrationServer.Build; + var build = cis.Object.Build; // Then build.Should().BeEmpty(); @@ -38,11 +41,14 @@ public void Build_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exit public void Build_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("CI_BUILD_ID", "123"); - var continuousIntegrationServer = new ContinuousIntegrationServer(); + var cis = new Mock + { + CallBase = true + }; + cis.Setup(s => s.GetEnvironmentVariable("CI_BUILD_ID")).Returns("123"); // When - var build = continuousIntegrationServer.Build; + var build = cis.Object.Build; // Then build.Should().Be("123"); } @@ -51,11 +57,14 @@ public void Build_Should_Be_Set_When_Enviornment_Variable_Exits() public void BuildUrl_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("CI_BUILD_URL", null); - var continuousIntegrationServer = new ContinuousIntegrationServer(); + var cis = new Mock + { + CallBase = true + }; + cis.Setup(s => s.GetEnvironmentVariable("CI_BUILD_URL")).Returns(string.Empty); // When - var buildUrl = continuousIntegrationServer.BuildUrl; + var buildUrl = cis.Object.BuildUrl; // Then buildUrl.Should().BeEmpty(); @@ -65,11 +74,14 @@ public void BuildUrl_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_E public void BuildUrl_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("CI_BUILD_URL", "www.google.com"); - var continuousIntegrationServer = new ContinuousIntegrationServer(); + var cis = new Mock + { + CallBase = true + }; + cis.Setup(s => s.GetEnvironmentVariable("CI_BUILD_URL")).Returns("www.google.com"); // When - var buildUrl = continuousIntegrationServer.BuildUrl; + var buildUrl = cis.Object.BuildUrl; // Then buildUrl.Should().Be("www.google.com"); } @@ -107,7 +119,7 @@ public void GetEnviornmentVariables_Should_Empty_Dictionary() var continuousIntegrationServer = new ContinuousIntegrationServer(); // When - var enviornmentVariables = continuousIntegrationServer.GetEnviornmentVariables; + var enviornmentVariables = continuousIntegrationServer.UserEnvironmentVariables; // Then enviornmentVariables.Should().BeEmpty(); @@ -117,11 +129,14 @@ public void GetEnviornmentVariables_Should_Empty_Dictionary() public void Job_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("CI_JOB_ID", null); - var continuousIntegrationServer = new ContinuousIntegrationServer(); + var cis = new Mock + { + CallBase = true + }; + cis.Setup(s => s.GetEnvironmentVariable("CI_JOB_ID")).Returns(string.Empty); // When - var job = continuousIntegrationServer.Job; + var job = cis.Object.Job; // Then job.Should().BeEmpty(); @@ -131,11 +146,14 @@ public void Job_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits( public void Job_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("CI_JOB_ID", "123"); - var continuousIntegrationServer = new ContinuousIntegrationServer(); + var cis = new Mock + { + CallBase = true + }; + cis.Setup(s => s.GetEnvironmentVariable("CI_JOB_ID")).Returns("123"); // When - var job = continuousIntegrationServer.Job; + var job = cis.Object.Job; // Then job.Should().Be("123"); diff --git a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/GitHubActionTests.cs b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/GitHubActionTests.cs index 0c5b616..ae38b2c 100644 --- a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/GitHubActionTests.cs +++ b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/GitHubActionTests.cs @@ -1,18 +1,20 @@ using System; using Codecov.Services.ContinuousIntegrationServers; using FluentAssertions; +using Moq; using Xunit; namespace Codecov.Tests.Services.ContiniousIntegrationServers { - public class GitHubActionTests : IDisposable + public class GitHubActionTests { [Fact] public void Branch_Should_Be_Empty_When_Environment_Variable_Does_Not_Exist() { // Given - Environment.SetEnvironmentVariable("GITHUB_REF", null); - var githubAction = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns(string.Empty); + var githubAction = ga.Object; // When var branch = githubAction.Branch; @@ -25,8 +27,9 @@ public void Branch_Should_Be_Empty_When_Environment_Variable_Does_Not_Exist() public void Branch_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("GITHUB_REF", "ref/heads/develop"); - var githubAction = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns("ref/heads/develop"); + var githubAction = ga.Object; // When var branch = githubAction.Branch; @@ -39,8 +42,9 @@ public void Branch_Should_Be_Set_When_Enviornment_Variable_Exits() public void Commit_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("GITHUB_SHA", null); - var githubAction = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_SHA")).Returns(string.Empty); + var githubAction = ga.Object; // When var commit = githubAction.Commit; @@ -53,8 +57,9 @@ public void Commit_Should_Be_Empty_String_When_Enviornment_Variable_Does_Not_Exi public void Commit_Should_Be_Set_When_Enviornment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("GITHUB_SHA", "123"); - var githubAction = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_SHA")).Returns("123"); + var githubAction = ga.Object; // When var commit = githubAction.Commit; @@ -67,11 +72,12 @@ public void Commit_Should_Be_Set_When_Enviornment_Variable_Exits() public void Detecter_Should_Be_False_When_Actions_Environment_Variable_Does_Not_Exist_Or_Is_Not_True(string environmentData) { // Given - Environment.SetEnvironmentVariable("GITHUB_ACTIONS", environmentData); - var githubActions = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_ACTIONS")).Returns(environmentData); + var githubAction = ga.Object; // When - var detecter = githubActions.Detecter; + var detecter = githubAction.Detecter; // Then detecter.Should().BeFalse(); @@ -81,11 +87,12 @@ public void Detecter_Should_Be_False_When_Actions_Environment_Variable_Does_Not_ public void Detecter_Should_Be_True_When_Actions_Environment_Variable_Exist_And_Is_True(string environmentData) { // Given - Environment.SetEnvironmentVariable("GITHUB_ACTIONS", environmentData); - var githubActions = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_ACTIONS")).Returns(environmentData); + var githubAction = ga.Object; // When - var detecter = githubActions.Detecter; + var detecter = githubAction.Detecter; // Then detecter.Should().BeTrue(); @@ -108,8 +115,9 @@ public void Service_Should_Be_Set_To_GitHubActions() public void Slug_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exist() { // Given - Environment.SetEnvironmentVariable("GITHUB_REPOSITORY", null); - var githubAction = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REPOSITORY")).Returns(string.Empty); + var githubAction = ga.Object; // When var slug = githubAction.Slug; @@ -122,8 +130,9 @@ public void Slug_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exist public void Slug_Should_Be_Set_When_Environment_Variable_Exist() { // Given - Environment.SetEnvironmentVariable("GITHUB_REPOSITORY", "foo/bar"); - var githubAction = new GitHubAction(); + var ga = new Mock() { CallBase = true }; + ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REPOSITORY")).Returns("foo/bar"); + var githubAction = ga.Object; // When var slug = githubAction.Slug; @@ -131,22 +140,5 @@ public void Slug_Should_Be_Set_When_Environment_Variable_Exist() // Then slug.Should().Be("foo/bar"); } - - public void Dispose() - { - // We will remove all environment variables that could have been set during unit test - var envVariable = new[] - { - "GITHUB_REF", - "GITHUB_SHA", - "GITHUB_ACTIONS", - "GITHUB_REPOSITORY" - }; - - foreach (var variable in envVariable) - { - Environment.SetEnvironmentVariable(variable, null); - } - } } } diff --git a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/TravisCiTests.cs b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/TravisCiTests.cs index f579ca8..c04f772 100644 --- a/Source/Codecov.Tests/Services/ContiniousIntegrationServers/TravisCiTests.cs +++ b/Source/Codecov.Tests/Services/ContiniousIntegrationServers/TravisCiTests.cs @@ -1,18 +1,20 @@ using System; using Codecov.Services.ContinuousIntegrationServers; using FluentAssertions; +using Moq; using Xunit; namespace Codecov.Tests.Services.ContiniousIntegrationServers { - public class TravisTests : IDisposable + public class TravisTests { [Fact] public void Branch_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_BRANCH", null); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_BRANCH")).Returns(string.Empty); + var travis = tr.Object; // When var branch = travis.Branch; @@ -25,8 +27,9 @@ public void Branch_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exi public void Branch_Should_Be_Set_When_Environment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_BRANCH", "develop"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_BRANCH")).Returns("develop"); + var travis = tr.Object; // When var branch = travis.Branch; @@ -39,8 +42,9 @@ public void Branch_Should_Be_Set_When_Environment_Variable_Exits() public void Build_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_JOB_NUMBER", null); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_JOB_NUMBER")).Returns(string.Empty); + var travis = tr.Object; // When var build = travis.Build; @@ -53,8 +57,9 @@ public void Build_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exit public void Build_Should_Be_Set_When_Environment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_JOB_NUMBER", "5.2"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_JOB_NUMBER")).Returns("5.2"); + var travis = tr.Object; // When var build = travis.Build; @@ -67,8 +72,9 @@ public void Build_Should_Be_Set_When_Environment_Variable_Exits() public void Commit_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_COMMIT", null); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_COMMIT")).Returns(string.Empty); + var travis = tr.Object; // When var commit = travis.Commit; @@ -81,8 +87,9 @@ public void Commit_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exi public void Commit_Should_Be_Set_When_Environment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_COMMIT", "123"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_COMMIT")).Returns("123"); + var travis = tr.Object; // When var commit = travis.Commit; @@ -95,9 +102,10 @@ public void Commit_Should_Be_Set_When_Environment_Variable_Exits() public void Detecter_Should_Be_False_When_Travis_Environment_Variable_Or_Ci_Environment_Variable_Does_Not_Exit_And_Both_Are_Not_Equal_To_True(string travisData, string ciData) { // Given - Environment.SetEnvironmentVariable("TRAVIS", travisData); - Environment.SetEnvironmentVariable("CI", ciData); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS")).Returns(travisData); + tr.Setup(s => s.GetEnvironmentVariable("CI")).Returns(ciData); + var travis = tr.Object; // When var detecter = travis.Detecter; @@ -112,9 +120,10 @@ public void Detecter_Should_Be_False_When_Travis_Environment_Variable_Or_Ci_Envi public void Detecter_Should_Be_True_When_Travis_Environment_Variable_And_Ci_Environment_Variable_Exist_And_Both_Are_Equal_To_True(string travisData, string ciData) { // Given - Environment.SetEnvironmentVariable("TRAVIS", travisData); - Environment.SetEnvironmentVariable("CI", ciData); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS")).Returns(travisData); + tr.Setup(s => s.GetEnvironmentVariable("CI")).Returns(ciData); + var travis = tr.Object; // When var detecter = travis.Detecter; @@ -123,13 +132,13 @@ public void Detecter_Should_Be_True_When_Travis_Environment_Variable_And_Ci_Envi detecter.Should().BeTrue(); } - [Theory, InlineData(null), InlineData("")] - public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exist(string jobUrl) + [Fact] + public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exist() { // Given - Environment.SetEnvironmentVariable("TRAVIS_JOB_WEB_URL", jobUrl); - - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_JOB_WEB_URL")).Returns(string.Empty); + var travis = tr.Object; // When var buildUrl = travis.BuildUrl; @@ -142,9 +151,9 @@ public void BuildUrl_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Ex public void BuildUrl_Should_Not_Be_Empty_String_When_Environment_Variable_Exist() { // Given - Environment.SetEnvironmentVariable("TRAVIS_JOB_WEB_URL", "https://travis-ci.org/some-job"); - - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_JOB_WEB_URL")).Returns("https://travis-ci.org/some-job"); + var travis = tr.Object; // When var buildUrl = travis.BuildUrl; @@ -153,13 +162,13 @@ public void BuildUrl_Should_Not_Be_Empty_String_When_Environment_Variable_Exist( buildUrl.Should().Be("https://travis-ci.org/some-job"); } - [Theory, InlineData(null), InlineData("")] - public void Job_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exit(string jobNumber) + [Fact] + public void Job_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exit() { // Given - Environment.SetEnvironmentVariable("TRAVIS_JOB_ID", jobNumber); - - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_JOB_ID")).Returns(string.Empty); + var travis = tr.Object; // When var job = travis.Job; @@ -172,8 +181,9 @@ public void Job_Should_Be_Empty_String_When_Environment_Variables_Do_Not_Exit(st public void Job_Should_Not_Be_Empty_String_When_Environment_Variables_Exit() { // Given - Environment.SetEnvironmentVariable("TRAVIS_JOB_ID", "15657"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_JOB_ID")).Returns("15657"); + var travis = tr.Object; // When var job = travis.Job; @@ -186,8 +196,9 @@ public void Job_Should_Not_Be_Empty_String_When_Environment_Variables_Exit() public void Pr_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_PULL_REQUEST", null); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_PULL_REQUEST")).Returns(string.Empty); + var travis = tr.Object; // When var pr = travis.Pr; @@ -200,8 +211,9 @@ public void Pr_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exits() public void Pr_Should_Be_Set_When_Environment_Variable_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_PULL_REQUEST", "123"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_PULL_REQUEST")).Returns("123"); + var travis = tr.Object; // When var pr = travis.Pr; @@ -214,8 +226,9 @@ public void Pr_Should_Be_Set_When_Environment_Variable_Exits() public void Slug_Should_Be_Empty_String_When_Environment_Variable_Does_Not_Exits() { // Given - Environment.SetEnvironmentVariable("TRAVIS_REPO_SLUG", null); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_REPO_SLUG")).Returns(string.Empty); + var travis = tr.Object; // When var slug = travis.Slug; @@ -229,7 +242,9 @@ public void Slug_Should_Be_Set_When_Environment_Variable_Exits() { // Given Environment.SetEnvironmentVariable("TRAVIS_REPO_SLUG", "foo/bar"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_REPO_SLUG")).Returns("foo/bar"); + var travis = tr.Object; // When var slug = travis.Slug; @@ -238,13 +253,13 @@ public void Slug_Should_Be_Set_When_Environment_Variable_Exits() slug.Should().Be("foo/bar"); } - [Theory, InlineData(null), InlineData("")] - public void Tag_Should_Empty_String_When_Environment_Variable_Does_Not_Exist(string tagData) + [Fact] + public void Tag_Should_Empty_String_When_Environment_Variable_Does_Not_Exist() { // Given - Environment.SetEnvironmentVariable("TRAVIS_TAG", tagData); - - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_TAG")).Returns(string.Empty); + var travis = tr.Object; // When var tag = travis.Tag; @@ -258,7 +273,9 @@ public void Tag_Should_Not_Be_Empty_String_When_Environment_Variable_Exist() { // Given Environment.SetEnvironmentVariable("TRAVIS_TAG", "v1.2.4"); - var travis = new Travis(); + var tr = new Mock() { CallBase = true }; + tr.Setup(s => s.GetEnvironmentVariable("TRAVIS_TAG")).Returns("v1.2.4"); + var travis = tr.Object; // When var tag = travis.Tag; @@ -266,29 +283,5 @@ public void Tag_Should_Not_Be_Empty_String_When_Environment_Variable_Exist() // Then tag.Should().Be("v1.2.4"); } - - public void Dispose() - { - // We will remove all environment variables that could have been set during unit test - var envVariable = new[] - { - "CI", - "CODECOV_SLUG", // We use this travis fork tests - "TRAVIS", - "TRAVIS_BRANCH", - "TRAVIS_JOB_NUMBER", - "TRAVIS_COMMIT", - "TRAVIS_JOB_ID", - "TRAVIS_PULL_REQUEST", - "TRAVIS_REPO_SLUG", - "TRAVIS_JOB_WEB_URL", - "TRAVIS_TAG", - }; - - foreach (var variable in envVariable) - { - Environment.SetEnvironmentVariable(variable, null); - } - } } } diff --git a/Source/Codecov/Coverage/EnviornmentVariables/EnviornmentVariables.cs b/Source/Codecov/Coverage/EnviornmentVariables/EnviornmentVariables.cs index 96fcde7..5fee310 100644 --- a/Source/Codecov/Coverage/EnviornmentVariables/EnviornmentVariables.cs +++ b/Source/Codecov/Coverage/EnviornmentVariables/EnviornmentVariables.cs @@ -15,7 +15,7 @@ public EnviornmentVariables(IEnviornmentVariablesOptions options, IContinuousInt _getEnviornmentVariables = new Lazy>(LoadEnviornmentVariables); } - public IDictionary GetEnviornmentVariables => _getEnviornmentVariables.Value; + public IDictionary UserEnvironmentVariables => _getEnviornmentVariables.Value; private IContinuousIntegrationServer ContinuousIntegrationServer { get; } @@ -23,9 +23,9 @@ public EnviornmentVariables(IEnviornmentVariablesOptions options, IContinuousInt public string GetEnvironmentVariable(string name) { - if (GetEnviornmentVariables.ContainsKey(name)) + if (UserEnvironmentVariables.ContainsKey(name)) { - return GetEnviornmentVariables[name]; + return UserEnvironmentVariables[name]; } var value = Environment.GetEnvironmentVariable(name); @@ -39,7 +39,7 @@ public string GetEnvironmentVariable(string name) private IDictionary LoadEnviornmentVariables() { - var enviornmentVariables = new Dictionary(ContinuousIntegrationServer.GetEnviornmentVariables); + var enviornmentVariables = new Dictionary(ContinuousIntegrationServer.UserEnvironmentVariables); const string codecovName = "CODECOV_ENV"; var codecovValue = Environment.GetEnvironmentVariable(codecovName); diff --git a/Source/Codecov/Coverage/Report/Report.cs b/Source/Codecov/Coverage/Report/Report.cs index 30db2f2..590f1e0 100644 --- a/Source/Codecov/Coverage/Report/Report.cs +++ b/Source/Codecov/Coverage/Report/Report.cs @@ -41,7 +41,7 @@ private string CombinedCoverage private ICoverage Coverage { get; } - private string Env => !EnviornmentVariables.GetEnviornmentVariables.Any() ? string.Empty : $"{string.Join("\n", EnviornmentVariables.GetEnviornmentVariables.Select(x => x.Key.Trim() + "=" + x.Value.Trim()).ToArray())}\n<<<<<< ENV\n"; + private string Env => !EnviornmentVariables.UserEnvironmentVariables.Any() ? string.Empty : $"{string.Join("\n", EnviornmentVariables.UserEnvironmentVariables.Select(x => x.Key.Trim() + "=" + x.Value.Trim()).ToArray())}\n<<<<<< ENV\n"; private IEnviornmentVariables EnviornmentVariables { get; } diff --git a/Source/Codecov/Program/UploadFacade.cs b/Source/Codecov/Program/UploadFacade.cs index 1a0a858..fd28496 100644 --- a/Source/Codecov/Program/UploadFacade.cs +++ b/Source/Codecov/Program/UploadFacade.cs @@ -99,10 +99,10 @@ public void Uploader() Log.Information("Reading reports."); Log.Information(string.Join("\n", Coverage.CoverageReports.Select(x => x.File))); - if (EnviornmentVariables.GetEnviornmentVariables.Any()) + if (EnviornmentVariables.UserEnvironmentVariables.Any()) { Log.Information("Appending build variables"); - Log.Information(string.Join("\n", EnviornmentVariables.GetEnviornmentVariables.Select(x => x.Key.Trim()).ToArray())); + Log.Information(string.Join("\n", EnviornmentVariables.UserEnvironmentVariables.Select(x => x.Key.Trim()).ToArray())); } if (CommandLineCommandLineOptions.Dump) diff --git a/Source/Codecov/Services/ContinuousIntegrationServers/Appveyor.cs b/Source/Codecov/Services/ContinuousIntegrationServers/Appveyor.cs index edecfa8..8b97b50 100644 --- a/Source/Codecov/Services/ContinuousIntegrationServers/Appveyor.cs +++ b/Source/Codecov/Services/ContinuousIntegrationServers/Appveyor.cs @@ -1,19 +1,30 @@ using System; using System.Linq; -using Codecov.Utilities; namespace Codecov.Services.ContinuousIntegrationServers { internal class AppVeyor : ContinuousIntegrationServer { - private readonly Lazy _branch = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_REPO_BRANCH")); - private readonly Lazy _build = new Lazy(LoadBuild); - private readonly Lazy _buildUrl = new Lazy(LoadBuildUrl); - private readonly Lazy _commit = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_REPO_COMMIT")); - private readonly Lazy _detecter = new Lazy(() => CheckEnvironmentVariables("CI", "APPVEYOR")); - private readonly Lazy _job = new Lazy(LoadJob); - private readonly Lazy _pr = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_PULL_REQUEST_NUMBER")); - private readonly Lazy _slug = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_REPO_NAME")); + private readonly Lazy _branch; + private readonly Lazy _build; + private readonly Lazy _buildUrl; + private readonly Lazy _commit; + private readonly Lazy _detecter; + private readonly Lazy _job; + private readonly Lazy _pr; + private readonly Lazy _slug; + + public AppVeyor() + { + _branch = new Lazy(() => GetEnvironmentVariable("APPVEYOR_REPO_BRANCH")); + _build = new Lazy(LoadBuild); + _buildUrl = new Lazy(LoadBuildUrl); + _commit = new Lazy(() => GetEnvironmentVariable("APPVEYOR_REPO_COMMIT")); + _detecter = new Lazy(() => CheckEnvironmentVariables("CI", "APPVEYOR")); + _job = new Lazy(LoadJob); + _pr = new Lazy(() => GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER")); + _slug = new Lazy(() => GetEnvironmentVariable("APPVEYOR_REPO_NAME")); + } public override string Branch => _branch.Value; @@ -33,18 +44,18 @@ internal class AppVeyor : ContinuousIntegrationServer public override string Slug => _slug.Value; - private static string LoadBuild() + private string LoadBuild() { - var build = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_JOB_ID"); + var build = GetEnvironmentVariable("APPVEYOR_JOB_ID"); return !string.IsNullOrWhiteSpace(build) ? Uri.EscapeDataString(build) : string.Empty; } - private static string LoadBuildUrl() + private string LoadBuildUrl() { - var hostUrl = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_URL"); - var accountName = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_ACCOUNT_NAME"); - var slug = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_PROJECT_SLUG"); - var jobId = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_JOB_ID"); + var hostUrl = GetEnvironmentVariable("APPVEYOR_URL"); + var accountName = GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME"); + var slug = GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG"); + var jobId = GetEnvironmentVariable("APPVEYOR_JOB_ID"); if (IsNullOrEmpty(hostUrl, accountName, slug, jobId) || !Uri.TryCreate(hostUrl, UriKind.Absolute, out var uri) || (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) { @@ -55,11 +66,11 @@ private static string LoadBuildUrl() return jobUrl; } - private static string LoadJob() + private string LoadJob() { - var accountName = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_ACCOUNT_NAME"); - var slug = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_PROJECT_SLUG"); - var version = EnviornmentVariable.GetEnviornmentVariable("APPVEYOR_BUILD_VERSION"); + var accountName = GetEnvironmentVariable("APPVEYOR_ACCOUNT_NAME"); + var slug = GetEnvironmentVariable("APPVEYOR_PROJECT_SLUG"); + var version = GetEnvironmentVariable("APPVEYOR_BUILD_VERSION"); if (string.IsNullOrWhiteSpace(accountName) || string.IsNullOrWhiteSpace(slug) || string.IsNullOrWhiteSpace(version)) { diff --git a/Source/Codecov/Services/ContinuousIntegrationServers/AzurePipelines.cs b/Source/Codecov/Services/ContinuousIntegrationServers/AzurePipelines.cs index 73c9a5e..a960eb6 100644 --- a/Source/Codecov/Services/ContinuousIntegrationServers/AzurePipelines.cs +++ b/Source/Codecov/Services/ContinuousIntegrationServers/AzurePipelines.cs @@ -1,24 +1,32 @@ using System; -using Codecov.Utilities; namespace Codecov.Services.ContinuousIntegrationServers { internal class AzurePipelines : ContinuousIntegrationServer { - private readonly Lazy _branch = new Lazy(LoadBranch); - private readonly Lazy _build = new Lazy(LoadBuild); + private readonly Lazy _branch; + private readonly Lazy _build; private readonly Lazy _buildUrl; - private readonly Lazy _commit = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("BUILD_SOURCEVERSION")); - private readonly Lazy _detecter = new Lazy(() => CheckEnvironmentVariables("TF_BUILD")); - private readonly Lazy _job = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("BUILD_BUILDID")); - private readonly Lazy _pr = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER")); - private readonly Lazy _project = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("SYSTEM_TEAMPROJECT")); - private readonly Lazy _serverUri = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")); - private readonly Lazy _slug = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("BUILD_REPOSITORY_NAME")); + private readonly Lazy _commit; + private readonly Lazy _detecter; + private readonly Lazy _job; + private readonly Lazy _pr; + private readonly Lazy _project; + private readonly Lazy _serverUri; + private readonly Lazy _slug; public AzurePipelines() { + _branch = new Lazy(LoadBranch); + _build = new Lazy(LoadBuild); _buildUrl = new Lazy(LoadBuildUrl); + _commit = new Lazy(() => GetEnvironmentVariable("BUILD_SOURCEVERSION")); + _detecter = new Lazy(() => CheckEnvironmentVariables("TF_BUILD")); + _job = new Lazy(() => GetEnvironmentVariable("BUILD_BUILDID")); + _pr = new Lazy(() => GetEnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER")); + _project = new Lazy(() => GetEnvironmentVariable("SYSTEM_TEAMPROJECT")); + _serverUri = new Lazy(() => GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONSERVERURI")); + _slug = new Lazy(() => GetEnvironmentVariable("BUILD_REPOSITORY_NAME")); } public override string Branch => _branch.Value; @@ -43,20 +51,20 @@ public AzurePipelines() public override string Slug => _slug.Value; - private static string LoadBranch() + private string LoadBranch() { - var pullRequestBranch = EnviornmentVariable.GetEnviornmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH"); + var pullRequestBranch = GetEnvironmentVariable("SYSTEM_PULLREQUEST_TARGETBRANCH"); if (!string.IsNullOrEmpty(pullRequestBranch)) { return pullRequestBranch; } - return EnviornmentVariable.GetEnviornmentVariable("BUILD_SOURCEBRANCHNAME"); + return GetEnvironmentVariable("BUILD_SOURCEBRANCHNAME"); } - private static string LoadBuild() + private string LoadBuild() { - var build = EnviornmentVariable.GetEnviornmentVariable("BUILD_BUILDNUMBER"); + var build = GetEnvironmentVariable("BUILD_BUILDNUMBER"); return !string.IsNullOrWhiteSpace(build) ? Uri.EscapeDataString(build) : string.Empty; } diff --git a/Source/Codecov/Services/ContinuousIntegrationServers/ContinuousIntegrationServer.cs b/Source/Codecov/Services/ContinuousIntegrationServers/ContinuousIntegrationServer.cs index e1f8917..0dbb7da 100644 --- a/Source/Codecov/Services/ContinuousIntegrationServers/ContinuousIntegrationServer.cs +++ b/Source/Codecov/Services/ContinuousIntegrationServers/ContinuousIntegrationServer.cs @@ -1,15 +1,21 @@ using System; using System.Collections.Generic; using System.Linq; -using Codecov.Utilities; namespace Codecov.Services.ContinuousIntegrationServers { internal class ContinuousIntegrationServer : IContinuousIntegrationServer { - private readonly Lazy _build = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("CI_BUILD_ID")); - private readonly Lazy _buildUrl = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("CI_BUILD_URL")); - private readonly Lazy _job = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("CI_JOB_ID")); + private readonly Lazy _build; + private readonly Lazy _buildUrl; + private readonly Lazy _job; + + public ContinuousIntegrationServer() + { + _build = new Lazy(() => GetEnvironmentVariable("CI_BUILD_ID")); + _buildUrl = new Lazy(() => GetEnvironmentVariable("CI_BUILD_URL")); + _job = new Lazy(() => GetEnvironmentVariable("CI_JOB_ID")); + } public virtual string Branch => string.Empty; @@ -21,7 +27,7 @@ internal class ContinuousIntegrationServer : IContinuousIntegrationServer public virtual bool Detecter => false; - public virtual IDictionary GetEnviornmentVariables => new Dictionary(); + public virtual IDictionary UserEnvironmentVariables => new Dictionary(); public virtual string Job => _job.Value; @@ -37,13 +43,13 @@ internal class ContinuousIntegrationServer : IContinuousIntegrationServer public virtual string Tag => string.Empty; - protected static bool CheckEnvironmentVariables(params string[] environmentVariables) + protected bool CheckEnvironmentVariables(params string[] environmentVariables) { var foundVariables = new List(); foreach (var variable in environmentVariables) { - var ci = EnviornmentVariable.GetEnviornmentVariable(variable); + var ci = GetEnvironmentVariable(variable); if (string.IsNullOrWhiteSpace(ci) || !ci.Equals("True", StringComparison.OrdinalIgnoreCase)) { @@ -57,25 +63,20 @@ protected static bool CheckEnvironmentVariables(params string[] environmentVaria return !string.IsNullOrEmpty(firstValue) && foundVariables.Skip(1).All(v => v.Equals(firstValue, StringComparison.Ordinal)); } - public string GetEnvironmentVariable(string name) + public virtual string GetEnvironmentVariable(string name) { - if (GetEnviornmentVariables.ContainsKey(name)) + if (UserEnvironmentVariables?.ContainsKey(name) == true) { - return GetEnviornmentVariables[name]; + return UserEnvironmentVariables[name]; } var value = Environment.GetEnvironmentVariable(name); - if (string.IsNullOrWhiteSpace(value)) - { - return null; - } - - return value; + return string.IsNullOrWhiteSpace(value) ? string.Empty : value; } protected void AddEnviornmentVariable(string name) { - if (GetEnviornmentVariables.ContainsKey(name)) + if (UserEnvironmentVariables.ContainsKey(name)) { return; } @@ -86,7 +87,7 @@ protected void AddEnviornmentVariable(string name) return; } - GetEnviornmentVariables[name] = value; + UserEnvironmentVariables[name] = value; } } } diff --git a/Source/Codecov/Services/ContinuousIntegrationServers/GitHubAction.cs b/Source/Codecov/Services/ContinuousIntegrationServers/GitHubAction.cs index 42d174d..7ecd997 100644 --- a/Source/Codecov/Services/ContinuousIntegrationServers/GitHubAction.cs +++ b/Source/Codecov/Services/ContinuousIntegrationServers/GitHubAction.cs @@ -1,14 +1,21 @@ using System; -using Codecov.Utilities; namespace Codecov.Services.ContinuousIntegrationServers { internal class GitHubAction : ContinuousIntegrationServer { - private readonly Lazy _branch = new Lazy(LoadBranch); - private readonly Lazy _commit = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("GITHUB_SHA")); - private readonly Lazy _detecter = new Lazy(() => CheckEnvironmentVariables("GITHUB_ACTIONS")); - private readonly Lazy _slug = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("GITHUB_REPOSITORY")); + private readonly Lazy _branch; + private readonly Lazy _commit; + private readonly Lazy _detecter; + private readonly Lazy _slug; + + public GitHubAction() + { + _branch = new Lazy(LoadBranch); + _commit = new Lazy(() => GetEnvironmentVariable("GITHUB_SHA")); + _detecter = new Lazy(() => CheckEnvironmentVariables("GITHUB_ACTIONS")); + _slug = new Lazy(() => GetEnvironmentVariable("GITHUB_REPOSITORY")); + } public override string Branch => _branch.Value; @@ -20,9 +27,9 @@ internal class GitHubAction : ContinuousIntegrationServer public override string Slug => _slug.Value; - private static string LoadBranch() + private string LoadBranch() { - var branch = EnviornmentVariable.GetEnviornmentVariable("GITHUB_REF"); + var branch = GetEnvironmentVariable("GITHUB_REF"); return string.IsNullOrWhiteSpace(branch) ? string.Empty : branch.StartsWith("ref/heads/") ? branch.Substring(10) : branch; } diff --git a/Source/Codecov/Services/ContinuousIntegrationServers/IEnviornmentVariables.cs b/Source/Codecov/Services/ContinuousIntegrationServers/IEnviornmentVariables.cs index 347103f..a617588 100644 --- a/Source/Codecov/Services/ContinuousIntegrationServers/IEnviornmentVariables.cs +++ b/Source/Codecov/Services/ContinuousIntegrationServers/IEnviornmentVariables.cs @@ -4,7 +4,7 @@ namespace Codecov.Services.ContinuousIntegrationServers { internal interface IEnviornmentVariables { - IDictionary GetEnviornmentVariables { get; } + IDictionary UserEnvironmentVariables { get; } string GetEnvironmentVariable(string name); } diff --git a/Source/Codecov/Services/ContinuousIntegrationServers/Travis.cs b/Source/Codecov/Services/ContinuousIntegrationServers/Travis.cs index d1f481b..a412f64 100644 --- a/Source/Codecov/Services/ContinuousIntegrationServers/Travis.cs +++ b/Source/Codecov/Services/ContinuousIntegrationServers/Travis.cs @@ -1,19 +1,31 @@ using System; -using Codecov.Utilities; namespace Codecov.Services.ContinuousIntegrationServers { internal class Travis : ContinuousIntegrationServer { - private readonly Lazy _branch = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_BRANCH")); - private readonly Lazy _build = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_JOB_NUMBER")); - private readonly Lazy _buildUrl = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_JOB_WEB_URL")); - private readonly Lazy _commit = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_COMMIT")); - private readonly Lazy _detecter = new Lazy(() => CheckEnvironmentVariables("CI", "TRAVIS")); - private readonly Lazy _job = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_JOB_ID")); - private readonly Lazy _pr = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_PULL_REQUEST")); - private readonly Lazy _slug = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_REPO_SLUG")); - private readonly Lazy _tag = new Lazy(() => EnviornmentVariable.GetEnviornmentVariable("TRAVIS_TAG")); + private readonly Lazy _branch; + private readonly Lazy _build; + private readonly Lazy _buildUrl; + private readonly Lazy _commit; + private readonly Lazy _detecter; + private readonly Lazy _job; + private readonly Lazy _pr; + private readonly Lazy _slug; + private readonly Lazy _tag; + + public Travis() + { + _branch = new Lazy(() => GetEnvironmentVariable("TRAVIS_BRANCH")); + _build = new Lazy(() => GetEnvironmentVariable("TRAVIS_JOB_NUMBER")); + _buildUrl = new Lazy(() => GetEnvironmentVariable("TRAVIS_JOB_WEB_URL")); + _commit = new Lazy(() => GetEnvironmentVariable("TRAVIS_COMMIT")); + _detecter = new Lazy(() => CheckEnvironmentVariables("CI", "TRAVIS")); + _job = new Lazy(() => GetEnvironmentVariable("TRAVIS_JOB_ID")); + _pr = new Lazy(() => GetEnvironmentVariable("TRAVIS_PULL_REQUEST")); + _slug = new Lazy(() => GetEnvironmentVariable("TRAVIS_REPO_SLUG")); + _tag = new Lazy(() => GetEnvironmentVariable("TRAVIS_TAG")); + } public override string Branch => _branch.Value; diff --git a/Source/Codecov/Url/Query.cs b/Source/Codecov/Url/Query.cs index efd0a07..ad3eda4 100644 --- a/Source/Codecov/Url/Query.cs +++ b/Source/Codecov/Url/Query.cs @@ -14,13 +14,15 @@ internal class Query : IQuery { private readonly Lazy _getQuery; + private readonly IEnviornmentVariables _environmentVariables; + public Query(IQueryOptions options, IEnumerable repositories, IBuild build, IYaml yaml, IEnviornmentVariables environmentVariables) { Options = options; Repositories = repositories; Build = build; Yaml = yaml; - EnvironmentVariables = environmentVariables; + _environmentVariables = environmentVariables; SetQueryParameters(); _getQuery = new Lazy(() => string.Join("&", QueryParameters.Select(x => $"{x.Key}={x.Value ?? string.Empty}"))); } @@ -37,8 +39,6 @@ public Query(IQueryOptions options, IEnumerable repositories, IBuil private IYaml Yaml { get; } - private IEnviornmentVariables EnvironmentVariables; - private static string EscapeKnownProblematicCharacters(string data) { var knownChars = new Dictionary @@ -216,7 +216,7 @@ private void SetSlug() } } - var slugEnv = EnvironmentVariables.GetEnviornmentVariables["CODECOV_SLUG"]; + var slugEnv = _environmentVariables.GetEnvironmentVariable("CODECOV_SLUG"); if (!string.IsNullOrWhiteSpace(slugEnv)) { QueryParameters["slug"] = WebUtility.UrlEncode(slugEnv.Trim()); @@ -248,7 +248,7 @@ private void SetToken() { QueryParameters["token"] = string.Empty; - var tokenEnv = EnvironmentVariables.GetEnvironmentVariable("CODECOV_TOKEN"); + var tokenEnv = _environmentVariables.GetEnvironmentVariable("CODECOV_TOKEN"); if (!string.IsNullOrWhiteSpace(tokenEnv)) { QueryParameters["token"] = tokenEnv.RemoveAllWhiteSpace();