From 458ea7a9c3b137e9e71a73c56977291f8a92e1b1 Mon Sep 17 00:00:00 2001 From: Terone Date: Fri, 29 Sep 2023 10:08:29 +0700 Subject: [PATCH] [#121] Add test case for testing separate strategies --- packages/cli-tool/README.md | 6 ++-- .../template/fetchingStrategy/copyStrategy.ts | 4 +-- .../src/template/initialize-vite-app.ts | 13 +++++--- .../test/commands/generate/index.test.ts | 32 +++++++++++++++++-- .../cli-tool/test/helpers/test-scenario.ts | 1 + 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/packages/cli-tool/README.md b/packages/cli-tool/README.md index f88c9bf..b665e62 100644 --- a/packages/cli-tool/README.md +++ b/packages/cli-tool/README.md @@ -91,16 +91,16 @@ To run the CLI on your local machine: > 💡 Running just `./bin/dev` without argument will display all the possible commands as well as additional information. -To test with local changes in either the `./packages/cra-template` or the `./vite-temaplte/` folders, use the following commands: +To test with local changes in either the `./packages/cra-template` or the `./vite-template/` folders, use the following commands: - For Vite: ```BASH - # Assuming the repository `react-temapltes` is in `~/Documents/Source/`. + # Assuming the repository `react-templates` is in `~/Documents/Source/`. # The generated app will be in `~/Documents/Source/vite-app` ./bin/dev generate vite-app ~/Documents/Source/ feature/gh88-replace-webpack-with-vite ``` - For Create React App (Webpack): ```BASH - # Assuming the repository `react-temapltes` is in `~/Documents/Source/`. + # Assuming the repository `react-templates` is in `~/Documents/Source/`. # The generated app will be in `~/Documents/Source/webpack-app` ./bin/dev generate webpack-app ~/Documents/Source/ file:react-templates/packages/cra-template ``` diff --git a/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts b/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts index 7fbdf92..689b307 100644 --- a/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts +++ b/packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts @@ -4,7 +4,7 @@ import { InitTemplateOptions } from '../.'; import runCommand from '../../helpers/child-process'; import { FetchStrategy } from './'; -const DEFAULT_TEMPLATE_REFERENCE = '../vite-template'; +const TEMPLATE_SOURCE_FILES = '../vite-template'; class CopyStrategy implements FetchStrategy { async fetchTemplateFiles(options: InitTemplateOptions): Promise { @@ -17,7 +17,7 @@ class CopyStrategy implements FetchStrategy { return runCommand( 'cp', - ['-r', DEFAULT_TEMPLATE_REFERENCE, options.dest], + ['-r', TEMPLATE_SOURCE_FILES, options.dest], ); } diff --git a/packages/cli-tool/src/template/initialize-vite-app.ts b/packages/cli-tool/src/template/initialize-vite-app.ts index 7181c02..062e7f2 100644 --- a/packages/cli-tool/src/template/initialize-vite-app.ts +++ b/packages/cli-tool/src/template/initialize-vite-app.ts @@ -3,14 +3,19 @@ import { CliUx } from '@oclif/core'; import { InitTemplateOptions } from '.'; import runCommand from '../helpers/child-process'; import { replaceLine } from '../helpers/file-editor'; -import { CopyStrategy } from './fetchingStrategy'; +import { CopyStrategy, DownloadStrategy } from './fetchingStrategy'; const replaceAppNameInFiles = ['package.json', 'index.html']; const fetchTemplateFiles = (options: InitTemplateOptions): Promise => { - // If given a branch name, use - // const fetchStrategy = new DownloadStrategy(); - const fetchStrategy = new CopyStrategy(); + let fetchStrategy: CopyStrategy | DownloadStrategy; + + // If passed templateReference in CLI, use the DownloadStrategy + if (options.templateReference && options.templateReference.trim() === '') { + fetchStrategy = new DownloadStrategy(); + } else { + fetchStrategy = new CopyStrategy(); + } return fetchStrategy.fetchTemplateFiles(options); }; diff --git a/packages/cli-tool/test/commands/generate/index.test.ts b/packages/cli-tool/test/commands/generate/index.test.ts index 3af0a7c..025b7d6 100644 --- a/packages/cli-tool/test/commands/generate/index.test.ts +++ b/packages/cli-tool/test/commands/generate/index.test.ts @@ -9,7 +9,9 @@ import { gitHubTestData, gitLabTestData, noVersionControlTestData } from '../../ import { TestScenario } from '../../helpers/test-scenario'; const craTemplateReference = 'file:./react-templates/packages/cra-template'; -const viteTemplateReference = '../vite-template'; +// TODO: Adjust viteTemplateReference to use commit hash of development branch for vite template +// https://github.com/nimblehq/react-templates/commit/52288d1e5e560bcc717f760f1fd19f7cb1b0085e +const viteTemplateReference = '52288d1e5e560bcc717f760f1fd19f7cb1b0085e'; const projectName = 'test-app'; const testFolderPath = '../../../'; @@ -21,6 +23,7 @@ const viteTestScenarios: TestScenario[] = [ versionControl: 'github', uiFramework: 'bootstrap', }, + templateReference: viteTemplateReference, testData: { filesShouldExist: [ ...gitHubTestData.filesShouldExist, @@ -42,6 +45,7 @@ const viteTestScenarios: TestScenario[] = [ versionControl: 'gitlab', uiFramework: 'tailwindCss', }, + templateReference: viteTemplateReference, testData: { filesShouldExist: [ ...gitLabTestData.filesShouldExist, @@ -57,6 +61,28 @@ const viteTestScenarios: TestScenario[] = [ ], }, }, + { + options: { + template: 'vite', + versionControl: 'github', + uiFramework: 'bootstrap', + }, + templateReference: '', + testData: { + filesShouldExist: [ + ...gitHubTestData.filesShouldExist, + ...bootstrapTestData.filesShouldExist, + ], + filesShouldNotExist: [ + ...gitHubTestData.filesShouldNotExist, + ...bootstrapTestData.filesShouldNotExist, + ], + filesShouldContain: [ + ...gitHubTestData.filesShouldContain, + ...bootstrapTestData.filesShouldContain, + ], + }, + }, ]; const craTestScenarios: TestScenario[] = [ { @@ -65,6 +91,7 @@ const craTestScenarios: TestScenario[] = [ versionControl: 'github', uiFramework: 'bootstrap', }, + templateReference: craTemplateReference, testData: { filesShouldExist: [ ...gitHubTestData.filesShouldExist, @@ -86,6 +113,7 @@ const craTestScenarios: TestScenario[] = [ versionControl: 'none', uiFramework: 'tailwindCss', }, + templateReference: craTemplateReference, testData: { filesShouldExist: [ ...noVersionControlTestData.filesShouldExist, @@ -126,7 +154,7 @@ describe('generate', () => { test .stdout() .stub(Inquirer, 'prompt', () => scenario.options) - .command(['generate', `${projectName}`, testFolderPath, scenario.options.template === 'vite' ? viteTemplateReference : craTemplateReference]) + .command(['generate', `${projectName}`, testFolderPath, scenario.templateReference]) .it( `generates a ${scenario.options.template} app ${projectName} with ${scenario.options.versionControl} and ${scenario.options.uiFramework}`, (ctx) => { diff --git a/packages/cli-tool/test/helpers/test-scenario.ts b/packages/cli-tool/test/helpers/test-scenario.ts index ce5ea63..95b49d4 100644 --- a/packages/cli-tool/test/helpers/test-scenario.ts +++ b/packages/cli-tool/test/helpers/test-scenario.ts @@ -4,5 +4,6 @@ import { TestData } from './test-data'; * into a single generate command execution */ export type TestScenario = { options: any; // eslint-disable-line @typescript-eslint/no-explicit-any + templateReference: string; testData: TestData; };