-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
squash: strict validation, test for bad input
- Loading branch information
1 parent
79ae91a
commit d7aaae1
Showing
2 changed files
with
81 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,97 @@ | ||
import { isWindows, mustCall, mustSucceed, mustNotCall } from '../common/index.mjs'; | ||
import { strictEqual } from 'node:assert'; | ||
import { pathToFileURL } from 'node:url'; | ||
import { strictEqual, throws } from 'node:assert'; | ||
import cp from 'node:child_process'; | ||
import url from 'node:url'; | ||
import tmpdir from '../common/tmpdir.js'; | ||
|
||
tmpdir.refresh(); | ||
|
||
const cwd = tmpdir.path; | ||
const cwdUrl = url.pathToFileURL(cwd); | ||
const whichCommand = isWindows ? 'where.exe cmd.exe' : 'which pwd'; | ||
const pwdFullPath = `${cp.execSync(whichCommand)}`.trim(); | ||
const pwdUrl = pathToFileURL(pwdFullPath); | ||
const pwdCommandAndOptions = isWindows ? | ||
[pwdUrl, ['/d', '/c', 'cd'], { cwd: pathToFileURL(cwd) }] : | ||
[pwdUrl, [], { cwd: pathToFileURL(cwd) }]; | ||
const pwdWHATWGUrl = url.pathToFileURL(pwdFullPath); | ||
|
||
{ | ||
cp.execFile(...pwdCommandAndOptions, mustSucceed((stdout) => { | ||
// Test for WHATWG URL instance, legacy URL, and URL-like object | ||
for (const pwdUrl of [ | ||
pwdWHATWGUrl, | ||
new url.URL(pwdWHATWGUrl), | ||
{ | ||
href: pwdWHATWGUrl.href, | ||
origin: pwdWHATWGUrl.origin, | ||
protocol: pwdWHATWGUrl.protocol, | ||
username: pwdWHATWGUrl.username, | ||
password: pwdWHATWGUrl.password, | ||
host: pwdWHATWGUrl.host, | ||
hostname: pwdWHATWGUrl.hostname, | ||
port: pwdWHATWGUrl.port, | ||
pathname: pwdWHATWGUrl.pathname, | ||
search: pwdWHATWGUrl.search, | ||
searchParams: new URLSearchParams(pwdWHATWGUrl.searchParams), | ||
hash: pwdWHATWGUrl.hash, | ||
}, | ||
]) { | ||
const pwdCommandAndOptions = isWindows ? | ||
[pwdUrl, ['/d', '/c', 'cd'], { cwd: cwdUrl }] : | ||
[pwdUrl, [], { cwd: cwdUrl }]; | ||
|
||
{ | ||
cp.execFile(...pwdCommandAndOptions, mustSucceed((stdout) => { | ||
strictEqual(`${stdout}`.trim(), cwd); | ||
})); | ||
} | ||
|
||
{ | ||
const stdout = cp.execFileSync(...pwdCommandAndOptions); | ||
strictEqual(`${stdout}`.trim(), cwd); | ||
})); | ||
} | ||
} | ||
|
||
{ | ||
const stdout = cp.execFileSync(...pwdCommandAndOptions); | ||
strictEqual(`${stdout}`.trim(), cwd); | ||
} | ||
{ | ||
const pwd = cp.spawn(...pwdCommandAndOptions); | ||
pwd.stdout.on('data', mustCall((stdout) => { | ||
strictEqual(`${stdout}`.trim(), cwd); | ||
})); | ||
pwd.stderr.on('data', mustNotCall()); | ||
pwd.on('close', mustCall((code) => { | ||
strictEqual(code, 0); | ||
})); | ||
} | ||
|
||
{ | ||
const pwd = cp.spawn(...pwdCommandAndOptions); | ||
pwd.stdout.on('data', mustCall((stdout) => { | ||
{ | ||
const stdout = cp.spawnSync(...pwdCommandAndOptions).stdout; | ||
strictEqual(`${stdout}`.trim(), cwd); | ||
})); | ||
pwd.stderr.on('data', mustNotCall()); | ||
pwd.on('close', mustCall((code) => { | ||
strictEqual(code, 0); | ||
})); | ||
} | ||
|
||
} | ||
|
||
{ | ||
const stdout = cp.spawnSync(...pwdCommandAndOptions).stdout; | ||
strictEqual(`${stdout}`.trim(), cwd); | ||
// Test for non-URL objects | ||
for (const badFile of [ | ||
Buffer.from(pwdFullPath), | ||
{}, | ||
{ href: pwdWHATWGUrl.href }, | ||
{ pathname: pwdWHATWGUrl.pathname }, | ||
]) { | ||
const pwdCommandAndOptions = isWindows ? | ||
[badFile, ['/d', '/c', 'cd'], { cwd: cwdUrl }] : | ||
[badFile, [], { cwd: cwdUrl }]; | ||
|
||
throws( | ||
() => cp.execFile(...pwdCommandAndOptions, mustNotCall()), | ||
{ code: 'ERR_INVALID_ARG_TYPE' }, | ||
); | ||
|
||
throws( | ||
() => cp.execFileSync(...pwdCommandAndOptions), | ||
{ code: 'ERR_INVALID_ARG_TYPE' }, | ||
); | ||
|
||
throws( | ||
() => cp.spawn(...pwdCommandAndOptions), | ||
{ code: 'ERR_INVALID_ARG_TYPE' }, | ||
); | ||
|
||
throws( | ||
() => cp.spawnSync(...pwdCommandAndOptions), | ||
{ code: 'ERR_INVALID_ARG_TYPE' }, | ||
); | ||
} |