From 4fea0ec646d1b2d735ec09300fe75ce85a422759 Mon Sep 17 00:00:00 2001 From: David Brooke <38883189+dmbrooke@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:07:10 -0400 Subject: [PATCH] Improve docs, add tests for result-component --- .../create-atomic-component-project/index.js | 2 +- .../atomic/create-atomic-component/index.js | 2 +- .../create-atomic-result-component/index.js | 2 +- .../tests/test.spec.ts | 107 ++++++++++++++---- 4 files changed, 86 insertions(+), 27 deletions(-) diff --git a/packages/ui/atomic/create-atomic-component-project/index.js b/packages/ui/atomic/create-atomic-component-project/index.js index 6997b47bc7..8fb17d516b 100755 --- a/packages/ui/atomic/create-atomic-component-project/index.js +++ b/packages/ui/atomic/create-atomic-component-project/index.js @@ -48,7 +48,7 @@ const main = () => { } else { handleErrors( new InvalidProjectDirectory( - 'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project (see [When to Use the Coveo CLI](https://docs.coveo.com/en/cli/#when-to-use-the-coveo-cli).' + 'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project (see [`atomic:cmp` documentation](https://docs.coveo.com/en/cli/#coveo-atomiccmp-name)).' ) ); } diff --git a/packages/ui/atomic/create-atomic-component/index.js b/packages/ui/atomic/create-atomic-component/index.js index e50a787fea..1a8a28c665 100755 --- a/packages/ui/atomic/create-atomic-component/index.js +++ b/packages/ui/atomic/create-atomic-component/index.js @@ -144,7 +144,7 @@ const ensureDirectoryValidity = () => { if (cwdFiles.length > 0 && !hasPackageInCwd) { handleErrors( new InvalidProjectDirectory( - 'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project (see [When to Use the Coveo CLI](https://docs.coveo.com/en/cli/#when-to-use-the-coveo-cli).' + 'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project (see [`atomic:cmp` documentation](https://docs.coveo.com/en/cli/#coveo-atomiccmp-name)).' ) ); } diff --git a/packages/ui/atomic/create-atomic-result-component/index.js b/packages/ui/atomic/create-atomic-result-component/index.js index a1b3bd9190..df0e99429a 100755 --- a/packages/ui/atomic/create-atomic-result-component/index.js +++ b/packages/ui/atomic/create-atomic-result-component/index.js @@ -144,7 +144,7 @@ const ensureDirectoryValidity = () => { if (cwdFiles.length > 0 && !hasPackageInCwd) { handleErrors( new InvalidProjectDirectory( - 'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project (see [When to Use the Coveo CLI](https://docs.coveo.com/en/cli/#when-to-use-the-coveo-cli).' + 'Current working directory is either not empty or not an npm project (no package.json found). Please try again in a valid project (see [`atomic:cmp` documentation](https://docs.coveo.com/en/cli/#coveo-atomiccmp-name)).' ) ); } diff --git a/packages/ui/atomic/create-atomic-result-component/tests/test.spec.ts b/packages/ui/atomic/create-atomic-result-component/tests/test.spec.ts index 7f8ce3bf97..b767e6a070 100644 --- a/packages/ui/atomic/create-atomic-result-component/tests/test.spec.ts +++ b/packages/ui/atomic/create-atomic-result-component/tests/test.spec.ts @@ -1,6 +1,6 @@ import {ChildProcess} from 'node:child_process'; import {join} from 'node:path'; -import {mkdirSync} from 'node:fs'; +import {mkdirSync, writeFileSync, rmSync} from 'node:fs'; import {npmSync} from '@coveo/do-npm'; import {startVerdaccio} from '@coveo/verdaccio-starter'; import {hashElement} from 'folder-hash'; @@ -18,6 +18,29 @@ describe(PACKAGE_NAME, () => { let npmConfigCache: string; let verdaccioUrl: string; + const assertAllAggregateErrorsFired = ( + tag: string, + ...expectedErrorMessages: string[] + ) => { + const {stderr} = npmSync( + ['init', PACKAGE_NAME.replace('/create-', '/'), '--', tag], + { + env: { + ...process.env, + npm_config_registry: verdaccioUrl, + npm_config_cache: npmConfigCache, + }, + cwd: testDirectory, + } + ); + + expectedErrorMessages.forEach((expectedMessage) => { + expect(stderr.toString()).toEqual( + expect.stringContaining(expectedMessage) + ); + }); + }; + beforeAll(async () => { ({verdaccioUrl, verdaccioProcess} = await startVerdaccio([ PACKAGE_NAME, @@ -52,29 +75,6 @@ describe(PACKAGE_NAME, () => { const leadingDashTag = '-dash'; const messedUpTag = '-My#--Component@-'; - const assertAllAggregateErrorsFired = ( - tag: string, - ...expectedErrorMessages: string[] - ) => { - const {stderr} = npmSync( - ['init', PACKAGE_NAME.replace('/create-', '/'), '--', tag], - { - env: { - ...process.env, - npm_config_registry: verdaccioUrl, - npm_config_cache: npmConfigCache, - }, - cwd: testDirectory, - } - ); - - expectedErrorMessages.forEach((expectedMessage) => { - expect(stderr.toString()).toEqual( - expect.stringContaining(expectedMessage) - ); - }); - }; - it.each([ leadingSpaceTag, trailingSpaceTag, @@ -148,6 +148,65 @@ describe(PACKAGE_NAME, () => { }); }); + describe('ensureDirectoryValidity', () => { + const assertAggregateErrorsNotFired = ( + tag: string, + expectedErrorMessages: string + ) => { + const {stderr} = npmSync( + ['init', PACKAGE_NAME.replace('/create-', '/'), '--', tag], + { + env: { + ...process.env, + npm_config_registry: verdaccioUrl, + npm_config_cache: npmConfigCache, + }, + cwd: testDirectory, + } + ); + + expect(stderr.toString()).not.toEqual( + expect.stringContaining(expectedErrorMessages) + ); + }; + + beforeEach(() => { + testDirectory = join(tempDirectory.name, 'dir-validity'); + mkdirSync(testDirectory, {recursive: true}); + }); + + afterEach(() => { + rmSync(testDirectory, {recursive: true}); + }); + + it('should not error when in empty directory', () => { + assertAggregateErrorsNotFired( + 'component-name', + 'Invalid project directory' + ); + }); + + it('should not error when in non-empty directory with package.json', () => { + writeFileSync(join(testDirectory, 'package.json'), '{}'); + assertAggregateErrorsNotFired( + 'component-name', + 'Invalid project directory' + ); + }); + + it('should error when in non-empty directory without package.json', () => { + writeFileSync( + join(testDirectory, '__init__.py'), + "# Wait, this isn't a Headless project :O" + ); + + assertAllAggregateErrorsFired( + 'component-name', + 'Invalid project directory' + ); + }); + }); + describe.each([ { describeName: 'when called without any args',