Skip to content

Commit

Permalink
test: use tmpdir.resolve() in fs tests
Browse files Browse the repository at this point in the history
PR-URL: #49126
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
LiviaMedeiros committed Aug 15, 2023
1 parent 74e0ca3 commit 6a68794
Show file tree
Hide file tree
Showing 52 changed files with 169 additions and 209 deletions.
7 changes: 3 additions & 4 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ if (common.isIBMi)

const assert = require('assert');
const fs = require('fs');
const path = require('path');

const { internalBinding } = require('internal/test/binding');
const { UV_ENOENT } = internalBinding('uv');

const tmpdir = require('../common/tmpdir');
const doesNotExist = path.join(tmpdir.path, '__this_should_not_exist');
const readOnlyFile = path.join(tmpdir.path, 'read_only_file');
const readWriteFile = path.join(tmpdir.path, 'read_write_file');
const doesNotExist = tmpdir.resolve('__this_should_not_exist');
const readOnlyFile = tmpdir.resolve('read_only_file');
const readWriteFile = tmpdir.resolve('read_write_file');

function createFileWithPerms(file, mode) {
fs.writeFileSync(file, '');
Expand Down
11 changes: 5 additions & 6 deletions test/parallel/test-fs-append-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const join = require('path').join;
const fs = require('fs');

const currentFileData = 'ABCD';
Expand All @@ -40,7 +39,7 @@ const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

// Test that empty file will be created and have content added.
const filename = join(tmpdir.path, 'append-sync.txt');
const filename = tmpdir.resolve('append-sync.txt');

fs.appendFileSync(filename, data);

Expand All @@ -49,7 +48,7 @@ const fileData = fs.readFileSync(filename);
assert.strictEqual(Buffer.byteLength(data), fileData.length);

// Test that appends data to a non empty file.
const filename2 = join(tmpdir.path, 'append-sync2.txt');
const filename2 = tmpdir.resolve('append-sync2.txt');
fs.writeFileSync(filename2, currentFileData);

fs.appendFileSync(filename2, data);
Expand All @@ -60,7 +59,7 @@ assert.strictEqual(Buffer.byteLength(data) + currentFileData.length,
fileData2.length);

// Test that appendFileSync accepts buffers.
const filename3 = join(tmpdir.path, 'append-sync3.txt');
const filename3 = tmpdir.resolve('append-sync3.txt');
fs.writeFileSync(filename3, currentFileData);

const buf = Buffer.from(data, 'utf8');
Expand All @@ -70,7 +69,7 @@ const fileData3 = fs.readFileSync(filename3);

assert.strictEqual(buf.length + currentFileData.length, fileData3.length);

const filename4 = join(tmpdir.path, 'append-sync4.txt');
const filename4 = tmpdir.resolve('append-sync4.txt');
fs.writeFileSync(filename4, currentFileData, common.mustNotMutateObjectDeep({ mode: m }));

[
Expand All @@ -95,7 +94,7 @@ assert.strictEqual(Buffer.byteLength(String(num)) + currentFileData.length,
fileData4.length);

// Test that appendFile accepts file descriptors.
const filename5 = join(tmpdir.path, 'append-sync5.txt');
const filename5 = tmpdir.resolve('append-sync5.txt');
fs.writeFileSync(filename5, currentFileData);

const filename5fd = fs.openSync(filename5, 'a+', 0o600);
Expand Down
21 changes: 10 additions & 11 deletions test/parallel/test-fs-append-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const join = require('path').join;

const tmpdir = require('../common/tmpdir');

Expand All @@ -43,7 +42,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that empty file will be created and have content added (callback API).
{
const filename = join(tmpdir.path, 'append.txt');
const filename = tmpdir.resolve('append.txt');

fs.appendFile(filename, s, common.mustSucceed(() => {
fs.readFile(filename, common.mustSucceed((buffer) => {
Expand All @@ -54,7 +53,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that empty file will be created and have content added (promise API).
{
const filename = join(tmpdir.path, 'append-promise.txt');
const filename = tmpdir.resolve('append-promise.txt');

fs.promises.appendFile(filename, s)
.then(common.mustCall(() => fs.promises.readFile(filename)))
Expand All @@ -66,7 +65,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that appends data to a non-empty file (callback API).
{
const filename = join(tmpdir.path, 'append-non-empty.txt');
const filename = tmpdir.resolve('append-non-empty.txt');
fs.writeFileSync(filename, currentFileData);

fs.appendFile(filename, s, common.mustSucceed(() => {
Expand All @@ -79,7 +78,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that appends data to a non-empty file (promise API).
{
const filename = join(tmpdir.path, 'append-non-empty-promise.txt');
const filename = tmpdir.resolve('append-non-empty-promise.txt');
fs.writeFileSync(filename, currentFileData);

fs.promises.appendFile(filename, s)
Expand All @@ -93,7 +92,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that appendFile accepts buffers (callback API).
{
const filename = join(tmpdir.path, 'append-buffer.txt');
const filename = tmpdir.resolve('append-buffer.txt');
fs.writeFileSync(filename, currentFileData);

const buf = Buffer.from(s, 'utf8');
Expand All @@ -107,7 +106,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that appendFile accepts buffers (promises API).
{
const filename = join(tmpdir.path, 'append-buffer-promises.txt');
const filename = tmpdir.resolve('append-buffer-promises.txt');
fs.writeFileSync(filename, currentFileData);

const buf = Buffer.from(s, 'utf8');
Expand All @@ -126,7 +125,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
code: 'ERR_INVALID_ARG_TYPE',
message: /"data"|"buffer"/
};
const filename = join(tmpdir.path, 'append-invalid-data.txt');
const filename = tmpdir.resolve('append-invalid-data.txt');

assert.throws(
() => fs.appendFile(filename, data, common.mustNotCall()),
Expand Down Expand Up @@ -154,7 +153,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that appendFile accepts file descriptors (callback API).
{
const filename = join(tmpdir.path, 'append-descriptors.txt');
const filename = tmpdir.resolve('append-descriptors.txt');
fs.writeFileSync(filename, currentFileData);

fs.open(filename, 'a+', common.mustSucceed((fd) => {
Expand All @@ -171,7 +170,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

// Test that appendFile accepts file descriptors (promises API).
{
const filename = join(tmpdir.path, 'append-descriptors-promises.txt');
const filename = tmpdir.resolve('append-descriptors-promises.txt');
fs.writeFileSync(filename, currentFileData);

let fd;
Expand All @@ -190,5 +189,5 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
}

assert.throws(
() => fs.appendFile(join(tmpdir.path, 'append6.txt'), console.log),
() => fs.appendFile(tmpdir.resolve('append6.txt'), console.log),
{ code: 'ERR_INVALID_ARG_TYPE' });
3 changes: 1 addition & 2 deletions test/parallel/test-fs-assert-encoding-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
const common = require('../common');
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');
const tmpdir = require('../common/tmpdir');

const testPath = path.join(tmpdir.path, 'assert-encoding-error');
const testPath = tmpdir.resolve('assert-encoding-error');
const options = 'test';
const expectedError = {
code: 'ERR_INVALID_ARG_VALUE',
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const fs = require('fs');
const path = require('path');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

fs.access(Buffer.from(tmpdir.path), common.mustSucceed());

const buf = Buffer.from(path.join(tmpdir.path, 'a.txt'));
const buf = Buffer.from(tmpdir.resolve('a.txt'));
fs.open(buf, 'w+', common.mustSucceed((fd) => {
assert(fd);
fs.close(fd, common.mustSucceed());
Expand Down
17 changes: 8 additions & 9 deletions test/parallel/test-fs-chmod-mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');

let mode;
Expand All @@ -26,7 +25,7 @@ function test(mode, asString) {
(mode | maskToIgnore).toString(8) : (mode | maskToIgnore);

{
const file = path.join(tmpdir.path, `chmod-async-${suffix}.txt`);
const file = tmpdir.resolve(`chmod-async-${suffix}.txt`);
fs.writeFileSync(file, 'test', 'utf-8');

fs.chmod(file, input, common.mustSucceed(() => {
Expand All @@ -35,15 +34,15 @@ function test(mode, asString) {
}

{
const file = path.join(tmpdir.path, `chmodSync-${suffix}.txt`);
const file = tmpdir.resolve(`chmodSync-${suffix}.txt`);
fs.writeFileSync(file, 'test', 'utf-8');

fs.chmodSync(file, input);
assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
}

{
const file = path.join(tmpdir.path, `fchmod-async-${suffix}.txt`);
const file = tmpdir.resolve(`fchmod-async-${suffix}.txt`);
fs.writeFileSync(file, 'test', 'utf-8');
fs.open(file, 'w', common.mustSucceed((fd) => {
fs.fchmod(fd, input, common.mustSucceed(() => {
Expand All @@ -54,7 +53,7 @@ function test(mode, asString) {
}

{
const file = path.join(tmpdir.path, `fchmodSync-${suffix}.txt`);
const file = tmpdir.resolve(`fchmodSync-${suffix}.txt`);
fs.writeFileSync(file, 'test', 'utf-8');
const fd = fs.openSync(file, 'w');

Expand All @@ -65,8 +64,8 @@ function test(mode, asString) {
}

if (fs.lchmod) {
const link = path.join(tmpdir.path, `lchmod-src-${suffix}`);
const file = path.join(tmpdir.path, `lchmod-dest-${suffix}`);
const link = tmpdir.resolve(`lchmod-src-${suffix}`);
const file = tmpdir.resolve(`lchmod-dest-${suffix}`);
fs.writeFileSync(file, 'test', 'utf-8');
fs.symlinkSync(file, link);

Expand All @@ -76,8 +75,8 @@ function test(mode, asString) {
}

if (fs.lchmodSync) {
const link = path.join(tmpdir.path, `lchmodSync-src-${suffix}`);
const file = path.join(tmpdir.path, `lchmodSync-dest-${suffix}`);
const link = tmpdir.resolve(`lchmodSync-src-${suffix}`);
const file = tmpdir.resolve(`lchmodSync-dest-${suffix}`);
fs.writeFileSync(file, 'test', 'utf-8');
fs.symlinkSync(file, link);

Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-fs-chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');

let mode_async;
Expand Down Expand Up @@ -74,8 +73,8 @@ if (common.isWindows) {
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const file1 = path.join(tmpdir.path, 'a.js');
const file2 = path.join(tmpdir.path, 'a1.js');
const file1 = tmpdir.resolve('a.js');
const file2 = tmpdir.resolve('a1.js');

// Create file1.
fs.closeSync(fs.openSync(file1, 'w'));
Expand Down Expand Up @@ -123,7 +122,7 @@ fs.open(file2, 'w', common.mustSucceed((fd) => {

// lchmod
if (fs.lchmod) {
const link = path.join(tmpdir.path, 'symbolic-link');
const link = tmpdir.resolve('symbolic-link');

fs.symlinkSync(file2, link);

Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-fs-copyfile-respect-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ tmpdir.refresh();

const assert = require('assert');
const fs = require('fs');
const path = require('path');

let n = 0;

function beforeEach() {
n++;
const source = path.join(tmpdir.path, `source${n}`);
const dest = path.join(tmpdir.path, `dest${n}`);
const source = tmpdir.resolve(`source${n}`);
const dest = tmpdir.resolve(`dest${n}`);
fs.writeFileSync(source, 'source');
fs.writeFileSync(dest, 'dest');
fs.chmodSync(dest, '444');
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-copyfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const {
UV_ENOENT,
UV_EEXIST
} = internalBinding('uv');
const path = require('path');
const src = fixtures.path('a.js');
const dest = path.join(tmpdir.path, 'copyfile.out');
const dest = tmpdir.resolve('copyfile.out');
const {
COPYFILE_EXCL,
COPYFILE_FICLONE,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tmpdir.refresh();

let dirc = 0;
function nextdir() {
return join(tmpdir.path, `copy_${++dirc}`);
return tmpdir.resolve(`copy_${++dirc}`);
}

// Synchronous implementation of copy.
Expand Down
13 changes: 6 additions & 7 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const path = require('path');

tmpdir.refresh();


const nonexistentFile = path.join(tmpdir.path, 'non-existent');
const nonexistentDir = path.join(tmpdir.path, 'non-existent', 'foo', 'bar');
const existingFile = path.join(tmpdir.path, 'existingFile.js');
const existingFile2 = path.join(tmpdir.path, 'existingFile2.js');
const existingDir = path.join(tmpdir.path, 'dir');
const nonexistentFile = tmpdir.resolve('non-existent');
const nonexistentDir = tmpdir.resolve('non-existent', 'foo', 'bar');
const existingFile = tmpdir.resolve('existingFile.js');
const existingFile2 = tmpdir.resolve('existingFile2.js');
const existingDir = tmpdir.resolve('dir');
const existingDir2 = fixtures.path('keys');
fs.mkdirSync(existingDir);
fs.writeFileSync(existingFile, 'test', 'utf-8');
Expand Down Expand Up @@ -297,7 +296,7 @@ function re(literals, ...values) {
return true;
};

const destFile = path.join(tmpdir.path, 'foo');
const destFile = tmpdir.resolve('foo');
fs.rename(nonexistentFile, destFile, common.mustCall(validateError));

assert.throws(
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-fmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require('../common');
const assert = require('assert');
const fs = require('fs');
const join = require('path').join;

const {
O_CREAT = 0,
Expand All @@ -18,7 +17,7 @@ tmpdir.refresh();
// Run this test on all platforms. While UV_FS_O_FILEMAP is only available on
// Windows, it should be silently ignored on other platforms.

const filename = join(tmpdir.path, 'fmap.txt');
const filename = tmpdir.resolve('fmap.txt');
const text = 'Memory File Mapping Test';

const mw = UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY;
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-fsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');

const fs = require('fs');
const path = require('path');

const fileFixture = fixtures.path('a.js');
const fileTemp = path.join(tmpdir.path, 'a.js');
const fileTemp = tmpdir.resolve('a.js');

// Copy fixtures to temp.
tmpdir.refresh();
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import glob from 'internal/fs/glob';

tmpdir.refresh();

const fixtureDir = resolve(tmpdir.path, 'fixtures');
const absDir = resolve(tmpdir.path, 'abs');
const fixtureDir = tmpdir.resolve('fixtures');
const absDir = tmpdir.resolve('abs');

async function setup() {
await mkdir(fixtureDir, { recursive: true });
Expand Down
Loading

0 comments on commit 6a68794

Please sign in to comment.