generated from dotnet/new-repo
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test cases for detecting CI environments and setting ContinuousIntegrationBuild #49
Merged
baronfel
merged 4 commits into
dotnet:main
from
MattKotsenas:refactor/ci-property-tests
Aug 30, 2024
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5230e33
Simplify RepositoryBranch tests
MattKotsenas e9e2112
Add tests for CI providers to ContinuousIntegrationBuild
MattKotsenas 04ab71c
Create new ProjectCollection per test
MattKotsenas d8255e2
Merge branch 'main' into refactor/ci-property-tests
baronfel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
tests/DotNet.ReproducibleBuilds.Tests/ContinuousIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
tests/DotNet.ReproducibleBuilds.Tests/DictionaryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
tests/DotNet.ReproducibleBuilds.Tests/ProjectCreatorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subtle detail, good catch