Skip to content

Commit

Permalink
test: add tmpdir.fileURL()
Browse files Browse the repository at this point in the history
PR-URL: #49040
Backport-PR-URL: #50669
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
LiviaMedeiros authored and targos committed Nov 23, 2023
1 parent c20fdb4 commit 65058d9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
11 changes: 11 additions & 0 deletions test/common/README.md
Expand Up @@ -1011,6 +1011,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()`

Deletes and recreates the testing temporary directory.
Expand Down Expand Up @@ -1080,6 +1090,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
Expand Down
11 changes: 10 additions & 1 deletion test/common/tmpdir.js
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { isMainThread } = require('worker_threads');

function rmSync(pathname) {
Expand Down Expand Up @@ -64,9 +65,17 @@ 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,
resolve,
};
2 changes: 1 addition & 1 deletion test/es-module/test-esm-extension-lookup-deprecation.mjs
@@ -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';
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-child-process-cwd.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down
14 changes: 5 additions & 9 deletions test/parallel/test-fs-mkdtemp.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-rm.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-runner-inspect.mjs
@@ -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';
Expand Down

0 comments on commit 65058d9

Please sign in to comment.