Skip to content

Commit

Permalink
Merge pull request #1213 from apiaryio/honzajavorek/separate-logging-…
Browse files Browse the repository at this point in the history
…from-reporters

Separate "logging" from "output"
  • Loading branch information
honzajavorek authored Feb 7, 2019
2 parents 3b13781 + c8cf96f commit 589c7eb
Show file tree
Hide file tree
Showing 45 changed files with 1,223 additions and 681 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A clear and concise description of what you expected to happen.
( paste your output here )
```

**Does `dredd --level=debug` uncover something?**
**Does `dredd --loglevel=debug` uncover something?**
If you run Dredd with debugging output, do you see any interesting information relevant to the bug?

**Can you send us failing test in a Pull Request?**
Expand Down
2 changes: 0 additions & 2 deletions docs/hooks/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ You can get a list of all transaction names available in your API description do
:emphasize-lines: 3, 5
$ dredd ./blog.apib http://127.0.0.1 --names
info: Beginning Dredd testing...
info: Articles > List articles
skip: GET (200) /articles
info: Articles > Publish an article
Expand All @@ -156,7 +155,6 @@ You can get a list of all transaction names available in your API description do
:emphasize-lines: 3, 5
$ dredd ./blog.yaml http://127.0.0.1 --names
info: Beginning Dredd testing...
info: Articles > List articles > 200 > application/json
skip: GET (200) /articles
info: Articles > Publish an article > 201 > application/json
Expand Down
4 changes: 1 addition & 3 deletions docs/how-to-guides.rst
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ Dredd will detect two HTTP transaction examples and will compile following trans
::

$ dredd api-description.apib http://127.0.0.1 --names
info: Beginning Dredd testing...
info: Resource > Update Resource > Example 1
info: Resource > Update Resource > Example 2

Expand Down Expand Up @@ -687,7 +686,6 @@ Command-line output of complex HTTP responses and expectations can be hard to re

$ dredd apiary.apib http://127.0.0.1 --reporter=apiary
warn: Apiary API Key or API Project Subdomain were not provided. Configure Dredd to be able to save test reports alongside your Apiary API project: https://dredd.org/en/latest/how-to-guides/#using-apiary-reporter-and-apiary-tests
info: Beginning Dredd testing...
pass: DELETE /honey duration: 884ms
complete: 1 passing, 0 failing, 0 errors, 0 skipped, 1 total
complete: Tests took 1631ms
Expand All @@ -714,7 +712,7 @@ As you can see, the parameters go like this:

::

$ dredd -c apiaryApiKey:<Apiary API Key> -c apiaryApiName:<API Project Subdomain>
$ dredd -j apiaryApiKey:<Apiary API Key> -j apiaryApiName:<API Project Subdomain>

In addition to using parameters and ``dredd.yml``, you can also use environment variables:

Expand Down
4 changes: 1 addition & 3 deletions docs/usage-cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ See below how sample configuration file could look like. The structure is the sa
inline-errors: false
details: false
method: []
level: info
timestamp: false
silent: false
loglevel: warning
path: []
blueprint: api-description.apib
endpoint: "http://127.0.0.1:3000"
Expand Down
4 changes: 1 addition & 3 deletions docs/usage-js.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Let’s have a look at an example configuration first. (Please also see the :ref
'dry-run': false, // Boolean, do not run any real HTTP transaction
'names': false, // Boolean, Print Transaction names and finish, similar to dry-run
'level': 'info', // String, log-level (info, silly, debug, verbose, ...)
'silent': false, // Boolean, Silences all logging output
'loglevel': 'warning', // String, logging level (debug, warning, error, silent)
'only': [], // Array of Strings, run only transaction that match these names
Expand All @@ -58,7 +57,6 @@ Let’s have a look at an example configuration first. (Please also see the :ref
'require': null, // String, When using nodejs hooks, require the given module before executing hooks
'color': true,
'timestamp': false
},
'emitter': EventEmitterInstance, // optional - listen to test progress, your own instance of EventEmitter
Expand Down
86 changes: 40 additions & 46 deletions lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ class CLI {
constructor(options = {}, cb) {
this.cb = cb;
this.finished = false;
({ exit: this.exit, custom: this.custom } = options);
this.exit = options.exit;
this.custom = options.custom || {};

this.setExitOrCallback();

if (!this.custom) { this.custom = {}; }

if (!this.custom.cwd || typeof this.custom.cwd !== 'string') {
this.custom.cwd = process.cwd();
}
Expand Down Expand Up @@ -52,20 +51,20 @@ Example:
.wrap(80);

this.argv = this.optimist.argv;
this.argv = applyLoggingOptions(this.argv);
applyLoggingOptions(this.argv);
}

// Gracefully terminate server
stopServer(callback) {
if (!this.serverProcess || !this.serverProcess.spawned) {
logger.verbose('No backend server process to terminate.');
logger.debug('No backend server process to terminate.');
return callback();
}
if (this.serverProcess.terminated) {
logger.debug('The backend server process has already terminated');
return callback();
}
logger.verbose('Terminating backend server process, PID', this.serverProcess.pid);
logger.debug('Terminating backend server process, PID', this.serverProcess.pid);
this.serverProcess.terminate({ force: true });
this.serverProcess.on('exit', () => callback());
}
Expand All @@ -80,28 +79,25 @@ Example:

if (this.exit) {
this._processExit = (exitStatus) => {
logger.verbose(`Exiting Dredd process with status '${exitStatus}'.`);
logger.debug('Using configured custom exit() method to terminate the Dredd process.');
logger.debug(`Using configured custom exit() method to terminate the Dredd process with status '${exitStatus}'.`);
this.finished = true;
this.stopServer(() => {
this.exit(exitStatus);
});
};
} else {
this._processExit = (exitStatus) => {
logger.verbose(`Exiting Dredd process with status '${exitStatus}'.`);
logger.debug('Using native process.exit() method to terminate the Dredd process.');
logger.debug(`Using native process.exit() method to terminate the Dredd process with status '${exitStatus}'.`);
this.stopServer(() => process.exit(exitStatus));
};
}
} else {
this._processExit = (exitStatus) => {
logger.verbose(`Exiting Dredd process with status '${exitStatus}'.`);
logger.debug('Using configured custom callback to terminate the Dredd process.');
logger.debug(`Using configured custom callback to terminate the Dredd process with status '${exitStatus}'.`);
this.finished = true;
if (this.sigIntEventAdded) {
if (this.serverProcess && !this.serverProcess.terminated) {
logger.verbose('Killing backend server process before Dredd exits.');
logger.debug('Killing backend server process before Dredd exits.');
this.serverProcess.signalKill();
}
process.removeEventListener('SIGINT', this.commandSigInt);
Expand Down Expand Up @@ -145,7 +141,7 @@ Example:
runExitingActions() {
// Run interactive config
if (this.argv._[0] === 'init' || this.argv.init === true) {
logger.silly('Starting interactive configuration.');
logger.debug('Starting interactive configuration.');
this.finished = true;
interactiveConfig(this.argv, (config) => {
configUtils.save(config);
Expand All @@ -156,13 +152,11 @@ Example:

// Show help
} else if (this.argv.help === true) {
logger.silly('Printing help.');
this.optimist.showHelp(console.error);
this._processExit(0);

// Show version
} else if (this.argv.version === true) {
logger.silly('Printing version.');
console.log(`\
${packageData.name} v${packageData.version} \
(${os.type()} ${os.release()}; ${os.arch()})\
Expand All @@ -173,10 +167,10 @@ ${packageData.name} v${packageData.version} \

