Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
(maint) Updated CI classes to use custom method for getting environme…
Browse files Browse the repository at this point in the history
…nt variables
  • Loading branch information
AdmiringWorm committed Jun 9, 2020
1 parent 8d387bf commit 24d67dd
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 368 deletions.
1 change: 1 addition & 0 deletions Source/Codecov.Tests/Codecov.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="FluentAssertions" Version="5.10.0" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -47,13 +47,13 @@ public void Should_Include_EnviornmentVariables_From_ContiniousIntegrationServer
// Given
var options = Substitute.For<IEnviornmentVariablesOptions>();
var continuousIntegrationServer = Substitute.For<IContinuousIntegrationServer>();
continuousIntegrationServer.GetEnviornmentVariables.Returns(new Dictionary<string, string> { { "foo", "bar" }, { "fizz", "bizz" } });
continuousIntegrationServer.UserEnvironmentVariables.Returns(new Dictionary<string, string> { { "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");
Expand All @@ -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");
Expand Down
12 changes: 6 additions & 6 deletions Source/Codecov.Tests/Coverage/Report/ReportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public void If_DisableNetwork_Is_True_The_Source_Code_Should_Be_Empty()
{
// Given
var enviornmentVariables = Substitute.For<IEnviornmentVariables>();
enviornmentVariables.GetEnviornmentVariables.Returns(new Dictionary<string, string> { { "foo", "bar" }, { "fizz", "bizz" } });
enviornmentVariables.UserEnvironmentVariables.Returns(new Dictionary<string, string> { { "foo", "bar" }, { "fizz", "bizz" } });
var options = Substitute.For<IReportOptions>();
options.DisableNetwork = true;
var sourceCode = Substitute.For<ISourceCode>();
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<ICoverage>();
Expand All @@ -45,13 +45,13 @@ public void Should_Generate_A_Report()
{
// Given
var enviornmentVariables = Substitute.For<IEnviornmentVariables>();
enviornmentVariables.GetEnviornmentVariables.Returns(new Dictionary<string, string> { { "foo", "bar" }, { "fizz", "bizz" } });
enviornmentVariables.UserEnvironmentVariables.Returns(new Dictionary<string, string> { { "foo", "bar" }, { "fizz", "bizz" } });
var options = Substitute.For<IReportOptions>();
options.DisableNetwork = false;
var sourceCode = Substitute.For<ISourceCode>();
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<ICoverage>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<object[]> 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)
Expand All @@ -38,8 +38,9 @@ public static IEnumerable<object[]> 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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH")).Returns(string.Empty);
var appVeyor = av.Object;

// When
var branch = appVeyor.Branch;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH")).Returns("develop");
var appVeyor = av.Object;

// When
var branch = appVeyor.Branch;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns(string.Empty);
var appVeyor = av.Object;

// When
var build = appVeyor.Build;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_JOB_ID")).Returns("Job 123");
var appVeyor = av.Object;

// When
var build = appVeyor.Build;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_COMMIT")).Returns(string.Empty);
var appVeyor = av.Object;

// When
var commit = appVeyor.Commit;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_COMMIT")).Returns("123");
var appVeyor = av.Object;

// When
var commit = appVeyor.Commit;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { 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;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER")).Returns(string.Empty);
var appVeyor = av.Object;

// When
var pr = appVeyor.Pr;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER")).Returns("123");
var appVeyor = av.Object;

// When
var pr = appVeyor.Pr;
Expand All @@ -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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_NAME")).Returns(string.Empty);
var appVeyor = av.Object;

// When
var slug = appVeyor.Slug;
Expand All @@ -283,39 +295,15 @@ 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<AppVeyor>() { CallBase = true };
av.Setup(s => s.GetEnvironmentVariable("APPVEYOR_REPO_NAME")).Returns("foo/bar");
var appVeyor = av.Object;

// When
var slug = appVeyor.Slug;

// 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);
}
}
}
}
Loading

0 comments on commit 24d67dd

Please sign in to comment.