Skip to content

Commit 5054297

Browse files
authored
fix: tests with yarn installed on windows (#1531)
1 parent d58f2d4 commit 5054297

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

test/format.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ import spawn from 'spawn-please'
66
import chaiSetup from './helpers/chaiSetup'
77
import stubVersions from './helpers/stubVersions'
88

9+
/**
10+
* Helper function to remove a directory while avoiding errors like:
11+
* Error: EBUSY: resource busy or locked, rmdir 'C:\Users\alice\AppData\Local\Temp\npm-check-updates-yc1wT3'
12+
*/
13+
async function removeDir(dirPath: string) {
14+
while (true) {
15+
try {
16+
await fs.access(dirPath, fs.constants.W_OK)
17+
} catch {
18+
continue
19+
}
20+
21+
break
22+
}
23+
24+
await fs.rm(dirPath, { recursive: true, force: true })
25+
}
26+
927
chaiSetup()
1028

1129
const bin = path.join(__dirname, '../build/cli.js')
@@ -68,7 +86,7 @@ describe('format', () => {
6886
const { stdout } = await spawn('node', [bin, '--format', 'lines'], {}, { cwd: tempDir })
6987
stdout.should.equals('ncu-test-v2@^2.0.0\nncu-test-tag@^1.1.0\n')
7088
} finally {
71-
await fs.rm(tempDir, { recursive: true, force: true })
89+
removeDir(tempDir)
7290
stub.restore()
7391
}
7492
})
@@ -103,7 +121,7 @@ describe('format', () => {
103121
},
104122
).should.eventually.be.rejectedWith('Cannot specify both --format lines and --jsonUpgraded.')
105123
} finally {
106-
await fs.rm(tempDir, { recursive: true, force: true })
124+
removeDir(tempDir)
107125
stub.restore()
108126
}
109127
})
@@ -138,7 +156,7 @@ describe('format', () => {
138156
},
139157
).should.eventually.be.rejectedWith('Cannot specify both --format lines and --jsonAll.')
140158
} finally {
141-
await fs.rm(tempDir, { recursive: true, force: true })
159+
removeDir(tempDir)
142160
stub.restore()
143161
}
144162
})
@@ -173,7 +191,7 @@ describe('format', () => {
173191
},
174192
).should.eventually.be.rejectedWith('Cannot use --format lines with other formatting options.')
175193
} finally {
176-
await fs.rm(tempDir, { recursive: true, force: true })
194+
removeDir(tempDir)
177195
stub.restore()
178196
}
179197
})

test/install.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ describe('install', () => {
3030

3131
try {
3232
const { stdout } = await spawn('node', [bin, '-u', '--packageFile', pkgFile])
33-
stripAnsi(stdout).should.include('Run npm install to install new versions')
33+
stripAnsi(stdout).should.match(/Run (npm|yarn) install to install new versions/)
3434
expect(await exists(path.join(tempDir, 'package-lock.json'))).to.be.false
35+
expect(await exists(path.join(tempDir, 'yarn.lock'))).to.be.false
3536
expect(await exists(path.join(tempDir, 'node_modules'))).to.be.false
3637
} finally {
3738
await fs.rm(tempDir, { recursive: true, force: true })
@@ -54,7 +55,7 @@ describe('install', () => {
5455

5556
try {
5657
const { stdout } = await spawn('node', [bin, '-u', '--packageFile', pkgFile, '--install', 'always'])
57-
stripAnsi(stdout).should.not.include('Run npm install to install new versions')
58+
stripAnsi(stdout).should.not.match(/Run (npm|yarn) install to install new versions/)
5859
expect(await exists(path.join(tempDir, 'package-lock.json'))).to.be.true
5960
expect(await exists(path.join(tempDir, 'node_modules'))).to.be.true
6061
} finally {
@@ -78,8 +79,9 @@ describe('install', () => {
7879

7980
try {
8081
const { stdout } = await spawn('node', [bin, '-u', '--packageFile', pkgFile, '--install', 'never'])
81-
stripAnsi(stdout).should.not.include('Run npm install to install new versions')
82+
stripAnsi(stdout).should.not.match(/Run (npm|yarn) install to install new versions/)
8283
expect(await exists(path.join(tempDir, 'package-lock.json'))).to.be.false
84+
expect(await exists(path.join(tempDir, 'yarn.lock'))).to.be.false
8385
expect(await exists(path.join(tempDir, 'node_modules'))).to.be.false
8486
} finally {
8587
await fs.rm(tempDir, { recursive: true, force: true })

0 commit comments

Comments
 (0)