Skip to content

Commit

Permalink
formatting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 1, 2023
1 parent 6bfd096 commit 49a2958
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/coverage
/dist
!/typedoc.json
!/tsconfig-*.json
!/.prettierignore
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# v4.1

- Improved hybrid module with no need to look at the `.default`
dangly bit. `.default` preserved as a reference to `rimraf`
dangly bit. `.default` preserved as a reference to `rimraf`
for compatibility with anyone who came to rely on it in v4.0.
- Accept and ignore `-rf` and `-fr` arguments to the bin.

Expand Down
36 changes: 21 additions & 15 deletions test/default-tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ const t = require('tap')

t.test('posix platform', async t => {
const { tmpdir } = require('os')
const { defaultTmp, defaultTmpSync } = t.mock('../dist/cjs/src/default-tmp.js', {
'../dist/cjs/src/platform.js': 'posix',
})
const { defaultTmp, defaultTmpSync } = t.mock(
'../dist/cjs/src/default-tmp.js',
{
'../dist/cjs/src/platform.js': 'posix',
}
)
t.equal(defaultTmpSync('anything'), tmpdir())
t.equal(await defaultTmp('anything').then(t => t), tmpdir())
})
Expand All @@ -20,19 +23,22 @@ t.test('windows', async t => {
throw Object.assign(new Error('no ents here'), { code: 'ENOENT' })
}
}
const { defaultTmp, defaultTmpSync } = t.mock('../dist/cjs/src/default-tmp.js', {
os: {
tmpdir: () => 'C:\\Windows\\Temp',
},
path: require('path').win32,
'../dist/cjs/src/platform.js': 'win32',
'../dist/cjs/src/fs.js': {
statSync: tempDirCheck,
promises: {
stat: async path => tempDirCheck(path),
const { defaultTmp, defaultTmpSync } = t.mock(
'../dist/cjs/src/default-tmp.js',
{
os: {
tmpdir: () => 'C:\\Windows\\Temp',
},
path: require('path').win32,
'../dist/cjs/src/platform.js': 'win32',
'../dist/cjs/src/fs.js': {
statSync: tempDirCheck,
promises: {
stat: async path => tempDirCheck(path),
},
},
},
})
}
)

const expect = {
'c:\\some\\path': 'C:\\Windows\\Temp',
Expand Down
6 changes: 1 addition & 5 deletions test/delete-many-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ const START = (process.env.RIMRAF_TEST_START_CHAR || 'a').charCodeAt(0)
const END = (process.env.RIMRAF_TEST_END_CHAR || 'f').charCodeAt(0)
const DEPTH = +process.env.RIMRAF_TEST_DEPTH || 4

const {
statSync,
mkdirSync,
readdirSync,
} = require('../dist/cjs/src/fs.js')
const { statSync, mkdirSync, readdirSync } = require('../dist/cjs/src/fs.js')
const { writeFileSync } = require('fs')
const { resolve, dirname } = require('path')

Expand Down
5 changes: 4 additions & 1 deletion test/ignore-enoent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const t = require('tap')
const { ignoreENOENT, ignoreENOENTSync } = require('../dist/cjs/src/ignore-enoent.js')
const {
ignoreENOENT,
ignoreENOENTSync,
} = require('../dist/cjs/src/ignore-enoent.js')

const enoent = Object.assign(new Error('no ent'), { code: 'ENOENT' })
const eperm = Object.assign(new Error('eperm'), { code: 'EPERM' })
Expand Down
15 changes: 12 additions & 3 deletions test/rimraf-manual.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const t = require('tap')

const { rimrafWindows, rimrafWindowsSync } = require('../dist/cjs/src/rimraf-windows.js')
const {
rimrafWindows,
rimrafWindowsSync,
} = require('../dist/cjs/src/rimraf-windows.js')

const { rimrafPosix, rimrafPosixSync } = require('../dist/cjs/src/rimraf-posix.js')
const {
rimrafPosix,
rimrafPosixSync,
} = require('../dist/cjs/src/rimraf-posix.js')

const { rimrafManual, rimrafManualSync } = require('../dist/cjs/src/rimraf-manual.js')
const {
rimrafManual,
rimrafManualSync,
} = require('../dist/cjs/src/rimraf-manual.js')

if (!process.env.__TESTING_RIMRAF_PLATFORM__) {
const otherPlatform = process.platform !== 'win32' ? 'win32' : 'posix'
Expand Down
9 changes: 6 additions & 3 deletions test/rimraf-move-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ t.only('actually delete some stuff', async t => {
// but actually do wait to clean them up, though
t.teardown(() => Promise.all(danglers))

const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
'../dist/cjs/src/fs.js': fsMock,
})
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
'../dist/cjs/src/fs.js': fsMock,
}
)

const { rimrafMoveRemove, rimrafMoveRemoveSync } = t.mock(
'../dist/cjs/src/rimraf-move-remove.js',
Expand Down
9 changes: 6 additions & 3 deletions test/rimraf-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ const fs = {
},
}

const { rimrafNative, rimrafNativeSync } = t.mock('../dist/cjs/src/rimraf-native.js', {
'../dist/cjs/src/fs.js': fs,
})
const { rimrafNative, rimrafNativeSync } = t.mock(
'../dist/cjs/src/rimraf-native.js',
{
'../dist/cjs/src/fs.js': fs,
}
)

t.test('calls the right node function', async t => {
await rimrafNative('path', { x: 'y' })
Expand Down
150 changes: 84 additions & 66 deletions test/rimraf-posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
// }

const t = require('tap')
const { rimrafPosix, rimrafPosixSync } = require('../dist/cjs/src/rimraf-posix.js')
const {
rimrafPosix,
rimrafPosixSync,
} = require('../dist/cjs/src/rimraf-posix.js')

const fs = require('../dist/cjs/src/fs.js')

Expand Down Expand Up @@ -56,92 +59,104 @@ t.test('actually delete some stuff', t => {
})

t.test('throw unlink errors', async t => {
const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
'../dist/cjs/src/fs.js': {
...fs,
unlinkSync: path => {
throw Object.assign(new Error('cannot unlink'), { code: 'FOO' })
},
promises: {
...fs.promises,
unlink: async path => {
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
'../dist/cjs/src/fs.js': {
...fs,
unlinkSync: path => {
throw Object.assign(new Error('cannot unlink'), { code: 'FOO' })
},
promises: {
...fs.promises,
unlink: async path => {
throw Object.assign(new Error('cannot unlink'), { code: 'FOO' })
},
},
},
},
})
}
)
const path = t.testdir(fixture)
t.throws(() => rimrafPosixSync(path, {}), { code: 'FOO' })
t.rejects(rimrafPosix(path, {}), { code: 'FOO' })
})

t.test('throw rmdir errors', async t => {
const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
'../dist/cjs/src/fs.js': {
...fs,
rmdirSync: path => {
throw Object.assign(new Error('cannot rmdir'), { code: 'FOO' })
},
promises: {
...fs.promises,
rmdir: async path => {
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
'../dist/cjs/src/fs.js': {
...fs,
rmdirSync: path => {
throw Object.assign(new Error('cannot rmdir'), { code: 'FOO' })
},
promises: {
...fs.promises,
rmdir: async path => {
throw Object.assign(new Error('cannot rmdir'), { code: 'FOO' })
},
},
},
},
})
}
)
const path = t.testdir(fixture)
t.throws(() => rimrafPosixSync(path, {}), { code: 'FOO' })
t.rejects(rimrafPosix(path, {}), { code: 'FOO' })
})

