Skip to content

Commit

Permalink
Some test struggles
Browse files Browse the repository at this point in the history
  • Loading branch information
kimjoar committed May 8, 2017
1 parent 237431d commit 28a1683
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 65 deletions.
267 changes: 267 additions & 0 deletions platform/cli/__tests__/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`displays help 1`] = `
Object {
"errors": Array [],
"exit": true,
"logs": Array [
"Usage: bin/kibana [options]
Kibana is an open source (Apache Licensed), browser-based analytics and search
dashboard for Elasticsearch.
Options:
--version, -v Show version number [boolean]
--help, -h Show help [boolean]
--config, -c Path to the config file, can be changed with the
\`CONFIG_PATH\` environment variable as well. Use multiple
--config args to include multiple config files. [string]
--elasticsearch, -e URI for Elasticsearch instance [string]
--host, -H The host to bind to [string]
--port, -p The port to bind Kibana to [number]
--quiet, -q Prevent all logging except errors
--silent, -Q Prevent all logging
--verbose Turns on verbose logging
--log-file, -l The file to log to [string]
--dev Run the server with development mode defaults
--ssl Dev only. Specify --no-ssl to not run the dev server
using HTTPS [boolean] [default: true]
--base-path Dev only. Specify --no-base-path to not put a proxy with
a random base path in front of the dev server
[boolean] [default: true]
--watch Dev only. Specify --no-watch to prevent automatic
restarts of the server in dev mode
[boolean] [default: true]
Documentation: https://elastic.co/kibana
",
],
"result": Object {
"$0": "--help",
"_": Array [],
"base-path": true,
"basePath": true,
"config": undefined,
"dev": undefined,
"elasticsearch": undefined,
"h": true,
"help": true,
"host": undefined,
"log-file": undefined,
"plugin-dir": undefined,
"port": undefined,
"quiet": undefined,
"silent": undefined,
"ssl": true,
"v": false,
"verbose": undefined,
"version": false,
"watch": true,
},
"warnings": Array [],
}
`;
exports[`displays version 1`] = `
Object {
"errors": Array [],
"exit": true,
"logs": Array [
"5.2.2",
],
"result": Object {
"$0": "--version",
"_": Array [],
"base-path": true,
"basePath": true,
"config": undefined,
"dev": undefined,
"elasticsearch": undefined,
"h": false,
"help": false,
"host": undefined,
"log-file": undefined,
"plugin-dir": undefined,
"port": undefined,
"quiet": undefined,
"silent": undefined,
"ssl": true,
"v": true,
"verbose": undefined,
"version": true,
"watch": true,
},
"warnings": Array [],
}
`;
exports[`fails for unknown options 1`] = `
Object {
"errors": Array [
"The following options were not recognized:
[\\"foo\\"]",
"",
"Specify --help for available options",
],
"exit": true,
"logs": Array [],
"result": Object {
"$0": "--foo",
"_": Array [],
"base-path": true,
"basePath": true,
"config": undefined,
"dev": undefined,
"elasticsearch": undefined,
"foo": true,
"h": false,
"help": false,
"host": undefined,
"log-file": undefined,
"plugin-dir": undefined,
"port": undefined,
"quiet": undefined,
"silent": undefined,
"ssl": true,
"v": false,
"verbose": undefined,
"version": false,
"watch": true,
},
"warnings": Array [],
}
`;
exports[`fails if config file does not exist 1`] = `
Object {
"errors": Array [
"Config file [/Users/kim/dev/elastic/kibana/foo] does not exist",
"",
"Specify --help for available options",
],
"exit": true,
"logs": Array [],
"result": Object {
"$0": "--config ./foo",
"_": Array [],
"base-path": true,
"basePath": true,
"c": "/Users/kim/dev/elastic/kibana/foo",
"config": "/Users/kim/dev/elastic/kibana/foo",
"dev": undefined,
"elasticsearch": undefined,
"h": false,
"help": false,
"host": undefined,
"log-file": undefined,
"plugin-dir": undefined,
"port": undefined,
"quiet": undefined,
"silent": undefined,
"ssl": true,
"v": false,
"verbose": undefined,
"version": false,
"watch": true,
},
"warnings": Array [],
}
`;
exports[`fails if port is not a number 1`] = `
Object {
"errors": Array [
"[port] must be a number, but was a string",
"",
"Specify --help for available options",
],
"exit": true,
"logs": Array [],
"result": Object {
"$0": "--port test",
"_": Array [],
"base-path": true,
"basePath": true,
"config": undefined,
"dev": undefined,
"elasticsearch": undefined,
"h": false,
"help": false,
"host": undefined,
"log-file": undefined,
"p": NaN,
"plugin-dir": undefined,
"port": NaN,
"quiet": undefined,
"silent": undefined,
"ssl": true,
"v": false,
"verbose": undefined,
"version": false,
"watch": true,
},
"warnings": Array [],
}
`;
exports[`handles args with dashes 1`] = `
Object {
"errors": Array [],
"exit": false,
"logs": Array [],
"result": Object {
"$0": "--base-path",
"_": Array [],
"base-path": true,
"basePath": true,
"config": undefined,
"dev": undefined,
"elasticsearch": undefined,
"h": false,
"help": false,
"host": undefined,
"log-file": undefined,
"plugin-dir": undefined,
"port": undefined,
"quiet": undefined,
"silent": undefined,
"ssl": true,
"v": false,
"verbose": undefined,
"version": false,
"watch": true,
},
"warnings": Array [],
}
`;
exports[`handles negative args 1`] = `
Object {
"errors": Array [],
"exit": false,
"logs": Array [],
"result": Object {
"$0": "--no-ssl --no-base-path",
"_": Array [],
"base-path": false,
"basePath": false,
"config": undefined,
"dev": undefined,
"elasticsearch": undefined,
"h": false,
"help": false,
"host": undefined,
"log-file": undefined,
"plugin-dir": undefined,
"port": undefined,
"quiet": undefined,
"silent": undefined,
"ssl": false,
"v": false,
"verbose": undefined,
"version": false,
"watch": true,
},
"warnings": Array [],
}
`;
72 changes: 72 additions & 0 deletions platform/cli/__tests__/captureTerminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { clone } from 'lodash';

