Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor + chore cleanup #322

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
8 changes: 4 additions & 4 deletions src/default-tmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import { tmpdir } from 'os'
import { parse, resolve } from 'path'
import { promises, statSync } from './fs.js'
import platform from './platform.js'
const { stat } = promises

const isDirSync = (path: string) => {
try {
return statSync(path).isDirectory()
} catch (er) {
} catch {
return false
}
}
Expand Down Expand Up @@ -60,10 +59,11 @@ const win32DefaultTmpSync = (path: string) => {
return root
}

// eslint-disable-next-line @typescript-eslint/require-await
const posixDefaultTmp = async () => tmpdir()
const posixDefaultTmpSync = () => tmpdir()

export const defaultTmp =
platform === 'win32' ? win32DefaultTmp : posixDefaultTmp
process.platform === 'win32' ? win32DefaultTmp : posixDefaultTmp
export const defaultTmpSync =
platform === 'win32' ? win32DefaultTmpSync : posixDefaultTmpSync
process.platform === 'win32' ? win32DefaultTmpSync : posixDefaultTmpSync
3 changes: 1 addition & 2 deletions src/path-arg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parse, resolve } from 'path'
import { inspect } from 'util'
import { RimrafAsyncOptions } from './index.js'
import platform from './platform.js'

const pathArg = (path: string, opt: RimrafAsyncOptions = {}) => {
const type = typeof path
Expand Down Expand Up @@ -39,7 +38,7 @@ const pathArg = (path: string, opt: RimrafAsyncOptions = {}) => {
})
}

