Skip to content

Commit 098cf74

Browse files
refackBridgeAR
authored andcommitted
test: regression test tmpdir
PR-URL: #28035 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
1 parent e08a98f commit 098cf74

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

test/parallel/test-common.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
const common = require('../common');
2424
const hijackstdio = require('../common/hijackstdio');
2525
const fixtures = require('../common/fixtures');
26+
const tmpdir = require('../common/tmpdir');
2627
const assert = require('assert');
2728
const { execFile } = require('child_process');
29+
const { writeFileSync, existsSync } = require('fs');
30+
const { join } = require('path');
2831

2932
// Test for leaked global detection
3033
{
@@ -124,25 +127,38 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
124127
assert.strictEqual(originalWrite, stream.write);
125128
});
126129

127-
// hijackStderr and hijackStdout again
128-
// for console
129-
[[ 'err', 'error' ], [ 'out', 'log' ]].forEach(([ type, method ]) => {
130-
hijackstdio[`hijackStd${type}`](common.mustCall(function(data) {
131-
assert.strictEqual(data, 'test\n');
130+
// Test `tmpdir`.
131+
{
132+
tmpdir.refresh();
133+
assert.ok(/\.tmp\.\d+/.test(tmpdir.path));
134+
const sentinelPath = join(tmpdir.path, 'gaga');
135+
writeFileSync(sentinelPath, 'googoo');
136+
tmpdir.refresh();
137+
assert.strictEqual(existsSync(tmpdir.path), true);
138+
assert.strictEqual(existsSync(sentinelPath), false);
139+
}
132140

133-
// throw an error
134-
throw new Error(`console ${type} error`);
135-
}));
141+
// hijackStderr and hijackStdout again for console
142+
// Must be last, since it uses `process.on('uncaughtException')`
143+
{
144+
[['err', 'error'], ['out', 'log']].forEach(([type, method]) => {
145+
hijackstdio[`hijackStd${type}`](common.mustCall(function(data) {
146+
assert.strictEqual(data, 'test\n');
136147

137-
console[method]('test');
138-
hijackstdio[`restoreStd${type}`]();
139-
});
148+
// throw an error
149+
throw new Error(`console ${type} error`);
150+
}));
151+
152+
console[method]('test');
153+
hijackstdio[`restoreStd${type}`]();
154+
});
140155

141-
let uncaughtTimes = 0;
142-
process.on('uncaughtException', common.mustCallAtLeast(function(e) {
143-
assert.strictEqual(uncaughtTimes < 2, true);
144-
assert.strictEqual(e instanceof Error, true);
145-
assert.strictEqual(
146-
e.message,
147-
`console ${([ 'err', 'out' ])[uncaughtTimes++]} error`);
148-
}, 2));
156+
let uncaughtTimes = 0;
157+
process.on('uncaughtException', common.mustCallAtLeast(function(e) {
158+
assert.strictEqual(uncaughtTimes < 2, true);
159+
assert.strictEqual(e instanceof Error, true);
160+
assert.strictEqual(
161+
e.message,
162+
`console ${(['err', 'out'])[uncaughtTimes++]} error`);
163+
}, 2));
164+
} // End of "Must be last".

0 commit comments

Comments
 (0)