Skip to content

Commit c012f4f

Browse files
lukekarrysisaacs
authored andcommitted
Mock process.platform with t.intercept
This removes the need for a separate platform.js file and test environ.
1 parent c72278a commit c012f4f

10 files changed

Lines changed: 134 additions & 238 deletions

File tree

src/default-tmp.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import { tmpdir } from 'os'
1212
import { parse, resolve } from 'path'
1313
import { promises, statSync } from './fs.js'
14-
import platform from './platform.js'
1514
const { stat } = promises
1615

1716
const isDirSync = (path: string) => {
1817
try {
1918
return statSync(path).isDirectory()
20-
} catch (er) {
19+
} catch {
2120
return false
2221
}
2322
}
@@ -60,10 +59,11 @@ const win32DefaultTmpSync = (path: string) => {
6059
return root
6160
}
6261

62+
// eslint-disable-next-line @typescript-eslint/require-await
6363
const posixDefaultTmp = async () => tmpdir()
6464
const posixDefaultTmpSync = () => tmpdir()
6565

6666
export const defaultTmp =
67-
platform === 'win32' ? win32DefaultTmp : posixDefaultTmp
67+
process.platform === 'win32' ? win32DefaultTmp : posixDefaultTmp
6868
export const defaultTmpSync =
69-
platform === 'win32' ? win32DefaultTmpSync : posixDefaultTmpSync
69+
process.platform === 'win32' ? win32DefaultTmpSync : posixDefaultTmpSync

src/path-arg.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { parse, resolve } from 'path'
22
import { inspect } from 'util'
33
import { RimrafAsyncOptions } from './index.js'
4-
import platform from './platform.js'
54

65
const pathArg = (path: string, opt: RimrafAsyncOptions = {}) => {
76
const type = typeof path
@@ -39,7 +38,7 @@ const pathArg = (path: string, opt: RimrafAsyncOptions = {}) => {
3938
})
4039
}
4140

