Skip to content

Commit cefb6e7

Browse files
Sync eng/common directory with azure-sdk-tools for PR 9013 (#37475)
* Spelling: Remove deprecated --no-install, replace with --no * Extract testing, use npm exec instead of npx * Move tests to eng/common-tests/ * Cleanup, documentation * More cleanup * Update $command --------- Co-authored-by: Daniel Jurek <[email protected]>
1 parent 91cf2f3 commit cefb6e7

File tree

2 files changed

+22
-226
lines changed

2 files changed

+22
-226
lines changed

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

Lines changed: 17 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ Optional location to use for cspell.json path. Default value is
3434
.PARAMETER ExitWithError
3535
Exit 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

6374
Set-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 = @()
27297
foreach ($file in $changedFilesList) {

eng/common/spelling/Invoke-Cspell.ps1

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ If set the PackageInstallCache will not be deleted. Use if there are multiple
2727
calls to Invoke-Cspell.ps1 to prevent creating multiple working directories and
2828
redundant calls `npm ci`.
2929
30-
.PARAMETER Test
31-
Run test functions against the script logic
32-
3330
.EXAMPLE
3431
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs 'sdk/*/*/PublicAPI/**/*.md'
3532
@@ -64,10 +61,7 @@ param(
6461
[string] $PackageInstallCache = (Join-Path ([System.IO.Path]::GetTempPath()) "cspell-tool-path"),
6562

6663
[Parameter()]
67-
[switch] $LeavePackageInstallCache,
68-
69-
[Parameter()]
70-
[switch] $Test
64+
[switch] $LeavePackageInstallCache
7165
)
7266

7367
Set-StrictMode -Version 3.0
@@ -82,30 +76,6 @@ if (!(Test-Path $CSpellConfigPath)) {
8276
exit 1
8377
}
8478

85-
function Test-VersionReportMatches() {
86-
# Arrange
87-
$expectedPackageVersion = '6.12.0'
88-
89-
# Act
90-
$actual = &"$PSScriptRoot/Invoke-Cspell.ps1" `
91-
-JobType '--version'
92-
93-
# Assert
94-
if ($actual -ne $expectedPackageVersion) {
95-
throw "Mismatched version. Expected:`n$expectedPackageVersion`n`nActual:`n$actual"
96-
}
97-
}
98-
99-
function TestInvokeCspell() {
100-
Test-VersionReportMatches
101-
}
102-
103-
if ($Test) {
104-
TestInvokeCspell
105-
Write-Host "Test complete"
106-
exit 0
107-
}
108-
10979
# Prepare the working directory if it does not already have requirements in
11080
# place.
11181
if (!(Test-Path $PackageInstallCache)) {
@@ -170,10 +140,11 @@ try {
170140
npm ci | Write-Host
171141

172142
# Use the mutated configuration file when calling cspell
173-
$command = "npx cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --relative"
143+
$command = "npm exec --no -- cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --relative"
174144
Write-Host $command
175-
$cspellOutput = npx `
176-
--no-install `
145+
$cspellOutput = npm exec `
146+
--no `
147+
-- `
177148
cspell `
178149
$JobType `
179150
--config $CSpellConfigPath `

0 commit comments

Comments
 (0)