Skip to content

Add overloads for preprocessor symbol and reference methods in Genera… - #2249

Merged
david-driscoll merged 2 commits into
mainfrom
feature/enhanced-collections
Jun 28, 2026
Merged

Add overloads for preprocessor symbol and reference methods in Genera…#2249
david-driscoll merged 2 commits into
mainfrom
feature/enhanced-collections

Conversation

@david-driscoll

Copy link
Copy Markdown
Member

…torTestContextBuilder

@github-actions github-actions Bot added this to the v10.0.4 milestone Jun 28, 2026
@github-actions

Copy link
Copy Markdown

Test Results

0 tests  ±0   0 ✅ ±0   0s ⏱️ ±0s
0 suites ±0   0 💤 ±0 
0 files   ±0   0 ❌ ±0 

Results for commit 6592476. ± Comparison against base commit 71f89e7.

@david-driscoll
david-driscoll merged commit 039d551 into main Jun 28, 2026
7 of 8 checks passed
@david-driscoll
david-driscoll deleted the feature/enhanced-collections branch June 28, 2026 17:52
@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.34177% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.82%. Comparing base (71f89e7) to head (6592476).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ng.SourceGenerators/GeneratorTestContextBuilder.cs 87.34% 9 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2249   +/-   ##
=======================================
  Coverage   59.82%   59.82%           
=======================================
  Files          52       52           
  Lines        3278     3278           
  Branches      210      210           
=======================================
  Hits         1961     1961           
  Misses       1251     1251           
  Partials       66       66           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions Bot added the ✨ mysterious We forgot to label this label Jun 28, 2026

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

The PR introduces several overloads to GeneratorTestContextBuilder to support IReadOnlyCollection types, but it is currently not up to project standards due to significant code duplication and a lack of unit tests. Codacy flagged 10 code clones where the logic for array and collection overloads is identical; these should be refactored to delegate calls rather than repeating logic to ensure maintainability.

Beyond maintenance concerns, there are functional risks: a copy-paste error in the generic WithCodeRefactoring method redirects calls to code fix logic instead, and a potential NullReferenceException exists when processing compilation references if an assembly fails to produce. All new functionality lacks test coverage, and several acceptance criteria regarding validation logic remain unverified.

About this PR

  • The PR introduces numerous new overloads and functional refactors using C# 12 features but provides no new or updated tests. Additionally, the PR description lacks the necessary context to explain the motivation for these specific overloads.

Test suggestions

  • Missing recommended test scenario: Verify AddPreprocessorSymbol correctly handles IReadOnlyCollection input
  • Missing recommended test scenario: Verify AddReferences correctly handles various collection types (string, Type, Assembly, MetadataReference)
  • Missing recommended test scenario: Verify AddSources and AddAdditionalTexts correctly handle collection inputs
  • Missing recommended test scenario: Verify AddCompilationReferences throws ArgumentException when a result missing a MetadataReference is provided
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Missing recommended test scenario: Verify AddPreprocessorSymbol correctly handles IReadOnlyCollection input
2. Missing recommended test scenario: Verify AddReferences correctly handles various collection types (string, Type, Assembly, MetadataReference)
3. Missing recommended test scenario: Verify AddSources and AddAdditionalTexts correctly handle collection inputs
4. Missing recommended test scenario: Verify AddCompilationReferences throws ArgumentException when a result missing a MetadataReference is provided

TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback

{
return Build().GenerateAsync(cancellationToken);
}
public Task<GeneratorTestResults> GenerateAsync(CancellationToken cancellationToken = default) => Build().GenerateAsync(cancellationToken);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Avoid using optional parameters in public methods like GenerateAsync. Instead, provide a parameterless overload that calls the parameterized version with the default value to ensure better binary compatibility for consumers.

See Issue in Codacy

? throw new ArgumentException("All additional compilations must have a metadata reference", nameof(additionalCompilations))
: ( AddReferencesInternal(additionalCompilations.Select(z => z.MetadataReference!).ToArray()) with
{
_referenceNames = _referenceNames.Union(additionalCompilations.Select(z => z.Assembly!.GetName().Name)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Potential NullReferenceException when accessing z.Assembly. While a null check for MetadataReference was added, you must also ensure the Assembly and its Name are valid before performing the Union operation on the ImmutableHashSet.

@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Collections.Immutable;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

The assembly should be marked with the CLSCompliant attribute to indicate compliance with the Common Language Specification (CLS). This ensures interoperability across different .NET languages.

Suggested change
using System.Collections.Immutable;
[assembly: System.CLSCompliant(true)]

See Issue in Codacy

/// </summary>
/// <param name="preprocessorSymbolNames"></param>
/// <returns></returns>
public GeneratorTestContextBuilder AddPreprocessorSymbol(params IReadOnlyCollection<string> preprocessorSymbolNames)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Suggestion: Significant logic duplication detected between the array and collection overloads for methods like AddCompilationReferences, AddReferences, and AddSources. This increases maintenance burden and class complexity. The array-based overloads should be refactored to delegate to the collection-based overloads while preserving [OverloadResolutionPriority(-1)].

See Clone in Codacy
See Issue in Codacy

/// <returns></returns>
public GeneratorTestContextBuilder WithCodeRefactoring<T>()
where T : CodeRefactoringProvider, new()
where T : CodeRefactoringProvider, new() => WithCodeFix(typeof(T));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ LOW RISK

The generic WithCodeRefactoring method incorrectly calls WithCodeFix(typeof(T)). It must call WithCodeRefactoring(typeof(T)) to maintain semantic consistency and ensure the type is added to the correct underlying collection.

@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 2 medium

Alerts:
⚠ 2 issues (≤ 0 issues of at least minor severity)

Results:
2 new issues

Category Results
BestPractice 1 medium
Complexity 1 medium

View in Codacy

🟢 Metrics 10 duplication

Metric Results
Duplication 10

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@github-actions github-actions Bot modified the milestones: v10.0.4, v10.0.5, v10.0.6, v10.0.7 Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✨ mysterious We forgot to label this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant