From 5cbb45b1167a3f54ee029ade8d9eed64f02858a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 5 Sep 2021 12:57:30 +0200 Subject: [PATCH 1/2] test: make tests pass on Windows with Unix EOL It is possible on Windows to configure Git to checkout line-endings "as-is", which for Node.js means Unix-style. This change makes the tests that rely on line endings pass in that case. --- test/common/index.js | 8 +++++++- test/es-module/test-esm-invalid-pjson.js | 4 ++-- test/parallel/test-source-map-enable.js | 2 +- test/wasi/test-wasi.js | 8 ++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/test/common/index.js b/test/common/index.js index 09cedea1dc88d1..2210c18a84dd62 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -885,8 +885,14 @@ const common = { throw new Error('common.PORT cannot be used in a parallelized test'); } return +process.env.NODE_COMMON_PORT || 12346; - } + }, + /** + * Returns the EOL character used by this Git checkout. + */ + get checkoutEOL() { + return fs.readFileSync(__filename).includes('\r\n') ? '\r\n' : '\n'; + }, }; const validProperties = new Set(Object.keys(common)); diff --git a/test/es-module/test-esm-invalid-pjson.js b/test/es-module/test-esm-invalid-pjson.js index 9f4711321230bf..7cd3e38b0b6555 100644 --- a/test/es-module/test-esm-invalid-pjson.js +++ b/test/es-module/test-esm-invalid-pjson.js @@ -1,6 +1,6 @@ 'use strict'; -const { mustCall, isWindows } = require('../common'); +const { mustCall, checkoutEOL } = require('../common'); const fixtures = require('../common/fixtures'); const { spawn } = require('child_process'); const { strictEqual, ok } = require('assert'); @@ -21,7 +21,7 @@ child.on('close', mustCall((code, signal) => { stderr.includes( `[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config ${invalidJson} ` + `while importing "invalid-pjson" from ${entry}. ` + - `Unexpected token } in JSON at position ${isWindows ? 16 : 14}` + `Unexpected token } in JSON at position ${checkoutEOL === '\r\n' ? 16 : 14}` ), stderr); })); diff --git a/test/parallel/test-source-map-enable.js b/test/parallel/test-source-map-enable.js index eab718813fc379..8e4f8e3a028062 100644 --- a/test/parallel/test-source-map-enable.js +++ b/test/parallel/test-source-map-enable.js @@ -250,7 +250,7 @@ function nextdir() { 'istanbul-throw.js', coverageDirectory ); - if (common.isWindows) { + if (common.checkoutEOL === '\r\n') { assert.deepStrictEqual(sourceMap.lineLengths, [1086, 31, 185, 649, 0]); } else { assert.deepStrictEqual(sourceMap.lineLengths, [1085, 30, 184, 648, 0]); diff --git a/test/wasi/test-wasi.js b/test/wasi/test-wasi.js index 8d99a91b4af570..6c00b5a7b72503 100644 --- a/test/wasi/test-wasi.js +++ b/test/wasi/test-wasi.js @@ -34,7 +34,7 @@ if (process.argv[2] === 'wasi-child') { } else { const assert = require('assert'); const cp = require('child_process'); - const { EOL } = require('os'); + const { checkoutEOL } = common; function runWASI(options) { console.log('executing', options.test); @@ -69,7 +69,7 @@ if (process.argv[2] === 'wasi-child') { } runWASI({ test: 'exitcode', exitCode: 120 }); runWASI({ test: 'fd_prestat_get_refresh' }); - runWASI({ test: 'freopen', stdout: `hello from input2.txt${EOL}` }); + runWASI({ test: 'freopen', stdout: `hello from input2.txt${checkoutEOL}` }); runWASI({ test: 'ftruncate' }); runWASI({ test: 'getentropy' }); @@ -87,10 +87,10 @@ if (process.argv[2] === 'wasi-child') { runWASI({ test: 'readdir' }); } - runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` }); + runWASI({ test: 'read_file', stdout: `hello from input.txt${checkoutEOL}` }); runWASI({ test: 'read_file_twice', - stdout: `hello from input.txt${EOL}hello from input.txt${EOL}` + stdout: `hello from input.txt${checkoutEOL}hello from input.txt${checkoutEOL}` }); runWASI({ test: 'stat' }); runWASI({ test: 'write_file' }); From 6af6553bf468f60be359eb0976a4d2a23144ac7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 6 Sep 2021 08:30:59 +0200 Subject: [PATCH 2/2] Update test/es-module/test-esm-invalid-pjson.js Co-authored-by: Antoine du Hamel --- test/es-module/test-esm-invalid-pjson.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/es-module/test-esm-invalid-pjson.js b/test/es-module/test-esm-invalid-pjson.js index 7cd3e38b0b6555..cdbebb17b4bb34 100644 --- a/test/es-module/test-esm-invalid-pjson.js +++ b/test/es-module/test-esm-invalid-pjson.js @@ -21,7 +21,7 @@ child.on('close', mustCall((code, signal) => { stderr.includes( `[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config ${invalidJson} ` + `while importing "invalid-pjson" from ${entry}. ` + - `Unexpected token } in JSON at position ${checkoutEOL === '\r\n' ? 16 : 14}` + `Unexpected token } in JSON at position ${12 + checkoutEOL.length * 2}` ), stderr); }));