Skip to content

Commit

Permalink
Fix CLI tests (rethinkdb#696)
Browse files Browse the repository at this point in the history
* refactor of cli to get tests working (in progress)

* fixed some cleanup and close bugs/incomplete behavior

* fixed orderly shutdown

* fixed all cli tests

* fixed eslint warnings

* fixed rdb data directory cleanup in schema test

* fixed broken server tests

* add explicit joi initialization

* fixed test/serve.js

* added missing file

* removed extraneous joi initialization
  • Loading branch information
Tryneus authored and deontologician committed Aug 3, 2016
1 parent 97951bb commit 07acb98
Show file tree
Hide file tree
Showing 34 changed files with 1,615 additions and 1,624 deletions.
65 changes: 30 additions & 35 deletions cli/src/create-cert.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';
const hasbin = require('hasbin');
const spawn = require('child_process').spawn;
const process = require('process');

const helpText = 'Generate a certificate';

const runCommand = (args) => {
const run = (args) => {
if (args.length) {
console.error("create-cert takes no arguments");
process.exit(1);
throw new Error('create-cert takes no arguments');
}

// TODO: user configuration?
Expand All @@ -29,40 +25,39 @@ const runCommand = (args) => {
'-days', settings.days,
];

hasbin(settings.binaryName, function(hasOpenSSL) {
// show the invocation that's about to be run
console.log(`> ${settings.binaryName} ${binArgs.join(' ')}`);
// if we don't have openssl, bail
if (!hasOpenSSL) {
return console.error(`Missing ${settings.binaryName}. Make sure it is on the path.`);
}

// otherwise start openssl
const sslProc = spawn(settings.binaryName, binArgs);
return new Promise((resolve, reject) => {
hasbin(settings.binaryName, (hasOpenSSL) => {
// show the invocation that's about to be run
console.log(`> ${settings.binaryName} ${binArgs.join(' ')}`);

// pipe output appropriately
sslProc.stdout.pipe(process.stdout, { end: false });
sslProc.stderr.pipe(process.stderr, { end: false });

// and say nice things to the user when it's done
sslProc.on('close', (code) => {
console.log(`OpenSSL exited with code ${code}.`);
if (code) {
return console.error(
'Something seems to have gone wrong; ' +
'check the output above for details.'
);
} else {
return console.log(
'Everything seems to be fine. ' +
'Remember to add your shiny new certificates to your Horizon config!'
);
// if we don't have openssl, bail
if (!hasOpenSSL) {
reject(new Error(`Missing ${settings.binaryName}. Make sure it is on the path.`));
}

// otherwise start openssl
const sslProc = spawn(settings.binaryName, binArgs);

// pipe output appropriately
sslProc.stdout.pipe(process.stdout, { end: false });
sslProc.stderr.pipe(process.stderr, { end: false });

// say nice things to the user when it's done
sslProc.on('error', reject);
sslProc.on('close', (code) => {
if (code) {
reject(new Error(`OpenSSL failed with code ${code}.`));
} else {
console.log('Everything seems to be fine. ' +
'Remember to add your shiny new certificates to your Horizon config!');
resolve();
}
});
});
});
};

module.exports = {
runCommand,
helpText,
run,
description: 'Generate a certificate',
};
61 changes: 30 additions & 31 deletions cli/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const argparse = require('argparse');
const checkProjectName = require('./utils/check-project-name');
const rethrow = require('./utils/rethrow');

const helpText = 'Initialize a horizon app directory';

const makeIndexHTML = (projectName) => `\
<!doctype html>
<html>
Expand Down Expand Up @@ -155,10 +153,10 @@ const gitignore = () => `\
rethinkdb_data
**/*.log
.hz/secrets.toml
`
`;

function parseArguments(args) {
const parser = new argparse.ArgumentParser({prog: 'hz init'});
const parseArguments = (args) => {
const parser = new argparse.ArgumentParser({ prog: 'hz init' });
parser.addArgument([ 'projectName' ],
{ action: 'store',
help: 'Name of directory to create. Defaults to current directory',
Expand All @@ -177,7 +175,7 @@ const fileExists = (pathName) => {
}
};

function maybeMakeDir(createDir, dirName) {
const maybeMakeDir = (createDir, dirName) => {
if (createDir) {
try {
fs.mkdirSync(dirName);
Expand All @@ -189,9 +187,9 @@ function maybeMakeDir(createDir, dirName) {
} else {
console.info(`Initializing in existing directory ${dirName}`);
}
}
};

function maybeChdir(chdirTo) {
const maybeChdir = (chdirTo) => {
if (chdirTo) {
try {
process.chdir(chdirTo);
Expand All @@ -203,9 +201,9 @@ function maybeChdir(chdirTo) {
}
}
}
}
};

function populateDir(projectName, dirWasPopulated, chdirTo, dirName) {
const populateDir = (projectName, dirWasPopulated, chdirTo, dirName) => {
const niceDir = chdirTo ? `${dirName}/` : '';
if (!dirWasPopulated && !fileExists('src')) {
fs.mkdirSync('src');
Expand Down Expand Up @@ -282,29 +280,30 @@ function populateDir(projectName, dirWasPopulated, chdirTo, dirName) {
} else {
console.info('.hz/secrets.toml already exists, not touching it.');
}
}

function runCommand(args) {
const parsed = parseArguments(args);
const check = checkProjectName(
parsed.projectName,
process.cwd(),
fs.readdirSync('.')
);
const projectName = check.projectName;
const dirName = check.dirName;
const chdirTo = check.chdirTo;
const createDir = check.createDir;
maybeMakeDir(createDir, dirName);
maybeChdir(chdirTo);

// Before we create things, check if the directory is empty
const dirWasPopulated = fs.readdirSync(process.cwd()).length !== 0;
populateDir(projectName, dirWasPopulated, chdirTo, dirName);
};

const run = (args) =>
Promise.resolve(args)
.then(parseArguments)
.then((parsed) => {
const check = checkProjectName(
parsed.projectName,
process.cwd(),
fs.readdirSync('.')
);
const projectName = check.projectName;
const dirName = check.dirName;
const chdirTo = check.chdirTo;
const createDir = check.createDir;
maybeMakeDir(createDir, dirName);
maybeChdir(chdirTo);

// Before we create things, check if the directory is empty
const dirWasPopulated = fs.readdirSync(process.cwd()).length !== 0;
populateDir(projectName, dirWasPopulated, chdirTo, dirName);
});

module.exports = {
runCommand,
helpText,
run,
description: 'Initialize a horizon app directory',
};
36 changes: 17 additions & 19 deletions cli/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const makeTokenCommand = require('./make-token');
// Mapping from command line strings to modules. To add a new command,
// add an entry in this object, and create a module with the following
// exported:
// - runCommand: main function for the command
// - helpText: a string to display in the hz help text
// - run: main function for the command
// - description: a string to display in the hz help text
const commands = {
init: initCommand,
serve: serveCommand,
Expand All @@ -30,46 +30,44 @@ const commands = {

const programName = path.basename(process.argv[1]);

function help() {
const help = () => {
console.log(`Usage: ${programName} subcommand [args...]`);
console.log(`Available subcommands:`);
Object.keys(commands).forEach(function (cmdName) {
console.log(` ${cmdName} - ${commands[cmdName].helpText}`);
});
}
console.log('Available subcommands:');
Object.keys(commands).forEach((cmdName) =>
console.log(` ${cmdName} - ${commands[cmdName].description}`)
);
};

const allArgs = process.argv.slice(2);
if (allArgs.length == 0) {
if (allArgs.length === 0) {
help();
process.exit(1);
}

const cmdName = allArgs[0];
const cmdArgs = allArgs.slice(1);

if (cmdName == "-h" || cmdName == "--help" || cmdName == "help") {
if (cmdName === '-h' || cmdName === '--help' || cmdName === 'help') {
help();
process.exit(0);
}

var command = commands[cmdName];
const command = commands[cmdName];
if (!command) {
console.log(chalk.red.bold(
console.error(chalk.red.bold(
`No such subcommand ${cmdName}, run with -h for help`));
process.exit(1);
}

function done(err) {
const done = (err) => {
if (err) {
console.log(chalk.red.bold(
console.error(chalk.red.bold(
`${cmdName} failed ` +
`with ${err.stack}`));
process.exit(1);
} else {
process.exit(0);
}
};

try {
command.runCommand(cmdArgs, done);
} catch (e) {
done(e);
}
command.run(cmdArgs).then(() => done()).catch(done);
Loading

0 comments on commit 07acb98

Please sign in to comment.