Skip to content

Commit 669ac03

Browse files
LiviaMedeirosRafaelGSS
authored andcommitted
test: add tmpdir.fileURL()
PR-URL: #49040 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 9d13503 commit 669ac03

7 files changed

+31
-16
lines changed

test/common/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,16 @@ The `tmpdir` module supports the use of a temporary directory for testing.
10271027

10281028
The realpath of the testing temporary directory.
10291029

1030+
### `fileURL([...paths])`
1031+
1032+
* `...paths` [\<string>][<string>]
1033+
* return [\<URL>][<URL>]
1034+
1035+
Resolves a sequence of paths into absolute url in the temporary directory.
1036+
1037+
When called without arguments, returns absolute url of the testing
1038+
temporary directory with explicit trailing `/`.
1039+
10301040
### `refresh(useSpawn)`
10311041

10321042
* `useSpawn` [\<boolean>][<boolean>] default = false
@@ -1092,6 +1102,7 @@ See [the WPT tests README][] for details.
10921102
[<Function>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
10931103
[<Object>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
10941104
[<RegExp>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
1105+
[<URL>]: https://developer.mozilla.org/en-US/docs/Web/API/URL
10951106
[<any>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types
10961107
[<bigint>]: https://github.com/tc39/proposal-bigint
10971108
[<boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type

test/common/tmpdir.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { spawnSync } = require('child_process');
44
const fs = require('fs');
55
const path = require('path');
6+
const { pathToFileURL } = require('url');
67
const { isMainThread } = require('worker_threads');
78

89
function rmSync(pathname, useSpawn) {
@@ -74,8 +75,16 @@ function hasEnoughSpace(size) {
7475
return bavail >= Math.ceil(size / bsize);
7576
}
7677

78+
function fileURL(...paths) {
79+
// When called without arguments, add explicit trailing slash
80+
const fullPath = path.resolve(tmpPath + path.sep, ...paths);
81+
82+
return pathToFileURL(fullPath);
83+
}
84+
7785
module.exports = {
86+
fileURL,
87+
hasEnoughSpace,
7888
path: tmpPath,
7989
refresh,
80-
hasEnoughSpace,
8190
};

test/es-module/test-esm-extension-lookup-deprecation.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawnPromisified } from '../common/index.mjs';
2-
import * as tmpdir from '../common/tmpdir.js';
2+
import tmpdir from '../common/tmpdir.js';
33

44
import assert from 'node:assert';
55
import { mkdir, writeFile } from 'node:fs/promises';

test/parallel/test-child-process-cwd.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ tmpdir.refresh();
2727

2828
const assert = require('assert');
2929
const { spawn } = require('child_process');
30-
const { pathToFileURL, URL } = require('url');
3130

3231
// Spawns 'pwd' with given options, then test
3332
// - whether the child pid is undefined or number,
@@ -88,7 +87,7 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
8887
testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path);
8988
const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
9089
testCwd({ cwd: shouldExistDir }, 'number', 0, shouldExistDir);
91-
testCwd({ cwd: pathToFileURL(tmpdir.path) }, 'number', 0, tmpdir.path);
90+
testCwd({ cwd: tmpdir.fileURL() }, 'number', 0, tmpdir.path);
9291

9392
// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
9493
testCwd({ cwd: '' }, 'number');

test/parallel/test-fs-mkdtemp.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const common = require('../common');
44
const assert = require('assert');
55
const fs = require('fs');
66
const path = require('path');
7-
const { pathToFileURL } = require('url');
87

98
const tmpdir = require('../common/tmpdir');
109
tmpdir.refresh();
@@ -41,27 +40,24 @@ function handler(err, folder) {
4140

4241
// Test with URL object
4342
{
44-
tmpdir.url = pathToFileURL(tmpdir.path);
45-
const urljoin = (base, path) => new URL(path, base);
46-
47-
const tmpFolder = fs.mkdtempSync(urljoin(tmpdir.url, 'foo.'));
43+
const tmpFolder = fs.mkdtempSync(tmpdir.fileURL('foo.'));
4844

4945
assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
5046
assert(fs.existsSync(tmpFolder));
5147

52-
const utf8 = fs.mkdtempSync(urljoin(tmpdir.url, '\u0222abc.'));
48+
const utf8 = fs.mkdtempSync(tmpdir.fileURL('\u0222abc.'));
5349
assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
5450
Buffer.byteLength('\u0222abc.XXXXXX'));
5551
assert(fs.existsSync(utf8));
5652

57-
fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), common.mustCall(handler));
53+
fs.mkdtemp(tmpdir.fileURL('bar.'), common.mustCall(handler));
5854

5955
// Same test as above, but making sure that passing an options object doesn't
6056
// affect the way the callback function is handled.
61-
fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), {}, common.mustCall(handler));
57+
fs.mkdtemp(tmpdir.fileURL('bar.'), {}, common.mustCall(handler));
6258

6359
// Warning fires only once
64-
fs.mkdtemp(urljoin(tmpdir.url, 'bar.X'), common.mustCall(handler));
60+
fs.mkdtemp(tmpdir.fileURL('bar.X'), common.mustCall(handler));
6561
}
6662

6763
// Test with Buffer

test/parallel/test-fs-rm.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ if (isGitPresent) {
270270
}
271271

272272
// Should accept URL
273-
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt'));
273+
const fileURL = tmpdir.fileURL('rm-file.txt');
274274
fs.writeFileSync(fileURL, '');
275275

276276
try {
@@ -376,7 +376,7 @@ if (isGitPresent) {
376376
}
377377

378378
// Should accept URL
379-
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt'));
379+
const fileURL = tmpdir.fileURL('rm-promises-file.txt');
380380
fs.writeFileSync(fileURL, '');
381381

382382
try {

test/parallel/test-runner-inspect.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as common from '../common/index.mjs';
2-
import * as tmpdir from '../common/tmpdir.js';
32
import * as fixtures from '../common/fixtures.mjs';
3+
import tmpdir from '../common/tmpdir.js';
44
import assert from 'node:assert';
55
import path from 'node:path';
66
import fs from 'node:fs/promises';

0 commit comments

Comments
 (0)