Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jobs:
dotnet pack src/bunit/ -c Release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
dotnet pack src/bunit.template/ -c Release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
dotnet pack src/bunit.web.query/ -c release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
dotnet pack src/bunit.generators/ -c release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true

# Publish the NuGet package as an artifact, so they can be used in the following jobs
- uses: actions/upload-artifact@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
dotnet pack src/bunit/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
dotnet pack src/bunit.template/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
dotnet pack src/bunit.web.query/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
dotnet pack src/bunit.generators/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true

- name: 🛠️ Upload library to NuGet.org repository
run: |
Expand Down
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
</ItemGroup>

<ItemGroup Label="Source Code Generators">
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.12.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.12.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.14.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.14.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.2" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions bunit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.generators", "src\bun
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bunit.tests", "tests\bunit.tests\bunit.tests.csproj", "{56889DE7-5E66-4E9C-815B-CBCFC9961612}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bunit.generators.tests", "tests\bunit.generators.tests\bunit.generators.tests.csproj", "{D08F7F1D-74B1-4A76-86A2-94918863740C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -96,6 +98,10 @@ Global
{56889DE7-5E66-4E9C-815B-CBCFC9961612}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56889DE7-5E66-4E9C-815B-CBCFC9961612}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56889DE7-5E66-4E9C-815B-CBCFC9961612}.Release|Any CPU.Build.0 = Release|Any CPU
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -109,6 +115,7 @@ Global
{AE3DFB52-2BF4-4806-AD82-7FB7B38AC17F} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
{A7C6A2AA-FF8F-4ED1-8590-5324FC566059} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
{56889DE7-5E66-4E9C-815B-CBCFC9961612} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
{D08F7F1D-74B1-4A76-86A2-94918863740C} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {24106918-1C86-4769-BDA6-9C80E64CD260}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp; // For GetInterceptableLocation API and InterceptableLocation type

namespace Bunit.Web.Stubs.AddStubMethodStubGenerator;

Expand Down Expand Up @@ -50,10 +51,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
return null;
}

var path = GetInterceptorFilePath(context.Node.SyntaxTree, context.SemanticModel.Compilation);
var lineSpan = context.SemanticModel.SyntaxTree.GetLineSpan(context.Node.Span);
var line = lineSpan.StartLinePosition.Line + 1;
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;
var interceptableLocation = context.SemanticModel.GetInterceptableLocation(invocation);
if (interceptableLocation is null)
{
return null;
}

var properties = symbol.GetAllMembersRecursively()
.OfType<IPropertySymbol>()
Expand All @@ -67,9 +69,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
TargetTypeNamespace = symbol.ContainingNamespace.ToDisplayString(),
TargetTypeName = symbol.ToDisplayString(),
Properties = properties,
Path = path,
Line = line,
Column = column,
Location = interceptableLocation,
};

static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation, SemanticModel semanticModel)
Expand All @@ -88,11 +88,6 @@ static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation,
return semanticModel.GetSymbolInfo(invocation).Symbol is IMethodSymbol { IsExtensionMethod: true, ReceiverType.Name: "ComponentFactoryCollection" };
}

static string GetInterceptorFilePath(SyntaxTree tree, Compilation compilation)
{
return compilation.Options.SourceReferenceResolver?.NormalizePath(tree.FilePath, baseFilePath: null) ?? tree.FilePath;
}

static bool IsParameterOrCascadingParameter(ISymbol member)
{
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
Expand Down Expand Up @@ -124,27 +119,21 @@ private static void Execute(ImmutableArray<AddStubClassInfo> classInfos, SourceP

private static void GenerateInterceptorCode(AddStubClassInfo stubbedComponentGroup, IEnumerable<AddStubClassInfo> stubClassGrouped, SourceProductionContext context)
{
// Generate the attribute
const string attribute = """
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
sealed file class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(string filePath, int line, int column)
{
_ = filePath;
_ = line;
_ = column;
}
}
}
""";

// Generate the interceptor
var interceptorSource = new StringBuilder(1000);
interceptorSource.AppendLine(HeaderProvider.Header);
interceptorSource.AppendLine(attribute);
interceptorSource.AppendLine("namespace System.Runtime.CompilerServices");
interceptorSource.AppendLine("{");
interceptorSource.AppendLine("\t[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true)]");
interceptorSource.AppendLine("\tsealed file class InterceptsLocationAttribute : global::System.Attribute");
interceptorSource.AppendLine("\t{");
interceptorSource.AppendLine("\t\tpublic InterceptsLocationAttribute(int version, string data)");
interceptorSource.AppendLine("\t\t{");
interceptorSource.AppendLine("\t\t\t_ = version;");
interceptorSource.AppendLine("\t\t\t_ = data;");
interceptorSource.AppendLine("\t\t}");
interceptorSource.AppendLine("\t}");
interceptorSource.AppendLine("}");
interceptorSource.AppendLine();
interceptorSource.AppendLine("namespace Bunit");
interceptorSource.AppendLine("{");
Expand All @@ -153,8 +142,7 @@ public InterceptsLocationAttribute(string filePath, int line, int column)

foreach (var hit in stubClassGrouped)
{
interceptorSource.AppendLine(
$"\t\t[global::System.Runtime.CompilerServices.InterceptsLocationAttribute(@\"{hit.Path}\", {hit.Line}, {hit.Column})]");
interceptorSource.AppendLine($"\t\t{hit.Location.GetInterceptsLocationAttributeSyntax()}");
}

interceptorSource.AppendLine(
Expand Down Expand Up @@ -213,9 +201,7 @@ internal sealed record AddStubClassInfo
public required string TargetTypeName { get; set; }
public string UniqueQualifier => $"{TargetTypeNamespace}.{StubClassName}";
public ImmutableArray<StubPropertyInfo> Properties { get; set; } = ImmutableArray<StubPropertyInfo>.Empty;
public required string Path { get; set; }
public int Line { get; set; }
public int Column { get; set; }
public required InterceptableLocation Location { get; set; }
}

internal sealed record StubPropertyInfo
Expand Down
Loading