42-
if (platform === 'win32') {
41+
if (process.platform === 'win32') {
4342
const badWinChars = /[*|"<>?:]/
4443
const { root } = parse(path)
4544
if (badWinChars.test(path.substring(root.length))) {

src/platform.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/rimraf-manual.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import platform from './platform.js'
2-
31
import { rimrafPosix, rimrafPosixSync } from './rimraf-posix.js'
42
import { rimrafWindows, rimrafWindowsSync } from './rimraf-windows.js'
53

6-
export const rimrafManual = platform === 'win32' ? rimrafWindows : rimrafPosix
4+
export const rimrafManual =
5+
process.platform === 'win32' ? rimrafWindows : rimrafPosix
76
export const rimrafManualSync =
8-
platform === 'win32' ? rimrafWindowsSync : rimrafPosixSync
7+
process.platform === 'win32' ? rimrafWindowsSync : rimrafPosixSync

src/use-native.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { RimrafAsyncOptions, RimrafOptions } from './index.js'
2-
import platform from './platform.js'
32

4-
const version = process.env.__TESTING_RIMRAF_NODE_VERSION__ || process.version
5-
const versArr = version.replace(/^v/, '').split('.')
6-
7-
/* c8 ignore start */
8-
const [major = 0, minor = 0] = versArr.map(v => parseInt(v, 10))
9-
/* c8 ignore stop */
3+
/* c8 ignore next */
4+
const [major = 0, minor = 0] = process.version
5+
.replace(/^v/, '')
6+
.split('.')
7+
.map(v => parseInt(v, 10))
108
const hasNative = major > 14 || (major === 14 && minor >= 14)
119

1210
// we do NOT use native by default on Windows, because Node's native
1311
// rm implementation is less advanced. Change this code if that changes.
1412
export const useNative: (opt?: RimrafAsyncOptions) => boolean =
15-
!hasNative || platform === 'win32' ?
13+
!hasNative || process.platform === 'win32' ?
1614
() => false
1715
: opt => !opt?.signal && !opt?.filter
1816
export const useNativeSync: (opt?: RimrafOptions) => boolean =
19-
!hasNative || platform === 'win32' ?
17+
!hasNative || process.platform === 'win32' ?
2018
() => false
2119
: opt => !opt?.signal && !opt?.filter

test/default-tmp.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { tmpdir } from 'os'
44
import { win32 } from 'path'
55

66
t.test('posix platform', async t => {
7+
t.intercept(process, 'platform', { value: 'posix' })
78
const { defaultTmp, defaultTmpSync } = (await t.mockImport(
8-
'../dist/esm/default-tmp.js',
9-
{
10-
'../dist/esm/platform.js': 'posix',
11-
},
12-
)) as typeof import('../dist/esm/default-tmp.js')
9+
'../src/default-tmp.js',
10+
)) as typeof import('../src/default-tmp.js')
1311
t.equal(defaultTmpSync('anything'), tmpdir())
1412
t.equal(await defaultTmp('anything').then(t => t), tmpdir())
1513
})
@@ -25,22 +23,22 @@ t.test('windows', async t => {
2523
throw Object.assign(new Error('no ents here'), { code: 'ENOENT' })
2624
}
2725
}
26+
t.intercept(process, 'platform', { value: 'win32' })
2827
const { defaultTmp, defaultTmpSync } = (await t.mockImport(
29-
'../dist/esm/default-tmp.js',
28+
'../src/default-tmp.js',
3029
{
3130
os: {
3231
tmpdir: () => 'C:\\Windows\\Temp',
3332
},
3433
path: win32,
35-
'../dist/esm/platform.js': 'win32',
36-
'../dist/esm/fs.js': {
34+
'../src/fs.js': {
3735
statSync: tempDirCheck,
3836
promises: {
3937
stat: async (path: string) => tempDirCheck(path),
4038
},
4139
},
4240
},
43-
)) as typeof import('../dist/esm/default-tmp.js')
41+
)) as typeof import('../src/default-tmp.js')
4442

4543
const expect = {
4644
'c:\\some\\path': 'C:\\Windows\\Temp',

test/path-arg.ts

Lines changed: 72 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,81 @@
11
import * as PATH from 'path'
22
import t from 'tap'
3-
import { fileURLToPath } from 'url'
43
import { inspect } from 'util'
54

6-
if (!process.env.__TESTING_RIMRAF_PLATFORM__) {
7-
const fake = process.platform === 'win32' ? 'posix' : 'win32'
8-
t.spawn(
9-
process.execPath,
10-
[...process.execArgv, fileURLToPath(import.meta.url)],
11-
{
12-
name: fake,
13-
env: {
14-
...process.env,
15-
__TESTING_RIMRAF_PLATFORM__: fake,
16-
},
17-
},
18-
)
19-
}
5+
for (const platform of ['win32', 'posix'] as const) {
6+
t.test(platform, async t => {
7+
t.intercept(process, 'platform', { value: platform })
8+
const path = PATH[platform] || PATH
9+
const { default: pathArg } = (await t.mockImport('../src/path-arg.js', {
10+
path,
11+
})) as typeof import('../src/path-arg.js')
2012

21-
const platform = process.env.__TESTING_RIMRAF_PLATFORM__ || process.platform
22-
const path = PATH[platform as 'win32' | 'posix'] || PATH
23-
const { default: pathArg } = (await t.mockImport('../dist/esm/path-arg.js', {
24-
path,
25-
})) as typeof import('../dist/esm/path-arg.js')
13+
t.equal(pathArg('a/b/c'), path.resolve('a/b/c'))
14+
t.throws(
15+
() => pathArg('a\0b'),
16+
Error('path must be a string without null bytes'),
17+
)
18+
if (platform === 'win32') {
19+
const badPaths = [
20+
'c:\\a\\b:c',
21+
'c:\\a\\b*c',
22+
'c:\\a\\b?c',
23+
'c:\\a\\b<c',
24+
'c:\\a\\b>c',
25+
'c:\\a\\b|c',
26+
'c:\\a\\b"c',
27+
]
28+
for (const path of badPaths) {
29+
const er = Object.assign(new Error('Illegal characters in path'), {
30+
path,
31+
code: 'EINVAL',
32+
})
33+
t.throws(() => pathArg(path), er)
34+
}
35+
}
2636

27-
const { resolve } = path
37+
t.throws(() => pathArg('/'), { code: 'ERR_PRESERVE_ROOT' })
2838

29-
t.equal(pathArg('a/b/c'), resolve('a/b/c'))
30-
t.throws(
31-
() => pathArg('a\0b'),
32-
Error('path must be a string without null bytes'),
33-
)
34-
if (platform === 'win32') {
35-
const badPaths = [
36-
'c:\\a\\b:c',
37-
'c:\\a\\b*c',
38-
'c:\\a\\b?c',
39-
'c:\\a\\b<c',
40-
'c:\\a\\b>c',
41-
'c:\\a\\b|c',
42-
'c:\\a\\b"c',
43-
]
44-
for (const path of badPaths) {
45-
const er = Object.assign(new Error('Illegal characters in path'), {
46-
path,
47-
code: 'EINVAL',
39+
t.throws(() => pathArg('/', { preserveRoot: undefined }), {
40+
code: 'ERR_PRESERVE_ROOT',
4841
})
49-
t.throws(() => pathArg(path), er)
50-
}
51-
}
52-
53-
t.throws(() => pathArg('/'), { code: 'ERR_PRESERVE_ROOT' })
54-
55-
t.throws(() => pathArg('/', { preserveRoot: undefined }), {
56-
code: 'ERR_PRESERVE_ROOT',
57-
})
58-
t.equal(pathArg('/', { preserveRoot: false }), resolve('/'))
42+
t.equal(pathArg('/', { preserveRoot: false }), path.resolve('/'))
5943

60-
//@ts-expect-error
61-
t.throws(() => pathArg({}), {
62-
code: 'ERR_INVALID_ARG_TYPE',
63-
path: {},
64-
message:
65-
'The "path" argument must be of type string. ' +
66-
'Received an instance of Object',
67-
name: 'TypeError',
68-
})
69-
//@ts-expect-error
70-
t.throws(() => pathArg([]), {
71-
code: 'ERR_INVALID_ARG_TYPE',
72-
path: [],
73-
message:
74-
'The "path" argument must be of type string. ' +
75-
'Received an instance of Array',
76-
name: 'TypeError',
77-
})
78-
//@ts-expect-error
79-
t.throws(() => pathArg(Object.create(null) as {}), {
80-
code: 'ERR_INVALID_ARG_TYPE',
81-
path: Object.create(null),
82-
message:
83-
'The "path" argument must be of type string. ' +
84-
`Received ${inspect(Object.create(null))}`,
85-
name: 'TypeError',
86-
})
87-
//@ts-expect-error
88-
t.throws(() => pathArg(true), {
89-
code: 'ERR_INVALID_ARG_TYPE',
90-
path: true,
91-
message:
92-
'The "path" argument must be of type string. ' +
93-
`Received type boolean true`,
94-
name: 'TypeError',
95-
})
44+
//@ts-expect-error
45+
t.throws(() => pathArg({}), {
46+
code: 'ERR_INVALID_ARG_TYPE',
47+
path: {},
48+
message:
49+
'The "path" argument must be of type string. ' +
50+
'Received an instance of Object',
51+
name: 'TypeError',
52+
})
53+
//@ts-expect-error
54+
t.throws(() => pathArg([]), {
55+
code: 'ERR_INVALID_ARG_TYPE',
56+
path: [],
57+
message:
58+
'The "path" argument must be of type string. ' +
59+
'Received an instance of Array',
60+
name: 'TypeError',
61+
})
62+
//@ts-expect-error
63+
t.throws(() => pathArg(Object.create(null) as object), {
64+
code: 'ERR_INVALID_ARG_TYPE',
65+
path: Object.create(null) as object,
66+
message:
67+
'The "path" argument must be of type string. ' +
68+
`Received ${inspect(Object.create(null))}`,
69+
name: 'TypeError',
70+
})
71+
//@ts-expect-error
72+
t.throws(() => pathArg(true), {
73+
code: 'ERR_INVALID_ARG_TYPE',
74+
path: true,
75+
message:
76+
'The "path" argument must be of type string. ' +
77+
`Received type boolean true`,
78+
name: 'TypeError',
79+
})
80+
})
81+
}

test/platform.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/rimraf-manual.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
import t from 'tap'
2-
import { fileURLToPath } from 'url'
3-
import { rimrafManual, rimrafManualSync } from '../dist/esm/rimraf-manual.js'
4-
import { rimrafPosix, rimrafPosixSync } from '../dist/esm/rimraf-posix.js'
5-
import { rimrafWindows, rimrafWindowsSync } from '../dist/esm/rimraf-windows.js'
2+
import { rimrafPosix, rimrafPosixSync } from '../src/rimraf-posix.js'
3+
import { rimrafWindows, rimrafWindowsSync } from '../src/rimraf-windows.js'
64

7-
if (!process.env.__TESTING_RIMRAF_PLATFORM__) {
8-
const otherPlatform = process.platform !== 'win32' ? 'win32' : 'posix'
9-
t.spawn(
10-
process.execPath,
11-
[...process.execArgv, fileURLToPath(import.meta.url)],
12-
{
13-
name: otherPlatform,
14-
env: {
15-
...process.env,
16-
__TESTING_RIMRAF_PLATFORM__: otherPlatform,
17-
},
18-
},
19-
)
20-
}
5+
for (const platform of ['win32', 'posix']) {
6+
t.test(platform, async t => {
7+
t.intercept(process, 'platform', { value: platform })
8+
const { rimrafManual, rimrafManualSync } = (await t.mockImport(
9+
'../src/rimraf-manual.js',
10+
)) as typeof import('../src/rimraf-manual.js')
2111

22-
const platform = process.env.__TESTING_RIMRAF_PLATFORM__ || process.platform
12+
const [expectManual, expectManualSync] =
13+
platform === 'win32' ?
14+
[rimrafWindows, rimrafWindowsSync]
15+
: [rimrafPosix, rimrafPosixSync]
2316

24-
const [expectManual, expectManualSync] =
25-
platform === 'win32' ?
26-
[rimrafWindows, rimrafWindowsSync]
27-
: [rimrafPosix, rimrafPosixSync]
28-
t.equal(rimrafManual, expectManual, 'got expected implementation')
29-
t.equal(rimrafManualSync, expectManualSync, 'got expected implementation')
17+
t.equal(rimrafManual, expectManual, 'got expected implementation')
18+
t.equal(rimrafManualSync, expectManualSync, 'got expected implementation')
19+
})
20+
}

0 commit comments

Comments
 (0)