Skip to content

Commit

Permalink
refactor: rename internal test env vars
Browse files Browse the repository at this point in the history
- All internal env values (i.e. for testing CI or testing GitHub Action creation) are prefixed with `TEST_RDME_`

- Renamed our `NODE_ENV` for our test from `test` to `rdme-test`

- Created a little `isTest` helper to check whether we're running tests
  • Loading branch information
kanadgupta committed Nov 2, 2022
1 parent 0278aa2 commit 6fbfedf
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 54 deletions.
4 changes: 2 additions & 2 deletions __tests__/cmds/categories/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/categories/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/changelogs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/changelogs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/custompages/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/custompages/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/docs/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/docs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/docs/prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/docs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
12 changes: 6 additions & 6 deletions __tests__/cmds/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/openapi/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/helpers/get-gha-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
}
4 changes: 2 additions & 2 deletions __tests__/lib/createGHA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions __tests__/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
File renamed without changes.
4 changes: 2 additions & 2 deletions src/lib/createGHA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/lib/isCI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
10 changes: 5 additions & 5 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand All @@ -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);
}

Expand All @@ -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}`));
}
Expand All @@ -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
Expand All @@ -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<OraOptions> = { isSilent: process.env.NODE_ENV === 'test' };
const opts: Writable<OraOptions> = { isSilent: isTest() };

// Cleans up ora output so it prints nicely alongside debug logs
/* istanbul ignore next */
Expand Down
Loading

0 comments on commit 6fbfedf

Please sign in to comment.