if (platform === 'win32') {
if (process.platform === 'win32') {
const badWinChars = /[*|"<>?:]/
const { root } = parse(path)
if (badWinChars.test(path.substring(root.length))) {
Expand Down
1 change: 0 additions & 1 deletion src/platform.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/rimraf-manual.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import platform from './platform.js'

import { rimrafPosix, rimrafPosixSync } from './rimraf-posix.js'
import { rimrafWindows, rimrafWindowsSync } from './rimraf-windows.js'

export const rimrafManual = platform === 'win32' ? rimrafWindows : rimrafPosix
export const rimrafManual =
process.platform === 'win32' ? rimrafWindows : rimrafPosix
export const rimrafManualSync =
platform === 'win32' ? rimrafWindowsSync : rimrafPosixSync
process.platform === 'win32' ? rimrafWindowsSync : rimrafPosixSync
16 changes: 7 additions & 9 deletions src/use-native.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { RimrafAsyncOptions, RimrafOptions } from './index.js'
import platform from './platform.js'

const version = process.env.__TESTING_RIMRAF_NODE_VERSION__ || process.version
const versArr = version.replace(/^v/, '').split('.')

/* c8 ignore start */
const [major = 0, minor = 0] = versArr.map(v => parseInt(v, 10))
/* c8 ignore stop */
/* c8 ignore next */
const [major = 0, minor = 0] = process.version
.replace(/^v/, '')
.split('.')
.map(v => parseInt(v, 10))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isaacs is it worth removing this version check is use-native now that v14.14.0 is no longer supported by engines?

const hasNative = major > 14 || (major === 14 && minor >= 14)

// we do NOT use native by default on Windows, because Node's native
// rm implementation is less advanced. Change this code if that changes.
export const useNative: (opt?: RimrafAsyncOptions) => boolean =
!hasNative || platform === 'win32' ?
!hasNative || process.platform === 'win32' ?
() => false
: opt => !opt?.signal && !opt?.filter
export const useNativeSync: (opt?: RimrafOptions) => boolean =
!hasNative || platform === 'win32' ?
!hasNative || process.platform === 'win32' ?
() => false
: opt => !opt?.signal && !opt?.filter
16 changes: 7 additions & 9 deletions test/default-tmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { tmpdir } from 'os'
import { win32 } from 'path'

t.test('posix platform', async t => {
t.intercept(process, 'platform', { value: 'posix' })
const { defaultTmp, defaultTmpSync } = (await t.mockImport(
'../dist/esm/default-tmp.js',
{
'../dist/esm/platform.js': 'posix',
},
)) as typeof import('../dist/esm/default-tmp.js')
'../src/default-tmp.js',
)) as typeof import('../src/default-tmp.js')
t.equal(defaultTmpSync('anything'), tmpdir())
t.equal(await defaultTmp('anything').then(t => t), tmpdir())
})
Expand All @@ -25,22 +23,22 @@ t.test('windows', async t => {
throw Object.assign(new Error('no ents here'), { code: 'ENOENT' })
}
}
t.intercept(process, 'platform', { value: 'win32' })
const { defaultTmp, defaultTmpSync } = (await t.mockImport(
'../dist/esm/default-tmp.js',
'../src/default-tmp.js',
{
os: {
tmpdir: () => 'C:\\Windows\\Temp',
},
path: win32,
'../dist/esm/platform.js': 'win32',
'../dist/esm/fs.js': {
'../src/fs.js': {
statSync: tempDirCheck,
promises: {
stat: async (path: string) => tempDirCheck(path),
},
},
},
)) as typeof import('../dist/esm/default-tmp.js')
)) as typeof import('../src/default-tmp.js')

const expect = {
'c:\\some\\path': 'C:\\Windows\\Temp',
Expand Down
158 changes: 72 additions & 86 deletions test/path-arg.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,81 @@
import * as PATH from 'path'
import t from 'tap'
import { fileURLToPath } from 'url'
import { inspect } from 'util'

if (!process.env.__TESTING_RIMRAF_PLATFORM__) {
const fake = process.platform === 'win32' ? 'posix' : 'win32'
t.spawn(
process.execPath,
[...process.execArgv, fileURLToPath(import.meta.url)],
{
name: fake,
env: {
...process.env,
__TESTING_RIMRAF_PLATFORM__: fake,
},
},
)
}
for (const platform of ['win32', 'posix'] as const) {
t.test(platform, async t => {
t.intercept(process, 'platform', { value: platform })
const path = PATH[platform] || PATH
const { default: pathArg } = (await t.mockImport('../src/path-arg.js', {
path,
})) as typeof import('../src/path-arg.js')

const platform = process.env.__TESTING_RIMRAF_PLATFORM__ || process.platform
const path = PATH[platform as 'win32' | 'posix'] || PATH
const { default: pathArg } = (await t.mockImport('../dist/esm/path-arg.js', {
path,
})) as typeof import('../dist/esm/path-arg.js')
t.equal(pathArg('a/b/c'), path.resolve('a/b/c'))
t.throws(
() => pathArg('a\0b'),
Error('path must be a string without null bytes'),
)
if (platform === 'win32') {
const badPaths = [
'c:\\a\\b:c',
'c:\\a\\b*c',
'c:\\a\\b?c',
'c:\\a\\b<c',
'c:\\a\\b>c',
'c:\\a\\b|c',
'c:\\a\\b"c',
]
for (const path of badPaths) {
const er = Object.assign(new Error('Illegal characters in path'), {
path,
code: 'EINVAL',
})
t.throws(() => pathArg(path), er)
}
}

const { resolve } = path
t.throws(() => pathArg('/'), { code: 'ERR_PRESERVE_ROOT' })

t.equal(pathArg('a/b/c'), resolve('a/b/c'))
t.throws(
() => pathArg('a\0b'),
Error('path must be a string without null bytes'),
)
if (platform === 'win32') {
const badPaths = [
'c:\\a\\b:c',
'c:\\a\\b*c',
'c:\\a\\b?c',
'c:\\a\\b<c',
'c:\\a\\b>c',
'c:\\a\\b|c',
'c:\\a\\b"c',
]
for (const path of badPaths) {
const er = Object.assign(new Error('Illegal characters in path'), {
path,
code: 'EINVAL',
t.throws(() => pathArg('/', { preserveRoot: undefined }), {
code: 'ERR_PRESERVE_ROOT',
})
t.throws(() => pathArg(path), er)
}
}

t.throws(() => pathArg('/'), { code: 'ERR_PRESERVE_ROOT' })

t.throws(() => pathArg('/', { preserveRoot: undefined }), {
code: 'ERR_PRESERVE_ROOT',
})
t.equal(pathArg('/', { preserveRoot: false }), resolve('/'))
t.equal(pathArg('/', { preserveRoot: false }), path.resolve('/'))

//@ts-expect-error
t.throws(() => pathArg({}), {
code: 'ERR_INVALID_ARG_TYPE',
path: {},
message:
'The "path" argument must be of type string. ' +
'Received an instance of Object',
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg([]), {
code: 'ERR_INVALID_ARG_TYPE',
path: [],
message:
'The "path" argument must be of type string. ' +
'Received an instance of Array',
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg(Object.create(null) as {}), {
code: 'ERR_INVALID_ARG_TYPE',
path: Object.create(null),
message:
'The "path" argument must be of type string. ' +
`Received ${inspect(Object.create(null))}`,
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg(true), {
code: 'ERR_INVALID_ARG_TYPE',
path: true,
message:
'The "path" argument must be of type string. ' +
`Received type boolean true`,
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg({}), {
code: 'ERR_INVALID_ARG_TYPE',
path: {},
message:
'The "path" argument must be of type string. ' +
'Received an instance of Object',
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg([]), {
code: 'ERR_INVALID_ARG_TYPE',
path: [],
message:
'The "path" argument must be of type string. ' +
'Received an instance of Array',
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg(Object.create(null) as object), {
code: 'ERR_INVALID_ARG_TYPE',
path: Object.create(null) as object,
message:
'The "path" argument must be of type string. ' +
`Received ${inspect(Object.create(null))}`,
name: 'TypeError',
})
//@ts-expect-error
t.throws(() => pathArg(true), {
code: 'ERR_INVALID_ARG_TYPE',
path: true,
message:
'The "path" argument must be of type string. ' +
`Received type boolean true`,
name: 'TypeError',
})
})
}
14 changes: 0 additions & 14 deletions test/platform.ts

This file was deleted.

41 changes: 16 additions & 25 deletions test/rimraf-manual.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import t from 'tap'
import { fileURLToPath } from 'url'
import { rimrafManual, rimrafManualSync } from '../dist/esm/rimraf-manual.js'
import { rimrafPosix, rimrafPosixSync } from '../dist/esm/rimraf-posix.js'
import { rimrafWindows, rimrafWindowsSync } from '../dist/esm/rimraf-windows.js'
import { rimrafPosix, rimrafPosixSync } from '../src/rimraf-posix.js'
import { rimrafWindows, rimrafWindowsSync } from '../src/rimraf-windows.js'

if (!process.env.__TESTING_RIMRAF_PLATFORM__) {
const otherPlatform = process.platform !== 'win32' ? 'win32' : 'posix'
t.spawn(
process.execPath,
[...process.execArgv, fileURLToPath(import.meta.url)],
{
name: otherPlatform,
env: {
...process.env,
__TESTING_RIMRAF_PLATFORM__: otherPlatform,
},
},
)
}
for (const platform of ['win32', 'posix']) {
t.test(platform, async t => {
t.intercept(process, 'platform', { value: platform })
const { rimrafManual, rimrafManualSync } = (await t.mockImport(
'../src/rimraf-manual.js',
)) as typeof import('../src/rimraf-manual.js')

const platform = process.env.__TESTING_RIMRAF_PLATFORM__ || process.platform
const [expectManual, expectManualSync] =
platform === 'win32' ?
[rimrafWindows, rimrafWindowsSync]
: [rimrafPosix, rimrafPosixSync]

const [expectManual, expectManualSync] =
platform === 'win32' ?
[rimrafWindows, rimrafWindowsSync]
: [rimrafPosix, rimrafPosixSync]
t.equal(rimrafManual, expectManual, 'got expected implementation')
t.equal(rimrafManualSync, expectManualSync, 'got expected implementation')
t.equal(rimrafManual, expectManual, 'got expected implementation')
t.equal(rimrafManualSync, expectManualSync, 'got expected implementation')
})
}
Loading