Skip to content
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

feat(): readyNotice as function #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ function initCtx(options) {
|| !isFileFound(targetSvc) && options.svc + ' is not found on disk'
|| !options.logPath && 'options.logPath is expected to be a path'
|| 'string' != typeof options.logPath && 'options.logPath is expected to be a path'
|| !options.readyNotice && 'options.readyNotice must be a string'
|| 'string' != typeof options.readyNotice && 'options.readyNotice must be a string'
|| !options.readyNotice && 'options.readyNotice must be either a string or a function'
|| ['string', 'function']
.indexOf(typeof options.readyNotice) && 'options.readyNotice must be either a string or a function'
|| !Array.isArray(options.args) && 'options.args must be an array'
|| 'number' != typeof options.timeout && 'options.timeout must be a number'
|| 'number' != typeof options.slow && 'options.slow must be a number'
Expand Down Expand Up @@ -183,7 +184,7 @@ function initCtx(options) {
, " - logPath - string, optional - path to logfile. default: './e2e.log'"
, " - timeout - integer, optional - timeout for server setup, default: " + defaults.timeout
, " - slow - integer, optional - slow bar indicator for server setup, default: " + defaults.timeout
, " - readyNotice - string, optional - message to expect on service output that"
, " - readyNotice - string or function, optional - message to expect on service output that"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace lines 187 and 188 with:

        , " - readyNotice - optional string or a function,"
        , "    - string:  message to expect on service output that indicates the service is ready."
        , "    - function: is passed the usual err-1st callback to call when the service is ready, or to pass an error when such error occurs."
        , "       may also return a Promise instread"
        , "    default: the string '" + defaults.readyNotice + "'"

, " indicates the service is ready. default: " + defaults.readyNotice
, " - args - array, optional, argumnets to be concatenated to the running command"
, " - term_code - string, optional, the termination message to send to the child, default: " + defaults.term_code
Expand Down Expand Up @@ -235,10 +236,11 @@ function initCtx(options) {
@param {string} ctx.sut - system under test - path to the script that
runs the target server
@param {string} ctx.logPath - path to log file
@param {string} ctx.readyNotice - output line expected on stdout of
the started target service that indicates that the service is running
@param {string|function} ctx.readyNotice - either output line expected on stdout of
the started target service that indicates that the service is running or a function that will
check that the service is up
@param {mocha.Test} test - the test context that implements .timetout(n), .slow(n) ....
@param {callback} done - callback
@param {function} done - callback
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not any function. an err-1st callback - hence callback.

@returns {undefined}
*/
function setup(ctx, test, done) {
Expand Down Expand Up @@ -284,14 +286,21 @@ function setup(ctx, test, done) {
child.stdout.on('data', (data) => {
data = data.toString()
writeLog(data)

if (~data.indexOf(ctx.readyNotice)) {
ctx.console.log('service started: %s', ctx.args.join(' '))
done()
done = null
}
})

if ('function' == typeof ctx.readyNotice) {
ctx.readyNotice(done)
done = null
} else {
child.stdout.on('data', (data) => {
if (done && ~data.indexOf(ctx.readyNotice)) {
ctx.console.log('service started: %s', ctx.args.join(' '))
done()
done = null
}
})
}

Copy link
Owner

@osher osher Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ('string' == typeof ctx.readyNotice) {
    const readyNotice = ctx.readyNotice
    ctx.readyNotice = () => new Promise(done => child.stdout.on('data', (data) => ~data.indexOf(readyNotice) && done()))
}

var promise = ctx.readyNotice((err) => {
    if (!done) return
    if (!err) ctx.console.log('service started: %s', ctx.args.join(' '))
    done(err)
    done = null
})
promise && promise.then && promise.catch((err) => err || new Error('ready hook rejection')).then((err) => {
    done(err instasnceof Error ? err : null)
    done = null
})

child.on('exit', (e) => {
child.exitted = true
ctx.console.log('\n\nservice termination ended', e || 'OK')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "e2e-helper",
"version": "0.9.4",
"version": "0.10.0",
Copy link
Owner

@osher osher Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its fully compatible with prev versions.
Since this version starts with 0, by semver rules the minor is acting like a major and (~ and ^ behave the same)
leave version bumping to me

"description": "end-to-end test helper, with facilitators for mocha",
"main": "lib",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions test/lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ module.exports =
{ svc: 'test/fixture/svc'
, readyNotice: null
}
, m: 'options.readyNotice must be a string'
, m: 'options.readyNotice must be either a string or a function'
}
, 'options.readyNotice not a string':
{ o:
{ svc: 'test/fixture/svc'
, readyNotice: -0.431
}
, m: 'options.readyNotice must be a string'
, m: 'options.readyNotice must be either a string or a function'
}
, 'options.args not an array':
{ o:
Expand Down