Repository navigation
Description
Summary
Two nearly-identical private helpers named TryGetOptionValue(IReadOnlyList<string> arguments, string option) — both scanning a command-line argument array for --option value or --option=value style entries — exist in the same Microsoft.Testing.Extensions.PackagedApp project, with subtly different option-name conventions (bare option string vs. a ---prefixed key built from a bare optionKey).
Duplication Details
Pattern: Duplicated command-line option scanning helper
-
Severity: Medium
-
Occurrences: 2 (plus a third related helper,
TryGetInlineOptionValue, only in one of the two files) -
Locations:
src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs(lines ~731-747),TryGetOptionValue, which also delegates to a siblingTryGetInlineOptionValue(lines ~770+) to support--option=valuesyntax. Used forResultsDirectoryOption/DiagnosticOutputDirectoryOptionlookups added in Enable MTP for UWP and AppContainer Windows apps #11484.src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs(lines ~104-113),TryGetOptionValue, a simpler variant (no inline=valuesupport, and it prefixes the key with--internally) used forTestHostControllerPidOptionKey/RetryPipeNameOptionKeylookups.
-
Code Sample (
PackagedAppConnectBackHandshake.cs):private static string? TryGetOptionValue(IReadOnlyList<string> arguments, string optionKey) { for (int i = 0; i < arguments.Count - 1; i++) { if (string.Equals(arguments[i], $"--{optionKey}", StringComparison.Ordinal)) { return arguments[i + 1]; } } return null; }
vs. (
PackagedAppTestHostLauncher.cs):private static string? TryGetOptionValue(IReadOnlyList<string> arguments, string option) { for (int i = 0; i < arguments.Count; i++) { if (string.Equals(arguments[i], option, StringComparison.Ordinal)) { return i + 1 < arguments.Count ? arguments[i + 1] : null; } if (TryGetInlineOptionValue(arguments[i], option, out string? value)) { return value; } } return null; }
Impact Analysis
- Maintainability: Two subtly different argument-parsing conventions live in the same project: one caller passes the bare option name and expects the callee to add
--, the other expects the full--optionstring and additionally supports inline=valuesyntax. This inconsistency itself is a maintenance hazard — a developer copying one call pattern to the other file will silently break lookups (e.g. double--prefix, or missing inline-value support). - Bug Risk:
PackagedAppConnectBackHandshake.TryGetOptionValuedoes not support--option=valuesyntax thatPackagedAppTestHostLauncher.TryGetOptionValuedoes. If a future option (e.g.--internal-testhostcontroller-pid=1234) is ever passed in the=valueform, the handshake lookup will silently fail to find it while the launcher's lookup would succeed — a hard-to-diagnose inconsistency. - Code Bloat: ~35 combined lines for what is fundamentally one command-line parsing primitive.
Refactoring Recommendations
- Extract a single shared
CommandLineArgumentHelper- Add one internal static helper (e.g.
CommandLineOptionParser.TryGetOptionValue(IReadOnlyList<string> arguments, string option)) supporting both--option valueand--option=valueforms, take a fully-prefixed option string as input (matching the more capablePackagedAppTestHostLauncherconvention), and have bothPackagedAppTestHostLauncherandPackagedAppConnectBackHandshakecall it, updating call sites to pass---prefixed keys consistently. - Estimated effort: Low (~1 hour), including updating call sites and existing unit tests (
PackagedAppConnectBackHandshakeTests.cs,PackagedAppTestHostLauncherTests.cs). - Benefits: One parsing convention repo-wide, inline
=valuesupport becomes available everywhere, eliminates the risk of the two helpers silently diverging further.
- Add one internal static helper (e.g.
Implementation Checklist
- Review duplication findings
- Prioritize refactoring tasks
- Create refactoring plan
- Implement changes
- Update tests
- Verify no functionality broken
Analysis Metadata
- Analyzed Files: 2 (PackagedAppTestHostLauncher.cs, PackagedAppConnectBackHandshake.cs)
- Detection Method: Semantic code analysis of command-line option parsing helpers
- Commit: 7287c75 (HEAD at analysis time)
🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · copilot · auto · 100.8 AIC · ⌖ 7.37 AIC · ⊞ 13.4K · [◷]( · ◷)
Add this agentic workflow to your repo
To install this agentic workflow, run
gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
- expires on Sep 30, 2026, 4:38 AM UTC
Activity