Skip to content

Commit

Permalink
[#104] Add test documentation and simplify testData usage
Browse files Browse the repository at this point in the history
  • Loading branch information
malparty committed Jul 13, 2022
1 parent 7b56e1c commit 3eb3839
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 102 deletions.
13 changes: 13 additions & 0 deletions packages/cli-tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ DESCRIPTION

## How to contribute

### Run

To run the CLI on your local machine:

```bash
Expand All @@ -97,6 +99,17 @@ If your changes also impacted the `cra-template` package, you can still test the

Find more the [OCLIF Documentation](https://oclif.io/docs/introduction.html)!

### Test

The tests generated from the combinaison of:
- `TestData` objects
- `Scenario` objects

`TestData` objects gather all the rules that should be tested for a given add-on.
When creating a new add-on, you need to create a new associated TestData object in the `./test/add-ons/**` folder.

`Scenario` objects enable to run the tests of multiple add-ons in a single `generate` command execution. As running the `generate` command is time-consuming, grouping several add-ons tests into a single scenario is a way to get tests results earlier.

## License

This project is Copyright (c) 2014 and onwards.
Expand Down
38 changes: 18 additions & 20 deletions packages/cli-tool/test/add-ons/ui-framework/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { TestData } from '../../helpers/test-data';

export const bootstrapTestData = (projectName: string): TestData => {
return {
filesShouldContain: [
{
path: `${projectName}/package.json`,
shouldContainString: 'bootstrap',
},
{
path: `${projectName}/src/assets/stylesheets/application.scss`,
shouldContainString: 'vendor/bootstrap',
},
],
filesShouldExist: [
`${projectName}/src/assets/stylesheets/vendor/bootstrap/index.scss`,
],
filesShouldNotExist: [
`${projectName}/tailwind.config.js`,
`${projectName}/src/assets/stylesheets/application.css`,
],
};
export const bootstrapTestData: TestData = {
filesShouldExist: [
'/src/assets/stylesheets/vendor/bootstrap/index.scss',
],
filesShouldNotExist: [
'/tailwind.config.js',
'/src/assets/stylesheets/application.css',
],
filesShouldContain: [
{
path: '/package.json',
shouldContainString: 'bootstrap',
},
{
path: '/src/assets/stylesheets/application.scss',
shouldContainString: 'vendor/bootstrap',
},
],
};
66 changes: 32 additions & 34 deletions packages/cli-tool/test/add-ons/ui-framework/tailwind-css.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import { TestData } from '../../helpers/test-data';

export const tailwindCssTestData = (projectName: string): TestData => {
return {
filesShouldExist: [
`${projectName}/tailwind.config.js`,
`${projectName}/postcss.config.js`,
`${projectName}/src/assets/stylesheets/application.css`,
],
filesShouldNotExist: [
`${projectName}/src/assets/stylesheets/vendor/bootstrap`,
`${projectName}/src/assets/stylesheets/application.scss`,
],
filesShouldContain: [
{
path: `${projectName}/package.json`,
shouldContainString: 'tailwindcss',
},
{
path: `${projectName}/package.json`,
shouldContainString: 'postcss-import',
},
{
path: `${projectName}/src/assets/stylesheets/application.css`,
shouldContainString: '@tailwind base;',
},
{
path: `${projectName}/src/assets/stylesheets/application.css`,
shouldContainString: '@tailwind components;',
},
{
path: `${projectName}/src/assets/stylesheets/application.css`,
shouldContainString: '@tailwind utilities;',
},
],
};
export const tailwindCssTestData: TestData = {
filesShouldExist: [
'/tailwind.config.js',
'/postcss.config.js',
'/src/assets/stylesheets/application.css',
],
filesShouldNotExist: [
'/src/assets/stylesheets/vendor/bootstrap',
'/src/assets/stylesheets/application.scss',
],
filesShouldContain: [
{
path: '/package.json',
shouldContainString: 'tailwindcss',
},
{
path: '/package.json',
shouldContainString: 'postcss-import',
},
{
path: '/src/assets/stylesheets/application.css',
shouldContainString: '@tailwind base;',
},
{
path: '/src/assets/stylesheets/application.css',
shouldContainString: '@tailwind components;',
},
{
path: '/src/assets/stylesheets/application.css',
shouldContainString: '@tailwind utilities;',
},
],
};
13 changes: 0 additions & 13 deletions packages/cli-tool/test/add-ons/version-control/github.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/cli-tool/test/add-ons/version-control/gitlab.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/cli-tool/test/add-ons/version-control/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TestData } from '../../helpers/test-data';

export const gitHubTestData: TestData = {
filesShouldExist: ['/.github'],
filesShouldNotExist: ['/.gitlab'],
filesShouldContain: [],
};

export const gitLabTestData: TestData = {
filesShouldExist: ['/.gitlab'],
filesShouldNotExist: ['/.github'],
filesShouldContain: [],
};
39 changes: 17 additions & 22 deletions packages/cli-tool/test/commands/generate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ import Inquirer from 'inquirer';

import { bootstrapTestData } from '../../add-ons/ui-framework/bootstrap';
import { tailwindCssTestData } from '../../add-ons/ui-framework/tailwind-css';
import { gitHubTestData } from '../../add-ons/version-control/github';
import { gitLabTestData } from '../../add-ons/version-control/gitlab';
import { gitHubTestData, gitLabTestData } from '../../add-ons/version-control';
import { TestScenario } from '../../helpers/test-scenario';

const templateRepoPath = 'file:./packages/cra-template';
const projectName = 'test-app';
const testFolderPath = '../../';

const gitHubData = gitHubTestData(projectName);
const gitLabData = gitLabTestData(projectName);

const bootstrapData = bootstrapTestData(projectName);
const tailwindCssData = tailwindCssTestData(projectName);
const projectPath = `${testFolderPath}${projectName}`;

const testScenarios: TestScenario[] = [
{
Expand All @@ -27,16 +22,16 @@ const testScenarios: TestScenario[] = [
},
testData: {
filesShouldExist: [
...gitHubData.filesShouldExist,
...bootstrapData.filesShouldExist,
...gitHubTestData.filesShouldExist,
...bootstrapTestData.filesShouldExist,
],
filesShouldNotExist: [
...gitHubData.filesShouldNotExist,
...bootstrapData.filesShouldNotExist,
...gitHubTestData.filesShouldNotExist,
...bootstrapTestData.filesShouldNotExist,
],
filesShouldContain: [
...gitHubData.filesShouldContain,
...bootstrapData.filesShouldContain,
...gitHubTestData.filesShouldContain,
...bootstrapTestData.filesShouldContain,
],
},
},
Expand All @@ -47,16 +42,16 @@ const testScenarios: TestScenario[] = [
},
testData: {
filesShouldExist: [
...gitLabData.filesShouldExist,
...tailwindCssData.filesShouldExist,
...gitLabTestData.filesShouldExist,
...tailwindCssTestData.filesShouldExist,
],
filesShouldNotExist: [
...gitLabData.filesShouldNotExist,
...tailwindCssData.filesShouldNotExist,
...gitLabTestData.filesShouldNotExist,
...tailwindCssTestData.filesShouldNotExist,
],
filesShouldContain: [
...gitLabData.filesShouldContain,
...tailwindCssData.filesShouldContain,
...gitLabTestData.filesShouldContain,
...tailwindCssTestData.filesShouldContain,
],
},
},
Expand Down Expand Up @@ -93,16 +88,16 @@ describe('generate', () => {
);

scenario.testData.filesShouldExist.forEach((file) => {
expect(fs.existsSync(`${testFolderPath}${file}`)).to.equal(true);
expect(fs.existsSync(`${projectPath}${file}`)).to.equal(true);
});

scenario.testData.filesShouldNotExist.forEach((file) => {
expect(fs.existsSync(`${testFolderPath}${file}`)).to.equal(false);
expect(fs.existsSync(`${projectPath}${file}`)).to.equal(false);
});

scenario.testData.filesShouldContain.forEach((file) => {
const contents = fs.readFileSync(
`${testFolderPath}${file.path}`,
`${projectPath}${file.path}`,
'utf-8',
);

Expand Down
21 changes: 21 additions & 0 deletions packages/cli-tool/test/helpers/test-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
/** Describe all tests to be run for a given add-on */
export type TestData = {
/** Path relative to the generated project root folder.
* List files or folders that are expected to be included within
* the generated project.
* E.g. '/src/assets/stylesheets/application.scss`
*/
filesShouldExist: string[];
/** Path relative to the generated project root folder.
* List files or folders that are expected to NOT be included within
* the generated project.
* E.g. '/src/assets/stylesheets/vendor/bootstrap`
*/
filesShouldNotExist: string[];
/** Path relative to the generated project root folder.
* List what file is expected to contain what string.
* E.g.
* ```
* {
* path: '/src/assets/stylesheets/application.scss`,
* shouldContainString: 'vendor/bootstrap`,
* }
* ```
*/
filesShouldContain: {
path: string;
shouldContainString: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-tool/test/helpers/test-scenario.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TestData } from './test-data';

/** TestScenario group several add-ons tests
* into a single generate command execution */
export type TestScenario = {
options: any; // eslint-disable-line @typescript-eslint/no-explicit-any
testData: TestData;
Expand Down

0 comments on commit 3eb3839

Please sign in to comment.