loadDreddFile() {
const configPath = this.argv.config;
logger.verbose('Loading configuration file:', configPath);
logger.debug('Loading configuration file:', configPath);

if (configPath && fs.existsSync(configPath)) {
logger.info(`Configuration '${configPath}' found, ignoring other arguments.`);
logger.debug(`Configuration '${configPath}' found, ignoring other arguments.`);
this.argv = configUtils.load(configPath);
}

Expand All @@ -188,7 +182,7 @@ ${packageData.name} v${packageData.version} \
}
});

this.argv = applyLoggingOptions(this.argv);
applyLoggingOptions(this.argv);
}

parseCustomConfig() {
Expand All @@ -197,33 +191,33 @@ ${packageData.name} v${packageData.version} \

runServerAndThenDredd() {
if (!this.argv.server) {
logger.verbose('No backend server process specified, starting testing at once');
logger.debug('No backend server process specified, starting testing at once');
this.runDredd(this.dreddInstance);
} else {
logger.verbose('Backend server process specified, starting backend server and then testing');
logger.debug('Backend server process specified, starting backend server and then testing');

const parsedArgs = spawnArgs(this.argv.server);
const command = parsedArgs.shift();

logger.verbose(`Using '${command}' as a server command, ${JSON.stringify(parsedArgs)} as arguments`);
logger.debug(`Using '${command}' as a server command, ${JSON.stringify(parsedArgs)} as arguments`);
this.serverProcess = spawn(command, parsedArgs);
logger.info(`Starting backend server process with command: ${this.argv.server}`);
logger.debug(`Starting backend server process with command: ${this.argv.server}`);

this.serverProcess.stdout.setEncoding('utf8');
this.serverProcess.stdout.on('data', data => process.stdout.write(data.toString()));

this.serverProcess.stderr.setEncoding('utf8');
this.serverProcess.stderr.on('data', data => process.stdout.write(data.toString()));

this.serverProcess.on('signalTerm', () => logger.verbose('Gracefully terminating the backend server process'));
this.serverProcess.on('signalKill', () => logger.verbose('Killing the backend server process'));
this.serverProcess.on('signalTerm', () => logger.debug('Gracefully terminating the backend server process'));
this.serverProcess.on('signalKill', () => logger.debug('Killing the backend server process'));

this.serverProcess.on('crash', (exitStatus, killed) => {
if (killed) { logger.info('Backend server process was killed'); }
if (killed) { logger.debug('Backend server process was killed'); }
});

this.serverProcess.on('exit', () => {
logger.info('Backend server process exited');
logger.debug('Backend server process exited');
});

this.serverProcess.on('error', (err) => {
Expand All @@ -234,22 +228,22 @@ ${packageData.name} v${packageData.version} \
// Ensure server is not running when dredd exits prematurely somewhere
process.on('beforeExit', () => {
if (this.serverProcess && !this.serverProcess.terminated) {
logger.verbose('Killing backend server process before Dredd exits');
logger.debug('Killing backend server process before Dredd exits');
this.serverProcess.signalKill();
}
});

// Ensure server is not running when dredd exits prematurely somewhere
process.on('exit', () => {
if (this.serverProcess && !this.serverProcess.terminated) {
logger.verbose('Killing backend server process on Dredd\'s exit');
logger.debug('Killing backend server process on Dredd\'s exit');
this.serverProcess.signalKill();
}
});

const waitSecs = parseInt(this.argv['server-wait'], 10);
const waitMilis = waitSecs * 1000;
logger.info(`Waiting ${waitSecs} seconds for backend server process to start`);
logger.debug(`Waiting ${waitSecs} seconds for backend server process to start`);

this.wait = setTimeout(() => {
this.runDredd(this.dreddInstance);
Expand All @@ -275,22 +269,22 @@ ${packageData.name} v${packageData.version} \
}

run() {
for (const task of [
this.setOptimistArgv,
this.parseCustomConfig,
this.runExitingActions,
this.loadDreddFile,
this.checkRequiredArgs,
this.moveBlueprintArgToPath,
]) {
task.call(this);
if (this.finished) { return; }
}
try {
for (const task of [
this.setOptimistArgv,
this.parseCustomConfig,
this.runExitingActions,
this.loadDreddFile,
this.checkRequiredArgs,
this.moveBlueprintArgToPath,
]) {
task.call(this);
if (this.finished) { return; }
}

const configurationForDredd = this.initConfig();
this.logDebuggingInfo(configurationForDredd);
const configurationForDredd = this.initConfig();
this.logDebuggingInfo(configurationForDredd);

try {
this.dreddInstance = this.initDredd(configurationForDredd);
} catch (e) {
this.exitWithStatus(e);
Expand Down Expand Up @@ -356,9 +350,9 @@ ${packageData.name} v${packageData.version} \
process.on('SIGINT', this.commandSigInt);
}

logger.verbose('Running Dredd instance.');
logger.debug('Running Dredd instance.');
dreddInstance.run((error, stats) => {
logger.verbose('Dredd instance run finished.');
logger.debug('Dredd instance run finished.');
this.exitWithStatus(error, stats);
});

Expand Down
18 changes: 9 additions & 9 deletions lib/Dredd.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ${proxySettings.join(', ')}. Please read documentation on how
Dredd works with proxies:
https://dredd.org/en/latest/how-it-works/#using-https-proxy
`;
logger.verbose(message);
logger.debug(message);
}
}

Expand Down Expand Up @@ -122,27 +122,27 @@ https://dredd.org/en/latest/how-it-works/#using-https-proxy
}

// Spin that merry-go-round
logger.verbose('Expanding glob patterns.');
logger.debug('Expanding glob patterns.');
this.expandGlobs((globsErr) => {
if (globsErr) { return callback(globsErr, this.stats); }

logger.verbose('Reading API description files.');
logger.debug('Reading API description files.');
this.loadFiles((loadErr) => {
if (loadErr) { return callback(loadErr, this.stats); }

logger.verbose('Parsing API description files and compiling a list of HTTP transactions to test.');
logger.debug('Parsing API description files and compiling a list of HTTP transactions to test.');
this.compileTransactions((compileErr) => {
if (compileErr) { return callback(compileErr, this.stats); }

logger.verbose('Starting reporters and waiting until all of them are ready.');
logger.debug('Starting reporters and waiting until all of them are ready.');
this.emitStart((emitStartErr) => {
if (emitStartErr) { return callback(emitStartErr, this.stats); }

logger.verbose('Starting transaction runner.');
logger.debug('Starting transaction runner.');
this.startRunner((runnerErr) => {
if (runnerErr) { return callback(runnerErr, this.stats); }

logger.verbose('Wrapping up testing.');
logger.debug('Wrapping up testing.');
this.transactionsComplete(callback);
});
});
Expand Down Expand Up @@ -196,7 +196,7 @@ API description document (or documents) not found on path:
async.eachLimit(this.configuration.files, 6, (fileUrlOrPath, loadCallback) => {
const { protocol, host } = url.parse(fileUrlOrPath);
if (host && ['http:', 'https:'].includes(protocol)) {
logger.verbose('Downloading remote file:', fileUrlOrPath);
logger.debug('Downloading remote file:', fileUrlOrPath);
this.downloadFile(fileUrlOrPath, loadCallback);
} else {
this.readLocalFile(fileUrlOrPath, loadCallback);
Expand Down Expand Up @@ -255,7 +255,7 @@ Is the provided path correct?
const fileData = this.configuration.data[filename];
if (!fileData.annotations) { fileData.annotations = []; }

logger.verbose('Compiling HTTP transactions from API description file:', filename);
logger.debug('Compiling HTTP transactions from API description file:', filename);
dreddTransactions.compile(fileData.raw, filename, (compilationError, compilationResult) => {
if (compilationError) { return next(compilationError); }

Expand Down
Loading

0 comments on commit 589c7eb

Please sign in to comment.