Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to exit gracefully when all files in the diff are excluded #1946

Merged
6 commits merged into from
Sep 13, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions eng/common/scripts/check-spelling-in-changed-files.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ foreach ($file in $changedFiles) {
$changedFilePaths += $file.Path
}

# The "files" list must always contain a file which exists, is not empty, and is
# not excluded in ignorePaths. In this case it will be a file with the contents
# "1" (no spelling errors will be detected)
$notExcludedFile = (New-TemporaryFile).ToString()
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
"1" >> $notExcludedFile
$changedFilePaths += $notExcludedFile

# Using GetTempPath because it works on linux and windows. Setting .json
# extension because cspell requires the file have a .json or .jsonc extension
$cspellConfigTemporaryPath = Join-Path `
Expand Down Expand Up @@ -79,17 +86,28 @@ Set-Content `
-Path $cspellConfigTemporaryPath `
-Value (ConvertTo-Json $cspellConfig -Depth 100)

# cspell will merge configurations from files in well-known locations. In this
# case the `.vscode/cspell.json` file is a well-known location. To force cspell
# to use ONLY the temporary config file we rename the config file in the
# well-known location to a temporary name. (`.vscode/cspell.json.ignore`)
Write-Host "Renaming $CspellConfigPath"
danieljurek marked this conversation as resolved.
Show resolved Hide resolved
$originalConfigPath = Resolve-Path $CspellConfigPath
Move-Item $originalConfigPath "$originalConfigPath.ignore" | Out-Null

# Use the mutated configuration file when calling cspell
Write-Host "npx cspell lint --config $cspellConfigTemporaryPath"
$spellingErrors = npx cspell lint --config $cspellConfigTemporaryPath
Write-Host "npx cspell lint --config $cspellConfigTemporaryPath --no-must-find-files "
$spellingErrors = npx cspell lint --config $cspellConfigTemporaryPath --no-must-find-files

Write-Host "cspell run complete, restoring original configuration."
Move-Item "$originalConfigPath.ignore" $originalConfigPath -Force | Out-Null

if ($spellingErrors) {
$errorLoggingFunction = Get-Item 'Function:LogWarning'
if ($ExitWithError) {
$errorLoggingFunction = Get-Item 'Function:LogError'
}

foreach ($spellingError in $spellingErrors) {
foreach ($spellingError in $spellingErrors) {
&$errorLoggingFunction $spellingError
}
&$errorLoggingFunction "Spelling errors detected. To correct false positives or learn about spell checking see: https://aka.ms/azsdk/engsys/spellcheck"
Expand All @@ -99,7 +117,8 @@ if ($spellingErrors) {
}
} else {
Write-Host "No spelling errors detected. Removing temporary config file."
Remove-Item -Path $cspellConfigTemporaryPath -Force
Remove-Item -Path $cspellConfigTemporaryPath -Force | Out-Null
Remove-Item -Path $notExcludedFile -Force | Out-Null
}

exit 0
Expand Down