From 28a16836a98c9fcf20f1564d924416798b93ca04 Mon Sep 17 00:00:00 2001 From: Kim Joar Bekkelund Date: Mon, 8 May 2017 13:32:46 +0200 Subject: [PATCH] Some test struggles --- .../__tests__/__snapshots__/cli.test.ts.snap | 267 ++++++++++++++++++ platform/cli/__tests__/captureTerminal.ts | 72 +++++ platform/cli/__tests__/cli.test.ts | 38 +++ platform/cli/cli.ts | 2 +- .../__tests__/__snapshots__/cli.test.ts.snap | 35 --- .../integration_tests/__tests__/cli.test.ts | 7 - platform/integration_tests/runCli.ts | 22 -- 7 files changed, 378 insertions(+), 65 deletions(-) create mode 100644 platform/cli/__tests__/__snapshots__/cli.test.ts.snap create mode 100644 platform/cli/__tests__/captureTerminal.ts create mode 100644 platform/cli/__tests__/cli.test.ts delete mode 100644 platform/integration_tests/__tests__/__snapshots__/cli.test.ts.snap delete mode 100644 platform/integration_tests/__tests__/cli.test.ts delete mode 100644 platform/integration_tests/runCli.ts diff --git a/platform/cli/__tests__/__snapshots__/cli.test.ts.snap b/platform/cli/__tests__/__snapshots__/cli.test.ts.snap new file mode 100644 index 000000000000000..e2dc30e78c37c3f --- /dev/null +++ b/platform/cli/__tests__/__snapshots__/cli.test.ts.snap @@ -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 [], +} +`; diff --git a/platform/cli/__tests__/captureTerminal.ts b/platform/cli/__tests__/captureTerminal.ts new file mode 100644 index 000000000000000..1d50ac5309ef3eb --- /dev/null +++ b/platform/cli/__tests__/captureTerminal.ts @@ -0,0 +1,72 @@ +import { clone } from 'lodash'; + +export function captureTerminal( + 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 + }; + } +} diff --git a/platform/cli/__tests__/cli.test.ts b/platform/cli/__tests__/cli.test.ts new file mode 100644 index 000000000000000..2f95533863141cf --- /dev/null +++ b/platform/cli/__tests__/cli.test.ts @@ -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(); +}); diff --git a/platform/cli/cli.ts b/platform/cli/cli.ts index 292eafbde3aca3f..5e2db6632bd5668 100644 --- a/platform/cli/cli.ts +++ b/platform/cli/cli.ts @@ -1,4 +1,4 @@ -import yargs from 'yargs'; +import * as yargs from 'yargs'; import * as args from './args'; import { Env } from '../env'; diff --git a/platform/integration_tests/__tests__/__snapshots__/cli.test.ts.snap b/platform/integration_tests/__tests__/__snapshots__/cli.test.ts.snap deleted file mode 100644 index 2e2e8e848b17037..000000000000000 --- a/platform/integration_tests/__tests__/__snapshots__/cli.test.ts.snap +++ /dev/null @@ -1,35 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`displays help 1`] = ` -"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 - -" -`; diff --git a/platform/integration_tests/__tests__/cli.test.ts b/platform/integration_tests/__tests__/cli.test.ts deleted file mode 100644 index 2bcc31247bc4821..000000000000000 --- a/platform/integration_tests/__tests__/cli.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { runCli } from '../runCli'; - -test('displays help', () => { - const result = runCli('.', ['--help']); - expect(result.status).toBe(0); - expect(result.stdout).toMatchSnapshot(); -}); diff --git a/platform/integration_tests/runCli.ts b/platform/integration_tests/runCli.ts deleted file mode 100644 index b003e60cb1c9431..000000000000000 --- a/platform/integration_tests/runCli.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { resolve } from 'path'; -import { spawnSync } from 'child_process'; - -const KIBANA_CLI_PATH = resolve(__dirname, '../../scripts/platform.js'); - -export function runCli(dir: string, args: string[] = []) { - const isRelative = dir[0] !== '/'; - - if (isRelative) { - dir = resolve(__dirname, dir); - } - - const result = spawnSync('node', [KIBANA_CLI_PATH, ...args], { - cwd: dir - }); - - return { - ...result, - stdout: result.stdout && result.stdout.toString(), - stderr: result.stderr && result.stderr.toString() - }; -};