Skip to content

Reduce Validation.cs duplication and improve code coverage#7

Merged
Malcolmnixon merged 3 commits intomainfrom
copilot/fix-code-coverage-duplicates
Mar 1, 2026
Merged

Reduce Validation.cs duplication and improve code coverage#7
Malcolmnixon merged 3 commits intomainfrom
copilot/fix-code-coverage-duplicates

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

Sonar flagged two quality issues: high duplicate-line density in Validation.cs and insufficient code coverage.

Duplicate Code Reduction

Extracted a RunValidationTest helper method following the SarifMark pattern, replacing ~130 lines of near-identical boilerplate across the three test methods with a single shared implementation that accepts a lambda validator:

private static void RunValidationTest(
    Context context,
    DemaConsulting.TestResults.TestResults testResults,
    string testName,
    string displayName,
    string[] additionalArgs,
    Func<string, string?> validator)

Each specific test now only provides its unique args and validation logic:

private static void RunHelpTest(Context context, DemaConsulting.TestResults.TestResults testResults)
{
    RunValidationTest(context, testResults, "NuGetCache_HelpDisplay", "Help Display Test",
        ["--help"],
        logContent => logContent.Contains("Usage:") && logContent.Contains("Options:")
            ? null : "Help text not found in log");
}

Coverage Improvements

Added 6 new tests covering previously untested paths:

  • Context_WriteError_SetsErrorExitCodeWriteErrorExitCode == 1
  • Context_WriteError_NotSilent_WritesToConsoleWriteError writes to stdout
  • Context_Create_LogFlag_WithoutValue_ThrowsArgumentException — missing --log value
  • Context_Create_ResultsFlag_WithoutValue_ThrowsArgumentException — missing --results value
  • IntegrationTest_ValidateWithResults_GeneratesJUnitFile.xml (JUnit) output branch of WriteResultsFile, placed in IntegrationTests.cs alongside the existing TRX test for consistency
  • Program_Run_WithValidateAndUnsupportedResultsFormat_SetsErrorExitCode — unsupported extension error path

Test count: 35 → 41.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code quality improvement

Related Issues

Pre-Submission Checklist

Before submitting this pull request, ensure you have completed the following:

Build and Test

  • Code builds successfully: dotnet build --configuration Release
  • All unit tests pass: dotnet test --configuration Release
  • Self-validation tests pass:
    dotnet run --project src/DemaConsulting.NuGet.CacheTool --configuration Release --framework net10.0
    --no-build -- --validate
  • Code produces zero warnings

Code Quality

  • Code formatting is correct: dotnet format --verify-no-changes
  • New code has appropriate XML documentation comments
  • Static analyzer warnings have been addressed

Quality Checks

Please run the following checks before submitting:

  • Spell checker passes: cspell "**/*.{md,cs}"
  • Markdown linter passes: markdownlint "**/*.md"
  • YAML linter passes: yamllint .

Testing

  • Added unit tests for new functionality
  • Updated existing tests if behavior changed
  • All tests follow the AAA (Arrange, Act, Assert) pattern
  • Test coverage is maintained or improved

Documentation

  • Updated README.md (if applicable)
  • Updated ARCHITECTURE.md (if applicable)
  • Added code examples for new features (if applicable)
  • Updated requirements.yaml (if applicable)

Additional Notes

CodeQL scan returned 0 alerts.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

… tests

Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix low code coverage and high duplicate lines Reduce Validation.cs duplication and improve code coverage Mar 1, 2026
…de TRX test

Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
@Malcolmnixon Malcolmnixon marked this pull request as ready for review March 1, 2026 15:27
@Malcolmnixon Malcolmnixon merged commit d2c766f into main Mar 1, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants