Skip to content

Commit

Permalink
Add support for deterministic builds
Browse files Browse the repository at this point in the history
  • Loading branch information
slang25 committed Mar 17, 2023
1 parent 52ec2af commit 0aa752d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/DeterministicTests/DeterministicTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<PackageReference Include="Xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<ProjectReference Include="..\Shouldly\Shouldly.csproj" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<Import Project="..\Shouldly\build.props" />
</Project>
2 changes: 0 additions & 2 deletions src/DocumentationExamples/DocumentationExamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,6 +15,5 @@
<PackageReference Include="PublicApiGenerator" Version="10.3.0" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[3.1.0]" />
</ItemGroup>
<Import Project="..\Shouldly\build.props" />

</Project>
1 change: 0 additions & 1 deletion src/Shouldly.Tests/Shouldly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<Reference Include="System.Memory" Version="4.5.3" />
</ItemGroup>
<Import Project="..\Shouldly\build.props" />
</Project>
42 changes: 42 additions & 0 deletions src/Shouldly/Internals/SourceCodeTextGetter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Shouldly.Internals;

Expand Down Expand Up @@ -43,6 +45,23 @@ private void ParseStackTrace(StackTrace? stackTrace)
ShouldlyFrameOffset = originatingFrame.index;

var fileName = originatingFrame.frame.GetFileName();
if (fileName?.StartsWith(@"/_/", StringComparison.Ordinal) == true)
{
var sourceRoot = ShouldlyConfiguration.SourceRoot;
if (sourceRoot == null)
{
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
if (assemblyLocation != null)
{
var assemblyDirectory = Path.GetDirectoryName(assemblyLocation);
TryFindGitRepoRoot(assemblyDirectory!, out sourceRoot);
}
}
if (sourceRoot != null)
{
fileName = fileName.Replace("/_/", sourceRoot + Path.PathSeparator);
}
}
_determinedOriginatingFrame = fileName != null && File.Exists(fileName);
_shouldMethod = shouldlyFrame.method.Name;
FileName = fileName;
Expand Down Expand Up @@ -134,4 +153,27 @@ private string GetCodePartFromParameter(int indexOfMethod, string codeLines, str
.RemoveBlock()
.Trim();
}

private static bool TryFindGitRepoRoot(string startDirectory, [NotNullWhen(true)] out string? gitRepoRoot)
{
try
{
var currentDirectory = new DirectoryInfo(startDirectory);
while (currentDirectory != null)
{
var gitDirectory = Path.Combine(currentDirectory.FullName, ".git");
if (Directory.Exists(gitDirectory))
{
gitRepoRoot = currentDirectory.FullName;
return true;
}

currentDirectory = currentDirectory.Parent;
}
}
catch { }

gitRepoRoot = null;
return false;
}
}
4 changes: 2 additions & 2 deletions src/Shouldly/Shouldly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
<ItemGroup>
<PackageReference Include="EmptyFiles" Version="4.1.0" PrivateAssets="None" />
<PackageReference Include="DiffEngine" Version="11.0.0" />
<Content Include="build.props" PackagePath="build\Shouldly.props" />
<Content Include="buildMultiTargeting.props" PackagePath="buildMultiTargeting\Shouldly.props" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
<None Include="..\..\assets\logo_128x128.png" Pack="true" PackagePath="assets" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' " >
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[5.0.0]" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Shouldly/ShouldlyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ public void Dispose()

public static double DefaultFloatingPointTolerance = 0.0d;
public static TimeSpan DefaultTaskTimeout = TimeSpan.FromSeconds(10);

/// <summary>
/// Should can enhance assertion failure messages if it can find the source code at the call site.
/// When using deterministic builds, set this property to explicitly tell Shouldly the root path which symbol paths with be relative to.
/// </summary>
public static string? SourceRoot { get; set; } = null;
}
10 changes: 0 additions & 10 deletions src/Shouldly/build.props

This file was deleted.

4 changes: 0 additions & 4 deletions src/Shouldly/buildMultiTargeting.props

This file was deleted.

0 comments on commit 0aa752d

Please sign in to comment.