Skip to content

Commit

Permalink
feat: Skip execution of the "ValidateCodeFormatting" task when build …
Browse files Browse the repository at this point in the history
…is running on Azure Pipelines

Running dotnet format on Azure Pipelines when using the .NET 9 SDK and Nerdbank.GitVersioning is installed causes the build to hang (see dotnet/sdk#44951).

To work around this, skip the task when running on Azure Pipelines and emit a warning about the task being skipped

Pull-Request: #142
  • Loading branch information
ap0llo authored Dec 19, 2024
1 parent 060d30b commit e6b1d12
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/SharedBuild.Test/Mocks/FakeAzurePipelinesContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Cake.Common.Build.AzurePipelines;
using Cake.Common.Build.AzurePipelines.Data;
using Cake.Core.Diagnostics;

namespace Grynwald.SharedBuild.Test.Mocks
{
internal class FakeAzurePipelinesContext : IAzurePipelinesContext
{
public IAzurePipelinesArtifactNames ArtifactNames => throw new NotImplementedException();

public bool IsActive
{
get => IsRunningOnAzurePipelines;
set => IsRunningOnAzurePipelines = value;
}

public bool IsRunningOnAzurePipelines { get; set; }

public AzurePipelinesEnvironmentInfo Environment => throw new NotImplementedException();

public IAzurePipelinesCommands Commands => throw new NotImplementedException();

public void PrintToLog(ICakeLog log)
{
throw new NotImplementedException();
}
}
}
10 changes: 8 additions & 2 deletions src/SharedBuild.Test/Mocks/FakeBuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public FilePath SolutionPath
set => m_SolutionPath = value;
}

public IAzurePipelinesContext AzurePipelines => throw new NotImplementedException();
/// <summary>
/// Gets the mock for <see cref="IBuildContext.AzurePipelines"/>
/// </summary>
public FakeAzurePipelinesContext AzurePipelines { get; } = new();

/// <inheritdoc />
IAzurePipelinesContext IBuildContext.AzurePipelines => AzurePipelines;

public IBuildSettings BuildSettings => throw new NotImplementedException();

Expand All @@ -43,7 +49,7 @@ public FilePath SolutionPath
public IReadOnlyCollection<IPushTarget> PushTargets => throw new NotImplementedException();

/// <summary>
/// Mock object for <see cref="IBuildContext.CodeFormattingSettings"/>
/// Gets the mock for <see cref="IBuildContext.CodeFormattingSettings"/>
/// </summary>
public FakeCodeFormattingSettings CodeFormattingSettings { get; } = new();

Expand Down
21 changes: 20 additions & 1 deletion src/SharedBuild.Test/Tasks/ValidateCodeFormattingTaskTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cake.Core.IO;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Grynwald.SharedBuild.Tasks;
using Grynwald.SharedBuild.Test.Mocks;
using Xunit;
Expand Down Expand Up @@ -28,6 +29,24 @@ public void Task_can_be_enabled_and_disabled_through_code_formatting_settings(bo
Assert.Equal(enableAutomaticFormatting, shouldRun);
}

[Fact]
public void Task_is_skipped_when_build_is_running_on_Azure_Pipelies()
{
// ARRANGE
var context = new FakeBuildContext();
context.CodeFormattingSettings.EnableAutomaticFormatting = true;
context.AzurePipelines.IsRunningOnAzurePipelines = true;

var sut = new ValidateCodeFormattingTask();

// ACT
var shouldRun = sut.ShouldRun(context);

// ASSERT
Assert.False(shouldRun);
var warning = Assert.Single(context.Log.Entries, x => x.Level == LogLevel.Warning);
Assert.Equal("Skipping task ValidateCodeFormatting since the build is running on Azure Pipelines. This is a workaround for https://github.com/dotnet/sdk/issues/44951", warning.Message);
}

[Fact]
public void Task_starts_dotnet_format_with_expected_parameters()
Expand Down
21 changes: 20 additions & 1 deletion src/SharedBuild/Tasks/ValidateCodeFormattingCodeTask.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Format;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting;

namespace Grynwald.SharedBuild.Tasks
Expand All @@ -10,7 +11,25 @@ namespace Grynwald.SharedBuild.Tasks
[IsDependeeOf(typeof(ValidateTask))]
public class ValidateCodeFormattingTask : FrostingTask<IBuildContext>
{
public override bool ShouldRun(IBuildContext context) => context.CodeFormattingSettings.EnableAutomaticFormatting;
public override bool ShouldRun(IBuildContext context)
{
if (!context.CodeFormattingSettings.EnableAutomaticFormatting)
{
return false;
}


// Running dotnet format on Azure Pipelines when using the .NET 9 SDK and Nerdbank.GitVersioning causes the build to hang
// See: https://github.com/dotnet/sdk/issues/44951
// To work around this, skip the task when running on Azure Pipelines
if (context.AzurePipelines.IsRunningOnAzurePipelines)
{
context.Log.Warning($"Skipping task {TaskNames.ValidateCodeFormatting} since the build is running on Azure Pipelines. This is a workaround for https://github.com/dotnet/sdk/issues/44951");
return false;
}

return true;
}

public override void Run(IBuildContext context)
{
Expand Down

0 comments on commit e6b1d12

Please sign in to comment.