Skip to content

Commit

Permalink
Merge pull request #179 from lucaslorentz/source-generators-fix
Browse files Browse the repository at this point in the history
Source generators fix
  • Loading branch information
lucaslorentz authored Dec 23, 2024
2 parents 885a87d + ef7b1c8 commit 45f95a6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
16 changes: 16 additions & 0 deletions sample/src/Sample/ClassWithGeneratedRegex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.RegularExpressions;

namespace Sample
{
// This class is used to validate that Minicover can instrument assemblies containing code generated by a source generator.
public static partial class ClassWithGeneratedRegex
{
[GeneratedRegex($@"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase)]
private static partial Regex EmailRegex();

public static bool IsEmail(string text)
{
return EmailRegex().IsMatch(text);
}
}
}
18 changes: 18 additions & 0 deletions sample/tests/Sample.UnitTests/ClassWithGeneratedRegexTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Sample.TryFinally;
using Xunit;

namespace Sample.UnitTests
{
public class ClassWithGeneratedRegexTest
{
[Fact]
public void Test()
{
var isEmail = ClassWithGeneratedRegex.IsEmail("[email protected]");
Assert.True(isEmail);
}
}
}
9 changes: 6 additions & 3 deletions sample/tests/Sample.UnitTests/Sample.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/MiniCover.Core/Extensions/DocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public static class DocumentExtensions
public static bool FileHasChanged(this Document document)
{
if (!File.Exists(document.Url))
{
// Ignore not found source generated files because they might be in memory
if (document.Url.EndsWith(".g.cs"))
return false;

return true;
}

using (var stream = File.OpenRead(document.Url))
{
Expand Down
12 changes: 5 additions & 7 deletions src/MiniCover.Core/Instrumentation/AssemblyInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public InstrumentedAssembly InstrumentAssemblyFile(

var resolver = ActivatorUtilities.CreateInstance<CustomAssemblyResolver>(_serviceProvider, assemblyDirectory);

_logger.LogTrace("Assembly resolver search directories: {directories}", new object[] { resolver.GetSearchDirectories() });
_logger.LogTrace("Assembly resolver search directories: {directories}", [resolver.GetSearchDirectories()]);

try
{
using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyFile.FullName, new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver }))
{
return InstrumentAssemblyDefinition(context, assemblyDefinition);
}
using var assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyFile.FullName, new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver });
return InstrumentAssemblyDefinition(context, assemblyDefinition);
}
catch (BadImageFormatException)
{
Expand All @@ -71,12 +69,12 @@ private InstrumentedAssembly InstrumentAssemblyDefinition(
var assemblyDocuments = assemblyDefinition.GetAllDocuments();

var changedDocuments = assemblyDocuments.Where(d => d.FileHasChanged()).ToArray();
if (changedDocuments.Any())
if (changedDocuments.Length != 0)
{
if (_logger.IsEnabled(LogLevel.Debug))
{
var changedFiles = changedDocuments.Select(d => d.Url).Distinct().ToArray();
_logger.LogDebug("Source files has changed: {changedFiles}", new object[] { changedFiles });
_logger.LogDebug("Source files has changed: {changedFiles}", [changedFiles]);
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/MiniCover/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"profiles": {
"MiniCover": {
"commandName": "Project",
"commandLineArgs": "instrument --workdir ../../../../../sample --parentdir ../"
}
}
}

0 comments on commit 45f95a6

Please sign in to comment.