Skip to content

Commit

Permalink
test: clean tmpdir on process exit
Browse files Browse the repository at this point in the history
  • Loading branch information
joaocgreis committed Jul 30, 2019
1 parent 7f06449 commit eedff96
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 50 deletions.
29 changes: 23 additions & 6 deletions test/addons/load-long-path/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ if (common.isWindows && (process.env.PROCESSOR_ARCHITEW6432 !== undefined))
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const { fork } = require('child_process');

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

// Make a path that is more than 260 chars long.
// Any given folder cannot have a name longer than 260 characters,
Expand All @@ -17,7 +17,6 @@ let addonDestinationDir = path.resolve(tmpdir.path);

for (let i = 0; i < 10; i++) {
addonDestinationDir = path.join(addonDestinationDir, 'x'.repeat(30));
fs.mkdirSync(addonDestinationDir);
}

const addonPath = path.join(__dirname,
Expand All @@ -26,11 +25,29 @@ const addonPath = path.join(__dirname,
'binding.node');
const addonDestinationPath = path.join(addonDestinationDir, 'binding.node');

// Loading an addon keeps the file open until the process terminates. Load
// the addon in a child process so that when the parent terminates the file
// is already closed and the tmpdir can be cleaned up.

// Child
if (process.argv[2] === 'child') {
// Attempt to load at long path destination
const addon = require(addonDestinationPath);
assert.notStrictEqual(addon, null);
assert.strictEqual(addon.hello(), 'world');
return;
}

// Parent
tmpdir.refresh();

// Copy binary to long path destination
fs.mkdirSync(addonDestinationDir, { recursive: true });
const contents = fs.readFileSync(addonPath);
fs.writeFileSync(addonDestinationPath, contents);

// Attempt to load at long path destination
const addon = require(addonDestinationPath);
assert.notStrictEqual(addon, null);
assert.strictEqual(addon.hello(), 'world');
// Run test
const child = fork(__filename, ['child'], { stdio: 'inherit' });
child.on('exit', common.mustCall(function(code) {
assert.strictEqual(code, 0);
}));
6 changes: 6 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,12 @@ The realpath of the testing temporary directory.

Deletes and recreates the testing temporary directory.

The first time refresh is run it adds a listener to process `'exit'` that
cleans the temporary directory. Thus, every file under `tmpdir.path` needs to
be closed before the test completes. A good way to do this is to add a
listener to process `'beforeExit'`. If a file needs to be left open until Node
completes, use a child process and call `refresh` only in the parent.

## WPT Module

### harness
Expand Down
23 changes: 23 additions & 0 deletions test/common/tmpdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ function rimrafSync(pathname, { spawn = true } = {}) {
}
rmdirSync(pathname, e);
}

if (fs.existsSync(pathname))
throw new Error(`Unable to rimraf ${pathname}`);
}

function rmdirSync(p, originalEr) {
Expand All @@ -80,7 +83,9 @@ function rmdirSync(p, originalEr) {
}
});
fs.rmdirSync(p);
return;
}
throw e;
}
}

Expand All @@ -93,9 +98,27 @@ const tmpdirName = '.tmp.' +
(process.env.TEST_SERIAL_ID || process.env.TEST_THREAD_ID || '0');
const tmpPath = path.join(testRoot, tmpdirName);

let firstRefresh = true;
function refresh(opts = {}) {
rimrafSync(this.path, opts);
fs.mkdirSync(this.path);

if (firstRefresh) {
firstRefresh = false;
// Clean only when a test uses refresh. This allows for child processes to
// use the tmpdir and only the parent will clean on exit.
process.on('exit', () => {
try {
// Change dit to avoid possible EBUSY
process.chdir(testRoot);
rimrafSync(tmpPath, { spawn: false });
} catch (e) {
console.error('Can\'t clean tmpdir:', tmpPath);
console.error('Files blocking:', fs.readdirSync(tmpPath));
throw e;
}
});
}
}

