From 530243eb99ea1ec2a89c94d22269d925336e13c2 Mon Sep 17 00:00:00 2001 From: Daniel Jurek Date: Fri, 20 Sep 2024 10:08:20 -0700 Subject: [PATCH] Extract testing, use npm exec instead of npx --- .../check-spelling-in-changed-files.ps1 | 197 +----------------- .../check-spelling-in-changed-files.tests.ps1 | 176 ++++++++++++++++ eng/common/spelling/Invoke-Cspell.Tests.ps1 | 13 ++ eng/common/spelling/Invoke-Cspell.ps1 | 5 +- 4 files changed, 199 insertions(+), 192 deletions(-) create mode 100644 eng/common/scripts/check-spelling-in-changed-files.tests.ps1 create mode 100644 eng/common/spelling/Invoke-Cspell.Tests.ps1 diff --git a/eng/common/scripts/check-spelling-in-changed-files.ps1 b/eng/common/scripts/check-spelling-in-changed-files.ps1 index 55bc3d0339..84b910fd5c 100644 --- a/eng/common/scripts/check-spelling-in-changed-files.ps1 +++ b/eng/common/scripts/check-spelling-in-changed-files.ps1 @@ -57,199 +57,14 @@ Param ( [switch] $ExitWithError, [Parameter()] - [switch] $Test + [string]$SourceCommittish = "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}", + + [Parameter()] + [string]$TargetCommittish = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/") ) Set-StrictMode -Version 3.0 -function TestSpellChecker() { - Test-Exit0WhenAllFilesExcluded - ResetTest - Test-Exit1WhenIncludedFileHasSpellingError - ResetTest - Test-Exit0WhenIncludedFileHasNoSpellingError - ResetTest - Test-Exit1WhenChangedFileAlreadyHasSpellingError - ResetTest - Test-Exit0WhenUnalteredFileHasSpellingError - ResetTest - Test-Exit0WhenSpellingErrorsAndNoExitWithError -} - -function Test-Exit0WhenAllFilesExcluded() { - # Arrange - "sepleing errrrrorrrrr" > ./excluded/excluded-file.txt - git add -A - git commit -m "One change" - - # Act - &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` - -CspellConfigPath "./.vscode/cspell.json" ` - -SpellCheckRoot "./" ` - -ExitWithError - - # Assert - if ($LASTEXITCODE -ne 0) { - throw "`$LASTEXITCODE != 0" - } -} - -function Test-Exit1WhenIncludedFileHasSpellingError() { - # Arrange - "sepleing errrrrorrrrr" > ./included/included-file.txt - git add -A - git commit -m "One change" - - # Act - &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` - -CspellConfigPath "./.vscode/cspell.json" ` - -SpellCheckRoot "./" ` - -ExitWithError - - # Assert - if ($LASTEXITCODE -ne 1) { - throw "`$LASTEXITCODE != 1" - } -} - -function Test-Exit0WhenIncludedFileHasNoSpellingError() { - # Arrange - "correct spelling" > ./included/included-file.txt - git add -A - git commit -m "One change" - - # Act - &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` - -CspellConfigPath "./.vscode/cspell.json" ` - -SpellCheckRoot "./" ` - -ExitWithError - - # Assert - if ($LASTEXITCODE -ne 0) { - throw "`$LASTEXITCODE != 0" - } -} - -function Test-Exit1WhenChangedFileAlreadyHasSpellingError() { - # Arrange - "sepleing errrrrorrrrr" > ./included/included-file.txt - git add -A - git commit -m "First change" - - "A statement without spelling errors" >> ./included/included-file.txt - git add -A - git commit -m "Second change" - - # Act - &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` - -CspellConfigPath "./.vscode/cspell.json" ` - -SpellCheckRoot "./" ` - -ExitWithError - - # Assert - if ($LASTEXITCODE -ne 1) { - throw "`$LASTEXITCODE != 1" - } -} - -function Test-Exit0WhenUnalteredFileHasSpellingError() { - # Arrange - "sepleing errrrrorrrrr" > ./included/included-file-1.txt - git add -A - git commit -m "One change" - - "A statement without spelling errors" > ./included/included-file-2.txt - git add -A - git commit -m "Second change" - - # Act - &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` - -CspellConfigPath "./.vscode/cspell.json" ` - -SpellCheckRoot "./" ` - -ExitWithError - - # Assert - if ($LASTEXITCODE -ne 0) { - throw "`$LASTEXITCODE != 0" - } -} - -function Test-Exit0WhenSpellingErrorsAndNoExitWithError() { - # Arrange - "sepleing errrrrorrrrr" > ./included/included-file-1.txt - git add -A - git commit -m "One change" - - # Act - &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` - -CspellConfigPath "./.vscode/cspell.json" ` - -SpellCheckRoot "./" - - # Assert - if ($LASTEXITCODE -ne 0) { - throw "`$LASTEXITCODE != 0" - } -} - -function SetupTest($workingDirectory) { - Write-Host "Create test temp dir: $workingDirectory" - New-Item -ItemType Directory -Force -Path $workingDirectory | Out-Null - - Push-Location $workingDirectory | Out-Null - git init - - New-Item -ItemType Directory -Force -Path "./excluded" - New-Item -ItemType Directory -Force -Path "./included" - New-Item -ItemType Directory -Force -Path "./.vscode" - - "Placeholder" > "./excluded/placeholder.txt" - "Placeholder" > "./included/placeholder.txt" - - $configJsonContent = @" -{ - "version": "0.1", - "language": "en", - "ignorePaths": [ - ".vscode/cspell.json", - "excluded/**" - ] -} -"@ - $configJsonContent > "./.vscode/cspell.json" - - git add -A - git commit -m "Init" -} - -function ResetTest() { - # Empty out the working tree - git checkout . - git clean -xdf - - $revCount = git rev-list --count HEAD - if ($revCount -gt 1) { - # Reset N-1 changes so there is only the initial commit - $revisionsToReset = $revCount - 1 - git reset --hard HEAD~$revisionsToReset - } -} - -function TeardownTest($workingDirectory) { - Pop-Location | Out-Null - Write-Host "Remove test temp dir: $workingDirectory" - Remove-Item -Path $workingDirectory -Recurse -Force | Out-Null -} - -if ($Test) { - $workingDirectory = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) - - SetupTest $workingDirectory - TestSpellChecker - TeardownTest $workingDirectory - Write-Host "Test complete" - exit 0 -} - $ErrorActionPreference = "Continue" . $PSScriptRoot/common.ps1 @@ -266,7 +81,9 @@ if (!(Test-Path $CspellConfigPath)) { # Lists names of files that were in some way changed between the # current branch and default target branch. Excludes files that were deleted to # prevent errors in Resolve-Path -$changedFilesList = Get-ChangedFiles +$changedFilesList = Get-ChangedFiles ` + -SourceCommittish $SourceCommittish ` + -TargetCommittish $TargetCommittish $changedFiles = @() foreach ($file in $changedFilesList) { diff --git a/eng/common/scripts/check-spelling-in-changed-files.tests.ps1 b/eng/common/scripts/check-spelling-in-changed-files.tests.ps1 new file mode 100644 index 0000000000..56606cd361 --- /dev/null +++ b/eng/common/scripts/check-spelling-in-changed-files.tests.ps1 @@ -0,0 +1,176 @@ +Describe 'Spell checking' { + BeforeAll { + $workingDirectory = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) + + Write-Host "Create test temp dir: $workingDirectory" + New-Item -ItemType Directory -Force -Path $workingDirectory | Out-Null + + Push-Location $workingDirectory | Out-Null + git init + + New-Item -ItemType Directory -Force -Path "./excluded" + New-Item -ItemType Directory -Force -Path "./included" + New-Item -ItemType Directory -Force -Path "./.vscode" + + "Placeholder" > "./excluded/placeholder.txt" + "Placeholder" > "./included/placeholder.txt" + + $configJsonContent = @" +{ + "version": "0.2", + "language": "en", + "ignorePaths": [ + ".vscode/cspell.json", + "excluded/**" + ] +} +"@ + $configJsonContent > "./.vscode/cspell.json" + + git add -A + git commit -m "Init" + } + + AfterAll { + Pop-Location | Out-Null + Write-Host "Remove test temp dir: $workingDirectory" + Remove-Item -Path $workingDirectory -Recurse -Force | Out-Null + } + + BeforeEach { + $initCommit = git rev-parse HEAD + } + + AfterEach { + # Empty out the working tree + git checkout . + git clean -xdf + + $revCount = git rev-list --count HEAD + if ($revCount -gt 1) { + # Reset N-1 changes so there is only the initial commit + $revisionsToReset = $revCount - 1 + git reset --hard HEAD~$revisionsToReset + } + } + + + It 'Exits 0 when all files are excluded' { + # Arrange + "sepleing errrrrorrrrr" > ./excluded/excluded-file.txt + git add -A + git commit -m "One change" + + # Act + &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` + -CspellConfigPath "./.vscode/cspell.json" ` + -SpellCheckRoot "./" ` + -ExitWithError ` + -SourceCommittish (git rev-parse HEAD) ` + -TargetCommittish $initCommit + + # Assert + $LASTEXITCODE | Should -Be 0 + } + + It 'Exits 1 when included file has spelling error' { + # Arrange + "sepleing errrrrorrrrr" > ./included/included-file.txt + git add -A + git commit -m "One change" + + # Act + &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` + -CspellConfigPath "./.vscode/cspell.json" ` + -SpellCheckRoot "./" ` + -ExitWithError ` + -SourceCommittish (git rev-parse HEAD) ` + -TargetCommittish $initCommit + + # Assert + $LASTEXITCODE | Should -Be 1 + } + + It 'Exits 0 when included file has no spelling error' { + # Arrange + "correct spelling" > ./included/included-file.txt + git add -A + git commit -m "One change" + + # Act + &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` + -CspellConfigPath "./.vscode/cspell.json" ` + -SpellCheckRoot "./" ` + -ExitWithError ` + -SourceCommittish (git rev-parse HEAD) ` + -TargetCommittish $initCommit + + # Assert + $LASTEXITCODE | Should -Be 0 + } + + It 'Exits 1 when changed file already has spelling error' { + # Arrange + "sepleing errrrrorrrrr" > ./included/included-file.txt + git add -A + git commit -m "First change" + + "A statement without spelling errors" >> ./included/included-file.txt + git add -A + git commit -m "Second change" + + # Act + &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` + -CspellConfigPath "./.vscode/cspell.json" ` + -SpellCheckRoot "./" ` + -ExitWithError ` + -SourceCommittish (git rev-parse HEAD) ` + -TargetCommittish $initCommit + + # Assert + $LASTEXITCODE | Should -Be 1 + } + + It 'Exits 0 when unaltered file has spelling error' { + # Arrange + "sepleing errrrrorrrrr" > ./included/included-file-1.txt + git add -A + git commit -m "One change" + + # Use baseCommit instead of initCommit to simulate a scenario where the + # file with the spelling error is already checked in. + $baseCommit = git rev-parse HEAD + + "A statement without spelling errors" > ./included/included-file-2.txt + git add -A + git commit -m "Second change" + + # Act + &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` + -CspellConfigPath "./.vscode/cspell.json" ` + -SpellCheckRoot "./" ` + -ExitWithError ` + -SourceCommittish (git rev-parse HEAD) ` + -TargetCommittish $baseCommit + + # Assert + $LASTEXITCODE | Should -Be 0 + } + + It 'Exits 0 when spelling errors and no ExitWithError' { + # Arrange + "sepleing errrrrorrrrr" > ./included/included-file-1.txt + git add -A + git commit -m "One change" + + # Act + &"$PSScriptRoot/check-spelling-in-changed-files.ps1" ` + -CspellConfigPath "./.vscode/cspell.json" ` + -SpellCheckRoot "./" ` + -SourceCommittish (git rev-parse HEAD) ` + -TargetCommittish $initCommit + + # Assert + $LASTEXITCODE | Should -Be 0 + } +} \ No newline at end of file diff --git a/eng/common/spelling/Invoke-Cspell.Tests.ps1 b/eng/common/spelling/Invoke-Cspell.Tests.ps1 new file mode 100644 index 0000000000..885149f7c9 --- /dev/null +++ b/eng/common/spelling/Invoke-Cspell.Tests.ps1 @@ -0,0 +1,13 @@ +Describe 'Tool Version' { + It 'Should have the correct version' { + # Arrange + $expectedPackageVersion = '6.31.2' + + # Act + $actual = &"$PSScriptRoot/../spelling/Invoke-Cspell.ps1" ` + -JobType '--version' + + # Assert + $actual | Should -Be $expectedPackageVersion + } +} diff --git a/eng/common/spelling/Invoke-Cspell.ps1 b/eng/common/spelling/Invoke-Cspell.ps1 index 061f8138af..c5028802a2 100644 --- a/eng/common/spelling/Invoke-Cspell.ps1 +++ b/eng/common/spelling/Invoke-Cspell.ps1 @@ -170,10 +170,11 @@ try { npm ci | Write-Host # Use the mutated configuration file when calling cspell - $command = "npx --no cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --relative" + $command = "npm exec cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --relative" Write-Host $command - $cspellOutput = npx ` + $cspellOutput = npm exec ` --no ` + -- ` cspell ` $JobType ` --config $CSpellConfigPath `