Fix SonarCloud and CodeQL code-quality issues - #73
Merged
Conversation
- Dispose PdfDocumentBuilder in ValidationPdf self-test (cs/local-not-disposed) - Replace foreach+re-open-entry loop with LINQ Select in FileAssertZipAssertTests to satisfy cs/linq/missed-select - Remove unreachable null-check on _pages in FileAssertPdfAssert (S2589) - Narrow BuildPageText parameter to List<Page> for CA1859 - Consolidate FileAssertFile's 13-parameter constructor into a FileTypeAsserts record struct to satisfy S107 - Use the return value of Assert.Single/Assert.IsType instead of re-deriving it via indexing/casting across 8 test files (xUnit2033) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request addresses code-quality findings by refactoring the core file-assert modeling API to reduce constructor parameter sprawl, tightening resource handling in PDF self-tests, and simplifying test assertions for clarity and maintainability.
Changes:
- Refactored
FileAssertFileto accept a singleFileTypeAssertsvalue grouping all optional file-type assert units. - Improved PDF-related resource handling and adjusted internal PDF helper typing for consistency.
- Updated multiple tests to use
Assert.Single(...)and local variables to avoid repeated index-based access.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/DemaConsulting.FileAssert/Modeling/FileAssertFile.cs | Groups optional file-type asserts into a single struct passed to the constructor and Create method. |
| src/DemaConsulting.FileAssert/Modeling/FileAssertPdfAssert.cs | Improves null-safety around _pages usage and updates BuildPageText parameter type to match call sites. |
| src/DemaConsulting.FileAssert/SelfTest/ValidationPdf.cs | Ensures PdfDocumentBuilder is disposed via using var. |
| test/DemaConsulting.FileAssert.Tests/Cli/ContextNewPropertiesTests.cs | Replaces index-based assertions with Assert.Single and a local variable. |
| test/DemaConsulting.FileAssert.Tests/Configuration/ConfigurationTests.cs | Simplifies hierarchy assertions using Assert.Single for collections. |
| test/DemaConsulting.FileAssert.Tests/Configuration/FileAssertConfigTests.cs | Uses Assert.Single and locals to make parsed-config expectations clearer. |
| test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertHtmlAssertTests.cs | Uses Assert.Single to validate a single error message more directly. |
| test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertJsonAssertTests.cs | Uses Assert.Single for error collection assertions in parse/IO failure tests. |
| test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertRuleTests.cs | Avoids repeated casts by capturing the typed rule via Assert.IsType<T>. |
| test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertYamlAssertTests.cs | Uses Assert.Single and a local variable for parse-failure error verification. |
| test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertZipAssertTests.cs | Adds missing LINQ using; refactors zip helper disposal and simplifies single-item assertions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request refactors how file-type assertion units are managed in
FileAssertFileby grouping them into a single struct, simplifying the constructor and related code. Additionally, it updates various tests to use more concise and expressive assertions, and makes minor improvements to resource handling and type safety.Refactoring and code simplification:
FileAssertFileconstructor now takes a singleFileTypeAssertsstruct containing all optional file-type assert units (text, pdf, xml, html, yaml, json, zip) instead of passing each one as a separate parameter. This change is reflected in the constructor, its documentation, and the staticCreatemethod. (src/DemaConsulting.FileAssert/Modeling/FileAssertFile.cs) [1] [2] [3]Test improvements:
Assert.Singleand variable assignment for clarity and to avoid duplicate index-based access. (test/DemaConsulting.FileAssert.Tests/Cli/ContextNewPropertiesTests.cs,test/DemaConsulting.FileAssert.Tests/Configuration/ConfigurationTests.cs,test/DemaConsulting.FileAssert.Tests/Configuration/FileAssertConfigTests.cs,test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertHtmlAssertTests.cs,test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertJsonAssertTests.cs,test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertRuleTests.cs,test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertYamlAssertTests.cs,test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertZipAssertTests.cs) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]Resource management and type safety:
usingstatement forPdfDocumentBuilderto ensure proper disposal. (src/DemaConsulting.FileAssert/SelfTest/ValidationPdf.cs)BuildPageTextmethod signature is updated to accept aList<Page>instead ofIReadOnlyList<Page>, matching usage. (src/DemaConsulting.FileAssert/Modeling/FileAssertPdfAssert.cs)RunDocumentAssertions, a null-forgiving operator is used for_pagesafter a guard, improving null-safety. (src/DemaConsulting.FileAssert/Modeling/FileAssertPdfAssert.cs)Minor improvements:
using System.Linq;directive for LINQ usage in zip file test helpers. (test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertZipAssertTests.cs)test/DemaConsulting.FileAssert.Tests/Modeling/FileAssertZipAssertTests.cs)