Skip to content

Commit 910a8af

Browse files
test: refactor repl save-load tests
refactor the test/parallel/test-repl-save-load.js file by: - making the tests in the file self-contained (instead of all of them sharing the same REPL instance and constantly calling `.clear` on it) - clearly separating and commenting the various tests to make clearer what is being tested PR-URL: #58715 Reviewed-By: Luigi Pinca <[email protected]>
1 parent d7becc5 commit 910a8af

7 files changed

+289
-154
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
require('../common');
4+
const ArrayStream = require('../common/arraystream');
5+
6+
const assert = require('node:assert');
7+
const fs = require('node:fs');
8+
const repl = require('node:repl');
9+
const path = require('node:path');
10+
11+
const tmpdir = require('../common/tmpdir');
12+
tmpdir.refresh();
13+
14+
// Test for saving a REPL session in editor mode
15+
16+
const input = new ArrayStream();
17+
18+
const replServer = repl.start({
19+
prompt: '',
20+
input,
21+
output: new ArrayStream(),
22+
allowBlockingCompletions: true,
23+
terminal: true,
24+
});
25+
26+
// Some errors are passed to the domain, but do not callback
27+
replServer._domain.on('error', assert.ifError);
28+
29+
input.run(['.editor']);
30+
31+
const commands = [
32+
'function testSave() {',
33+
'return "saved";',
34+
'}',
35+
];
36+
37+
input.run(commands);
38+
39+
replServer.write('', { ctrl: true, name: 'd' });
40+
41+
const filePath = path.resolve(tmpdir.path, 'test.save.js');
42+
43+
input.run([`.save ${filePath}`]);
44+
45+
assert.strictEqual(fs.readFileSync(filePath, 'utf8'),
46+
`${commands.join('\n')}\n`);
47+
48+
replServer.close();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
5+
6+
const assert = require('node:assert');
7+
const repl = require('node:repl');
8+
9+
const tmpdir = require('../common/tmpdir');
10+
tmpdir.refresh();
11+
12+
// Test for the appropriate handling of cases in which REPL saves fail
13+
14+
const input = new ArrayStream();
15+
const output = new ArrayStream();
16+
17+
const replServer = repl.start({
18+
prompt: '',
19+
input,
20+
output,
21+
allowBlockingCompletions: true,
22+
});
23+
24+
// Some errors are passed to the domain, but do not callback
25+
replServer._domain.on('error', assert.ifError);
26+
27+
// NUL (\0) is disallowed in filenames in UNIX-like operating systems and
28+
// Windows so we can use that to test failed saves.
29+
const invalidFilePath = tmpdir.resolve('\0\0\0\0\0');
30+
31+
output.write = common.mustCall(function(data) {
32+
assert.strictEqual(data, `Failed to save: ${invalidFilePath}\n`);
33+
output.write = () => {};
34+
});
35+
36+
input.run([`.save ${invalidFilePath}`]);
37+
38+
replServer.close();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
5+
6+
const assert = require('node:assert');
7+
const repl = require('node:repl');
8+
9+
const tmpdir = require('../common/tmpdir');
10+
tmpdir.refresh();
11+
12+
// Tests that an appropriate error is displayed if the user tries to load a directory instead of a file
13+
14+
const input = new ArrayStream();
15+
const output = new ArrayStream();
16+
17+
const replServer = repl.start({
18+
prompt: '',
19+
input,
20+
output,
21+
allowBlockingCompletions: true,
22+
});
23+
24+
// Some errors are passed to the domain, but do not callback
25+
replServer._domain.on('error', assert.ifError);
26+
27+
const dirPath = tmpdir.path;
28+
29+
output.write = common.mustCall(function(data) {
30+
assert.strictEqual(data, `Failed to load: ${dirPath} is not a valid file\n`);
31+
output.write = () => {};
32+
});
33+
34+
input.run([`.load ${dirPath}`]);
35+
36+
replServer.close();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
5+
6+
const assert = require('node:assert');
7+
const repl = require('node:repl');
8+
9+
const tmpdir = require('../common/tmpdir');
10+
tmpdir.refresh();
11+
12+
// Tests that an appropriate error is displayed if the user tries to load a non existent file
13+
14+
const input = new ArrayStream();
15+
const output = new ArrayStream();
16+
17+
const replServer = repl.start({
18+
prompt: '',
19+
input,
20+
output,
21+
allowBlockingCompletions: true,
22+
});
23+
24+
// Some errors are passed to the domain, but do not callback
25+
replServer._domain.on('error', assert.ifError);
26+
27+
const filePath = tmpdir.resolve('file.does.not.exist');
28+
29+
output.write = common.mustCall(function(data) {
30+
assert.strictEqual(data, `Failed to load: ${filePath}\n`);
31+
output.write = () => {};
32+
});
33+
34+
input.run([`.load ${filePath}`]);
35+
36+
replServer.close();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
5+
6+
const assert = require('node:assert');
7+
const repl = require('node:repl');
8+
9+
const tmpdir = require('../common/tmpdir');
10+
tmpdir.refresh();
11+
12+
// Tests that an appropriate error is displayed if .load is called without a filename
13+
14+
const input = new ArrayStream();
15+
const output = new ArrayStream();
16+
17+
const replServer = repl.start({
18+
prompt: '',
19+
input,
20+
output,
21+
allowBlockingCompletions: true,
22+
});
23+
24+
// Some errors are passed to the domain, but do not callback
25+
replServer._domain.on('error', assert.ifError);
26+
27+
output.write = common.mustCall(function(data) {
28+
assert.strictEqual(data, 'The "file" argument must be specified\n');
29+
output.write = () => {};
30+
});
31+
32+
input.run(['.load']);
33+
34+
replServer.close();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
5+
6+
const assert = require('node:assert');
7+
const repl = require('node:repl');
8+
9+
const tmpdir = require('../common/tmpdir');
10+
tmpdir.refresh();
11+
12+
// Tests that an appropriate error is displayed if .save is called without a filename
13+
14+
const input = new ArrayStream();
15+
const output = new ArrayStream();
16+
17+
const replServer = repl.start({
18+
prompt: '',
19+
input,
20+
output,
21+
allowBlockingCompletions: true,
22+
});
23+
24+
// Some errors are passed to the domain, but do not callback
25+
replServer._domain.on('error', assert.ifError);
26+
27+
output.write = common.mustCall(function(data) {
28+
assert.strictEqual(data, 'The "file" argument must be specified\n');
29+
output.write = () => {};
30+
});
31+
32+
input.run(['.save']);
33+
34+
replServer.close();

0 commit comments

Comments
 (0)