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

File tree

7 files changed

+31
-16
lines changed

7 files changed

+31
-16
lines changed

‎test/common/README.md‎

Lines changed: 11 additions & 0 deletions
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‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

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

68+
functionfileURL(...paths){
69+
// When called without arguments, add explicit trailing slash
70+
constfullPath=path.resolve(tmpPath+path.sep, ...paths);
71+
72+
returnpathToFileURL(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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import{spawnPromisified}from'../common/index.mjs';
2-
import*astmpdirfrom'../common/tmpdir.js';
2+
importtmpdirfrom'../common/tmpdir.js';
33

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ tmpdir.refresh();
2727

2828
constassert=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
constshouldExistDir=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‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const common = require('../common');
44
constassert=require('assert');
55
constfs=require('fs');
66
constpath=require('path');
7-
const{ pathToFileURL }=require('url');
87

98
consttmpdir=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-
consturljoin=(base,path)=>newURL(path,base);
46-
47-
consttmpFolder=fs.mkdtempSync(urljoin(tmpdir.url,'foo.'));
43+
consttmpFolder=fs.mkdtempSync(tmpdir.fileURL('foo.'));
4844

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

52-
constutf8=fs.mkdtempSync(urljoin(tmpdir.url,'\u0222abc.'));
48+
constutf8=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‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ if (isGitPresent){
270270
}
271271

272272
// Should accept URL
273-
constfileURL=pathToFileURL(path.join(tmpdir.path,'rm-file.txt'));
273+
constfileURL=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-
constfileURL=pathToFileURL(path.join(tmpdir.path,'rm-promises-file.txt'));
379+
constfileURL=tmpdir.fileURL('rm-promises-file.txt');
380380
fs.writeFileSync(fileURL,'');
381381

382382
try{

‎test/parallel/test-runner-inspect.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import*ascommonfrom'../common/index.mjs';
2-
import*astmpdirfrom'../common/tmpdir.js';
32
import*asfixturesfrom'../common/fixtures.mjs';
3+
importtmpdirfrom'../common/tmpdir.js';
44
importassertfrom'node:assert';
55
importpathfrom'node:path';
66
importfsfrom'node:fs/promises';

0 commit comments

Comments
(0)