@@ -34,8 +34,16 @@ Optional location to use for cspell.json path. Default value is
3434. PARAMETER ExitWithError
3535Exit with error code 1 if spelling errors are detected.
3636
37- . PARAMETER Test
38- Run test functions against the script logic
37+ . PARAMETER SourceCommittish
38+ Commit SHA (or ref) used for file list generation. This is the later commit. The
39+ default value is useful for Azure DevOps pipelines. The default value is
40+ `${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}`
41+
42+ . PARAMETER TargetCommittish
43+ Commit SHA (or ref) used for file list generation. This is the "base" commit.
44+ The default value is useful for Azure DevOps pipelines. The default value is
45+ `origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}` with some string manipulation to
46+ remove the `refs/heads/` prefix.
3947
4048. EXAMPLE
4149./eng/common/scripts/check-spelling-in-changed-files.ps1
@@ -57,199 +65,14 @@ Param (
5765 [switch ] $ExitWithError ,
5866
5967 [Parameter ()]
60- [switch ] $Test
68+ [string ]$SourceCommittish = " ${env: SYSTEM_PULLREQUEST_SOURCECOMMITID} " ,
69+
70+ [Parameter ()]
71+ [string ]$TargetCommittish = (" origin/${env: SYSTEM_PULLREQUEST_TARGETBRANCH} " -replace " refs/heads/" )
6172)
6273
6374Set-StrictMode - Version 3.0
6475
65- function TestSpellChecker () {
66- Test-Exit0WhenAllFilesExcluded
67- ResetTest
68- Test-Exit1WhenIncludedFileHasSpellingError
69- ResetTest
70- Test-Exit0WhenIncludedFileHasNoSpellingError
71- ResetTest
72- Test-Exit1WhenChangedFileAlreadyHasSpellingError
73- ResetTest
74- Test-Exit0WhenUnalteredFileHasSpellingError
75- ResetTest
76- Test-Exit0WhenSpellingErrorsAndNoExitWithError
77- }
78-
79- function Test-Exit0WhenAllFilesExcluded () {
80- # Arrange
81- " sepleing errrrrorrrrr" > ./ excluded/ excluded- file.txt
82- git add - A
83- git commit - m " One change"
84-
85- # Act
86- & " $PSScriptRoot /check-spelling-in-changed-files.ps1" `
87- - CspellConfigPath " ./.vscode/cspell.json" `
88- - SpellCheckRoot " ./" `
89- - ExitWithError
90-
91- # Assert
92- if ($LASTEXITCODE -ne 0 ) {
93- throw " `$ LASTEXITCODE != 0"
94- }
95- }
96-
97- function Test-Exit1WhenIncludedFileHasSpellingError () {
98- # Arrange
99- " sepleing errrrrorrrrr" > ./ included/ included- file.txt
100- git add - A
101- git commit - m " One change"
102-
103- # Act
104- & " $PSScriptRoot /check-spelling-in-changed-files.ps1" `
105- - CspellConfigPath " ./.vscode/cspell.json" `
106- - SpellCheckRoot " ./" `
107- - ExitWithError
108-
109- # Assert
110- if ($LASTEXITCODE -ne 1 ) {
111- throw " `$ LASTEXITCODE != 1"
112- }
113- }
114-
115- function Test-Exit0WhenIncludedFileHasNoSpellingError () {
116- # Arrange
117- " correct spelling" > ./ included/ included- file.txt
118- git add - A
119- git commit - m " One change"
120-
121- # Act
122- & " $PSScriptRoot /check-spelling-in-changed-files.ps1" `
123- - CspellConfigPath " ./.vscode/cspell.json" `
124- - SpellCheckRoot " ./" `
125- - ExitWithError
126-
127- # Assert
128- if ($LASTEXITCODE -ne 0 ) {
129- throw " `$ LASTEXITCODE != 0"
130- }
131- }
132-
133- function Test-Exit1WhenChangedFileAlreadyHasSpellingError () {
134- # Arrange
135- " sepleing errrrrorrrrr" > ./ included/ included- file.txt
136- git add - A
137- git commit - m " First change"
138-
139- " A statement without spelling errors" >> ./ included/ included- file.txt
140- git add - A
141- git commit - m " Second change"
142-
143- # Act
144- & " $PSScriptRoot /check-spelling-in-changed-files.ps1" `
145- - CspellConfigPath " ./.vscode/cspell.json" `
146- - SpellCheckRoot " ./" `
147- - ExitWithError
148-
149- # Assert
150- if ($LASTEXITCODE -ne 1 ) {
151- throw " `$ LASTEXITCODE != 1"
152- }
153- }
154-
155- function Test-Exit0WhenUnalteredFileHasSpellingError () {
156- # Arrange
157- " sepleing errrrrorrrrr" > ./ included/ included- file- 1. txt
158- git add - A
159- git commit - m " One change"
160-
161- " A statement without spelling errors" > ./ included/ included- file- 2. txt
162- git add - A
163- git commit - m " Second change"
164-
165- # Act
166- & " $PSScriptRoot /check-spelling-in-changed-files.ps1" `
167- - CspellConfigPath " ./.vscode/cspell.json" `
168- - SpellCheckRoot " ./" `
169- - ExitWithError
170-
171- # Assert
172- if ($LASTEXITCODE -ne 0 ) {
173- throw " `$ LASTEXITCODE != 0"
174- }
175- }
176-
177- function Test-Exit0WhenSpellingErrorsAndNoExitWithError () {
178- # Arrange
179- " sepleing errrrrorrrrr" > ./ included/ included- file- 1. txt
180- git add - A
181- git commit - m " One change"
182-
183- # Act
184- & " $PSScriptRoot /check-spelling-in-changed-files.ps1" `
185- - CspellConfigPath " ./.vscode/cspell.json" `
186- - SpellCheckRoot " ./"
187-
188- # Assert
189- if ($LASTEXITCODE -ne 0 ) {
190- throw " `$ LASTEXITCODE != 0"
191- }
192- }
193-
194- function SetupTest ($workingDirectory ) {
195- Write-Host " Create test temp dir: $workingDirectory "
196- New-Item - ItemType Directory - Force - Path $workingDirectory | Out-Null
197-
198- Push-Location $workingDirectory | Out-Null
199- git init
200-
201- New-Item - ItemType Directory - Force - Path " ./excluded"
202- New-Item - ItemType Directory - Force - Path " ./included"
203- New-Item - ItemType Directory - Force - Path " ./.vscode"
204-
205- " Placeholder" > " ./excluded/placeholder.txt"
206- " Placeholder" > " ./included/placeholder.txt"
207-
208- $configJsonContent = @"
209- {
210- "version": "0.1",
211- "language": "en",
212- "ignorePaths": [
213- ".vscode/cspell.json",
214- "excluded/**"
215- ]
216- }
217- "@
218- $configJsonContent > " ./.vscode/cspell.json"
219-
220- git add - A
221- git commit - m " Init"
222- }
223-
224- function ResetTest () {
225- # Empty out the working tree
226- git checkout .
227- git clean - xdf
228-
229- $revCount = git rev- list -- count HEAD
230- if ($revCount -gt 1 ) {
231- # Reset N-1 changes so there is only the initial commit
232- $revisionsToReset = $revCount - 1
233- git reset -- hard HEAD~$revisionsToReset
234- }
235- }
236-
237- function TeardownTest ($workingDirectory ) {
238- Pop-Location | Out-Null
239- Write-Host " Remove test temp dir: $workingDirectory "
240- Remove-Item - Path $workingDirectory - Recurse - Force | Out-Null
241- }
242-
243- if ($Test ) {
244- $workingDirectory = Join-Path ([System.IO.Path ]::GetTempPath()) ([System.IO.Path ]::GetRandomFileName())
245-
246- SetupTest $workingDirectory
247- TestSpellChecker
248- TeardownTest $workingDirectory
249- Write-Host " Test complete"
250- exit 0
251- }
252-
25376$ErrorActionPreference = " Continue"
25477. $PSScriptRoot / common.ps1
25578
@@ -266,7 +89,9 @@ if (!(Test-Path $CspellConfigPath)) {
26689# Lists names of files that were in some way changed between the
26790# current branch and default target branch. Excludes files that were deleted to
26891# prevent errors in Resolve-Path
269- $changedFilesList = Get-ChangedFiles
92+ $changedFilesList = Get-ChangedFiles `
93+ - SourceCommittish $SourceCommittish `
94+ - TargetCommittish $TargetCommittish
27095
27196$changedFiles = @ ()
27297foreach ($file in $changedFilesList ) {
0 commit comments