Skip to content

Commit

Permalink
fix: make fs.rm recursive and forcible
Browse files Browse the repository at this point in the history
  • Loading branch information
Torathion committed Mar 7, 2024
1 parent 8d9cfed commit f208591
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 108 deletions.
18 changes: 9 additions & 9 deletions src/lib/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ const npm = (
options.packageManager === 'pnpm'
? spawnPnpm
: options.packageManager === 'yarn'
? spawnYarn
: options.packageManager === 'bun'
? spawnBun
: spawnNpm
? spawnYarn
: options.packageManager === 'bun'
? spawnBun
: spawnNpm
)(args, npmOptions, spawnOptionsMerged as any)
}

Expand Down Expand Up @@ -94,10 +94,10 @@ const doctor = async (run: Run, options: Options): Promise<void> => {
options.packageManager === 'yarn'
? 'yarn.lock'
: options.packageManager === 'pnpm'
? 'pnpm-lock.yaml'
: options.packageManager === 'bun'
? 'bun.lockb'
: 'package-lock.json'
? 'pnpm-lock.yaml'
: options.packageManager === 'bun'
? 'bun.lockb'
: 'package-lock.json'
const { pkg, pkgFile }: PackageInfo = await loadPackageFileForDoctor(options)

// flatten all deps into one so we can iterate over them
Expand Down Expand Up @@ -232,7 +232,7 @@ const doctor = async (run: Run, options: Options): Promise<void> => {
if (lockFile) {
await fs.writeFile(lockFileName, lockFile)
} else {
await fs.rm(lockFileName)
await fs.rm(lockFileName, { recursive: true, force: true })
}

// save the last package file with passing tests
Expand Down
13 changes: 7 additions & 6 deletions test/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Index } from '../src/types/IndexType'
import { Version } from '../src/types/Version'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -116,7 +117,7 @@ describe('bin', async function () {
const pkgData = JSON.parse(text)
pkgData.should.have.property('express')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand All @@ -133,7 +134,7 @@ describe('bin', async function () {
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand All @@ -153,7 +154,7 @@ describe('bin', async function () {
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand All @@ -170,7 +171,7 @@ describe('bin', async function () {
ugradedPkg.dependencies.should.have.property('express')
ugradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand All @@ -187,7 +188,7 @@ describe('bin', async function () {
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand All @@ -214,7 +215,7 @@ describe('bin', async function () {
const output = await spawn('node', [bin, '--packageFile', pkgFile, '--filter', 'ncu-test-v2 ncu-test-tag'])
output.should.include('"ncu-test-v2 ncu-test-tag"')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down
5 changes: 3 additions & 2 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CACHE_DELIMITER, resolvedDefaultCacheFile } from '../src/lib/cache'
import { CacheData } from '../src/types/Cacher'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -35,7 +36,7 @@ describe('cache', () => {
[`ncu-test-alpha${CACHE_DELIMITER}latest`]: '1.0.0',
})
} finally {
await fs.rm(resolvedDefaultCacheFile)
await fs.rm(resolvedDefaultCacheFile, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -95,7 +96,7 @@ describe('cache', () => {
[`ncu-test-alpha${CACHE_DELIMITER}greatest`]: '2.0.0-alpha.2',
})
} finally {
await fs.rm(resolvedDefaultCacheFile)
await fs.rm(resolvedDefaultCacheFile, rmOptions)
stub.restore()
}
})
Expand Down
7 changes: 4 additions & 3 deletions test/deep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ncu from '../src/'
import mergeOptions from '../src/lib/mergeOptions'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -65,7 +66,7 @@ describe('--deep', function () {
deepJsonOut['packages/sub1/package.json'].dependencies.should.have.property('express')
deepJsonOut['packages/sub2/package.json'].dependencies.should.have.property('express')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
}
})

Expand All @@ -85,7 +86,7 @@ describe('--deep', function () {
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
}
})

Expand Down Expand Up @@ -115,7 +116,7 @@ describe('--deep', function () {
json.should.have.property(path.join(tempDir, 'packages/sub2/package.json').replace(/\\/g, '/'))
json.should.have.property(path.join(tempDir, 'package.json').replace(/\\/g, '/'))
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
}
})
})
Expand Down
13 changes: 7 additions & 6 deletions test/dep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path'
import ncu from '../src/'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -98,7 +99,7 @@ describe('--dep', () => {
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -145,7 +146,7 @@ describe('--dep', () => {
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('--dep', () => {
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -236,7 +237,7 @@ describe('--dep', () => {
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -276,7 +277,7 @@ describe('--dep', () => {
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -318,7 +319,7 @@ describe('--dep', () => {
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down
35 changes: 18 additions & 17 deletions test/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { chalkInit } from '../src/lib/chalk'
import { PackageManagerName } from '../src/types/PackageManagerName'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -68,13 +69,13 @@ const testPass = ({ packageManager }: { packageManager: PackageManagerName }) =>

// cleanup before assertions in case they fail
await fs.writeFile(pkgPath, pkgOriginal)
await fs.rm(nodeModulesPath)
await fs.rm(lockfilePath)
await fs.rm(nodeModulesPath, rmOptions)
await fs.rm(lockfilePath, rmOptions)

// delete yarn cache
if (packageManager === 'yarn') {
await fs.rm(path.join(cwd, '.yarn'))
await fs.rm(path.join(cwd, '.pnp.js'))
await fs.rm(path.join(cwd, '.yarn'), rmOptions)
await fs.rm(path.join(cwd, '.pnp.js'), rmOptions)
}

// bun prints the run header to stderr instead of stdout
Expand Down Expand Up @@ -133,13 +134,13 @@ const testFail = ({ packageManager }: { packageManager: PackageManagerName }) =>
} finally {
pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
await fs.writeFile(pkgPath, pkgOriginal)
await fs.rm(nodeModulesPath)
await fs.rm(lockfilePath)
await fs.rm(nodeModulesPath, rmOptions)
await fs.rm(lockfilePath, rmOptions)

// delete yarn cache
if (packageManager === 'yarn') {
await fs.rm(path.join(cwd, '.yarn'))
await fs.rm(path.join(cwd, '.pnp.js'))
await fs.rm(path.join(cwd, '.yarn'), rmOptions)
await fs.rm(path.join(cwd, '.pnp.js'), rmOptions)
}
}

Expand Down Expand Up @@ -234,8 +235,8 @@ describe('doctor', function () {

// cleanup before assertions in case they fail
await fs.writeFile(pkgPath, pkgOriginal)
await fs.rm(lockfilePath)
await fs.rm(nodeModulesPath)
await fs.rm(lockfilePath, rmOptions)
await fs.rm(nodeModulesPath, rmOptions)

// stderr should be empty
stderr.should.equal('')
Expand Down Expand Up @@ -276,8 +277,8 @@ describe('doctor', function () {

// cleanup before assertions in case they fail
await fs.writeFile(pkgPath, pkgOriginal)
await fs.rm(lockfilePath)
await fs.rm(nodeModulesPath)
await fs.rm(lockfilePath, rmOptions)
await fs.rm(nodeModulesPath, rmOptions)

// stderr should be empty
stderr.should.equal('')
Expand Down Expand Up @@ -317,8 +318,8 @@ describe('doctor', function () {

// cleanup before assertions in case they fail
await fs.writeFile(pkgPath, pkgOriginal)
await fs.rm(lockfilePath)
await fs.rm(nodeModulesPath)
await fs.rm(lockfilePath, rmOptions)
await fs.rm(nodeModulesPath, rmOptions)

// stderr should be empty
stderr.should.equal('')
Expand Down Expand Up @@ -358,8 +359,8 @@ describe('doctor', function () {

// cleanup before assertions in case they fail
await fs.writeFile(pkgPath, pkgOriginal)
await fs.rm(lockfilePath)
await fs.rm(nodeModulesPath)
await fs.rm(lockfilePath, rmOptions)
await fs.rm(nodeModulesPath, rmOptions)

// stderr should be empty
stderr.should.equal('')
Expand Down Expand Up @@ -430,7 +431,7 @@ else {

pkgUpgraded = JSON.parse(await fs.readFile(pkgPath, 'utf-8'))
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
}

// stdout should include successful upgrades
Expand Down
3 changes: 2 additions & 1 deletion test/filterResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path'
import ncu from '../src/'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -49,7 +50,7 @@ describe('filterResults', () => {
expect(upgraded).to.have.property('ncu-test-v2', '3.0.0')
expect(upgraded).to.not.have.property('ncu-test-return-version')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down
11 changes: 6 additions & 5 deletions test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path'
import spawn from 'spawn-please'
import chaiSetup from './helpers/chaiSetup'
import stubNpmView from './helpers/stubNpmView'
import { rmOptions } from './helpers/testConstants'

chaiSetup()

Expand Down Expand Up @@ -49,7 +50,7 @@ describe('format', () => {
const output = await spawn('node', [bin, '--format', 'repo'], { cwd: tempDir })
output.should.include('https://github.com/Mitsunee/modern-diacritics')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
}
})

Expand Down Expand Up @@ -77,7 +78,7 @@ describe('format', () => {
const output = await spawn('node', [bin, '--format', 'lines'], { cwd: tempDir })
output.should.equals('ncu-test-v2@^2.0.0\nncu-test-tag@^1.1.0\n')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -107,7 +108,7 @@ describe('format', () => {
cwd: tempDir,
}).should.eventually.be.rejectedWith('Cannot specify both --format lines and --jsonUpgraded.')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -137,7 +138,7 @@ describe('format', () => {
cwd: tempDir,
}).should.eventually.be.rejectedWith('Cannot specify both --format lines and --jsonAll.')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down Expand Up @@ -167,7 +168,7 @@ describe('format', () => {
cwd: tempDir,
}).should.eventually.be.rejectedWith('Cannot use --format lines with other formatting options.')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
await fs.rm(tempDir, rmOptions)
stub.restore()
}
})
Expand Down
Loading

0 comments on commit f208591

Please sign in to comment.