Skip to content

Commit 1af02a1

Browse files
committed
lib, test: fix lint and increase coverage for spawn EACCES handling
- Use ||= operator in lib/internal/child_process.js - Add descriptive comment to empty catch block in test - Add test cases for non-executable files, empty PATH entries, and missing PATH in envPairs to cover verifyENOENT logic. Fixes: https://github.com/nodejs/node/issues/XXXXX (if applicable)
1 parent 6b74734 commit 1af02a1

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

test/parallel/test-child-process-spawn-path-access.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,46 @@ try {
6060
} catch (err) {
6161
assert.strictEqual(err.code, 'ENOENT');
6262
}
63+
64+
// Case: File exists but is not executable. Should NOT be normalized to ENOENT.
65+
if (!common.isWindows) {
66+
const nonExecFile = path.join(tmpdir.path, 'non-executable');
67+
fs.writeFileSync(nonExecFile, 'echo "should not run"');
68+
fs.chmodSync(nonExecFile, '644');
69+
70+
const env2 = { ...process.env, PATH: tmpdir.path };
71+
const child2 = cp.spawn('non-executable', { env: env2 });
72+
child2.on('error', common.mustCall((err) => {
73+
// It should stay EACCES because the file actually exists
74+
assert.strictEqual(err.code, 'EACCES');
75+
}));
76+
77+
// Also test empty PATH entry
78+
const env3 = { ...process.env, PATH: `${path.delimiter}${env.PATH}` };
79+
const child3 = cp.spawn(command, { env: env3 });
80+
child3.on('error', common.mustCall((err) => {
81+
assert.strictEqual(err.code, 'ENOENT');
82+
}));
83+
}
84+
85+
// Case: No PATH in envPairs
86+
const env4 = { ...process.env };
87+
delete env4.PATH;
88+
const child4 = cp.spawn(command, { env: env4 });
89+
child4.on('error', common.mustCall((err) => {
90+
assert.strictEqual(err.code, 'ENOENT');
91+
}));
92+
93+
// Case: envPath ||= process.env.PATH (no env passed)
94+
if (!common.isWindows) {
95+
const oldPath = process.env.PATH;
96+
process.env.PATH = `${noPermDir}${path.delimiter}${oldPath}`;
97+
try {
98+
const child5 = cp.spawn(command);
99+
child5.on('error', common.mustCall((err) => {
100+
assert.strictEqual(err.code, 'ENOENT');
101+
}));
102+
} finally {
103+
process.env.PATH = oldPath;
104+
}
105+
}

0 commit comments

Comments
 (0)