Skip to content

Commit

Permalink
[#121] Add additonal test scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
bterone committed Sep 29, 2023
1 parent 3568cda commit d2e6c4e
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 73 deletions.
12 changes: 7 additions & 5 deletions packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { CliUx } from '@oclif/core';

import { FetchStrategy } from '.';
import { InitTemplateOptions } from '../.';
import runCommand from '../../helpers/child-process';
import { FetchStrategy } from './';

const TEMPLATE_SOURCE_FILES = '../vite-template';

class CopyStrategy implements FetchStrategy {
constructor(public selectedTemplate: string) {
this.selectedTemplate = selectedTemplate;
}

async fetchTemplateFiles(options: InitTemplateOptions): Promise<void> {
return this.copyTemplateFiles(options)
.then(() => this.renameFolder(options));
Expand All @@ -17,7 +19,7 @@ class CopyStrategy implements FetchStrategy {

return runCommand(
'cp',
['-r', TEMPLATE_SOURCE_FILES, options.dest],
['-r', `../${this.selectedTemplate}`, options.dest],
);
}

Expand All @@ -26,7 +28,7 @@ class CopyStrategy implements FetchStrategy {

return runCommand(
'mv',
[`${options.dest}/vite-template`, `${options.dest}/${options.appName}`],
[`${options.dest}/${this.selectedTemplate}/`, `${options.dest}/${options.appName}/`],
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { CliUx } from '@oclif/core';

import { FetchStrategy } from '.';
import { InitTemplateOptions } from '../.';
import runCommand from '../../helpers/child-process';
import { downloadRepository } from '../../helpers/github';
import { FetchStrategy } from './';

const TEMPLATE_OWNER = 'nimblehq';
const TEMPLATE_REPO = 'react-templates';

class DownloadStrategy implements FetchStrategy {
constructor(public selectedTemplate: string) {
this.selectedTemplate = selectedTemplate;
}

async fetchTemplateFiles(options: InitTemplateOptions): Promise<void> {
return this.downloadTemplateRepository(options)
.then(() => this.extractViteTemplateFolder(options))
.then(() => this.extractDownloadedTemplateFolder(options))
.then(() => this.renameFolder(options))
.then(() => this.cleanTemporaryFiles(options));
}
Expand All @@ -32,7 +36,7 @@ class DownloadStrategy implements FetchStrategy {
);
}

private extractViteTemplateFolder(options: InitTemplateOptions): Promise<void> {
private extractDownloadedTemplateFolder(options: InitTemplateOptions): Promise<void> {
CliUx.ux.info('Extracting template source files...');

return runCommand(
Expand All @@ -48,7 +52,7 @@ class DownloadStrategy implements FetchStrategy {

return runCommand(
'mv',
[`${TEMPLATE_REPO}-${path}`, options.appName],
[`${TEMPLATE_REPO}-${path}/${this.selectedTemplate}/`, options.appName],
options.dest,
);
}
Expand Down
8 changes: 5 additions & 3 deletions packages/cli-tool/src/template/fetchingStrategy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { InitTemplateOptions } from '../.';
import CopyStrategy from './copyStrategy';
import DownloadStrategy from './downloadStrategy';

export type FetchStrategy = {
fetchTemplateFiles: (options: InitTemplateOptions) => Promise<void>;
};
export interface FetchStrategy {
selectedTemplate: string;

fetchTemplateFiles(options: InitTemplateOptions): Promise<void>;
}

export { CopyStrategy, DownloadStrategy };
2 changes: 1 addition & 1 deletion packages/cli-tool/src/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export const TEMPLATE_OPTIONS = new Map<templateOptions, string>([

export const initializeTemplate = async({
appName,
dest,
templateOption,
templateReference,
dest,
}: {
templateOption: templateOptions;
} & InitTemplateOptions): Promise<void> => {
Expand Down
9 changes: 4 additions & 5 deletions packages/cli-tool/src/template/initialize-vite-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ const replcaeNimbleNameInFiles = ['package.json'];
const fetchTemplateFiles = (options: InitTemplateOptions): Promise<void> => {
let fetchStrategy: CopyStrategy | DownloadStrategy;

// If passed templateReference in CLI, use the DownloadStrategy
// TODO: Decide if we want to keep DownloadStrategy long-term
if (options.templateReference && options.templateReference.trim() === '') {
fetchStrategy = new DownloadStrategy();
// TODO: Decide if we want to use DownloadStrategy long-term
if (!options.templateReference || options.templateReference.trim() === '') {
fetchStrategy = new CopyStrategy('vite-template');
} else {
fetchStrategy = new CopyStrategy();
fetchStrategy = new DownloadStrategy('vite-template');
}

return fetchStrategy.fetchTemplateFiles(options);
Expand Down
6 changes: 1 addition & 5 deletions packages/cli-tool/test/add-ons/ui-framework/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { TestData } from '../../helpers/test-data';

export const bootstrapTestData: TestData = {
filesShouldExist: [
filePaths: [
'/src/assets/stylesheets/vendor/bootstrap/index.scss',
],
filesShouldNotExist: [
'/tailwind.config.js',
'/src/assets/stylesheets/application.css',
],
filesShouldContain: [
{
path: '/package.json',
Expand Down
6 changes: 1 addition & 5 deletions packages/cli-tool/test/add-ons/ui-framework/tailwind-css.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { TestData } from '../../helpers/test-data';

export const tailwindCssTestData: TestData = {
filesShouldExist: [
filePaths: [
'/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',
Expand Down
9 changes: 3 additions & 6 deletions packages/cli-tool/test/add-ons/version-control/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { TestData } from '../../helpers/test-data';

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

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

export const noVersionControlTestData: TestData = {
filesShouldExist: [],
filesShouldNotExist: ['/.github', '/.gitlab'],
filePaths: ['/.github', '/.gitlab'],
filesShouldContain: [],
};
51 changes: 22 additions & 29 deletions packages/cli-tool/test/commands/generate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const viteTestScenarios: TestScenario[] = [
templateReference: viteTemplateReference,
testData: {
filesShouldExist: [
...gitHubTestData.filesShouldExist,
...bootstrapTestData.filesShouldExist,
...gitHubTestData.filePaths,
...bootstrapTestData.filePaths,
],
filesShouldNotExist: [
...gitHubTestData.filesShouldNotExist,
...bootstrapTestData.filesShouldNotExist,
...gitLabTestData.filePaths,
...tailwindCssTestData.filePaths,
],
filesShouldContain: [
...gitHubTestData.filesShouldContain,
Expand All @@ -48,12 +48,12 @@ const viteTestScenarios: TestScenario[] = [
templateReference: viteTemplateReference,
testData: {
filesShouldExist: [
...gitLabTestData.filesShouldExist,
...tailwindCssTestData.filesShouldExist,
...gitLabTestData.filePaths,
...tailwindCssTestData.filePaths,
],
filesShouldNotExist: [
...gitLabTestData.filesShouldNotExist,
...tailwindCssTestData.filesShouldNotExist,
...gitHubTestData.filePaths,
...bootstrapTestData.filePaths,
],
filesShouldContain: [
...gitLabTestData.filesShouldContain,
Expand All @@ -64,23 +64,18 @@ const viteTestScenarios: TestScenario[] = [
{
options: {
template: 'vite',
versionControl: 'github',
uiFramework: 'bootstrap',
versionControl: 'none',
uiFramework: 'none',
},
templateReference: '',
testData: {
filesShouldExist: [
...gitHubTestData.filesShouldExist,
...bootstrapTestData.filesShouldExist,
],
filesShouldExist: [],
filesShouldNotExist: [
...gitHubTestData.filesShouldNotExist,
...bootstrapTestData.filesShouldNotExist,
],
filesShouldContain: [
...gitHubTestData.filesShouldContain,
...bootstrapTestData.filesShouldContain,
...noVersionControlTestData.filePaths,
...bootstrapTestData.filePaths,
...tailwindCssTestData.filePaths,
],
filesShouldContain: [],
},
},
];
Expand All @@ -94,12 +89,12 @@ const craTestScenarios: TestScenario[] = [
templateReference: craTemplateReference,
testData: {
filesShouldExist: [
...gitHubTestData.filesShouldExist,
...bootstrapTestData.filesShouldExist,
...gitHubTestData.filePaths,
...bootstrapTestData.filePaths,
],
filesShouldNotExist: [
...gitHubTestData.filesShouldNotExist,
...bootstrapTestData.filesShouldNotExist,
...gitLabTestData.filePaths,
...tailwindCssTestData.filePaths,
],
filesShouldContain: [
...gitHubTestData.filesShouldContain,
Expand All @@ -116,15 +111,13 @@ const craTestScenarios: TestScenario[] = [
templateReference: craTemplateReference,
testData: {
filesShouldExist: [
...noVersionControlTestData.filesShouldExist,
...tailwindCssTestData.filesShouldExist,
...tailwindCssTestData.filePaths,
],
filesShouldNotExist: [
...noVersionControlTestData.filesShouldNotExist,
...tailwindCssTestData.filesShouldNotExist,
...noVersionControlTestData.filePaths,
...bootstrapTestData.filePaths,
],
filesShouldContain: [
...noVersionControlTestData.filesShouldContain,
...tailwindCssTestData.filesShouldContain,
],
},
Expand Down
8 changes: 1 addition & 7 deletions packages/cli-tool/test/helpers/test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ export type TestData = {
* 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[];
filePaths: string[];
/** Path relative to the generated project root folder.
* List what file is expected to contain what string.
* E.g.
Expand Down
31 changes: 28 additions & 3 deletions packages/cli-tool/test/helpers/test-scenario.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
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
templateReference: string;
testData: TestData;
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;
}[];
};
};

0 comments on commit d2e6c4e

Please sign in to comment.