From 2c08eac6d3e2ebf326ca37e58802243f542127f4 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 6 Dec 2024 11:49:57 -0600 Subject: [PATCH] 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 | 6 +++++ 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, 107 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 10d4787a6..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 + + @@ -25,6 +29,8 @@ StdOutEncoding="utf-8" StdErrEncoding="utf-8" IgnoreExitCode="true" + IgnoreStandardErrorWarningFormat="true" + CustomErrorRegularExpression="^Error " Command=""$(CSharpier_dotnet_Path)" exec "$(CSharpierDllPath)" $(CSharpierArgs) --no-msbuild-check --compilation-errors-as-warnings "$(MSBuildProjectDirectory)" > $(NullOutput) " /> 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