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

Commit

Permalink
(GH-114) Implemented dynamic loading of ci server parsers
Browse files Browse the repository at this point in the history
resolves #114
  • Loading branch information
AdmiringWorm committed Jun 22, 2020
1 parent 7b649d1 commit a7ee280
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
using System;
using Codecov.Factories;
using Codecov.Services.ContinuousIntegrationServers;
using FluentAssertions;
using Xunit;

namespace Codecov.Tests.Factories
{
public class ContinuousIntegrationServerFactoryTests
{
public ContinuousIntegrationServerFactoryTests()
{
var environmentVariables = new[] {
"CI",
"APPVEYOR",
"TF_BUILD",
"GITHUB_ACTIONS",
"GITHUB_ACTION",
"JENKINS_URL",
"TEAMCITY_VERSION",
"TRAVIS"
};

foreach (var environmentVariable in environmentVariables)
{
Environment.SetEnvironmentVariable(environmentVariable, null, EnvironmentVariableTarget.Process);
}
}

#region AppVeyor Detection

[Fact]
public void Create_ShouldNotDetectAppVeyorWhenAppveyorIsNull()
{
Environment.SetEnvironmentVariable("APPVEYOR", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<AppVeyor>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectAppVeyorWhenAppveyorIsFalse()
{
Environment.SetEnvironmentVariable("APPVEYOR", "False");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<AppVeyor>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectAppVeyorWhenCiIsNull()
{
Environment.SetEnvironmentVariable("CI", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<AppVeyor>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectAppVeyorWhenCiIsFalse()
{
Environment.SetEnvironmentVariable("CI", "False");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<AppVeyor>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldDetectAppVeyorWhenCiAndAppVeyorIsTrue()
{
Environment.SetEnvironmentVariable("CI", "True");
Environment.SetEnvironmentVariable("APPVEYOR", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<AppVeyor>();
}

#endregion AppVeyor Detection

#region Azure Pipelines Detection

[Fact]
public void Create_ShouldNotDetectAzurePipelinesWhenTfBuildIsNull()
{
var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<AzurePipelines>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectAzurePipelinesWhenTfBuildIsFalse()
{
Environment.SetEnvironmentVariable("TF_BUILD", "False");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<AzurePipelines>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldDetectAzurePipelinesWhenTfBuildIsTrue()
{
Environment.SetEnvironmentVariable("TF_BUILD", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<AzurePipelines>();
}

#endregion Azure Pipelines Detection

#region GitHub Action Detection

[Fact]
public void Create_ShouldNotDetectGitHubActionWhenGitHubActionsAndGitHubActionIsNull()
{
var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<GitHubAction>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectGitHubActionWhenGitHubActionsIsFalse()
{
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", "False");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<GitHubAction>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldDetectGitHubActionWhenGitHubActionsIsTrue()
{
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<GitHubAction>();
}

[Fact]
public void Create_ShouldDetectGitHubActionWhenGitHubActionIsNotNull()
{
Environment.SetEnvironmentVariable("GITHUB_ACTION", "Some-Kind-Of-Value");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<GitHubAction>();
}

#endregion GitHub Action Detection

#region Jenkins Detection

[Fact]
public void Create_ShouldNotDetectJenkinsWhenJenkinsUrlIsNull()
{
var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<Jenkins>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldDetectJenkinsWhenJenkinsUrlIsNotNull()
{
Environment.SetEnvironmentVariable("JENKINS_URL", "https://example.org");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<Jenkins>();
}

#endregion Jenkins Detection

#region TeamCity Detection

[Fact]
public void Create_ShouldNotDetectTeamcityWhenTeamcityVersionIsNull()
{
var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<TeamCity>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldDetectTeamcityWhenTeamcityVersionIsNotNull()
{
Environment.SetEnvironmentVariable("TEAMCITY_VERSION", "1.0.0");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<TeamCity>();
}

#endregion TeamCity Detection

#region Travis Detection

[Fact]
public void Create_ShouldNotDetectTravisWhenTravisIsNull()
{
Environment.SetEnvironmentVariable("TRAVIS", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<Travis>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectTravisWhenTravisIsFalse()
{
Environment.SetEnvironmentVariable("TRAVIS", "False");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<Travis>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectTravisWhenCiIsNull()
{
Environment.SetEnvironmentVariable("CI", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<Travis>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldNotDetectTravisWhenCiIsFalse()
{
Environment.SetEnvironmentVariable("CI", "False");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().NotBeOfType<Travis>().And.BeOfType<ContinuousIntegrationServer>();
}

[Fact]
public void Create_ShouldDetectTravisWhenCiAndTravisIsTrue()
{
Environment.SetEnvironmentVariable("CI", "True");
Environment.SetEnvironmentVariable("TRAVIS", "True");

var ci = ContinuousIntegrationServerFactory.Create();

ci.Should().BeOfType<Travis>();
}

#endregion Travis Detection
}
}
24 changes: 22 additions & 2 deletions Source/Codecov/Factories/ContinuousIntegrationServerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Codecov.Services.ContinuousIntegrationServers;

Expand All @@ -7,9 +9,27 @@ internal static class ContinuousIntegrationServerFactory
{
public static IContinuousIntegrationServer Create()
{
var continuousIntegrationServers = new IContinuousIntegrationServer[] { new AppVeyor(), new Travis(), new TeamCity(), new AzurePipelines(), new Jenkins(), new GitHubAction() };
var buildServer = continuousIntegrationServers.FirstOrDefault(x => x.Detecter);
var assembly = typeof(ContinuousIntegrationServerFactory).Assembly;
var interfaceType = typeof(IContinuousIntegrationServer);

var continuousServers = assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract && interfaceType.IsAssignableFrom(t));
var buildServer = GetFirstDetectedCiServer(continuousServers);

return buildServer ?? new ContinuousIntegrationServer();
}

private static IContinuousIntegrationServer GetFirstDetectedCiServer(IEnumerable<Type> supportedServers)
{
foreach (var t in supportedServers)
{
var buildServer = (IContinuousIntegrationServer)Activator.CreateInstance(t);
if (buildServer.Detecter)
{
return buildServer;
}
}

return null;
}
}
}

0 comments on commit a7ee280

Please sign in to comment.