Skip to content

Commit

Permalink
Add tests for CI providers to RepositoryBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas committed Aug 27, 2024
1 parent 509ec14 commit b18402c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace DotNet.ReproducibleBuilds.Tests;

internal sealed class EnvironmentVariableSuppressor : IDisposable
{
private readonly string? _value;
private readonly string _name;

public EnvironmentVariableSuppressor(string name)
{
_name = name;
_value = Environment.GetEnvironmentVariable(name);
Environment.SetEnvironmentVariable(name, null);
}

public void Dispose()
{
Environment.SetEnvironmentVariable(_name, _value);
}
}
8 changes: 4 additions & 4 deletions tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ internal static class ProjectTemplates
{
private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(ProjectTemplates).Assembly.Location)!;

public static ProjectCreator ReproducibleBuildProject(this ProjectCreatorTemplates templates, DirectoryInfo path)
public static ProjectCreator ReproducibleBuildProject(this ProjectCreatorTemplates templates, FileInfo project)
{
FileInfo project = new(path.Combine("test.csproj"));
DirectoryInfo directory = project.Directory ?? throw new ArgumentException("Project's path does not appear to have a parent.", nameof(project));

_ = ProjectCreator
.Create(path: path.Combine("obj", $"{project.Name}.tests.g.props"))
.Create(path: directory.Combine("obj", $"{project.Name}.tests.g.props"))
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.props"))
.Save();

_ = ProjectCreator
.Create(path: path.Combine("obj", $"{project.Name}.tests.g.targets"))
.Create(path: directory.Combine("obj", $"{project.Name}.tests.g.targets"))
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.targets"))
.Save();

Expand Down
59 changes: 56 additions & 3 deletions tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SourceLinkTests : TestBase
public void PublishRepositoryUrlIsSet(bool? publishRepositoryUrl, bool expected)
{
ProjectCreator.Templates
.ReproducibleBuildProject(TestRootPath)
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.PropertyGroup()
.Property("PublishRepositoryUrl", publishRepositoryUrl.ToLowerInvariant())
.Project
Expand All @@ -27,7 +27,7 @@ public void PublishRepositoryUrlIsSet(bool? publishRepositoryUrl, bool expected)
public void DebugTypeIsSet(string? debugType, string expected)
{
ProjectCreator.Templates
.ReproducibleBuildProject(TestRootPath)
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.PropertyGroup()
.Property("DebugType", debugType)
.Project
Expand All @@ -42,11 +42,64 @@ public void DebugTypeIsSet(string? debugType, string expected)
public void EmbedUntrackedSourcesIsSet(bool? embedUntrackedSources, bool expected)
{
ProjectCreator.Templates
.ReproducibleBuildProject(TestRootPath)
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.PropertyGroup()
.Property("PublishRepositoryUrl", embedUntrackedSources.ToLowerInvariant())
.Project
.GetPropertyValue("PublishRepositoryUrl")
.Should().Be(expected.ToLowerInvariant());
}

[Theory]
[InlineData("GITHUB_REF", "refs/pull/1234/merge", "pr1234")]
[InlineData("GITHUB_REF", "refs/heads/my-branch", "my-branch")]
[InlineData("GITHUB_REF", "refs/tags/v1.2.3", "v1.2.3")]

[InlineData("BUILD_SOURCEBRANCH", "refs/heads/my-branch", "my-branch")]
[InlineData("BUILD_SOURCEBRANCH", "refs/tags/v1.2.3", "v1.2.3")]

[InlineData("APPVEYOR_PULL_REQUEST_NUMBER", "1234", "pr1234")]
[InlineData("APPVEYOR_REPO_TAG_NAME", "refs/tags/v1.2.3", "refs/tags/v1.2.3")]
[InlineData("APPVEYOR_REPO_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")]

[InlineData("TEAMCITY_BUILD_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")]

[InlineData("TRAVIS_PULL_REQUEST", "1234", "pr1234")]
[InlineData("TRAVIS_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")]

[InlineData("CIRCLE_PR_NUMBER", "1234", "pr1234")]
[InlineData("CIRCLE_TAG", "refs/heads/v1.2.3", "refs/heads/v1.2.3")]
[InlineData("CIRCLE_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")]

[InlineData("CI_COMMIT_TAG", "refs/tags/v1.2.3", "refs/tags/v1.2.3")]
[InlineData("CI_MERGE_REQUEST_IID", "1234", "pr1234")]
[InlineData("CI_COMMIT_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")]

[InlineData("BUDDY_EXECUTION_PULL_REQUEST_NO", "1234", "pr1234")]
[InlineData("BUDDY_EXECUTION_TAG", "refs/tags/v1.2.3", "refs/tags/v1.2.3")]
[InlineData("BUDDY_EXECUTION_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")]
public void RepositoryBranchIsSet(string ci, string original, string expected)
{
using EnvironmentVariableSuppressor hostSuppressor = new("BUILD_SOURCEBRANCH"); // Suppress our own CI provider variables (i.e. Azure DevOps)
using EnvironmentVariableSuppressor ciSuppressor = new(ci); // Suppress the mock CI provider (just in case).

// If RepositoryBranch is set, it should take precedence over the CI provider variables
ProjectCreator.Templates
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.PropertyGroup()
.Property("RepositoryBranch", "explicitly-set")
.Property(ci, original)
.Project
.GetPropertyValue("RepositoryBranch")
.Should().Be("explicitly-set", "because explicitly setting `RepositoryBranch` should always win.");

// If RepositoryBranch is not set, it should be set from the CI provider property
ProjectCreator.Templates
.ReproducibleBuildProject(GetRandomFile(".csproj"))
.PropertyGroup()
.Property(ci, original)
.Project
.GetPropertyValue("RepositoryBranch")
.Should().Be(expected);
}
}
5 changes: 5 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ protected virtual void Dispose(bool isDisposing)
}
}
}

protected FileInfo GetRandomFile(string? extension = null)
{
return new(TestRootPath.Combine($"{Path.GetRandomFileName()}{extension ?? string.Empty}"));
}
}

0 comments on commit b18402c

Please sign in to comment.