-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Extract watchRun
into separate module
#3930
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict'; | ||
|
||
juergba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const utils = require('../utils'); | ||
const Context = require('../context'); | ||
const Mocha = require('../mocha'); | ||
|
||
/** | ||
* Exports the `watchRun` function that runs mocha in "watch" mode. | ||
* @see module:lib/cli/run-helpers | ||
* @module | ||
* @private | ||
*/ | ||
|
||
/** | ||
* Run Mocha in "watch" mode | ||
* @param {Mocha} mocha - Mocha instance | ||
* @param {Object} opts - Options | ||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param {string[]} opts.extension - List of extensions to watch | ||
* @param {string|RegExp} opts.grep - Grep for test titles | ||
* @param {string} opts.ui - User interface | ||
* @param {string[]} opts.files - Array of test files | ||
* @private | ||
*/ | ||
module.exports = (mocha, {extension, grep, ui, files}) => { | ||
let runner; | ||
|
||
console.log(); | ||
hideCursor(); | ||
process.on('SIGINT', () => { | ||
showCursor(); | ||
console.log('\n'); | ||
// By UNIX/Posix convention this indicates that the process was | ||
// killed by SIGINT which has portable number 2. | ||
process.exit(128 + 2); | ||
}); | ||
|
||
const watchFiles = utils.files(process.cwd(), extension); | ||
let runAgain = false; | ||
|
||
const loadAndRun = () => { | ||
try { | ||
mocha.files = files; | ||
runAgain = false; | ||
runner = mocha.run(() => { | ||
runner = null; | ||
if (runAgain) { | ||
rerun(); | ||
} | ||
}); | ||
} catch (e) { | ||
console.log(e.stack); | ||
} | ||
}; | ||
|
||
const purge = () => { | ||
watchFiles.forEach(Mocha.unloadFile); | ||
}; | ||
|
||
loadAndRun(); | ||
|
||
const rerun = () => { | ||
purge(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would think there should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d like to avoid changing behavior in this PR. It is also not clear to me which behavior we exactly want. |
||
eraseLine(); | ||
if (!grep) { | ||
mocha.grep(null); | ||
} | ||
mocha.suite = mocha.suite.clone(); | ||
mocha.suite.ctx = new Context(); | ||
mocha.ui(ui); | ||
loadAndRun(); | ||
}; | ||
|
||
utils.watch(watchFiles, () => { | ||
runAgain = true; | ||
if (runner) { | ||
runner.abort(); | ||
} else { | ||
rerun(); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* Hide the cursor. | ||
* @ignore | ||
* @private | ||
*/ | ||
const hideCursor = () => { | ||
process.stdout.write('\u001b[?25l'); | ||
}; | ||
|
||
/** | ||
* Show the cursor. | ||
* @ignore | ||
* @private | ||
*/ | ||
const showCursor = () => { | ||
process.stdout.write('\u001b[?25h'); | ||
}; | ||
|
||
/** | ||
* Erases the line on stdout | ||
* @private | ||
*/ | ||
const eraseLine = () => { | ||
process.stdout.write('\u001b[2K'); | ||
}; |
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.
Why not just replace the
exports
here, eliminating "diff" lines?