From f9fa29e9b3b98ed4d7a391697b7a39ade59446ee Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 6 Dec 2024 11:49:57 -0600 Subject: [PATCH 1/6] Fixing problem where linux doesn't work the same as windows with csharpier.msbuild. Add new msbuild tests. closes #1357 --- .csharpierignore | 4 ++- .github/workflows/validate_pull_request.yml | 10 +++++--- Src/CSharpier.MsBuild/README.md | 17 ++++++++++--- .../build/CSharpier.MsBuild.targets | 4 +++ Tests/MsBuild/.csharpierignore | 0 Tests/MsBuild/Directory.Build.props | 5 ++++ Tests/MsBuild/Run.ps1 | 25 ++++++++++++++++++- Tests/MsBuild/Scenarios.json | 8 +++--- .../FileWithCompileError.cs | 4 +++ .../Project.csproj | 12 +++++++++ .../UnformattedFileCausesError/Project.csproj | 12 +++++++++ .../UnformattedFile.cs | 7 ++++++ Tests/MsBuild/nuget.config | 10 ++++++++ 13 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 Tests/MsBuild/.csharpierignore create mode 100644 Tests/MsBuild/Directory.Build.props create mode 100644 Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/FileWithCompileError.cs create mode 100644 Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/Project.csproj create mode 100644 Tests/MsBuild/TestCases/UnformattedFileCausesError/Project.csproj create mode 100644 Tests/MsBuild/TestCases/UnformattedFileCausesError/UnformattedFile.cs diff --git a/.csharpierignore b/.csharpierignore index 13f737722..236410b22 100644 --- a/.csharpierignore +++ b/.csharpierignore @@ -1 +1,3 @@ -Uploads/ \ No newline at end of file +Uploads/ + +Tests/MsBuild/TestCases/ diff --git a/.github/workflows/validate_pull_request.yml b/.github/workflows/validate_pull_request.yml index 14fded337..777185432 100644 --- a/.github/workflows/validate_pull_request.yml +++ b/.github/workflows/validate_pull_request.yml @@ -27,11 +27,15 @@ jobs: dotnet tool restore dotnet csharpier . --check test_msbuild: - runs-on: ubuntu-latest + strategy: + matrix: + os: [ ubuntu-latest, windows-latest ] + runs-on: ${{ matrix.os }} name: Test CSharpier.MSBuild steps: - uses: actions/checkout@v2 - uses: actions/setup-dotnet@v3 - - run: | + - env: + GithubOS: ${{matrix.os}} + run: | pwsh ./Tests/MsBuild/Run.ps1 - diff --git a/Src/CSharpier.MsBuild/README.md b/Src/CSharpier.MsBuild/README.md index 8b0903a35..2226efa94 100644 --- a/Src/CSharpier.MsBuild/README.md +++ b/Src/CSharpier.MsBuild/README.md @@ -4,7 +4,16 @@ One way to test the changes in the build/* files - Edit those files at `C:\Users\[Username]\.nuget\packages\csharpier.msbuild\[VersionNumber]\build` - Ensure you revert those files and make the same changes to the files here. -Another way -- the validate PR GH action does this, currently only uses the net8 sdk -- dotnet pack Src/CSharpier.MsBuild/CSharpier.MsBuild.csproj -o nupkg /p:Version=0.0.1 -- docker build -f ./Tests/CSharpier.MsBuild.Test/Dockerfile . +Some automated tests exist +- the validate PR GH action runs these, mostly around framework versions +- cd ./Tests/MsBuild +- ./Run.ps1 + +Other things that would be really really nice to automate +- exits properly in release when no files formatted + https://github.com/belav/csharpier/issues/1357 +- same as above if thing set +- formats files in debug +- formats files if told to in release +- a few scenarios in here I think, compilation errors etc + https://github.com/belav/csharpier/issues/1131 diff --git a/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets b/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets index f5c7c8255..211ac9221 100644 --- a/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets +++ b/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets @@ -11,12 +11,16 @@ $(TargetFramework) $(TargetFrameworks.Split(';')[0]) NUL + true /dev/null + false + + diff --git a/Tests/MsBuild/.csharpierignore b/Tests/MsBuild/.csharpierignore new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/MsBuild/Directory.Build.props b/Tests/MsBuild/Directory.Build.props new file mode 100644 index 000000000..c416fb784 --- /dev/null +++ b/Tests/MsBuild/Directory.Build.props @@ -0,0 +1,5 @@ + + + false + + \ No newline at end of file diff --git a/Tests/MsBuild/Run.ps1 b/Tests/MsBuild/Run.ps1 index adb76860b..810afa2cf 100644 --- a/Tests/MsBuild/Run.ps1 +++ b/Tests/MsBuild/Run.ps1 @@ -19,9 +19,15 @@ New-Item $basePath -ItemType Directory | Out-Null $failureMessage = "" foreach ($scenario in $scenarios) { + # these fail on windows in GH and because they use dockerfiles for the scenarios we don't need to run them twice anyway + if ($env:GithubOS -eq "windows-latest") { + continue + } + Write-Host "::group::$($scenario.name)" $scenarioPath = Join-Path $basePath $scenario.name + Write-Host $scenarioPath New-Item $scenarioPath -ItemType Directory | Out-Null $dockerFile = Join-Path $scenarioPath "DockerFile" @@ -38,7 +44,7 @@ RUN dotnet build -c Release $csprojFile = Join-Path $scenarioPath "Project.csproj" $csharpierFrameworkVersion = "" - if ($null -ne $scenario.csharpier_frameworkVersion) { + if ([bool]($scenario.PSobject.Properties.name -match "csharpier_frameworkVersion")) { $csharpierFrameworkVersion = " $($scenario.csharpier_frameworkVersion) " @@ -67,6 +73,23 @@ RUN dotnet build -c Release Write-Host "::endgroup::" } + +Write-Host "::group::UnformattedFileCausesError" + +$output = (& dotnet build -c Release ./TestCases/UnformattedFileCausesError/Project.csproj) | Out-String +Write-Host $output +if ($LASTEXITCODE -ne 1) { + $failureMessage += "::error::The TestCase UnformattedFileCausesError did not return an exit code of 1`n" +} +# test output to see what is in it + +Write-Host "::endgroup::" + + +# OneError +# any other scenarior to test? + + if ($failureMessage -ne "") { Write-Host $failureMessage exit 1 diff --git a/Tests/MsBuild/Scenarios.json b/Tests/MsBuild/Scenarios.json index a960fdf48..cb1868f20 100644 --- a/Tests/MsBuild/Scenarios.json +++ b/Tests/MsBuild/Scenarios.json @@ -1,21 +1,21 @@ [ { - "name": "sdk:8.0_netstandard2.0", + "name": "sdk-8.0_netstandard2.0", "sdk": "mcr.microsoft.com/dotnet/sdk:8.0", "targetFrameworks": "netstandard2.0" }, { - "name": "sdk:8.0_net8.0-windows", + "name": "sdk-8.0_net8.0-windows", "sdk": "mcr.microsoft.com/dotnet/sdk:8.0", "targetFrameworks": "net8.0-windows" }, { - "name": "sdk:8.0_net6.0;net7.07", + "name": "sdk-8.0_net6.0;net7.07", "sdk": "mcr.microsoft.com/dotnet/sdk:8.0", "targetFrameworks": "net6.0;net7.0" }, { - "name": "sdk:8.0_csharpier-net8.0", + "name": "sdk-8.0_csharpier-net8.0", "sdk": "mcr.microsoft.com/dotnet/sdk:8.0", "targetFrameworks": "netstandard2.0", "csharpier_frameworkVersion": "net8.0" diff --git a/Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/FileWithCompileError.cs b/Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/FileWithCompileError.cs new file mode 100644 index 000000000..47f23fa9d --- /dev/null +++ b/Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/FileWithCompileError.cs @@ -0,0 +1,4 @@ +namespace Net8; + +public class Class1 +{ diff --git a/Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/Project.csproj b/Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/Project.csproj new file mode 100644 index 000000000..f719d5263 --- /dev/null +++ b/Tests/MsBuild/TestCases/FileThatCantCompileCausesOneError/Project.csproj @@ -0,0 +1,12 @@ + + + net8.0 + false + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Tests/MsBuild/TestCases/UnformattedFileCausesError/Project.csproj b/Tests/MsBuild/TestCases/UnformattedFileCausesError/Project.csproj new file mode 100644 index 000000000..f719d5263 --- /dev/null +++ b/Tests/MsBuild/TestCases/UnformattedFileCausesError/Project.csproj @@ -0,0 +1,12 @@ + + + net8.0 + false + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Tests/MsBuild/TestCases/UnformattedFileCausesError/UnformattedFile.cs b/Tests/MsBuild/TestCases/UnformattedFileCausesError/UnformattedFile.cs new file mode 100644 index 000000000..bb5ff02b8 --- /dev/null +++ b/Tests/MsBuild/TestCases/UnformattedFileCausesError/UnformattedFile.cs @@ -0,0 +1,7 @@ +namespace Net8; + +public class Class1 +{ + + +} diff --git a/Tests/MsBuild/nuget.config b/Tests/MsBuild/nuget.config index 09743ecda..4fea3fab9 100644 --- a/Tests/MsBuild/nuget.config +++ b/Tests/MsBuild/nuget.config @@ -1,6 +1,16 @@ + + + + + + + + + + \ No newline at end of file From d3caf3731390c2359918674c70755ae5eb20ac3a Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 7 Dec 2024 12:29:35 -0600 Subject: [PATCH 2/6] Making some logic re-usable --- Tests/MsBuild/Run.ps1 | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/Tests/MsBuild/Run.ps1 b/Tests/MsBuild/Run.ps1 index 810afa2cf..5a80983d1 100644 --- a/Tests/MsBuild/Run.ps1 +++ b/Tests/MsBuild/Run.ps1 @@ -75,19 +75,16 @@ RUN dotnet build -c Release Write-Host "::group::UnformattedFileCausesError" - -$output = (& dotnet build -c Release ./TestCases/UnformattedFileCausesError/Project.csproj) | Out-String -Write-Host $output -if ($LASTEXITCODE -ne 1) { - $failureMessage += "::error::The TestCase UnformattedFileCausesError did not return an exit code of 1`n" -} -# test output to see what is in it - +$output = [TestHelper]::RunTestCase("UnformattedFileCausesError", $true) +# TODO do we need to test output? Write-Host "::endgroup::" +Write-Host "::group::FileThatCantCompileCausesOneError" +$output = [TestHelper]::RunTestCase("FileThatCantCompileCausesOneError", $true) +# TODO what do we need to look for in the output? +Write-Host "::endgroup::" -# OneError -# any other scenarior to test? +# TODO any other scenarior to test? if ($failureMessage -ne "") { @@ -95,3 +92,21 @@ if ($failureMessage -ne "") { exit 1 } +class TestHelper { + static [string] RunTestCase([string] $testCase, [bool] $expectErrorCode) { + $output = (& dotnet build -c Release ./TestCases/$($testCase)/Project.csproj) | Out-String + Write-Host $output + + $expectedExitCode = 0 + if ($expectErrorCode -eq $true) { + $expectedExitCode = 1 + } + + if ($LASTEXITCODE -ne $expectedExitCode) { + $failureMessage += "::error::The TestCase $testCase did not return an exit code of $expectedExitCode`n" + } + + return $output + } +} + From bb50680365e2ccf3fe2374084ee1f2595c9581a8 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 7 Dec 2024 12:34:54 -0600 Subject: [PATCH 3/6] reintroduce this problem so we can test for it. --- Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets b/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets index 211ac9221..4afb7752f 100644 --- a/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets +++ b/Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets @@ -11,12 +11,10 @@ $(TargetFramework) $(TargetFrameworks.Split(';')[0]) NUL - true /dev/null - false @@ -31,7 +29,7 @@ IgnoreExitCode="true" IgnoreStandardErrorWarningFormat="true" CustomErrorRegularExpression="^Error " - Command=""$(CSharpier_dotnet_Path)" exec "$(CSharpierDllPath)" $(CSharpierArgs) --no-msbuild-check --compilation-errors-as-warnings "$(MSBuildProjectDirectory)" > $(NullOutput) " /> + Command=""$(CSharpier_dotnet_Path)" exec "$(CSharpierDllPath)" $(CSharpierArgs) --no-msbuild-check "$(MSBuildProjectDirectory)" > $(NullOutput) " /> - diff --git a/Tests/MsBuild/Run.ps1 b/Tests/MsBuild/Run.ps1 index 9c04d17f7..693c66fd6 100644 --- a/Tests/MsBuild/Run.ps1 +++ b/Tests/MsBuild/Run.ps1 @@ -19,7 +19,7 @@ New-Item $basePath -ItemType Directory | Out-Null $failureMessages = @() foreach ($scenario in $scenarios) { - # these fail on windows in GH and because they use dockerfiles for the scenarios we don't need to run them twice anyway + # these fail on windows in GH and because they use docker for the scenarios we don't need to run them twice anyway if ($env:GithubOS -eq "windows-latest") { continue }