module.exports = {
Expand Down
10 changes: 0 additions & 10 deletions test/parallel/test-fs-append-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,3 @@ const fileData5 = fs.readFileSync(filename5);

assert.strictEqual(Buffer.byteLength(data) + currentFileData.length,
fileData5.length);

// Exit logic for cleanup.

process.on('exit', function() {
fs.unlinkSync(filename);
fs.unlinkSync(filename2);
fs.unlinkSync(filename3);
fs.unlinkSync(filename4);
fs.unlinkSync(filename5);
});
1 change: 1 addition & 0 deletions test/parallel/test-fs-buffertype-writesync.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ v.forEach((value) => {
const fd = fs.openSync(filePath, 'w');
fs.writeSync(fd, value);
assert.strictEqual(fs.readFileSync(filePath).toString(), String(value));
fs.closeSync(fd);
});
1 change: 1 addition & 0 deletions test/parallel/test-fs-fsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fs.open(fileTemp, 'a', 0o777, common.mustCall(function(err, fd) {
assert.ifError(err);
fs.fsync(fd, common.mustCall(function(err) {
assert.ifError(err);
fs.closeSync(fd);
}));
}));
}));
Expand Down
4 changes: 0 additions & 4 deletions test/parallel/test-fs-long-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,3 @@ fs.writeFile(fullPath, 'ok', common.mustCall(function(err) {
assert.ifError(err);
}));
}));

process.on('exit', function() {
fs.unlinkSync(fullPath);
});
4 changes: 2 additions & 2 deletions test/parallel/test-fs-options-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ if (common.canCreateSymLink()) {
{
const fileName = path.resolve(tmpdir.path, 'streams');
fs.WriteStream(fileName, options).once('open', common.mustCall(() => {
fs.ReadStream(fileName, options);
}));
fs.ReadStream(fileName, options).destroy();
})).end();
}
4 changes: 4 additions & 0 deletions test/parallel/test-fs-promises-file-handle-append-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ async function validateAppendBuffer() {
await fileHandle.appendFile(buffer);
const appendedFileData = fs.readFileSync(filePath);
assert.deepStrictEqual(appendedFileData, buffer);

await fileHandle.close();
}

async function validateAppendString() {
Expand All @@ -33,6 +35,8 @@ async function validateAppendString() {
const stringAsBuffer = Buffer.from(string, 'utf8');
const appendedFileData = fs.readFileSync(filePath);
assert.deepStrictEqual(appendedFileData, stringAsBuffer);

await fileHandle.close();
}

validateAppendBuffer()
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-promises-file-handle-chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ async function validateFilePermission() {
await fileHandle.chmod(newPermissions);
const statsAfterMod = fs.statSync(filePath);
assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);

await fileHandle.close();
}

validateFilePermission().then(common.mustCall());
4 changes: 4 additions & 0 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ async function validateRead() {
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);

await fileHandle.close();
}

async function validateEmptyRead() {
Expand All @@ -38,6 +40,8 @@ async function validateEmptyRead() {
fs.closeSync(fd);
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);

await fileHandle.close();
}

