Skip to content

Commit 65058d9

Browse files
LiviaMedeirostargos
authored andcommitted
test: add tmpdir.fileURL()
PR-URL: #49040 Backport-PR-URL: #50669 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent c20fdb4 commit 65058d9

7 files changed

+31
-16
lines changed

test/common/README.md

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

10121012
The realpath of the testing temporary directory.
10131013

1014+
### `fileURL([...paths])`
1015+
1016+
* `...paths` [\<string>][<string>]
1017+
* return [\<URL>][<URL>]
1018+
1019+
Resolves a sequence of paths into absolute url in the temporary directory.
1020+
1021+
When called without arguments, returns absolute url of the testing
1022+
temporary directory with explicit trailing `/`.
1023+
10141024
### `refresh()`
10151025

10161026
Deletes and recreates the testing temporary directory.
@@ -1080,6 +1090,7 @@ See [the WPT tests README][] for details.
10801090
[<Function>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
10811091
[<Object>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
10821092
[<RegExp>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
1093+
[<URL>]: https://developer.mozilla.org/en-US/docs/Web/API/URL
10831094
[<any>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types
10841095
[<bigint>]: https://github.com./tc39/proposal-bigint
10851096
[<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
@@ -2,6 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5+
const { pathToFileURL } = require('url');
56
const { isMainThread } = require('worker_threads');
67

78
function rmSync(pathname) {
@@ -64,9 +65,17 @@ function hasEnoughSpace(size) {
6465
return bavail >= Math.ceil(size / bsize);
6566
}
6667

68+
function fileURL(...paths) {
69+
// When called without arguments, add explicit trailing slash
70+
const fullPath = path.resolve(tmpPath + path.sep, ...paths);
71+
72+
return pathToFileURL(fullPath);
73+
}
74+
6775
module.exports = {
76+
fileURL,
77+
hasEnoughSpace,
6878
path: tmpPath,
6979
refresh,
70-
hasEnoughSpace,
7180
resolve,
7281
};

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)