Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensuring generators can generate into a specific path #515

Merged
merged 6 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/cli/__tests__/__utils__/generate-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ export async function generatePlugin(
.withOptions(mergedOptions)
.withPrompts(mergedPrompts);

return join(dir, `checkup-plugin-${mergedOptions.name}`);
return options.path
? join(dir, options.path, `checkup-plugin-${mergedOptions.name}`)
: join(dir, `checkup-plugin-${mergedOptions.name}`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ exports[`config-init-generator should write a config 1`] = `
}
"
`;

exports[`config-init-generator should write a config in custom path 1`] = `
"{
\\"excludePaths\\": [],
\\"plugins\\": [],
\\"tasks\\": {}
}
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,165 @@ Array [
]
`;

exports[`plugin generator generates plugin with defaults in custom path 1`] = `
"{
\\"name\\": \\"checkup-plugin-my-plugin\\",
\\"description\\": \\"Checkup plugin\\",
\\"version\\": \\"0.0.0\\",
\\"author\\": \\"\\",
\\"dependencies\\": {
\\"@checkup/core\\": \\"^0.0.1\\",
\\"@oclif/command\\": \\"^1\\",
\\"@oclif/config\\": \\"^1\\",
\\"tslib\\": \\"^1\\"
},
\\"devDependencies\\": {
\\"@checkup/test-helpers\\": \\"^0.0.1\\",
\\"@oclif/dev-cli\\": \\"^1\\",
\\"@types/jest\\": \\"^25.1.3\\",
\\"@types/node\\": \\"^13\\",
\\"jest\\": \\"^25.1.0\\",
\\"ts-jest\\": \\"^25.2.1\\",
\\"ts-node\\": \\"^8\\",
\\"typescript\\": \\"^3.8\\"
},
\\"engines\\": {
\\"node\\": \\">= 12.*\\"
},
\\"files\\": [
\\"/lib\\",
\\"/oclif.manifest.json\\"
],
\\"keywords\\": [
\\"checkup-plugin\\",
\\"oclif-plugin\\"
],
\\"license\\": \\"MIT\\",
\\"oclif\\": {
\\"devPlugins\\": [
\\"@oclif/plugin-help\\"
],
\\"hooks\\": {
\\"register-tasks\\": \\"./lib/hooks/register-tasks\\"
}
},
\\"repository\\": \\"\\",
\\"scripts\\": {
\\"build\\": \\"yarn clean && tsc\\",
\\"build:watch\\": \\"yarn build -w\\",
\\"clean\\": \\"rm -rf lib\\",
\\"postpack\\": \\"rm -f oclif.manifest.json\\",
\\"prepack\\": \\"rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme\\",
\\"test\\": \\"jest --no-cache\\",
\\"version\\": \\"oclif-dev readme && git add README.md\\"
},
\\"types\\": \\"lib/index.d.ts\\",
\\"main\\": \\"lib/index.js\\"
}
"
`;

exports[`plugin generator generates plugin with defaults in custom path 2`] = `
"# checkup-plugin-my-plugin

Checkup plugin

[![oclif](https://img.shields.io/badge/cli-oclif-brightgreen.svg)](https://oclif.io)
[![Version](https://img.shields.io/npm/v/plugin-default.svg)](https://npmjs.org/package/plugin-default)
[![Downloads/week](https://img.shields.io/npm/dw/plugin-default.svg)](https://npmjs.org/package/plugin-default)
[![License](https://img.shields.io/npm/l/plugin-default.svg)](https://github.com/checkupjs/checkup/blob/master/package.json)

<!-- toc -->

# Usage

<!-- usage -->

# Commands

<!-- commands -->
"
`;

exports[`plugin generator generates plugin with defaults in custom path 3`] = `
"module.exports = {
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'js', 'json'],
transform: { '\\\\\\\\.ts$': 'ts-jest' },
coverageReporters: ['lcov', 'text-summary'],
collectCoverageFrom: ['src/**/*.ts'],
coveragePathIgnorePatterns: ['/templates/'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
testPathIgnorePatterns: ['__fixtures__'],
};
"
`;

exports[`plugin generator generates plugin with defaults in custom path 4`] = `
"{
\\"compilerOptions\\": {
\\"declaration\\": true,
\\"importHelpers\\": true,
\\"module\\": \\"commonjs\\",
\\"strict\\": true,
\\"target\\": \\"es2017\\",
\\"allowSyntheticDefaultImports\\": true,
\\"sourceMap\\": true,
\\"outDir\\": \\"lib\\",
\\"rootDir\\": \\"src\\",
\\"types\\": [\\"jest\\", \\"node\\"]
},
\\"include\\": [\\"src/**/*\\"]
}
"
`;

exports[`plugin generator generates plugin with defaults in custom path 5`] = `
"export default {};

export * from './types';
"
`;

exports[`plugin generator generates plugin with defaults in custom path 6`] = `
"import { Hook } from '@oclif/config';
import { getPluginName } from '@checkup/core';

const hook: Hook<'register-tasks'> = async function({ context, tasks }: any) {
let pluginName = getPluginName(__dirname);
};

export default hook;
"
`;

exports[`plugin generator generates plugin with defaults in custom path 7`] = `""`;

exports[`plugin generator generates plugin with defaults in custom path 8`] = `
Array [
".gitkeep",
]
`;

exports[`plugin generator generates plugin with defaults in custom path 9`] = `
Array [
".gitkeep",
]
`;

exports[`plugin generator generates plugin with defaults in custom path 10`] = `
Array [
".gitkeep",
]
`;

exports[`plugin generator generates plugin with unmodified name with defaults 1`] = `
"{
\\"name\\": \\"checkup-plugin-foo\\",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,102 @@ export default hook;
"
`;

exports[`task generator generates correct files with TypeScript for defaults in custom path 1`] = `
"import { BaseTask, Task, TaskMetaData, TaskResult } from '@checkup/core';
import MyFooTaskResult from '../results/my-foo-task-result';

export default class MyFooTask extends BaseTask implements Task {
meta: TaskMetaData = {
taskName: 'my-foo',
friendlyTaskName: 'My Foo',
taskClassification: {
category: '',
},
};

async run(): Promise<TaskResult> {
let result: MyFooTaskResult = new MyFooTaskResult(this.meta);

return result;
}
}
"
`;

exports[`task generator generates correct files with TypeScript for defaults in custom path 2`] = `
"import { BaseTaskResult, TaskResult, ui } from '@checkup/core';

export default class MyFooTaskResult extends BaseTaskResult implements TaskResult {
toConsole() {
ui.styledHeader(this.meta.friendlyTaskName);
}

toJson() {
return {
meta: this.meta,
result: {},
};
}
}
"
`;

exports[`task generator generates correct files with TypeScript for defaults in custom path 3`] = `
"import { CheckupProject, stdout, getTaskContext } from '@checkup/test-helpers';
import { getPluginName } from '@checkup/core';
import MyFooTask from '../src/tasks/my-foo-task';
import MyFooTaskResult from '../src/results/my-foo-task-result';

describe('my-foo-task', () => {
let project: CheckupProject;
let pluginName = getPluginName(__dirname);

beforeEach(() => {
project = new CheckupProject('checkup-app', '0.0.0', project => {
project.addDependency('ember-cli', '^3.15.0');
});

project.writeSync();
project.gitInit();
});

afterEach(() => {
project.dispose();
});

it('can read task and output to console', async () => {
const result = await new MyFooTask(pluginName, getTaskContext({}, {cwd: project.baseDir})).run();
const taskResult = <MyFooTaskResult>result;

taskResult.toConsole();

expect(stdout()).toMatchSnapshot();
});

it('can read task as JSON', async () => {
const result = await new MyFooTask(pluginName, getTaskContext({}, {cwd: project.baseDir})).run();
const taskResult = <MyFooTaskResult>result;

expect(taskResult.toJson()).toMatchSnapshot();
});
});
"
`;

exports[`task generator generates correct files with TypeScript for defaults in custom path 4`] = `
"import { Hook } from '@oclif/config';
import MyFooTask from '../tasks/my-foo-task';
import { getPluginName } from '@checkup/core';

const hook: Hook<'register-tasks'> = async function({ context, tasks }: any) {
let pluginName = getPluginName(__dirname);
tasks.registerTask(new MyFooTask(pluginName, context));
};

export default hook;
"
`;

exports[`task generator generates correct files with category 1`] = `
"import { BaseTask, Task, TaskMetaData, TaskResult } from '@checkup/core';
import MyFooTaskResult from '../results/my-foo-task-result';
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/__tests__/generators/generate-config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ describe('config-init-generator', () => {
expect(testRoot(dir).file('.checkuprc').contents).toMatchSnapshot();
});

it('should write a config in custom path', async () => {
let tmp = createTmpDir();

const dir = await helpers.run(ConfigGenerator).cd(tmp).withOptions({ path: './lib' });

expect(testRoot(join(dir, 'lib')).file('.checkuprc').contents).toMatchSnapshot();
});

it('should error if a checkuprc file is already present', async () => {
await expect(
helpers.run(ConfigGenerator).inTmpDir(function (dir) {
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/__tests__/generators/generate-plugin-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ describe('plugin generator', () => {
expect(root.directory('src/tasks').contents).toMatchSnapshot();
});

it('generates plugin with defaults in custom path', async () => {
let dir = await generatePlugin({ path: './lib' });

let root = testRoot(dir);

expect(root.file('package.json').contents).toMatchSnapshot();
expect(root.file('README.md').contents).toMatchSnapshot();
expect(root.file('jest.config.js').contents).toMatchSnapshot();
expect(root.file('tsconfig.json').contents).toMatchSnapshot();
expect(root.file('src/index.ts').contents).toMatchSnapshot();
expect(root.file('src/hooks/register-tasks.ts').contents).toMatchSnapshot();
expect(root.file('src/types/index.ts').contents).toMatchSnapshot();
expect(root.directory('__tests__').contents).toMatchSnapshot();
expect(root.directory('src/results').contents).toMatchSnapshot();
expect(root.directory('src/tasks').contents).toMatchSnapshot();
});

it('generates plugin with JavaScript defaults', async () => {
let dir = await generatePlugin({ defaults: false }, { typescript: false });

Expand Down
16 changes: 16 additions & 0 deletions packages/cli/__tests__/generators/generate-task-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable jest/expect-expect */

import * as helpers from 'yeoman-test';
import { resolve } from 'path';

import TaskGenerator from '../../src/generators/task';
import { generatePlugin } from '../__utils__/generate-plugin';
Expand Down Expand Up @@ -35,6 +36,21 @@ describe('task generator', () => {
assertPluginFiles(dir);
});

it('generates correct files with TypeScript for defaults in custom path', async () => {
let baseDir = await generatePlugin({ path: './lib' });
await helpers
.run(TaskGenerator, { namespace: 'checkup:task' })
.cd(resolve(baseDir, '../..'))
.withOptions({
name: 'my-foo',
defaults: true,
path: './lib/checkup-plugin-my-plugin',
});

assertTaskFiles('my-foo', baseDir);
assertPluginFiles(baseDir);
});

it('generates multiple correct files with TypeScript for defaults', async () => {
let baseDir = await generatePlugin();
let dir = await helpers
Expand Down
Loading