-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
test: add tests for REPL custom evals #57691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aduh95
merged 4 commits into
nodejs:main
from
dario-piotrowicz:dario/test/repl-custom-eval
Apr 7, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const ArrayStream = require('../common/arraystream'); | ||
const assert = require('assert'); | ||
const { describe, it } = require('node:test'); | ||
|
||
const repl = require('repl'); | ||
|
||
describe('repl with custom eval', () => { | ||
it('uses the custom eval function as expected', () => { | ||
const output = getReplOutput('Convert this to upper case', { | ||
terminal: true, | ||
eval: (code, _ctx, _replRes, cb) => cb(null, code.toUpperCase()), | ||
}); | ||
assert.match( | ||
output, | ||
/Convert this to upper case\r\n'CONVERT THIS TO UPPER CASE\\n'/ | ||
); | ||
}); | ||
|
||
it('surfaces errors as expected', () => { | ||
const output = getReplOutput('Convert this to upper case', { | ||
terminal: true, | ||
eval: (_code, _ctx, _replRes, cb) => cb(new Error('Testing Error')), | ||
}); | ||
assert.match(output, /Uncaught Error: Testing Error\n/); | ||
}); | ||
|
||
it('provides a repl context to the eval callback', async () => { | ||
const context = await new Promise((resolve) => { | ||
const r = repl.start({ | ||
eval: (_cmd, context) => resolve(context), | ||
}); | ||
r.context = { foo: 'bar' }; | ||
r.write('\n.exit\n'); | ||
}); | ||
assert.strictEqual(context.foo, 'bar'); | ||
}); | ||
|
||
it('provides a global context to the eval callback', async () => { | ||
const context = await new Promise((resolve) => { | ||
const r = repl.start({ | ||
useGlobal: true, | ||
eval: (_cmd, context) => resolve(context), | ||
}); | ||
global.foo = 'global_bar'; | ||
r.write('\n.exit\n'); | ||
}); | ||
|
||
assert.strictEqual(context.foo, 'global_bar'); | ||
delete global.foo; | ||
}); | ||
|
||
it('does not access the global context if `useGlobal` is false', async () => { | ||
const context = await new Promise((resolve) => { | ||
const r = repl.start({ | ||
useGlobal: false, | ||
eval: (_cmd, context) => resolve(context), | ||
}); | ||
global.foo = 'global_bar'; | ||
r.write('\n.exit\n'); | ||
}); | ||
|
||
assert.notStrictEqual(context.foo, 'global_bar'); | ||
delete global.foo; | ||
}); | ||
|
||
/** | ||
* Default preprocessor transforms | ||
* function f() {} to | ||
* var f = function f() {} | ||
* This test ensures that original input is preserved. | ||
* Reference: https://github.com/nodejs/node/issues/9743 | ||
*/ | ||
it('preserves the original input', async () => { | ||
const cmd = await new Promise((resolve) => { | ||
const r = repl.start({ | ||
eval: (cmd) => resolve(cmd), | ||
}); | ||
r.write('function f() {}\n.exit\n'); | ||
}); | ||
assert.strictEqual(cmd, 'function f() {}\n'); | ||
}); | ||
|
||
it("doesn't show previews by default", () => { | ||
const input = "'Hello custom' + ' eval World!'"; | ||
const output = getReplOutput(input, { | ||
terminal: true, | ||
eval: (code, _ctx, _replRes, cb) => cb(null, eval(code)), | ||
}, false); | ||
assert.strictEqual(output, input); | ||
assert.doesNotMatch(output, /Hello custom eval World!/); | ||
}); | ||
|
||
it('does show previews if `preview` is set to `true`', () => { | ||
const input = "'Hello custom' + ' eval World!'"; | ||
const output = getReplOutput(input, { | ||
terminal: true, | ||
eval: (code, _ctx, _replRes, cb) => cb(null, eval(code)), | ||
preview: true, | ||
}, false); | ||
|
||
const escapedInput = input.replace(/\+/g, '\\+'); | ||
dario-piotrowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.match( | ||
output, | ||
new RegExp(`${escapedInput}\n// 'Hello custom eval World!'`) | ||
); | ||
}); | ||
}); | ||
|
||
function getReplOutput(input, replOptions, run = true) { | ||
dario-piotrowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const inputStream = new ArrayStream(); | ||
const outputStream = new ArrayStream(); | ||
|
||
repl.start({ | ||
input: inputStream, | ||
output: outputStream, | ||
...replOptions, | ||
}); | ||
|
||
let output = ''; | ||
outputStream.write = (chunk) => (output += chunk); | ||
|
||
inputStream.emit('data', input); | ||
|
||
if (run) { | ||
inputStream.run(['']); | ||
} | ||
|
||
return output; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably run them in parallel when running the file standalone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 🙂
But is
{ concurrency: !process.env.TEST_PARALLEL }
correct?a falsy concurrency value means that the tests won't be run in parallel right? so we are saying that if
TEST_PARALLEL
is truthy then the tests here need to be run sequentially? or am I misunderstanding?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TEST_PARALLEL
is set by Python runner when it's running test in parallel. When it is, we don't want the Node.js one to parallelize on its own, otherwise it could oversubscribe the machine – although I'm not sure if that's the case, I originally thoughtgetReplOutput
was spawning a subprocess, but if that's not the case, it shouldn't really matterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation @aduh95 🙏 (it is still a bit murky to me but I do get the gist of it 😅)
Regarding
getReplOutput
no, I am quite sure that it doesn't spawn a subprocess, since it simply starts aREPLServer
which does run in the same process (and it usesrunInContext
andrunInThisContext
to evalutate code)So given the above, are you happy with the current version of the code? 🙂
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it runs the test serially, so no I'm not happy with it unless we have a good reason to do that 😅 Would
concurrency: true
work? If so, we should use it, if not, we should setconcurrency: false
explicitly with a comment explaining why – but please treat this as a nit and feel free to ignore if you preferThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah okok I see, no I'm totally happy to add
concurrency: true
🙂👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
concurrency: true
added (63034cb) 🙂Also
concurrency: true
was actually causing some failures because the fact that I was using the same global variablefoo
in two different tests so I fixed that, this also helped me notice that theuseGlobale: false
test could be improved to make sure global variables are inherited by the REPL (which is a documented behavior: https://nodejs.org/api/repl.html#global-and-local-scope), thanks for that! 😄 🫶Please have a look and let me know if things look good to you now