From ebbea7e4b6e08bf735111690d54603cba590a89e Mon Sep 17 00:00:00 2001 From: Terone <bterone@gmail.com> Date: Fri, 29 Sep 2023 15:30:41 +0700 Subject: [PATCH] [#121] Add additonal test scenario --- .../template/fetchingStrategy/copyStrategy.ts | 12 +++-- .../fetchingStrategy/downloadStrategy.ts | 12 +++-- .../src/template/fetchingStrategy/index.ts | 4 +- packages/cli-tool/src/template/index.ts | 2 +- .../src/template/initialize-vite-app.ts | 9 ++-- .../test/add-ons/ui-framework/bootstrap.ts | 6 +-- .../test/add-ons/ui-framework/tailwind-css.ts | 6 +-- .../test/add-ons/version-control/index.ts | 9 ++-- .../test/commands/generate/index.test.ts | 51 ++++++++----------- packages/cli-tool/test/helpers/test-data.ts | 8 +-- .../cli-tool/test/helpers/test-scenario.ts | 31 +++++++++-- 11 files changed, 79 insertions(+), 71 deletions(-) diff --git a/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts b/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts index 689b307..bc9630a 100644 --- a/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts +++ b/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts @@ -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)); @@ -17,7 +19,7 @@ class CopyStrategy implements FetchStrategy { return runCommand( 'cp', - ['-r', TEMPLATE_SOURCE_FILES, options.dest], + ['-r', `../${this.selectedTemplate}`, options.dest], ); } @@ -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}/`], ); } } diff --git a/packages/cli-tool/src/template/fetchingStrategy/downloadStrategy.ts b/packages/cli-tool/src/template/fetchingStrategy/downloadStrategy.ts index 271d7ec..8b0b3b4 100644 --- a/packages/cli-tool/src/template/fetchingStrategy/downloadStrategy.ts +++ b/packages/cli-tool/src/template/fetchingStrategy/downloadStrategy.ts @@ -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)); } @@ -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( @@ -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, ); } diff --git a/packages/cli-tool/src/template/fetchingStrategy/index.ts b/packages/cli-tool/src/template/fetchingStrategy/index.ts index 79b419e..9730ca1 100644 --- a/packages/cli-tool/src/template/fetchingStrategy/index.ts +++ b/packages/cli-tool/src/template/fetchingStrategy/index.ts @@ -3,7 +3,9 @@ import CopyStrategy from './copyStrategy'; import DownloadStrategy from './downloadStrategy'; export type FetchStrategy = { + selectedTemplate: string; + fetchTemplateFiles: (options: InitTemplateOptions) => Promise<void>; -}; +} export { CopyStrategy, DownloadStrategy }; diff --git a/packages/cli-tool/src/template/index.ts b/packages/cli-tool/src/template/index.ts index 5eb1d71..4d0e884 100644 --- a/packages/cli-tool/src/template/index.ts +++ b/packages/cli-tool/src/template/index.ts @@ -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> => { diff --git a/packages/cli-tool/src/template/initialize-vite-app.ts b/packages/cli-tool/src/template/initialize-vite-app.ts index 43ee285..d74364c 100644 --- a/packages/cli-tool/src/template/initialize-vite-app.ts +++ b/packages/cli-tool/src/template/initialize-vite-app.ts @@ -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); diff --git a/packages/cli-tool/test/add-ons/ui-framework/bootstrap.ts b/packages/cli-tool/test/add-ons/ui-framework/bootstrap.ts index fba0c9a..ec8a3a4 100644 --- a/packages/cli-tool/test/add-ons/ui-framework/bootstrap.ts +++ b/packages/cli-tool/test/add-ons/ui-framework/bootstrap.ts @@ -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', diff --git a/packages/cli-tool/test/add-ons/ui-framework/tailwind-css.ts b/packages/cli-tool/test/add-ons/ui-framework/tailwind-css.ts index 26a18ed..938d9f9 100644 --- a/packages/cli-tool/test/add-ons/ui-framework/tailwind-css.ts +++ b/packages/cli-tool/test/add-ons/ui-framework/tailwind-css.ts @@ -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', diff --git a/packages/cli-tool/test/add-ons/version-control/index.ts b/packages/cli-tool/test/add-ons/version-control/index.ts index b7c6b90..3f13bca 100644 --- a/packages/cli-tool/test/add-ons/version-control/index.ts +++ b/packages/cli-tool/test/add-ons/version-control/index.ts @@ -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: [], }; diff --git a/packages/cli-tool/test/commands/generate/index.test.ts b/packages/cli-tool/test/commands/generate/index.test.ts index 025b7d6..7ed7299 100644 --- a/packages/cli-tool/test/commands/generate/index.test.ts +++ b/packages/cli-tool/test/commands/generate/index.test.ts @@ -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, @@ -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, @@ -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: [], }, }, ]; @@ -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, @@ -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, ], }, diff --git a/packages/cli-tool/test/helpers/test-data.ts b/packages/cli-tool/test/helpers/test-data.ts index 4770612..3ee7b4d 100644 --- a/packages/cli-tool/test/helpers/test-data.ts +++ b/packages/cli-tool/test/helpers/test-data.ts @@ -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. diff --git a/packages/cli-tool/test/helpers/test-scenario.ts b/packages/cli-tool/test/helpers/test-scenario.ts index 95b49d4..0a2c057 100644 --- a/packages/cli-tool/test/helpers/test-scenario.ts +++ b/packages/cli-tool/test/helpers/test-scenario.ts @@ -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; + }[]; + }; };