async function validateLargeRead() {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async function validateReadFile() {

const readFileData = await fileHandle.readFile();
assert.deepStrictEqual(buffer, readFileData);

await fileHandle.close();
}

async function validateReadFileProc() {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-promises-file-handle-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function validateStat() {
const fileHandle = await open(filePath, 'w+');
const stats = await fileHandle.stat();
assert.ok(stats.mtime instanceof Date);
await fileHandle.close();
}

validateStat()
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-promises-file-handle-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function validateSync() {
const ret = await handle.read(Buffer.alloc(11), 0, 11, 0);
assert.strictEqual(ret.bytesRead, 11);
assert.deepStrictEqual(ret.buffer, buf);
await handle.close();
}

validateSync();
2 changes: 2 additions & 0 deletions test/parallel/test-fs-promises-file-handle-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ async function validateTruncate() {

await fileHandle.truncate(5);
assert.deepStrictEqual((await readFile(filename)).toString(), 'Hello');

await fileHandle.close();
}

validateTruncate().then(common.mustCall());
8 changes: 8 additions & 0 deletions test/parallel/test-fs-promises-file-handle-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ async function validateWrite() {
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);

await fileHandle.close();
}

async function validateEmptyWrite() {
Expand All @@ -32,6 +34,8 @@ async function validateEmptyWrite() {
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);

await fileHandle.close();
}

async function validateNonUint8ArrayWrite() {
Expand All @@ -42,6 +46,8 @@ async function validateNonUint8ArrayWrite() {
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(Buffer.from(buffer, 'utf8'), readFileData);

await fileHandle.close();
}

async function validateNonStringValuesWrite() {
Expand All @@ -55,6 +61,8 @@ async function validateNonStringValuesWrite() {
const readFileData = fs.readFileSync(filePathForHandle);
const expected = ['123', '[object Object]', '[object Map]'].join('');
assert.deepStrictEqual(Buffer.from(expected, 'utf8'), readFileData);

await fileHandle.close();
}

Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ async function validateWriteFile() {
await fileHandle.writeFile(buffer);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);

await fileHandle.close();
}

validateWriteFile()
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-promises-readfile-with-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ async function readFileTest() {

/* readFile() should read from position five, instead of zero. */
assert.deepStrictEqual((await handle.readFile()).toString(), ' World');

await handle.close();
}


Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-promises-writefile-with-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ async function writeFileTest() {

/* New content should be written at position five, instead of zero. */
assert.deepStrictEqual(readFileSync(fn).toString(), 'HelloWorld');

await handle.close();
}


Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async function getHandle(dest) {
{
const handle = await getHandle(dest);
assert.strictEqual(typeof handle, 'object');
await handle.close();
}

// file stats
Expand All @@ -106,6 +107,7 @@ async function getHandle(dest) {

await handle.datasync();
await handle.sync();
await handle.close();
}

// Test fs.read promises when length to read is zero bytes
Expand All @@ -119,6 +121,7 @@ async function getHandle(dest) {
assert.strictEqual(ret.bytesRead, 0);

await unlink(dest);
await handle.close();
}

// Bytes written to file match buffer
Expand All @@ -130,6 +133,7 @@ async function getHandle(dest) {
const ret = await handle.read(Buffer.alloc(bufLen), 0, bufLen, 0);
assert.strictEqual(ret.bytesRead, bufLen);
assert.deepStrictEqual(ret.buffer, buf);
await handle.close();
}

// Truncate file to specified length
Expand All @@ -143,6 +147,7 @@ async function getHandle(dest) {
assert.deepStrictEqual(ret.buffer, buf);
await truncate(dest, 5);
assert.deepStrictEqual((await readFile(dest)).toString(), 'hello');
await handle.close();
}

// Invalid change of ownership
Expand Down Expand Up @@ -181,6 +186,8 @@ async function getHandle(dest) {
message: 'The value of "gid" is out of range. ' +
'It must be >= 0 && < 4294967296. Received -1'
});

await handle.close();
}

// Set modification times
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-readdir-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fs.readdir(readdirDir, {
// Check for correct types when the binding returns unknowns
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;
const oldReaddir = binding.readdir;
process.on('beforeExit', () => { binding.readdir = oldReaddir; });
binding.readdir = common.mustCall((path, encoding, types, req, ctx) => {
if (req) {
const oldCb = req.oncomplete;
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-fs-readfile-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function tempFdSync(callback) {

/* readFileSync() should read from position five, instead of zero. */
assert.deepStrictEqual(fs.readFileSync(fd).toString(), ' World');

fs.closeSync(fd);
}

{
Expand All @@ -89,6 +91,8 @@ function tempFdSync(callback) {
assert.ifError(err);
/* readFile() should read from position five, instead of zero. */
assert.deepStrictEqual(data.toString(), ' World');

fs.closeSync(fd);
}));
}));
}));
Expand Down
4 changes: 0 additions & 4 deletions test/parallel/test-fs-readfile-pipe-large.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,3 @@ exec(cmd, { maxBuffer: 1000000 }, common.mustCall((err, stdout, stderr) => {
);
console.log('ok');
}));

process.on('exit', function() {
fs.unlinkSync(filename);
});
4 changes: 0 additions & 4 deletions test/parallel/test-fs-readfilesync-pipe-large.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,3 @@ exec(
console.log('ok');
})
);

process.on('exit', function() {
fs.unlinkSync(filename);
});
1 change: 1 addition & 0 deletions test/parallel/test-fs-ready-event-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ const writeStream = fs.createWriteStream(writeFile, { autoClose: true });
assert.strictEqual(writeStream.pending, true);
writeStream.on('ready', common.mustCall(() => {
assert.strictEqual(writeStream.pending, false);
writeStream.end();
}));
Loading

0 comments on commit eedff96

Please sign in to comment.