Skip to content

Commit

Permalink
[7.x] [plugin-generator] rewrite tests to match new generated… (#61675)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <spalger@users.noreply.github.com>

Co-authored-by: spalger <spalger@users.noreply.github.com>
  • Loading branch information
Spencer and spalger authored Mar 27, 2020
1 parent 2fb533d commit 6035f8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 145 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ module.exports = {
'x-pack/dev-tools/mocha/setup_mocha.js',
'x-pack/scripts/*.js',
],
excludedFiles: ['**/integration_tests/**/*'],
rules: {
'import/no-commonjs': 'off',
'prefer-object-spread/prefer-object-spread': 'off',
Expand Down
173 changes: 28 additions & 145 deletions packages/kbn-plugin-generator/integration_tests/generate_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,163 +17,46 @@
* under the License.
*/

/* eslint-disable no-restricted-syntax */
import { spawn } from 'child_process';
import Fs from 'fs';
import { resolve } from 'path';
import util from 'util';
import { stat, readFileSync } from 'fs';
import { snakeCase } from 'lodash';
import { promisify } from 'util';

import del from 'del';
import { ProcRunner, ToolingLog } from '@kbn/dev-utils';
import { createLegacyEsTestCluster } from '@kbn/test';
import execa from 'execa';
import { snakeCase } from 'lodash';

const statP = util.promisify(stat);
const statAsync = promisify(Fs.stat);
const ROOT_DIR = resolve(__dirname, '../../../');
const oneMinute = 60000;

describe(`running the plugin-generator via 'node scripts/generate_plugin.js plugin-name' with default config`, () => {
const pluginName = 'ispec-plugin';
const snakeCased = snakeCase(pluginName);
const generatedPath = resolve(ROOT_DIR, `plugins/${snakeCased}`);
const collect = xs => data => xs.push(data + ''); // Coerce from Buffer to String

beforeAll(() => {
jest.setTimeout(oneMinute * 10);
});

beforeAll(done => {
const create = spawn(process.execPath, ['scripts/generate_plugin.js', pluginName], {
cwd: ROOT_DIR,
});
create.stdout.on('data', function selectDefaults() {
create.stdin.write('\n'); // Generate a plugin with default options.
});
create.on('close', done);
});

afterAll(() => {
del.sync(generatedPath, { force: true });
});

it(`should succeed on creating a plugin in a directory named 'plugins/${snakeCased}`, async () => {
const stats = await statP(generatedPath);
expect(stats.isDirectory()).toBe(true);
});

// skipped until internationalization is re-introduced
it.skip(`should create an internationalization config file with a blank line appended to satisfy the parser`, async () => {
// Link to the error that happens when the blank line is not there:
// https://github.com/elastic/kibana/pull/45044#issuecomment-530092627
const intlFile = `${generatedPath}/.i18nrc.json`;
expect(readFileSync(intlFile, 'utf8').endsWith('\n\n')).toBe(true);
});

describe(`then running`, () => {
it(`'yarn test:karma' should exit 0`, async () => {
await execa('yarn', ['test:karma'], {
cwd: generatedPath,
env: {
DISABLE_JUNIT_REPORTER: '1',
},
});
});

it.skip(`'yarn build' should exit 0`, async () => {
await execa('yarn', ['build'], { cwd: generatedPath });
});
const pluginName = 'ispec-plugin';
const snakeCased = snakeCase(pluginName);
const generatedPath = resolve(ROOT_DIR, `plugins/${snakeCased}`);

describe('with es instance', () => {
const log = new ToolingLog({
level: 'verbose',
writeTo: process.stdout,
});
const pr = new ProcRunner(log);

const es = createLegacyEsTestCluster({ license: 'basic', log });
beforeAll(es.start);
afterAll(es.stop);
afterAll(() => pr.teardown());
beforeAll(async () => {
await del(generatedPath, { force: true });
});

it(`'yarn start' should result in the spec plugin being initialized on kibana's stdout`, async () => {
await pr.run('kibana', {
cmd: 'yarn',
args: [
'start',
'--optimize.enabled=false',
'--logging.json=false',
'--logging.verbose=true',
'--migrations.skip=true',
],
cwd: generatedPath,
wait: new RegExp('\\[ispecPlugin\\]\\[plugins\\] Setting up plugin'),
});
await pr.stop('kibana');
});
});
afterAll(async () => {
await del(generatedPath, { force: true });
});

it(`'yarn preinstall' should exit 0`, async () => {
await execa('yarn', ['preinstall'], { cwd: generatedPath });
it('generates a plugin', async () => {
await new Promise((resolve, reject) => {
const proc = spawn(process.execPath, ['scripts/generate_plugin.js', pluginName], {
cwd: ROOT_DIR,
stdio: 'pipe',
});

it.skip(`'yarn lint' should exit 0`, async () => {
await execa('yarn', ['lint'], { cwd: generatedPath });
proc.stdout.on('data', function selectDefaults() {
proc.stdin.write('\n'); // Generate a plugin with default options.
});

it(`'yarn kbn --help' should print out the kbn help msg`, done => {
const helpMsg = `
usage: kbn <command> [<args>]
By default commands are run for Kibana itself, all packages in the 'packages/'
folder and for all plugins in './plugins' and '../kibana-extra'.
Available commands:
bootstrap - Install dependencies and crosslink projects
clean - Remove the node_modules and target directories from all projects.
run - Run script defined in package.json in each package that contains that script.
watch - Runs \`kbn:watch\` script for every project.
Global options:
-e, --exclude Exclude specified project. Can be specified multiple times to exclude multiple projects, e.g. '-e kibana -e @kbn/pm'.
-i, --include Include only specified projects. If left unspecified, it defaults to including all projects.
--oss Do not include the x-pack when running command.
--skip-kibana-plugins Filter all plugins in ./plugins and ../kibana-extra when running command.
`;
const outData = [];
const kbnHelp = spawn('yarn', ['kbn', '--help'], { cwd: generatedPath });
kbnHelp.stdout.on('data', collect(outData));
kbnHelp.on('close', () => {
expect(outData.join('\n')).toContain(helpMsg);
done();
});
});

it(`'yarn es --help' should print out the es help msg`, done => {
const helpMsg = `
usage: es <command> [<args>]
Assists with running Elasticsearch for Kibana development
Available commands:
snapshot - Downloads and run from a nightly snapshot
source - Build and run from source
archive - Install and run from an Elasticsearch tar
build_snapshots - Build and collect ES snapshots
Global options:
--help
`;
const outData = [];
const kbnHelp = spawn('yarn', ['es', '--help'], { cwd: generatedPath });
kbnHelp.stdout.on('data', collect(outData));
kbnHelp.on('close', () => {
expect(outData.join('\n')).toContain(helpMsg);
done();
});
});
proc.on('close', resolve);
proc.on('error', reject);
});

const stats = await statAsync(generatedPath);
if (!stats.isDirectory()) {
throw new Error(`Expected [${generatedPath}] to be a directory`);
}
});

0 comments on commit 6035f8a

Please sign in to comment.