From 42167f10dd794638e1ef98d70f115af6520d4026 Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Tue, 20 Aug 2024 10:14:51 +0800 Subject: [PATCH 01/15] lib: fix realpathSync resolving to invalid path (#54200) --- src/node.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 50d1d17942194a..1250f57a6751d3 100644 --- a/src/node.cc +++ b/src/node.cc @@ -383,7 +383,7 @@ MaybeLocal StartExecution(Environment* env, StartExecutionCallback cb) { return StartExecution(env, "internal/main/watch_mode"); } - if (!first_argv.empty() && first_argv != "-") { + if (!first_argv.empty() && first_argv != "-" && first_argv != "/dev/stdin") { return StartExecution(env, "internal/main/run_main_module"); } From 7d6746f80285472f911d005290e77db27ba49b08 Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Tue, 20 Aug 2024 13:51:30 +0800 Subject: [PATCH 02/15] add test --- test/parallel/test-linux-dev-stdin.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/parallel/test-linux-dev-stdin.js diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js new file mode 100644 index 00000000000000..7c3300acd2c4ec --- /dev/null +++ b/test/parallel/test-linux-dev-stdin.js @@ -0,0 +1,12 @@ +'use strict'; +const common = require('../common'); + +if (!common.isLinux) { + common.skip('Linux only test'); +} + +const child_process = require("child_process") +const assert = require('assert'); + +const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, {stdio: 'pipe'}) +assert.strictEqual(cp.toString(), '1\n'); \ No newline at end of file From 03152d10bc531719682a1db237a9ebe4dca7ba22 Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Tue, 20 Aug 2024 13:54:24 +0800 Subject: [PATCH 03/15] comment. --- test/parallel/test-linux-dev-stdin.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index 7c3300acd2c4ec..4b9b950c169751 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -1,6 +1,11 @@ 'use strict'; const common = require('../common'); +// +// This test ensures Node.js doesn't crash on linux when reading from /dev/stdin in the CLI +// ref: https://github.com/nodejs/node/issues/54200 +// + if (!common.isLinux) { common.skip('Linux only test'); } @@ -9,4 +14,4 @@ const child_process = require("child_process") const assert = require('assert'); const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, {stdio: 'pipe'}) -assert.strictEqual(cp.toString(), '1\n'); \ No newline at end of file +assert.strictEqual(cp.toString(), '1\n'); From bba8b9a6777d63fe90cc341115ac0c1b64d97454 Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Tue, 20 Aug 2024 14:01:31 +0800 Subject: [PATCH 04/15] run NODE=$(command -v node) make lint-js-fix --- test/parallel/test-linux-dev-stdin.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index 4b9b950c169751..1dfdbf88e3ed16 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -7,11 +7,11 @@ const common = require('../common'); // if (!common.isLinux) { - common.skip('Linux only test'); + common.skip('Linux only test'); } -const child_process = require("child_process") +const child_process = require('child_process'); const assert = require('assert'); -const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, {stdio: 'pipe'}) +const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); assert.strictEqual(cp.toString(), '1\n'); From cd869f7be730385420b978b5d222128681f7b60d Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Wed, 21 Aug 2024 09:03:54 +0800 Subject: [PATCH 05/15] Revert "cli: fix can't execute /dev/stdin on linux (#54200)" This reverts commit b998fded5febb0632fb9eac48d352e6d660679e1. --- src/node.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 1250f57a6751d3..50d1d17942194a 100644 --- a/src/node.cc +++ b/src/node.cc @@ -383,7 +383,7 @@ MaybeLocal StartExecution(Environment* env, StartExecutionCallback cb) { return StartExecution(env, "internal/main/watch_mode"); } - if (!first_argv.empty() && first_argv != "-" && first_argv != "/dev/stdin") { + if (!first_argv.empty() && first_argv != "-") { return StartExecution(env, "internal/main/run_main_module"); } From b26006128468c956e6d974ceb30242a2c177aced Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Wed, 21 Aug 2024 11:40:53 +0800 Subject: [PATCH 06/15] lib: fix realpathSync resolving to invalid path --- lib/fs.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/fs.js b/lib/fs.js index 53e4d5b829b5f1..ed84a10a6f744c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2731,6 +2731,14 @@ function realpathSync(p, options) { } resolvedLink = pathModule.resolve(previous, linkTarget); + // If resolvedLink is not valid or is a pipe/socket, stop resolving, break out of the loop + // issue: https://github.com/nodejs/node/issues/54200 + const resolvedLinkStats = binding.lstat(resolvedLink, false, undefined, false /* throwIfNoEntry */); + if (!resolvedLinkStats || isFileType(resolvedLinkStats, S_IFIFO) || + isFileType(resolvedLinkStats, S_IFSOCK)) { + break; + } + cache?.set(base, resolvedLink); if (!isWindows) seenLinks.set(id, linkTarget); } From e793947fe92987eeba843c8c748509ebaa25edbb Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Wed, 21 Aug 2024 12:37:44 +0800 Subject: [PATCH 07/15] test symlink to /dev/stdin --- test/parallel/test-linux-dev-stdin.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index 1dfdbf88e3ed16..3a92a96694e9d8 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -12,6 +12,15 @@ if (!common.isLinux) { const child_process = require('child_process'); const assert = require('assert'); +const fs = require('fs'); const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); assert.strictEqual(cp.toString(), '1\n'); + +if (fs.existsSync('/tmp/stdin')) { + fs.unlinkSync('/tmp/stdin'); +} +fs.symlinkSync('/dev/stdin', '/tmp/stdin'); +const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" /tmp/stdin`, { stdio: 'pipe' }); +assert.strictEqual(cp2.toString(), '2\n'); +fs.unlinkSync('/tmp/stdin'); From ba1a3b35be8ecc352eaaddeee6cb1547443139bf Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Thu, 22 Aug 2024 08:49:30 +0800 Subject: [PATCH 08/15] apply reviews from RedYetiDev --- test/parallel/test-linux-dev-stdin.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index 3a92a96694e9d8..d6e93494575290 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -1,26 +1,25 @@ 'use strict'; const common = require('../common'); +const tmpdir = require('../common/tmpdir'); +const fs = require('fs'); // -// This test ensures Node.js doesn't crash on linux when reading from /dev/stdin in the CLI +// This test ensures Node.js doesn't crash when reading from /dev/stdin as an input. // ref: https://github.com/nodejs/node/issues/54200 // -if (!common.isLinux) { - common.skip('Linux only test'); +if (!fs.existsSync('/dev/stdin')) { + common.skip('Only test on platforms having /dev/stdin'); } const child_process = require('child_process'); const assert = require('assert'); -const fs = require('fs'); const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); assert.strictEqual(cp.toString(), '1\n'); -if (fs.existsSync('/tmp/stdin')) { - fs.unlinkSync('/tmp/stdin'); -} -fs.symlinkSync('/dev/stdin', '/tmp/stdin'); -const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" /tmp/stdin`, { stdio: 'pipe' }); +tmpdir.refresh(); +const tmp = tmpdir.resolve('./stdin'); +fs.symlinkSync('/dev/stdin', tmp); +const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" ${tmp}`, { stdio: 'pipe' }); assert.strictEqual(cp2.toString(), '2\n'); -fs.unlinkSync('/tmp/stdin'); From 97187692a34f0062d7ee53f35644a7a2bef973bd Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Thu, 22 Aug 2024 08:52:31 +0800 Subject: [PATCH 09/15] Test reading sourcecode from a symlink to the `readlink /dev/stdin` --- test/parallel/test-linux-dev-stdin.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index d6e93494575290..a2e14d290defb8 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -15,11 +15,17 @@ if (!fs.existsSync('/dev/stdin')) { const child_process = require('child_process'); const assert = require('assert'); +// Test reading sourcecode from /dev/stdin const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); assert.strictEqual(cp.toString(), '1\n'); +// Test reading sourcecode from a symlink to /dev/stdin tmpdir.refresh(); const tmp = tmpdir.resolve('./stdin'); fs.symlinkSync('/dev/stdin', tmp); const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" ${tmp}`, { stdio: 'pipe' }); assert.strictEqual(cp2.toString(), '2\n'); + +// Test reading sourcecode from a symlink to the `readlink /dev/stdin` +const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" $(readlink /dev/stdin)`, { stdio: 'pipe' }); +assert.strictEqual(cp3.toString(), '3\n'); From de425014923c7034e6d287f28fd6f15509099e5d Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Thu, 22 Aug 2024 08:59:38 +0800 Subject: [PATCH 10/15] use node:test to split up tests --- test/parallel/test-linux-dev-stdin.js | 30 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index a2e14d290defb8..142785db78db74 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -14,18 +14,24 @@ if (!fs.existsSync('/dev/stdin')) { const child_process = require('child_process'); const assert = require('assert'); +const { describe, it } = require('node:test'); -// Test reading sourcecode from /dev/stdin -const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); -assert.strictEqual(cp.toString(), '1\n'); +describe('Test reading SourceCode from stdin and it\'s symlinks', () => { + it('Test reading sourcecode from /dev/stdin', () => { + const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); + assert.strictEqual(cp.toString(), '1\n'); + }); -// Test reading sourcecode from a symlink to /dev/stdin -tmpdir.refresh(); -const tmp = tmpdir.resolve('./stdin'); -fs.symlinkSync('/dev/stdin', tmp); -const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" ${tmp}`, { stdio: 'pipe' }); -assert.strictEqual(cp2.toString(), '2\n'); + it('Test reading sourcecode from a symlink to /dev/stdin', () => { + tmpdir.refresh(); + const tmp = tmpdir.resolve('./stdin'); + fs.symlinkSync('/dev/stdin', tmp); + const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" ${tmp}`, { stdio: 'pipe' }); + assert.strictEqual(cp2.toString(), '2\n'); + }); -// Test reading sourcecode from a symlink to the `readlink /dev/stdin` -const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" $(readlink /dev/stdin)`, { stdio: 'pipe' }); -assert.strictEqual(cp3.toString(), '3\n'); + it('Test reading sourcecode from a symlink to the `readlink /dev/stdin`', () => { + const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" $(readlink /dev/stdin)`, { stdio: 'pipe' }); + assert.strictEqual(cp3.toString(), '3\n'); + }); +}); From dc106fb0e49befb0da8fccf94a5ee678da600e3e Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Thu, 22 Aug 2024 09:02:09 +0800 Subject: [PATCH 11/15] use fs.readlinkSync instead of system readlink utility --- repros/52737.mjs | 13 +++++++++++++ repros/53225.mjs | 16 ++++++++++++++++ repros/test.js | 7 +++++++ stdin | 1 + test/parallel/test-linux-dev-stdin.js | 3 ++- 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 repros/52737.mjs create mode 100644 repros/53225.mjs create mode 100644 repros/test.js create mode 120000 stdin diff --git a/repros/52737.mjs b/repros/52737.mjs new file mode 100644 index 00000000000000..825943346b7178 --- /dev/null +++ b/repros/52737.mjs @@ -0,0 +1,13 @@ +import { createContext, runInContext } from "vm"; +runInContext( + ` + "use strict"; + Reflect.defineProperty(globalThis, "FOO", { + get: () => { + throw new Error("This should not get triggered"); + }, + }); + Reflect.has(globalThis, "FOO"); + `, + createContext(), +); \ No newline at end of file diff --git a/repros/53225.mjs b/repros/53225.mjs new file mode 100644 index 00000000000000..43bd7d37633f1f --- /dev/null +++ b/repros/53225.mjs @@ -0,0 +1,16 @@ +import {serialize, deserialize} from 'node:v8'; + +const error = AbortSignal.abort().reason; +console.log(error instanceof DOMException); +// true +console.log(error); +// DOMException [AbortError]: This operation was aborted +// at new DOMException (node:internal/per_context/domexception:53:5) +// at AbortSignal.abort (node:internal/abort_controller:205:14) +// at ... + +const clonedError = deserialize(serialize(error)); +console.log(clonedError instanceof DOMException); +// false +console.log(clonedError); +// {} \ No newline at end of file diff --git a/repros/test.js b/repros/test.js new file mode 100644 index 00000000000000..16009f62903b38 --- /dev/null +++ b/repros/test.js @@ -0,0 +1,7 @@ +const fs =require('fs'); + +const p = fs.realpathSync("/dev/stdin"); + +console.log(fs.statSync(p)); +console.log(fs.statSync("/dev/stdin")); +console.log(p) \ No newline at end of file diff --git a/stdin b/stdin new file mode 120000 index 00000000000000..2b90982885aea6 --- /dev/null +++ b/stdin @@ -0,0 +1 @@ +/dev/stdin \ No newline at end of file diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index 142785db78db74..acc914e7c76f32 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -31,7 +31,8 @@ describe('Test reading SourceCode from stdin and it\'s symlinks', () => { }); it('Test reading sourcecode from a symlink to the `readlink /dev/stdin`', () => { - const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" $(readlink /dev/stdin)`, { stdio: 'pipe' }); + const devStdin = fs.readlinkSync('/dev/stdin'); + const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" "${devStdin}"`, { stdio: 'pipe' }); assert.strictEqual(cp3.toString(), '3\n'); }); }); From 0d10bd3a7136a8b57afd630353ca4060cd009cae Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Thu, 22 Aug 2024 09:13:30 +0800 Subject: [PATCH 12/15] remove unwanted contents --- repros/52737.mjs | 13 ------------- repros/53225.mjs | 16 ---------------- repros/test.js | 7 ------- stdin | 1 - 4 files changed, 37 deletions(-) delete mode 100644 repros/52737.mjs delete mode 100644 repros/53225.mjs delete mode 100644 repros/test.js delete mode 120000 stdin diff --git a/repros/52737.mjs b/repros/52737.mjs deleted file mode 100644 index 825943346b7178..00000000000000 --- a/repros/52737.mjs +++ /dev/null @@ -1,13 +0,0 @@ -import { createContext, runInContext } from "vm"; -runInContext( - ` - "use strict"; - Reflect.defineProperty(globalThis, "FOO", { - get: () => { - throw new Error("This should not get triggered"); - }, - }); - Reflect.has(globalThis, "FOO"); - `, - createContext(), -); \ No newline at end of file diff --git a/repros/53225.mjs b/repros/53225.mjs deleted file mode 100644 index 43bd7d37633f1f..00000000000000 --- a/repros/53225.mjs +++ /dev/null @@ -1,16 +0,0 @@ -import {serialize, deserialize} from 'node:v8'; - -const error = AbortSignal.abort().reason; -console.log(error instanceof DOMException); -// true -console.log(error); -// DOMException [AbortError]: This operation was aborted -// at new DOMException (node:internal/per_context/domexception:53:5) -// at AbortSignal.abort (node:internal/abort_controller:205:14) -// at ... - -const clonedError = deserialize(serialize(error)); -console.log(clonedError instanceof DOMException); -// false -console.log(clonedError); -// {} \ No newline at end of file diff --git a/repros/test.js b/repros/test.js deleted file mode 100644 index 16009f62903b38..00000000000000 --- a/repros/test.js +++ /dev/null @@ -1,7 +0,0 @@ -const fs =require('fs'); - -const p = fs.realpathSync("/dev/stdin"); - -console.log(fs.statSync(p)); -console.log(fs.statSync("/dev/stdin")); -console.log(p) \ No newline at end of file diff --git a/stdin b/stdin deleted file mode 120000 index 2b90982885aea6..00000000000000 --- a/stdin +++ /dev/null @@ -1 +0,0 @@ -/dev/stdin \ No newline at end of file From ebcdb79945fa856ebce6324b6600d432ebd4a386 Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Fri, 23 Aug 2024 08:45:54 +0800 Subject: [PATCH 13/15] fix nitpicks --- lib/fs.js | 4 ++-- test/parallel/test-linux-dev-stdin.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index ed84a10a6f744c..932d665a005e5e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2731,8 +2731,8 @@ function realpathSync(p, options) { } resolvedLink = pathModule.resolve(previous, linkTarget); - // If resolvedLink is not valid or is a pipe/socket, stop resolving, break out of the loop - // issue: https://github.com/nodejs/node/issues/54200 + // If resolvedLink is not valid, or is a pipe/socket, stop resolving and break out of the loop + // Ref: https://github.com/nodejs/node/issues/54200 const resolvedLinkStats = binding.lstat(resolvedLink, false, undefined, false /* throwIfNoEntry */); if (!resolvedLinkStats || isFileType(resolvedLinkStats, S_IFIFO) || isFileType(resolvedLinkStats, S_IFSOCK)) { diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index acc914e7c76f32..a1c0f173ce5f6c 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -5,7 +5,7 @@ const fs = require('fs'); // // This test ensures Node.js doesn't crash when reading from /dev/stdin as an input. -// ref: https://github.com/nodejs/node/issues/54200 +// Ref: https://github.com/nodejs/node/issues/54200 // if (!fs.existsSync('/dev/stdin')) { From 041e418afed37b840ec0c5ba8df8160a2368d52e Mon Sep 17 00:00:00 2001 From: CGQAQ Date: Fri, 23 Aug 2024 11:37:27 +0800 Subject: [PATCH 14/15] try to fix macos readlink wrong result --- test/parallel/test-linux-dev-stdin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index a1c0f173ce5f6c..1b6503e9c0f5f5 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -30,7 +30,7 @@ describe('Test reading SourceCode from stdin and it\'s symlinks', () => { assert.strictEqual(cp2.toString(), '2\n'); }); - it('Test reading sourcecode from a symlink to the `readlink /dev/stdin`', () => { + it('Test reading sourcecode from a symlink to the `readlink -f /dev/stdin`', () => { const devStdin = fs.readlinkSync('/dev/stdin'); const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" "${devStdin}"`, { stdio: 'pipe' }); assert.strictEqual(cp3.toString(), '3\n'); From d6063ae866553f13e3d5a5e8c1e509cda1dc87e4 Mon Sep 17 00:00:00 2001 From: cgqaq Date: Sat, 24 Aug 2024 16:34:31 +0800 Subject: [PATCH 15/15] fix macOS test --- test/parallel/test-linux-dev-stdin.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-linux-dev-stdin.js b/test/parallel/test-linux-dev-stdin.js index 1b6503e9c0f5f5..578360aee9fa22 100644 --- a/test/parallel/test-linux-dev-stdin.js +++ b/test/parallel/test-linux-dev-stdin.js @@ -15,10 +15,14 @@ if (!fs.existsSync('/dev/stdin')) { const child_process = require('child_process'); const assert = require('assert'); const { describe, it } = require('node:test'); +const { join } = require('path'); -describe('Test reading SourceCode from stdin and it\'s symlinks', () => { +describe("Test reading SourceCode from stdin and it's symlinks", () => { it('Test reading sourcecode from /dev/stdin', () => { - const cp = child_process.execSync(`printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, { stdio: 'pipe' }); + const cp = child_process.execSync( + `printf 'console.log(1)' | "${process.execPath}" /dev/stdin`, + { stdio: 'pipe' } + ); assert.strictEqual(cp.toString(), '1\n'); }); @@ -26,13 +30,25 @@ describe('Test reading SourceCode from stdin and it\'s symlinks', () => { tmpdir.refresh(); const tmp = tmpdir.resolve('./stdin'); fs.symlinkSync('/dev/stdin', tmp); - const cp2 = child_process.execSync(`printf 'console.log(2)' | "${process.execPath}" ${tmp}`, { stdio: 'pipe' }); + const cp2 = child_process.execSync( + `printf 'console.log(2)' | "${process.execPath}" ${tmp}`, + { stdio: 'pipe' } + ); assert.strictEqual(cp2.toString(), '2\n'); }); it('Test reading sourcecode from a symlink to the `readlink -f /dev/stdin`', () => { const devStdin = fs.readlinkSync('/dev/stdin'); - const cp3 = child_process.execSync(`printf 'console.log(3)' | "${process.execPath}" "${devStdin}"`, { stdio: 'pipe' }); + let devStdinRealPath = devStdin; + if (common.isMacOS) { + // macOS `readlink` gives back the relative path, so we need to convert it to absolute path. + devStdinRealPath = join('/dev', devStdin); + } + + const cp3 = child_process.execSync( + `printf 'console.log(3)' | "${process.execPath}" "${devStdinRealPath}"`, + { stdio: 'pipe' } + ); assert.strictEqual(cp3.toString(), '3\n'); }); });