Skip to content

Commit

Permalink
Add tests for CI providers to ContinuousIntegrationBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas committed Aug 29, 2024
1 parent 5230e33 commit e9e2112
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/DotNet.ReproducibleBuilds/DotNet.ReproducibleBuilds.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@

<!-- Enable ContinuousIntegrationBuild when running on CI -->

<!-- Azure Pipelines / DevOpsServer -->
<!-- Azure Pipelines / DevOpsServer
https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#system-variables-devops-services
-->
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<!-- GitHub Actions -->
<!-- GitHub Actions
https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
-->
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<!-- AppVeyor -->
<!-- AppVeyor
https://www.appveyor.com/docs/environment-variables/
-->
<PropertyGroup Condition="'$(APPVEYOR)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
Expand All @@ -32,17 +38,23 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<!-- Travis CI -->
<!-- Travis CI
https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
-->
<PropertyGroup Condition="'$(TRAVIS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<!-- Circle CI -->
<!-- Circle CI
https://circleci.com/docs/variables/#built-in-environment-variables
-->
<PropertyGroup Condition="'$(CIRCLECI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<!-- AWS CodeBuild -->
<!-- AWS CodeBuild
https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
-->
<PropertyGroup Condition="'$(CODEBUILD_BUILD_ID)' != '' and '$(AWS_REGION)' != '' ">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using FluentAssertions;
using Microsoft.Build.Utilities.ProjectCreation;

namespace DotNet.ReproducibleBuilds.Tests;

public class ContinuousIntegrationTests : TestBase
{
private const string ContinuousIntegrationBuild = "ContinuousIntegrationBuild";

[Theory]
[InlineData(null, "")]
[InlineData(true, "true")]
[InlineData(false, "false")]
public void RespectsSetValue(bool? value, string expected)
{
using EnvironmentVariableSuppressor hostSuppressor = new("TF_BUILD"); // Suppress our own CI provider variables (i.e. Azure DevOps)

ProjectCreator.Templates
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.PropertyGroup()
.Property(ContinuousIntegrationBuild, value?.ToLowerInvariant())
.Project
.GetPropertyValue(ContinuousIntegrationBuild)
.Should().Be(expected);
}

[Theory]
[MemberData(nameof(MemberData))]
public void RespectsGlobalProperites(Dictionary<string, string> envVars)
{
using EnvironmentVariableSuppressor hostSuppressor = new("TF_BUILD"); // Suppress our own CI provider variables (i.e. Azure DevOps)

// If ContinuousIntegrationBuild is not set, it should be set from the CI provider property
ProjectCreator.Templates
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.ProjectWithGlobalProperties(envVars)
.GetPropertyValue(ContinuousIntegrationBuild)
.Should().Be(true.ToLowerInvariant());

// If ContinuousIntegrationBuild is set, it should take precedence over the CI provider variables
ProjectCreator.Templates
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.ProjectWithGlobalProperties(envVars.With(ContinuousIntegrationBuild, false.ToLowerInvariant()))
.GetPropertyValue(ContinuousIntegrationBuild)
.Should().Be(false.ToLowerInvariant(), "because explicitly setting `ContinuousIntegrationBuild` should always win.");
}

public static TheoryData<Dictionary<string, string>> MemberData()
{
return new TheoryData<Dictionary<string, string>>
{
{ new() { ["TF_BUILD"] = "True" } },
{ new() { ["GITHUB_ACTIONS"] = "true" } },
{ new() { ["APPVEYOR"] = "True" } },
{ new() { ["CI"] = "true" } },
{ new() { ["TRAVIS"] = "true" } },
{ new() { ["CIRCLECI"] = "true" } },
{ new() { ["CODEBUILD_BUILD_ID"] = "abc:123", ["AWS_REGION"] = "us-east-1" } },
{ new() { ["BUILD_ID"] = "123", ["BUILD_URL"] = "https://buildserver.invalid/jenkins/job/MyJobName/123/" } },
{ new() { ["BUILD_ID"] = "123", ["PROJECT_ID"] = "234" } },
{ new() { ["TEAMCITY_VERSION"] = "10" } },
{ new() { ["JB_SPACE_API_URL"] = "https://api.invalid/url" } },
};
}
}
7 changes: 7 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DotNet.ReproducibleBuilds.Tests;

internal static class DictionaryExtensions
{
public static IDictionary<TKey, TValue> With<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
=> new Dictionary<TKey, TValue>(dictionary) { [key] = value };
}
14 changes: 14 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/ProjectCreatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Build.Evaluation;
using Microsoft.Build.Utilities.ProjectCreation;

namespace DotNet.ReproducibleBuilds.Tests;

internal static class ProjectCreatorExtensions
{
public static Project ProjectWithGlobalProperties(this ProjectCreator creator, IDictionary<string, string> properties)
{
creator.TryGetProject(out Project project, properties);

return project;
}
}

0 comments on commit e9e2112

Please sign in to comment.