Skip to content

Commit 234120c

Browse files
authored
Embed pdb instead of using snupkg (#82)
1 parent b77ceef commit 234120c

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

src/build/Workleap.DotNet.CodingStandards.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
<EnableNETAnalyzers Condition="'$(EnableNETAnalyzers)' == ''">true</EnableNETAnalyzers>
77
<AnalysisLevel Condition="'$(AnalysisLevel)' == ''">latest-all</AnalysisLevel>
88
<EnforceCodeStyleInBuild Condition="'$(EnforceCodeStyleInBuild)' == ''">true</EnforceCodeStyleInBuild>
9-
<IncludeSymbols Condition="'$(IncludeSymbols)' == ''">true</IncludeSymbols>
10-
<SymbolPackageFormat Condition="'$(SymbolPackageFormat)' == ''">snupkg</SymbolPackageFormat>
9+
10+
<EmbedUntrackedSources Condition="'$(EmbedUntrackedSources)' == ''">true</EmbedUntrackedSources>
11+
<DebugType Condition="'$(DebugType)' == ''">embedded</DebugType>
1112

1213
<!-- Prevent warning when the .NET SDK version and the Microsoft.CodeAnalysis.NetAnalyzers version mismatch -->
1314
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>

tests/Workleap.DotNet.CodingStandards.Tests/CodingStandardTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.IO.Compression;
2+
using System.Reflection.PortableExecutable;
13
using Workleap.DotNet.CodingStandards.Tests.Helpers;
24
using Xunit.Abstractions;
35

@@ -142,6 +144,7 @@ public async Task ReportVulnerablePackage_Debug_ShouldReportWarning()
142144
Assert.False(data.HasError("NU1903"));
143145
Assert.True(data.HasWarning("NU1903"));
144146
}
147+
145148
[Fact]
146149
public async Task ReportVulnerablePackage_DisabledWarningOnPackage()
147150
{
@@ -170,4 +173,80 @@ public async Task ReportVulnerablePackage_DisabledWarningOnPackage()
170173
Assert.False(data.HasError("NU1903"));
171174
Assert.False(data.HasWarning("NU1903"));
172175
}
176+
177+
[Fact]
178+
public async Task PdbShouldBeEmbedded_Dotnet_Build()
179+
{
180+
using var project = new ProjectBuilder(fixture, testOutputHelper);
181+
project.AddFile("test.csproj", $"""
182+
<Project Sdk="Microsoft.NET.Sdk">
183+
<PropertyGroup>
184+
<TargetFramework>net8.0</TargetFramework>
185+
<ImplicitUsings>enable</ImplicitUsings>
186+
<Nullable>enable</Nullable>
187+
<ErrorLog>{ProjectBuilder.SarifFileName},version=2.1</ErrorLog>
188+
<RootNamespace>Foo</RootNamespace>
189+
</PropertyGroup>
190+
191+
<ItemGroup>
192+
<PackageReference Include="Workleap.DotNet.CodingStandards" Version="*" />
193+
</ItemGroup>
194+
</Project>
195+
""");
196+
197+
project.AddFile("Sample.cs", """
198+
namespace Foo;
199+
public static class Sample { }
200+
""");
201+
var data = await project.BuildAndGetOutput(["--configuration", "Release"]);
202+
203+
var outputFiles = Directory.GetFiles(Path.Combine(project.RootFolder, "bin", "Release", "net8.0"));
204+
await AssertPdbIsEmbedded(outputFiles);
205+
}
206+
207+
[Fact]
208+
public async Task PdbShouldBeEmbedded_Dotnet_Pack()
209+
{
210+
using var project = new ProjectBuilder(fixture, testOutputHelper);
211+
project.AddFile("test.csproj", $"""
212+
<Project Sdk="Microsoft.NET.Sdk">
213+
<PropertyGroup>
214+
<TargetFramework>net8.0</TargetFramework>
215+
<ImplicitUsings>enable</ImplicitUsings>
216+
<Nullable>enable</Nullable>
217+
<ErrorLog>{ProjectBuilder.SarifFileName},version=2.1</ErrorLog>
218+
<RootNamespace>Foo</RootNamespace>
219+
</PropertyGroup>
220+
221+
<ItemGroup>
222+
<PackageReference Include="Workleap.DotNet.CodingStandards" Version="*" />
223+
</ItemGroup>
224+
</Project>
225+
""");
226+
227+
project.AddFile("Sample.cs", """
228+
namespace Foo;
229+
public static class Sample { }
230+
""");
231+
var data = await project.PackAndGetOutput(["--configuration", "Release"]);
232+
233+
var extractedPath = Path.Combine(project.RootFolder, "extracted");
234+
var files = Directory.GetFiles(Path.Combine(project.RootFolder, "bin", "Release"));
235+
Assert.Single(files); // Only the .nupkg should be generated
236+
var nupkg = files.Single(f => f.EndsWith(".nupkg", StringComparison.OrdinalIgnoreCase));
237+
ZipFile.ExtractToDirectory(nupkg, extractedPath);
238+
239+
var outputFiles = Directory.GetFiles(extractedPath, "*", SearchOption.AllDirectories);
240+
await AssertPdbIsEmbedded(outputFiles);
241+
}
242+
243+
private static async Task AssertPdbIsEmbedded(string[] outputFiles)
244+
{
245+
Assert.DoesNotContain(outputFiles, f => f.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase));
246+
var dllPath = outputFiles.Single(f => f.EndsWith(".dll", StringComparison.OrdinalIgnoreCase));
247+
await using var stream = File.OpenRead(dllPath);
248+
var peReader = new PEReader(stream);
249+
var debug = peReader.ReadDebugDirectory();
250+
Assert.Contains(debug, entry => entry.Type == DebugDirectoryEntryType.EmbeddedPortablePdb);
251+
}
173252
}

