Skip to content

Commit

Permalink
test: only refresh tmpDir for tests that need it
Browse files Browse the repository at this point in the history
Expose `common.refreshTmpDir()` and only call it
for tests that use common.tmpDir or common.PIPE.

A positive side effect is the removal of a code
smell where child processes were detected by the
presence of `.send()`. Now each process can decide
for itself if it needs to refresh tmpDir.

PR-URL: #1954
Reviewed-By: Rod Vagg <[email protected]>
Reviewed-By: Johan Bergström <[email protected]>
  • Loading branch information
Trott committed Jun 14, 2015
1 parent 88d7904 commit 7c79490
Show file tree
Hide file tree
Showing 53 changed files with 92 additions and 77 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/fixtures
test/**/node_modules
test/parallel/test-fs-non-number-arguments-throw.js
test/disabled
test/tmp*/
22 changes: 9 additions & 13 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,17 @@ function rmdirSync(p, originalEr) {
}
}

function refreshTmpDir() {
if (!process.send) { // Not a child process
try {
rimrafSync(exports.tmpDir);
} catch (e) {
}
exports.refreshTmpDir = function() {
try {
rimrafSync(exports.tmpDir);
} catch (e) {
}

try {
fs.mkdirSync(exports.tmpDir);
} catch (e) {
}
try {
fs.mkdirSync(exports.tmpDir);
} catch (e) {
}
}
};

if (process.env.TEST_THREAD_ID) {
// Distribute ports in parallel tests
Expand All @@ -74,8 +72,6 @@ if (process.env.TEST_THREAD_ID) {
}
exports.tmpDir = path.join(exports.testDir, exports.tmpDirName);

refreshTmpDir();

var opensslCli = null;
var inFreeBSDJail = null;
var localhostIPv4 = null;
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/listen-on-socket-and-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var common = require('../common');
var net = require('net');

common.refreshTmpDir();

var server = net.createServer().listen(common.PIPE, function() {
console.log('child listening');
process.send('listening');
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-child-process-fork-exec-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (process.env.FORK) {
process.exit();
}
else {
common.refreshTmpDir();
try {
fs.unlinkSync(copyPath);
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-cluster-http-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var cluster = require('cluster');
var http = require('http');

if (cluster.isMaster) {
common.refreshTmpDir();
var ok = false;
var worker = cluster.fork();
worker.on('message', function(msg) {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-cwd-enoent-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if (process.platform === 'sunos' || process.platform === 'win32') {
}

var dirname = common.tmpDir + '/cwd-does-not-exist-' + process.pid;
common.refreshTmpDir();
fs.mkdirSync(dirname);
process.chdir(dirname);
fs.rmdirSync(dirname);
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-cwd-enoent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if (process.platform === 'sunos' || process.platform === 'win32') {
}

var dirname = common.tmpDir + '/cwd-does-not-exist-' + process.pid;
common.refreshTmpDir();
fs.mkdirSync(dirname);
process.chdir(dirname);
fs.rmdirSync(dirname);
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-file-write-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var assert = require('assert');
var path = require('path');
var fs = require('fs');
var fn = path.join(common.tmpDir, 'write.txt');
common.refreshTmpDir();
var file = fs.createWriteStream(fn, {
highWaterMark: 10
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-file-write-stream2.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function removeTestFile() {
}


removeTestFile();
common.refreshTmpDir();

// drain at 0, return false at 10.
file = fs.createWriteStream(filepath, {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-file-write-stream3.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function removeTestFile() {
}


removeTestFile();
common.refreshTmpDir();


function run_test_1() {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var createFileWithPerms = function(file, mode) {
fs.chmodSync(file, mode);
};

common.refreshTmpDir();
createFileWithPerms(readOnlyFile, 0o444);
createFileWithPerms(readWriteFile, 0o666);

Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-append-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var data = '南越国是前203年至前111年存在于岭南地区的一个国
'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';

common.refreshTmpDir();

// test that empty file will be created and have content added
var filename = join(common.tmpDir, 'append-sync.txt');

Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-append-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var s = '南越国是前203年至前111年存在于岭南地区的一个国家

var ncallbacks = 0;

common.refreshTmpDir();

// test that empty file will be created and have content added
fs.appendFile(filename, s, function(e) {
if (e) throw e;
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ fs.open(file2, 'a', function(err, fd) {
if (fs.lchmod) {
var link = path.join(common.tmpDir, 'symbolic-link');

try {
fs.unlinkSync(link);
} catch (er) {}
common.refreshTmpDir();
fs.symlinkSync(file2, link);

fs.lchmod(link, mode_async, function(err) {
Expand Down
7 changes: 1 addition & 6 deletions test/parallel/test-fs-long-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ var fileNameLen = Math.max(260 - common.tmpDir.length - 1, 1);
var fileName = path.join(common.tmpDir, new Array(fileNameLen + 1).join('x'));
var fullPath = path.resolve(fileName);

try {
fs.unlinkSync(fullPath);
}
catch (e) {
// Ignore.
}
common.refreshTmpDir();

console.log({
filenameLength: fileName.length,
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function unlink(pathname) {
}
}

common.refreshTmpDir();

(function() {
var ncalls = 0;
var pathname = common.tmpDir + '/test1';
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-read-stream-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var input = 'hello world';
var output = '';
var fd, stream;

common.refreshTmpDir();
fs.writeFileSync(file, input);
fd = fs.openSync(file, 'r');

Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-readfile-pipe-large.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var fs = require('fs');

var filename = path.join(common.tmpDir, '/readfile_pipe_large_test.txt');
var dataExpected = new Array(1000000).join('a');
common.refreshTmpDir();
fs.writeFileSync(filename, dataExpected);

if (process.argv[2] === 'child') {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-readfilesync-pipe-large.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var fs = require('fs');

var filename = path.join(common.tmpDir, '/readfilesync_pipe_large_test.txt');
var dataExpected = new Array(1000000).join('a');
common.refreshTmpDir();
fs.writeFileSync(filename, dataExpected);

if (process.argv[2] === 'child') {
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-fs-realpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var async_completed = 0, async_expected = 0, unlink = [];
var isWindows = process.platform === 'win32';
var skipSymlinks = false;

common.refreshTmpDir();

var root = '/';
if (isWindows) {
// something like "C:\\"
Expand Down Expand Up @@ -575,9 +577,6 @@ function runTest() {
var tmpDirs = ['cycles', 'cycles/folder'];
tmpDirs.forEach(function(t) {
t = tmp(t);
var s;
try { s = fs.statSync(t); } catch (ex) {}
if (s) return;
fs.mkdirSync(t, 0o700);
});
fs.writeFileSync(tmp('cycles/root.js'), "console.error('roooot!');");
Expand Down
6 changes: 1 addition & 5 deletions test/parallel/test-fs-sir-writes-alot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ var join = require('path').join;

var filename = join(common.tmpDir, 'out.txt');

try {
fs.unlinkSync(filename);
} catch (e) {
// might not exist, that's okay.
}
common.refreshTmpDir();

var fd = fs.openSync(filename, 'w');

Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-stream-double-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var common = require('../common');
var assert = require('assert');
var fs = require('fs');

common.refreshTmpDir();

test1(fs.createReadStream(__filename));
test2(fs.createReadStream(__filename));
test3(fs.createReadStream(__filename));
Expand Down
5 changes: 1 addition & 4 deletions test/parallel/test-fs-symlink-dir-junction-relative.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ var linkPath2 = path.join(common.tmpDir, 'junction2');
var linkTarget = path.join(common.fixturesDir);
var linkData = '../fixtures';

// Prepare.
try { fs.mkdirSync(common.tmpDir); } catch (e) {}
try { fs.unlinkSync(linkPath1); } catch (e) {}
try { fs.unlinkSync(linkPath2); } catch (e) {}
common.refreshTmpDir();

// Test fs.symlink()
fs.symlink(linkData, linkPath1, 'junction', function(err) {
Expand Down
5 changes: 1 addition & 4 deletions test/parallel/test-fs-symlink-dir-junction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ var expected_tests = 4;
var linkData = path.join(common.fixturesDir, 'cycles/');
var linkPath = path.join(common.tmpDir, 'cycles_link');

// Delete previously created link
try {
fs.unlinkSync(linkPath);
} catch (e) {}
common.refreshTmpDir();

console.log('linkData: ' + linkData);
console.log('linkPath: ' + linkPath);
Expand Down
12 changes: 2 additions & 10 deletions test/parallel/test-fs-symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ var expected_tests = 2;

var is_windows = process.platform === 'win32';

common.refreshTmpDir();

var runtest = function(skip_symlinks) {
if (!skip_symlinks) {
// test creating and reading symbolic link
var linkData = path.join(common.fixturesDir, '/cycles/root.js');
var linkPath = path.join(common.tmpDir, 'symlink1.js');

// Delete previously created link
try {
fs.unlinkSync(linkPath);
} catch (e) {}

fs.symlink(linkData, linkPath, function(err) {
if (err) throw err;
console.log('symlink done');
Expand All @@ -36,11 +33,6 @@ var runtest = function(skip_symlinks) {
var srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
var dstPath = path.join(common.tmpDir, 'link1.js');

// Delete previously created link
try {
fs.unlinkSync(dstPath);
} catch (e) {}

fs.link(srcPath, dstPath, function(err) {
if (err) throw err;
console.log('hard link done');
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-truncate-GH-6233.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var fs = require('fs');

var filename = common.tmpDir + '/truncate-file.txt';

common.refreshTmpDir();

// Synchronous test.
(function() {
fs.writeFileSync(filename, '0123456789');
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-truncate-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ var assert = require('assert');
var path = require('path');
var fs = require('fs');
var tmp = common.tmpDir;
if (!fs.existsSync(tmp))
fs.mkdirSync(tmp);
common.refreshTmpDir();
var filename = path.resolve(tmp, 'truncate-file.txt');

var success = 0;
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var filename = path.resolve(tmp, 'truncate-file.txt');
var data = new Buffer(1024 * 16);
data.fill('x');

common.refreshTmpDir();

var stat;

// truncateSync
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-write-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var path = require('path'),
writeCalled = 0;


common.refreshTmpDir();

fs.open(filename, 'w', 0o644, function(err, fd) {
openCalled++;
if (err) throw err;
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-write-file-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var data = [

data = data.join('\n');

common.refreshTmpDir();

var buf = new Buffer(data, 'base64');
fs.writeFileSync(join(common.tmpDir, 'test.jpg'), buf);

Expand Down
20 changes: 2 additions & 18 deletions test/parallel/test-fs-write-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ if (isWindows) {
mode = 0o755;
}

common.refreshTmpDir();

// Test writeFileSync
var file1 = path.join(common.tmpDir, 'testWriteFileSync.txt');
removeFile(file1);

fs.writeFileSync(file1, '123', {mode: mode});

Expand All @@ -37,11 +38,8 @@ assert.equal('123', content);

assert.equal(mode, fs.statSync(file1).mode & 0o777);

removeFile(file1);

// Test appendFileSync
var file2 = path.join(common.tmpDir, 'testAppendFileSync.txt');
removeFile(file2);

fs.appendFileSync(file2, 'abc', {mode: mode});

Expand All @@ -50,23 +48,9 @@ assert.equal('abc', content);

assert.equal(mode, fs.statSync(file2).mode & mode);

removeFile(file2);

// Verify that all opened files were closed.
assert.equal(0, openCount);

// Removes a file if it exists.
function removeFile(file) {
try {
if (isWindows)
fs.chmodSync(file, 0o666);
fs.unlinkSync(file);
} catch (err) {
if (err && err.code !== 'ENOENT')
throw err;
}
}

function openSync() {
openCount++;
return fs._openSync.apply(fs, arguments);
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-write-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var assert = require('assert');
var fs = require('fs');
var join = require('path').join;

common.refreshTmpDir();

var filename = join(common.tmpDir, 'test.txt');

common.error('writing to ' + filename);
Expand Down
Loading

0 comments on commit 7c79490

Please sign in to comment.