diff --git a/__tests__/cmds/categories/create.test.ts b/__tests__/cmds/categories/create.test.ts index 99a35450a..7429792f4 100644 --- a/__tests__/cmds/categories/create.test.ts +++ b/__tests__/cmds/categories/create.test.ts @@ -22,11 +22,11 @@ describe('rdme categories:create', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(categoriesCreate.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no title provided', () => { diff --git a/__tests__/cmds/categories/index.test.ts b/__tests__/cmds/categories/index.test.ts index 0c9bf8956..ddcc0d658 100644 --- a/__tests__/cmds/categories/index.test.ts +++ b/__tests__/cmds/categories/index.test.ts @@ -22,11 +22,11 @@ describe('rdme categories', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(categories.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should return all categories for a single page', async () => { diff --git a/__tests__/cmds/changelogs/index.test.ts b/__tests__/cmds/changelogs/index.test.ts index b655ba02d..f7e65e87f 100644 --- a/__tests__/cmds/changelogs/index.test.ts +++ b/__tests__/cmds/changelogs/index.test.ts @@ -30,11 +30,11 @@ describe('rdme changelogs', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(changelogs.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no path provided', () => { diff --git a/__tests__/cmds/changelogs/single.test.ts b/__tests__/cmds/changelogs/single.test.ts index 83f883f6c..032eedba0 100644 --- a/__tests__/cmds/changelogs/single.test.ts +++ b/__tests__/cmds/changelogs/single.test.ts @@ -30,11 +30,11 @@ describe('rdme changelogs (single)', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(changelogs.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no file path provided', () => { diff --git a/__tests__/cmds/custompages/index.test.ts b/__tests__/cmds/custompages/index.test.ts index abdb5dc1a..461f19e0d 100644 --- a/__tests__/cmds/custompages/index.test.ts +++ b/__tests__/cmds/custompages/index.test.ts @@ -30,11 +30,11 @@ describe('rdme custompages', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(custompages.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no path provided', () => { diff --git a/__tests__/cmds/custompages/single.test.ts b/__tests__/cmds/custompages/single.test.ts index 2456d70fa..15c0a7c02 100644 --- a/__tests__/cmds/custompages/single.test.ts +++ b/__tests__/cmds/custompages/single.test.ts @@ -30,11 +30,11 @@ describe('rdme custompages (single)', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(custompages.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no file path provided', () => { diff --git a/__tests__/cmds/docs/edit.test.ts b/__tests__/cmds/docs/edit.test.ts index 02bf91f46..ebd3364da 100644 --- a/__tests__/cmds/docs/edit.test.ts +++ b/__tests__/cmds/docs/edit.test.ts @@ -40,15 +40,15 @@ describe('rdme docs:edit', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docsEdit.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should log deprecation notice', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docsEdit.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; expect(getWarningCommandOutput()).toMatch('is now deprecated'); }); diff --git a/__tests__/cmds/docs/index.test.ts b/__tests__/cmds/docs/index.test.ts index 0e46e33ed..6002fa3d5 100644 --- a/__tests__/cmds/docs/index.test.ts +++ b/__tests__/cmds/docs/index.test.ts @@ -40,9 +40,9 @@ describe('rdme docs', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no path provided', async () => { diff --git a/__tests__/cmds/docs/prune.test.ts b/__tests__/cmds/docs/prune.test.ts index 055f389a7..ccbcd1398 100644 --- a/__tests__/cmds/docs/prune.test.ts +++ b/__tests__/cmds/docs/prune.test.ts @@ -28,11 +28,11 @@ describe('rdme docs:prune', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docsPrune.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no folder provided', () => { diff --git a/__tests__/cmds/docs/single.test.ts b/__tests__/cmds/docs/single.test.ts index 8e37368db..e030eb854 100644 --- a/__tests__/cmds/docs/single.test.ts +++ b/__tests__/cmds/docs/single.test.ts @@ -33,9 +33,9 @@ describe('rdme docs (single)', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no file path provided', async () => { diff --git a/__tests__/cmds/openapi/index.test.ts b/__tests__/cmds/openapi/index.test.ts index 86eb35779..7d6cc1e8b 100644 --- a/__tests__/cmds/openapi/index.test.ts +++ b/__tests__/cmds/openapi/index.test.ts @@ -405,11 +405,11 @@ describe('rdme openapi', () => { describe('CI spec selection', () => { beforeEach(() => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; }); afterEach(() => { - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error out if multiple possible spec matches were found', () => { @@ -737,11 +737,11 @@ describe('rdme openapi', () => { describe('CI version handling', () => { beforeEach(() => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; }); afterEach(() => { - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should omit version header in CI environment', async () => { @@ -842,11 +842,11 @@ describe('rdme openapi', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(openapi.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if `--create` and `--update` flags are passed simultaneously', () => { diff --git a/__tests__/cmds/openapi/validate.test.ts b/__tests__/cmds/openapi/validate.test.ts index dfa768296..4d31631b4 100644 --- a/__tests__/cmds/openapi/validate.test.ts +++ b/__tests__/cmds/openapi/validate.test.ts @@ -115,18 +115,18 @@ describe('rdme openapi:validate', () => { describe('CI tests', () => { beforeEach(() => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; }); afterEach(() => { - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should successfully validate prompt and not run GHA onboarding', async () => { - process.env.TEST_CREATEGHA = 'true'; + process.env.TEST_RDME_CREATEGHA = 'true'; const spec = '__tests__/__fixtures__/petstore-simple-weird-version.json'; await expect(validate.run({ spec })).resolves.toBe(chalk.green(`${spec} is a valid OpenAPI API definition!`)); - delete process.env.TEST_CREATEGHA; + delete process.env.TEST_RDME_CREATEGHA; }); it('should fail if user attempts to pass `--github` flag in CI environment', () => { diff --git a/__tests__/cmds/versions/create.test.ts b/__tests__/cmds/versions/create.test.ts index 765c5336e..879c03a26 100644 --- a/__tests__/cmds/versions/create.test.ts +++ b/__tests__/cmds/versions/create.test.ts @@ -23,11 +23,11 @@ describe('rdme versions:create', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(createVersion.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no version provided', () => { diff --git a/__tests__/cmds/versions/delete.test.ts b/__tests__/cmds/versions/delete.test.ts index d27443335..0817d866a 100644 --- a/__tests__/cmds/versions/delete.test.ts +++ b/__tests__/cmds/versions/delete.test.ts @@ -23,11 +23,11 @@ describe('rdme versions:delete', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(deleteVersion.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should delete a specific version', async () => { diff --git a/__tests__/cmds/versions/index.test.ts b/__tests__/cmds/versions/index.test.ts index 3c64ea5ff..c5893c071 100644 --- a/__tests__/cmds/versions/index.test.ts +++ b/__tests__/cmds/versions/index.test.ts @@ -45,9 +45,9 @@ describe('rdme versions', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(versions.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should make a request to get a list of existing versions', async () => { diff --git a/__tests__/cmds/versions/update.test.ts b/__tests__/cmds/versions/update.test.ts index a45c7d0b7..0848e9504 100644 --- a/__tests__/cmds/versions/update.test.ts +++ b/__tests__/cmds/versions/update.test.ts @@ -23,11 +23,11 @@ describe('rdme versions:update', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(updateVersion.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should update a specific version object', async () => { diff --git a/__tests__/helpers/get-gha-setup.ts b/__tests__/helpers/get-gha-setup.ts index 031ac459a..e880d0fc9 100644 --- a/__tests__/helpers/get-gha-setup.ts +++ b/__tests__/helpers/get-gha-setup.ts @@ -33,7 +33,7 @@ export function before(writeFileSyncCb) { // no need to construct everything global.Date = jest.fn(() => DATE_TO_USE); - process.env.TEST_CREATEGHA = 'true'; + process.env.TEST_RDME_CREATEGHA = 'true'; const spy = jest.spyOn(getPkgVersion, 'getPkgVersion'); spy.mockReturnValue(Promise.resolve('7.8.9')); @@ -44,7 +44,7 @@ export function before(writeFileSyncCb) { */ export function after() { configstore.clear(); - delete process.env.TEST_CREATEGHA; + delete process.env.TEST_RDME_CREATEGHA; jest.clearAllMocks(); process.chdir(testWorkingDir); } diff --git a/__tests__/lib/createGHA.test.ts b/__tests__/lib/createGHA.test.ts index 86e15434d..13c17f34a 100644 --- a/__tests__/lib/createGHA.test.ts +++ b/__tests__/lib/createGHA.test.ts @@ -175,11 +175,11 @@ describe('#createGHA', () => { }); it('should not run if in a CI environment', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!'); // asserts that git commands aren't run in CI expect(git.checkIsRepo).not.toHaveBeenCalled(); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should not run if repo only contains non-GitHub remotes', () => { diff --git a/__tests__/setup.js b/__tests__/setup.js index 029058c1c..c46a875ec 100644 --- a/__tests__/setup.js +++ b/__tests__/setup.js @@ -2,3 +2,7 @@ // colorization here for all tests. // https://github.com/chalk/supports-color/issues/106 process.env.FORCE_COLOR = 0; + +// Sets our test NODE_ENV to a custom value in case of false positives +// if someone is using this tool in a testing environment +process.env.NODE_ENV = 'rdme-test'; diff --git a/config/test.json b/config/rdme-test.json similarity index 100% rename from config/test.json rename to config/rdme-test.json diff --git a/src/lib/createGHA/index.ts b/src/lib/createGHA/index.ts index 48c850974..cee316ae1 100644 --- a/src/lib/createGHA/index.ts +++ b/src/lib/createGHA/index.ts @@ -15,7 +15,7 @@ import { transcludeString } from 'hercule/promises'; import { checkFilePath, cleanFileName } from '../checkFile'; import configstore from '../configstore'; import { getPkgVersion } from '../getPkgVersion'; -import isCI from '../isCI'; +import isCI, { isTest } from '../isCI'; import { debug, info } from '../logger'; import promptTerminal from '../promptWrapper'; @@ -188,7 +188,7 @@ export default async function createGHA( // is a repo, but only contains non-GitHub remotes (isRepo && containsNonGitHubRemote && !containsGitHubRemote) || // not testing this function - (process.env.NODE_ENV === 'test' && !process.env.TEST_CREATEGHA) + (isTest() && !process.env.TEST_RDME_CREATEGHA) ) { debug('not running GHA onboarding workflow, exiting'); // We return the original command message and pretend this command flow never happened. diff --git a/src/lib/isCI.ts b/src/lib/isCI.ts index df2621c63..d33050bd8 100644 --- a/src/lib/isCI.ts +++ b/src/lib/isCI.ts @@ -9,6 +9,13 @@ export function isGHA() { return ciDetect() === 'github-actions'; } +/** + * Small env check to determine if we're running our testbed + */ +export function isTest() { + return process.env.NODE_ENV === 'rdme-test'; +} + /** * Small check to ensure we're in a safe CI environment. * @@ -17,5 +24,5 @@ export function isGHA() { */ export default function isCI() { /* istanbul ignore next */ - return (ciDetect() && process.env.NODE_ENV !== 'test') || process.env.TEST_CI; + return (ciDetect() && !isTest()) || !!process.env.TEST_RDME_CI; } diff --git a/src/lib/logger.ts b/src/lib/logger.ts index f3d1b9135..26caa8433 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; import config from 'config'; import debugModule from 'debug'; -import { isGHA } from './isCI'; +import { isGHA, isTest } from './isCI'; const debugPackage = debugModule(config.get('cli')); @@ -15,7 +15,7 @@ const debugPackage = debugModule(config.get('cli')); */ function debug(input: string) { /* istanbul ignore next */ - if (isGHA() && process.env.NODE_ENV !== 'test') core.debug(`rdme: ${input}`); + if (isGHA() && !isTest()) core.debug(`rdme: ${input}`); return debugPackage(input); } @@ -24,7 +24,7 @@ function debug(input: string) { */ function warn(input: string) { /* istanbul ignore next */ - if (isGHA() && process.env.NODE_ENV !== 'test') return core.warning(input); + if (isGHA() && !isTest()) return core.warning(input); // eslint-disable-next-line no-console return console.warn(chalk.yellow(`⚠️ Warning! ${input}`)); } @@ -36,7 +36,7 @@ function warn(input: string) { */ function info(input: string, includeEmojiPrefix = true) { /* istanbul ignore next */ - if (isGHA() && process.env.NODE_ENV !== 'test') return core.notice(input); + if (isGHA() && !isTest()) return core.notice(input); /* istanbul ignore next */ if (!includeEmojiPrefix) return console.info(input); // eslint-disable-line no-console // eslint-disable-next-line no-console @@ -45,7 +45,7 @@ function info(input: string, includeEmojiPrefix = true) { function oraOptions() { // Disables spinner in tests so it doesn't pollute test output - const opts: Writable = { isSilent: process.env.NODE_ENV === 'test' }; + const opts: Writable = { isSilent: isTest() }; // Cleans up ora output so it prints nicely alongside debug logs /* istanbul ignore next */ diff --git a/src/lib/promptWrapper.ts b/src/lib/promptWrapper.ts index 644bea791..540160ff3 100644 --- a/src/lib/promptWrapper.ts +++ b/src/lib/promptWrapper.ts @@ -1,6 +1,7 @@ -import ciDetect from '@npmcli/ci-detect'; import prompts from 'prompts'; +import isCI from './isCI'; + /** * The `prompts` library doesn't always interpret CTRL+C and release the terminal back to the user * so we need handle this ourselves. This function is just a simple overload of the main `prompts` @@ -28,7 +29,7 @@ export default async function promptTerminal( * prompt is not being run in a CI environment. */ function onRender() { - if (ciDetect() && process.env.NODE_ENV !== 'test') { + if (isCI()) { process.stdout.write('\n'); process.stdout.write( 'Yikes! Looks like we were about to prompt you for something in a CI environment. Are you missing an argument?'