tests/Workleap.DotNet.CodingStandards.Tests/Helpers/ProjectBuilder.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ internal sealed class ProjectBuilder : IDisposable
1313
private readonly TemporaryDirectory _directory;
1414
private readonly ITestOutputHelper _testOutputHelper;
1515

16+
public string RootFolder => this._directory.FullPath;
17+
1618
public ProjectBuilder(PackageFixture fixture, ITestOutputHelper testOutputHelper)
1719
{
1820
this._testOutputHelper = testOutputHelper;
@@ -92,11 +94,21 @@ public void AddCsprojFile(Dictionary<string, string>? properties = null, Diction
9294
File.WriteAllText(this._directory.GetPath("test.csproj"), content);
9395
}
9496

95-
public async Task<SarifFile> BuildAndGetOutput(string[]? buildArguments = null)
97+
public Task<SarifFile> BuildAndGetOutput(string[]? buildArguments = null)
98+
{
99+
return this.ExecuteDotnetCommandAndGetOutput("build", buildArguments);
100+
}
101+
102+
public Task<SarifFile> PackAndGetOutput(string[]? buildArguments = null)
103+
{
104+
return this.ExecuteDotnetCommandAndGetOutput("pack", buildArguments);
105+
}
106+
107+
private async Task<SarifFile> ExecuteDotnetCommandAndGetOutput(string command, string[]? buildArguments = null)
96108
{
97109
var result = await Cli.Wrap("dotnet")
98110
.WithWorkingDirectory(this._directory.FullPath)
99-
.WithArguments(["build", .. (buildArguments ?? [])])
111+
.WithArguments([command, .. (buildArguments ?? [])])
100112
.WithEnvironmentVariables(env => env.Set("CI", null).Set("GITHUB_ACTIONS", null))
101113
.WithStandardOutputPipe(PipeTarget.ToDelegate(this._testOutputHelper.WriteLine))
102114
.WithStandardErrorPipe(PipeTarget.ToDelegate(this._testOutputHelper.WriteLine))

0 commit comments

Comments
 (0)