GeezerVison is showing github.com — test: add `tmpdir.fileURL()` · nodejs/node@65058d9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/common/README.md +11Lines changed: 11 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1011,6 +1011,16 @@ The `tmpdir` module supports the use of a temporary directory for testing. | 1011 | 1011 |
| 1012 | 1012 | The realpath of the testing temporary directory. | 1013 | 1013 |
| | 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 | + | 1014 | 1024 | ### `refresh()` | 1015 | 1025 |
| 1016 | 1026 | Deletes and recreates the testing temporary directory. | @@ -1080,6 +1090,7 @@ See [the WPT tests README][] for details. | 1080 | 1090 | [<Function>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function | 1081 | 1091 | [<Object>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object | 1082 | 1092 | [<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 | 1083 | 1094 | [<any>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types | 1084 | 1095 | [<bigint>]: https://github.com/tc39/proposal-bigint | 1085 | 1096 | [<boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type | |
Collapse file Copy file name to clipboardExpand all lines: test/common/tmpdir.js +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const fs = require('fs'); | 4 | 4 | const path = require('path'); | | 5 | +const { pathToFileURL } = require('url'); | 5 | 6 | const { isMainThread } = require('worker_threads'); | 6 | 7 |
| 7 | 8 | function rmSync(pathname) { | @@ -64,9 +65,17 @@ function hasEnoughSpace(size) { | 64 | 65 | return bavail >= Math.ceil(size / bsize); | 65 | 66 | } | 66 | 67 |
| | 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 | + | 67 | 75 | module.exports = { | | 76 | + fileURL, | | 77 | + hasEnoughSpace, | 68 | 78 | path: tmpPath, | 69 | 79 | refresh, | 70 | | - hasEnoughSpace, | 71 | 80 | resolve, | 72 | 81 | }; |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-extension-lookup-deprecation.mjs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { spawnPromisified } from '../common/index.mjs'; | 2 | | -import * as tmpdir from '../common/tmpdir.js'; | | 2 | +import tmpdir from '../common/tmpdir.js'; | 3 | 3 |
| 4 | 4 | import assert from 'node:assert'; | 5 | 5 | import { mkdir, writeFile } from 'node:fs/promises'; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-cwd.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,6 @@ tmpdir.refresh(); | 27 | 27 |
| 28 | 28 | const assert = require('assert'); | 29 | 29 | const { spawn } = require('child_process'); | 30 | | -const { pathToFileURL, URL } = require('url'); | 31 | 30 |
| 32 | 31 | // Spawns 'pwd' with given options, then test | 33 | 32 | // - whether the child pid is undefined or number, | @@ -88,7 +87,7 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) { | 88 | 87 | testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path); | 89 | 88 | const shouldExistDir = common.isWindows ? process.env.windir : '/dev'; | 90 | 89 | 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); | 92 | 91 |
| 93 | 92 | // Spawn() shouldn't try to chdir() to invalid arg, so this should just work | 94 | 93 | testCwd({ cwd: '' }, 'number'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-mkdtemp.js +5-9Lines changed: 5 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,7 +4,6 @@ const common = require('../common'); | 4 | 4 | const assert = require('assert'); | 5 | 5 | const fs = require('fs'); | 6 | 6 | const path = require('path'); | 7 | | -const { pathToFileURL } = require('url'); | 8 | 7 |
| 9 | 8 | const tmpdir = require('../common/tmpdir'); | 10 | 9 | tmpdir.refresh(); | @@ -41,27 +40,24 @@ function handler(err, folder) { | 41 | 40 |
| 42 | 41 | // Test with URL object | 43 | 42 | { | 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.')); | 48 | 44 |
| 49 | 45 | assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); | 50 | 46 | assert(fs.existsSync(tmpFolder)); | 51 | 47 |
| 52 | | - const utf8 = fs.mkdtempSync(urljoin(tmpdir.url, '\u0222abc.')); | | 48 | + const utf8 = fs.mkdtempSync(tmpdir.fileURL('\u0222abc.')); | 53 | 49 | assert.strictEqual(Buffer.byteLength(path.basename(utf8)), | 54 | 50 | Buffer.byteLength('\u0222abc.XXXXXX')); | 55 | 51 | assert(fs.existsSync(utf8)); | 56 | 52 |
| 57 | | - fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), common.mustCall(handler)); | | 53 | + fs.mkdtemp(tmpdir.fileURL('bar.'), common.mustCall(handler)); | 58 | 54 |
| 59 | 55 | // Same test as above, but making sure that passing an options object doesn't | 60 | 56 | // 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)); | 62 | 58 |
| 63 | 59 | // 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)); | 65 | 61 | } | 66 | 62 |
| 67 | 63 | // Test with Buffer | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-rm.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -270,7 +270,7 @@ if (isGitPresent) { | 270 | 270 | } | 271 | 271 |
| 272 | 272 | // Should accept URL | 273 | | - const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt')); | | 273 | + const fileURL = tmpdir.fileURL('rm-file.txt'); | 274 | 274 | fs.writeFileSync(fileURL, ''); | 275 | 275 |
| 276 | 276 | try { | @@ -376,7 +376,7 @@ if (isGitPresent) { | 376 | 376 | } | 377 | 377 |
| 378 | 378 | // Should accept URL | 379 | | - const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt')); | | 379 | + const fileURL = tmpdir.fileURL('rm-promises-file.txt'); | 380 | 380 | fs.writeFileSync(fileURL, ''); | 381 | 381 |
| 382 | 382 | try { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-inspect.mjs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as common from '../common/index.mjs'; | 2 | | -import * as tmpdir from '../common/tmpdir.js'; | 3 | 2 | import * as fixtures from '../common/fixtures.mjs'; | | 3 | +import tmpdir from '../common/tmpdir.js'; | 4 | 4 | import assert from 'node:assert'; | 5 | 5 | import path from 'node:path'; | 6 | 6 | import fs from 'node:fs/promises'; | |
|