mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
test: add tmpdir.fileURL()
PR-URL: https://github.com/nodejs/node/pull/49040 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
1 parent
9d13503c4e
commit
669ac03520
7 files changed
+31
-16
No files matched your search
@@ -1027,6 +1027,16 @@ The `tmpdir` module supports the use of a temporary directory for testing.
|
||||
|
||||
The realpath of the testing temporary directory.
|
||||
|
||||
### `fileURL([...paths])`
|
||||
|
||||
* `...paths` [\<string>][<string>]
|
||||
* return [\<URL>][<URL>]
|
||||
|
||||
Resolves a sequence of paths into absolute url in the temporary directory.
|
||||
|
||||
When called without arguments, returns absolute url of the testing
|
||||
temporary directory with explicit trailing `/`.
|
||||
|
||||
### `refresh(useSpawn)`
|
||||
|
||||
* `useSpawn` [\<boolean>][<boolean>] default = false
|
||||
@@ -1092,6 +1102,7 @@ See [the WPT tests README][] for details.
|
||||
[<Function>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
|
||||
[<Object>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
[<RegExp>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
|
||||
[<URL>]: https://developer.mozilla.org/en-US/docs/Web/API/URL
|
||||
[<any>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types
|
||||
[<bigint>]: https://github.com/tc39/proposal-bigint
|
||||
[<boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type
|
||||
|
||||
+10
-1
@@ -3,6 +3,7 @@
|
||||
const { spawnSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { pathToFileURL } = require('url');
|
||||
const { isMainThread } = require('worker_threads');
|
||||
|
||||
function rmSync(pathname, useSpawn) {
|
||||
@@ -74,8 +75,16 @@ function hasEnoughSpace(size) {
|
||||
return bavail >= Math.ceil(size / bsize);
|
||||
}
|
||||
|
||||
function fileURL(...paths) {
|
||||
// When called without arguments, add explicit trailing slash
|
||||
const fullPath = path.resolve(tmpPath + path.sep, ...paths);
|
||||
|
||||
return pathToFileURL(fullPath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fileURL,
|
||||
hasEnoughSpace,
|
||||
path: tmpPath,
|
||||
refresh,
|
||||
hasEnoughSpace,
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { spawnPromisified } from '../common/index.mjs';
|
||||
import * as tmpdir from '../common/tmpdir.js';
|
||||
import tmpdir from '../common/tmpdir.js';
|
||||
|
||||
import assert from 'node:assert';
|
||||
import { mkdir, writeFile } from 'node:fs/promises';
|
||||
|
||||
@@ -27,7 +27,6 @@ tmpdir.refresh();
|
||||
|
||||
const assert = require('assert');
|
||||
const { spawn } = require('child_process');
|
||||
const { pathToFileURL, URL } = require('url');
|
||||
|
||||
// Spawns 'pwd' with given options, then test
|
||||
// - whether the child pid is undefined or number,
|
||||
@@ -88,7 +87,7 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
|
||||
testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path);
|
||||
const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
|
||||
testCwd({ cwd: shouldExistDir }, 'number', 0, shouldExistDir);
|
||||
testCwd({ cwd: pathToFileURL(tmpdir.path) }, 'number', 0, tmpdir.path);
|
||||
testCwd({ cwd: tmpdir.fileURL() }, 'number', 0, tmpdir.path);
|
||||
|
||||
// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
|
||||
testCwd({ cwd: '' }, 'number');
|
||||
|
||||
@@ -4,7 +4,6 @@ const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { pathToFileURL } = require('url');
|
||||
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
@@ -41,27 +40,24 @@ function handler(err, folder) {
|
||||
|
||||
// Test with URL object
|
||||
{
|
||||
tmpdir.url = pathToFileURL(tmpdir.path);
|
||||
const urljoin = (base, path) => new URL(path, base);
|
||||
|
||||
const tmpFolder = fs.mkdtempSync(urljoin(tmpdir.url, 'foo.'));
|
||||
const tmpFolder = fs.mkdtempSync(tmpdir.fileURL('foo.'));
|
||||
|
||||
assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
|
||||
assert(fs.existsSync(tmpFolder));
|
||||
|
||||
const utf8 = fs.mkdtempSync(urljoin(tmpdir.url, '\u0222abc.'));
|
||||
const utf8 = fs.mkdtempSync(tmpdir.fileURL('\u0222abc.'));
|
||||
assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
|
||||
Buffer.byteLength('\u0222abc.XXXXXX'));
|
||||
assert(fs.existsSync(utf8));
|
||||
|
||||
fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), common.mustCall(handler));
|
||||
fs.mkdtemp(tmpdir.fileURL('bar.'), common.mustCall(handler));
|
||||
|
||||
// Same test as above, but making sure that passing an options object doesn't
|
||||
// affect the way the callback function is handled.
|
||||
fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), {}, common.mustCall(handler));
|
||||
fs.mkdtemp(tmpdir.fileURL('bar.'), {}, common.mustCall(handler));
|
||||
|
||||
// Warning fires only once
|
||||
fs.mkdtemp(urljoin(tmpdir.url, 'bar.X'), common.mustCall(handler));
|
||||
fs.mkdtemp(tmpdir.fileURL('bar.X'), common.mustCall(handler));
|
||||
}
|
||||
|
||||
// Test with Buffer
|
||||
|
||||
@@ -270,7 +270,7 @@ if (isGitPresent) {
|
||||
}
|
||||
|
||||
// Should accept URL
|
||||
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt'));
|
||||
const fileURL = tmpdir.fileURL('rm-file.txt');
|
||||
fs.writeFileSync(fileURL, '');
|
||||
|
||||
try {
|
||||
@@ -376,7 +376,7 @@ if (isGitPresent) {
|
||||
}
|
||||
|
||||
// Should accept URL
|
||||
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt'));
|
||||
const fileURL = tmpdir.fileURL('rm-promises-file.txt');
|
||||
fs.writeFileSync(fileURL, '');
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as common from '../common/index.mjs';
|
||||
import * as tmpdir from '../common/tmpdir.js';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
import tmpdir from '../common/tmpdir.js';
|
||||
import assert from 'node:assert';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs/promises';
|
||||
|
||||
Reference in new issue
Block a user