File: check-spelling-in-changed-files.ps1

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (312 lines) | stat: -rw-r--r-- 8,491 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# cSpell:ignore LASTEXITCODE
# cSpell:ignore errrrrorrrrr
# cSpell:ignore sepleing
<#
.SYNOPSIS
Uses cspell (from NPM) to check spelling of recently changed files

.DESCRIPTION
This script checks files that have changed relative to a base branch (default
branch) for spelling errors. Dictionaries and spelling configurations reside
in a configurable `cspell.json` location.

This script uses `npx` and assumes that NodeJS (and by extension `npm` and
`npx`) are installed on the machine. If it does not detect `npx` it will warn
the user and exit with an error.

The entire file is scanned, not just changed sections. Spelling errors in parts
of the file not touched will still be shown.

This script copies the config file supplied in CspellConfigPath to a temporary
location, mutates the config file to include only the files that have changed,
and then uses the mutated config file to call cspell. In the case of success
the temporary file is deleted. In the case of failure the temporary file, whose
location was logged to the console, remains on disk.

.PARAMETER SpellCheckRoot
Root folder from which to generate relative paths for spell checking. Mostly
used in testing.

.PARAMETER CspellConfigPath
Optional location to use for cspell.json path. Default value is 
`./.vscode/cspell.json`

.PARAMETER ExitWithError
Exit with error code 1 if spelling errors are detected.

.PARAMETER Test
Run test functions against the script logic

.EXAMPLE
./eng/common/scripts/check-spelling-in-changed-files.ps1 

This will run spell check with changes in the current branch with respect to 
`target_branch_name`

#>

[CmdletBinding()]
Param (
    [Parameter()]
    [string] $CspellConfigPath = (Resolve-Path "$PSScriptRoot/../../../.vscode/cspell.json"),

    [Parameter()]
    [string] $SpellCheckRoot = (Resolve-Path "$PSScriptRoot/../../../"),

    [Parameter()]
    [switch] $ExitWithError,

    [Parameter()]
    [switch] $Test
)

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

if ((Get-Command git | Measure-Object).Count -eq 0) {
    LogError "Could not locate git. Install git https://git-scm.com/downloads"
    exit 1
}

if (!(Test-Path $CspellConfigPath)) {
    LogError "Could not locate config file $CspellConfigPath"
    exit 1
}

# 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

$changedFiles = @()
foreach ($file in $changedFilesList) {
  $changedFiles += Resolve-Path $file
}

$changedFilesCount = ($changedFiles | Measure-Object).Count
Write-Host "Git Detected $changedFilesCount changed file(s). Files checked by cspell may exclude files according to cspell.json"

if ($changedFilesCount -eq 0) {
    Write-Host "No changes detected"
    exit 0
}

$changedFilePaths = @()
foreach ($file in $changedFiles) {
    $changedFilePaths += $file.Path
}

$spellingErrors = &"$PSScriptRoot/../spelling/Invoke-Cspell.ps1" `
  -CspellConfigPath $CspellConfigPath `
  -SpellCheckRoot $SpellCheckRoot `
  -ScanGlobs $changedFilePaths

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

    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"

    if ($ExitWithError) {
        exit 1
    }
} else {
  Write-Host "No spelling errors detected"
}

exit 0