export function captureTerminal<T extends string[]>(
fn: (argv: T) => any,
argv: T
) {
const _exit = process.exit;
const _emit = process.emit;
const _env = process.env;
const _argv = process.argv;

const _error = console.error;
const _log = console.log;
const _warn = console.warn;

let exit = false;
process.exit = () => {
exit = true;
};

const env = clone(process.env);
env._ = 'node';
process.env = env;
process.argv = argv;

const errors: any[] = [];
const logs: any[] = [];
const warnings: any[] = [];

console.error = (msg: any) => {
errors.push(msg);
};
console.log = (msg: any) => {
logs.push(msg);
};
console.warn = (msg: any) => {
warnings.push(msg);
};

let result: T;

try {
result = fn(argv);
} finally {
reset();
}

return done();

function reset() {
process.exit = _exit;
process.emit = _emit;
process.env = _env;
process.argv = _argv;

console.error = _error;
console.log = _log;
console.warn = _warn;
}

function done() {
reset();

return {
errors,
logs,
warnings,
exit,
result
};
}
}
38 changes: 38 additions & 0 deletions platform/cli/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
jest.mock('../../../package.json', () => ({
default: {
version: '5.2.2'
}
}));

import { parseArgv } from '../cli';
import { captureTerminal } from './captureTerminal';

test('displays help', () => {
expect(captureTerminal(parseArgv, ['--help'])).toMatchSnapshot();
});

test('displays version', () => {
expect(captureTerminal(parseArgv, ['--version'])).toMatchSnapshot();
});

test('fails for unknown options', () => {
expect(captureTerminal(parseArgv, ['--foo'])).toMatchSnapshot();
});

test('fails if port is not a number', () => {
expect(captureTerminal(parseArgv, ['--port', 'test'])).toMatchSnapshot();
});

test('fails if config file does not exist', () => {
expect(captureTerminal(parseArgv, ['--config', './foo'])).toMatchSnapshot();
});

test('handles args with dashes', () => {
expect(captureTerminal(parseArgv, ['--base-path'])).toMatchSnapshot();
});

test('handles negative args', () => {
expect(
captureTerminal(parseArgv, ['--no-ssl', '--no-base-path'])
).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion platform/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import yargs from 'yargs';
import * as yargs from 'yargs';

import * as args from './args';
import { Env } from '../env';
Expand Down
Loading

0 comments on commit 28a1683

Please sign in to comment.