Skip to content

feat(1004): add OutputPath parameter to Invoke-LinkLanguageCheck.ps1#1229

Open
PratikWayase wants to merge 4 commits intomicrosoft:mainfrom
PratikWayase:feat/1004-add-outputpath-param
Open

feat(1004): add OutputPath parameter to Invoke-LinkLanguageCheck.ps1#1229
PratikWayase wants to merge 4 commits intomicrosoft:mainfrom
PratikWayase:feat/1004-add-outputpath-param

Conversation

@PratikWayase
Copy link
Copy Markdown
Contributor

Description

This PR adds an -OutputPath parameter to Invoke-LinkLanguageCheck.ps1 to enable callers to redirect the JSON results file without modifying the script.

Changes:

  • Added -OutputPath parameter with default value "logs/link-lang-check-results.json" (maintains backward compatibility)
  • Replaced hardcoded output path references with $OutputPath parameter
  • Updated comment-based help to document the new parameter
  • Added comprehensive Pester tests verifying:
    • Default path behavior (writes to logs/link-lang-check-results.json)
    • Custom path behavior (writes to specified location)
    • Parent directory auto-creation for nested custom paths
    • Output file contains valid JSON structure
  • Fixed PSScriptAnalyzer warning: removed unused $repoRoot variable assignment

Impact: Improves script reusability, testability, and flexibility for CI/CD pipelines without breaking existing invocations.

Related Issue(s)

Closes #1004

Type of Change

Select all that apply:

Code & Documentation:

  • Bug fix (non-breaking change fixing an issue)
  • New feature (non-breaking change adding functionality)
  • Breaking change (fix or feature causing existing functionality to change)
  • Documentation update

Infrastructure & Configuration:

  • GitHub Actions workflow
  • Linting configuration (markdown, PowerShell, etc.)
  • Security configuration
  • DevContainer configuration
  • Dependency update

AI Artifacts:

  • Reviewed contribution with prompt-builder agent and addressed all feedback
  • Copilot instructions (.github/instructions/*.instructions.md)
  • Copilot prompt (.github/prompts/*.prompt.md)
  • Copilot agent (.github/agents/*.agent.md)
  • Copilot skill (.github/skills/*/SKILL.md)

Other:

  • Script/automation (.ps1, .sh, .py)
  • Other (please describe):

Testing

Unit Tests:

  • Ran Invoke-Pester -Path "scripts/tests/linting/Invoke-LinkLanguageCheck.Tests.ps1" — 32/32 tests pass
  • Verified default path: script writes to logs/link-lang-check-results.json when -OutputPath omitted
  • Verified custom path: script writes to specified location when -OutputPath provided
  • Verified directory creation: parent directories auto-created for nested custom paths
  • Verified JSON output: file contains valid structure with timestamp, script, summary, and issues fields

Linting:

  • Ran npm run lint:ps — PSScriptAnalyzer passes with no warnings
  • Verified comment-based help is properly formatted

@PratikWayase PratikWayase requested a review from a team as a code owner March 29, 2026 18:00
Copy link
Copy Markdown
Contributor

@katriendg katriendg left a comment

Choose a reason for hiding this comment

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

Thanks @PratikWayase for your contribution.
Two small comments I believe we could update before merging?

}
}

#endregion
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add the trailing empty newline back? It may have been removed by a VS code tool by accident.

$logsDir = Join-Path $script:RepoRoot "logs"
if (Test-Path $logsDir) { Remove-Item $logsDir -Recurse -Force }

Invoke-LinkLanguageCheckCore -ExcludePaths @() -OutputPath $script:DefaultOutputPath | Out-Null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this adds the -OutputPath paramater to the test call, but if the test is intended to test the default output path matches, even when the argument is not passed in then it should not pass and still validate the output is where you're expecting

Copy link
Copy Markdown
Contributor

@katriendg katriendg left a comment

Choose a reason for hiding this comment

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

Four issues found — one is a broken test that needs to be fixed before merging:

  1. High (tests): -OutputPath is commented out in the "writes to custom path" test — it never exercises the custom path and the assertion will fail.
  2. Medium (script): git rev-parse --show-toplevel is called twice — first with output discarded, then again to capture the value. Consolidate to a single call.
  3. Low (script): Duplicate $OutputPath default in the inner function param block — single source of truth violated.
  4. Low (script): No guard for an empty $outputDir when $OutputPath is a bare filename — New-Item -Path "" will throw.

Comment on lines 44 to +50
if ($LASTEXITCODE -ne 0) {
Write-Error "Not in a git repository"
return 1
}

$logsDir = Join-Path $repoRoot "logs"
if (-not (Test-Path $logsDir)) {
New-Item -ItemType Directory -Path $logsDir -Force | Out-Null
$repoRoot = git rev-parse --show-toplevel
if (-not [System.IO.Path]::IsPathRooted($OutputPath)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug (Medium) — git rev-parse called twice, discarding output the first time

git rev-parse --show-toplevel > $null 2>&1   # output thrown away
if ($LASTEXITCODE -ne 0) { ... }

$repoRoot = git rev-parse --show-toplevel    # called again to capture

git is invoked twice unnecessarily — the first call discards its output, then it's called a second time just to capture the value. The original pattern was correct. Consolidate back to a single call:

$repoRoot = git rev-parse --show-toplevel 2>$null
if ($LASTEXITCODE -ne 0) {
    Write-Error "Not in a git repository"
    return 1
}

[string[]]$ExcludePaths = @()
[string[]]$ExcludePaths = @(),
[string]$OutputPath = "logs/link-lang-check-results.json"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low — Duplicate default for $OutputPath creates a maintenance liability

The inner function repeats the exact same default value "logs/link-lang-check-results.json" that the outer script param already declares. If the default ever changes, it needs to be updated in two places and it's easy for them to drift out of sync.

Since the outer script param always passes the resolved value through to Invoke-LinkLanguageCheckCore, the inner function should not carry its own default:

[string]$OutputPath   # no default; outer script always supplies the value

Comment on lines +97 to +99
$outputDir = Split-Path -Parent $OutputPath
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low — Split-Path -Parent on a bare filename returns "", causing New-Item to throw

When $OutputPath has no directory component (e.g. "results.json"), Split-Path -Parent returns an empty string. Test-Path "" returns $false, so execution reaches New-Item -Path "" which throws an error.

Add a guard before the Test-Path check:

$outputDir = Split-Path -Parent $OutputPath
if ($outputDir -and -not (Test-Path $outputDir)) {
    New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}

(Same issue exists in the second write block at the bottom of the function — fix both.)

}

It 'writes to custom path when OutputPath is specified' {
$customDir = Split-Path $script:CustomOutputPath -Parent
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug (High) — -OutputPath is commented out, so this test never exercises the custom path

Invoke-LinkLanguageCheckCore -ExcludePaths @() #-OutputPath $script:CustomOutputPath | Out-Null

The -OutputPath $script:CustomOutputPath argument is accidentally commented out. The function runs with the default path (logs/link-lang-check-results.json), not the custom one. The assertion Test-Path $script:CustomOutputPath | Should -BeTrue will then always fail (or give a false positive if the default-path file happens to exist from a prior test run). Uncomment the argument:

Invoke-LinkLanguageCheckCore -ExcludePaths @() -OutputPath $script:CustomOutputPath | Out-Null

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.

Add -OutputPath parameter to Invoke-LinkLanguageCheck.ps1

2 participants