Add overloads for preprocessor symbol and reference methods in Genera… - #2249
Conversation
…torTestContextBuilder
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
🟡 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.
| ? 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)), |
There was a problem hiding this comment.
🟡 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; | |||
There was a problem hiding this comment.
🟡 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.
| using System.Collections.Immutable; | |
| [assembly: System.CLSCompliant(true)] |
| /// </summary> | ||
| /// <param name="preprocessorSymbolNames"></param> | ||
| /// <returns></returns> | ||
| public GeneratorTestContextBuilder AddPreprocessorSymbol(params IReadOnlyCollection<string> preprocessorSymbolNames) |
There was a problem hiding this comment.
🟡 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)].
| /// <returns></returns> | ||
| public GeneratorTestContextBuilder WithCodeRefactoring<T>() | ||
| where T : CodeRefactoringProvider, new() | ||
| where T : CodeRefactoringProvider, new() => WithCodeFix(typeof(T)); |
There was a problem hiding this comment.
⚪ 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.
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| BestPractice | 1 medium |
| Complexity | 1 medium |
🟢 Metrics 10 duplication
Metric Results Duplication 10
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
…torTestContextBuilder