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

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
@@ -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‎

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

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

78+
functionfileURL(...paths){
79+
// When called without arguments, add explicit trailing slash
80+
constfullPath=path.resolve(tmpPath+path.sep, ...paths);
81+
82+
returnpathToFileURL(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‎

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)