Skip to content

Commit

Permalink
Fixing problem where linux doesn't work the same as windows with csha…
Browse files Browse the repository at this point in the history
…rpier.msbuild. Add new msbuild tests.

closes #1357
  • Loading branch information
belav committed Dec 7, 2024
1 parent 384f7a1 commit 2c08eac
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .csharpierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Uploads/
Uploads/

Tests/MsBuild/TestCases/
10 changes: 7 additions & 3 deletions .github/workflows/validate_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 13 additions & 4 deletions Src/CSharpier.MsBuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
<FirstTargetFramework Condition=" '$(TargetFrameworks)' == '' ">$(TargetFramework)</FirstTargetFramework>
<FirstTargetFramework Condition=" '$(FirstTargetFrameworks)' == '' ">$(TargetFrameworks.Split(';')[0])</FirstTargetFramework>
<NullOutput>NUL</NullOutput>
<IgnoreExitCode>true</IgnoreExitCode>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<NullOutput>/dev/null</NullOutput>
<IgnoreExitCode>false</IgnoreExitCode>
</PropertyGroup>

<!-- https://github.com/belav/csharpier/issues/1131 -->

<Target Name="CSharpierFormatInner" Condition="'$(CSharpier_Bypass)' != 'true'">
<!-- IgnoreExitCode cleans up the output when files aren't formatted and fail the check -->
<!-- &gt; NullOutput suppresses the output from this so that compilation errors will show up a single time -->
Expand All @@ -25,6 +29,8 @@
StdOutEncoding="utf-8"
StdErrEncoding="utf-8"
IgnoreExitCode="true"
IgnoreStandardErrorWarningFormat="true"
CustomErrorRegularExpression="^Error "
Command="&quot;$(CSharpier_dotnet_Path)&quot; exec &quot;$(CSharpierDllPath)&quot; $(CSharpierArgs) --no-msbuild-check --compilation-errors-as-warnings &quot;$(MSBuildProjectDirectory)&quot; &gt; $(NullOutput) " />
</Target>

Expand Down
Empty file added Tests/MsBuild/.csharpierignore
Empty file.
5 changes: 5 additions & 0 deletions Tests/MsBuild/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>
25 changes: 24 additions & 1 deletion Tests/MsBuild/Run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = "
<CSharpier_FrameworkVersion>$($scenario.csharpier_frameworkVersion)</CSharpier_FrameworkVersion>
"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Tests/MsBuild/Scenarios.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Net8;

public class Class1
{
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions Tests/MsBuild/TestCases/UnformattedFileCausesError/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Net8;

public class Class1
{


}
10 changes: 10 additions & 0 deletions Tests/MsBuild/nuget.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<packageSources>
<add key="Local" value="nupkg" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<packageSourceMapping>
<packageSource key="Local">
<package pattern="CSharpier.MsBuild" />
</packageSource>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>

0 comments on commit 2c08eac

Please sign in to comment.