t.test('throw unexpected readdir errors', async t => {
const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
'../dist/cjs/src/fs.js': {
...fs,
readdirSync: path => {
throw Object.assign(new Error('cannot readdir'), { code: 'FOO' })
},
promises: {
...fs.promises,
readdir: async path => {
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
'../dist/cjs/src/fs.js': {
...fs,
readdirSync: path => {
throw Object.assign(new Error('cannot readdir'), { code: 'FOO' })
},
promises: {
...fs.promises,
readdir: async path => {
throw Object.assign(new Error('cannot readdir'), { code: 'FOO' })
},
},
},
},
})
}
)
const path = t.testdir(fixture)
t.throws(() => rimrafPosixSync(path, {}), { code: 'FOO' })
t.rejects(rimrafPosix(path, {}), { code: 'FOO' })
})

t.test('ignore ENOENTs from unlink/rmdir', async t => {
const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
'../dist/cjs/src/fs.js': {
...fs,
// simulate a case where two rimrafs are happening in parallel,
// so the deletion happens AFTER the readdir, but before ours.
rmdirSync: path => {
fs.rmdirSync(path)
fs.rmdirSync(path)
},
unlinkSync: path => {
fs.unlinkSync(path)
fs.unlinkSync(path)
},
promises: {
...fs.promises,
rmdir: async path => {
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
'../dist/cjs/src/fs.js': {
...fs,
// simulate a case where two rimrafs are happening in parallel,
// so the deletion happens AFTER the readdir, but before ours.
rmdirSync: path => {
fs.rmdirSync(path)
fs.rmdirSync(path)
return fs.promises.rmdir(path)
},
unlink: async path => {
unlinkSync: path => {
fs.unlinkSync(path)
fs.unlinkSync(path)
return fs.promises.unlink(path)
},
promises: {
...fs.promises,
rmdir: async path => {
fs.rmdirSync(path)
return fs.promises.rmdir(path)
},
unlink: async path => {
fs.unlinkSync(path)
return fs.promises.unlink(path)
},
},
},
},
})
}
)
const { statSync } = fs
t.test('sync', t => {
const path = t.testdir(fixture)
Expand All @@ -161,18 +176,21 @@ t.test('ignore ENOENTs from unlink/rmdir', async t => {
t.test('rimraffing root, do not actually rmdir root', async t => {
let ROOT = null
const { parse } = require('path')
const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
path: {
...require('path'),
parse: path => {
const p = parse(path)
if (path === ROOT) {
p.root = path
}
return p
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
path: {
...require('path'),
parse: path => {
const p = parse(path)
if (path === ROOT) {
p.root = path
}
return p
},
},
},
})
}
)
t.test('async', async t => {
ROOT = t.testdir(fixture)
await rimrafPosix(ROOT, { preserveRoot: false })
Expand Down
9 changes: 6 additions & 3 deletions test/rimraf-windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ t.only('actually delete some stuff', async t => {
// but actually do wait to clean them up, though
t.teardown(() => Promise.all(danglers))

const { rimrafPosix, rimrafPosixSync } = t.mock('../dist/cjs/src/rimraf-posix.js', {
'../dist/cjs/src/fs.js': fsMock,
})
const { rimrafPosix, rimrafPosixSync } = t.mock(
'../dist/cjs/src/rimraf-posix.js',
{
'../dist/cjs/src/fs.js': fsMock,
}
)

const { rimrafWindows, rimrafWindowsSync } = t.mock(
'../dist/cjs/src/rimraf-windows.js',
Expand Down

0 comments on commit 49a2958

Please sign in to comment.