Skip to content

Commit f134acb

Browse files
committed
Merge branch 'v9' into next
2 parents 010821e + 8723686 commit f134acb

File tree

11 files changed

+201
-205
lines changed

11 files changed

+201
-205
lines changed

__tests__/commands/openapi/validate.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import prompts from 'prompts';
44
import { describe, beforeAll, beforeEach, afterEach, it, expect, vi } from 'vitest';
55

66
import Command from '../../../src/commands/openapi/validate.js';
7-
import { after, before } from '../../helpers/get-gha-setup.js';
7+
import { gitMock } from '../../helpers/git-mock.js';
88
import { runCommand, runCommandWithHooks, type OclifOutput } from '../../helpers/oclif.js';
99

1010
describe('rdme openapi validate', () => {
@@ -112,13 +112,13 @@ describe('rdme openapi validate', () => {
112112
let yamlOutput;
113113

114114
beforeEach(() => {
115-
before((fileName, data) => {
115+
gitMock.before((fileName, data) => {
116116
yamlOutput = data;
117117
});
118118
});
119119

120120
afterEach(() => {
121-
after();
121+
gitMock.after();
122122
});
123123

124124
it('should create GHA workflow if user passes in spec via prompts', async () => {

__tests__/helpers/get-gha-setup.ts

-48
This file was deleted.

__tests__/helpers/get-git-mock.ts

-47
This file was deleted.

__tests__/helpers/git-mock.ts

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import type { Response } from 'simple-git';
2+
3+
import fs from 'node:fs';
4+
5+
import { vi } from 'vitest';
6+
7+
import configstore from '../../src/lib/configstore.js';
8+
import * as getPkgVersion from '../../src/lib/getPkg.js';
9+
import { git } from '../../src/lib/git.js';
10+
import * as isCI from '../../src/lib/isCI.js';
11+
12+
const fsWriteFileSync = fs.writeFileSync;
13+
14+
/**
15+
* Creates a mock function for testing `git.remote`.
16+
*/
17+
export function getGitRemoteMock(
18+
/** remote to return (usually `origin`) */
19+
remote = 'origin',
20+
/** git URL for the given remote */
21+
remoteUrl = 'https://github.com/readmeio/rdme.git',
22+
/** the HEAD branch */
23+
defaultBranch = 'main',
24+
) {
25+
return vi.fn<(typeof git)['remote']>(arr => {
26+
// first call (used to grab remote for usage in subsequent commands)
27+
if (!arr.length) {
28+
if (!remote) return Promise.reject(new Error('Bad mock uh oh')) as unknown as Response<string>;
29+
return Promise.resolve(remote) as unknown as Response<string>;
30+
}
31+
// second call (used to grab default branch)
32+
if (arr.length === 2 && arr[0] === 'show' && arr[1] === remote) {
33+
if (remote === 'bad-remote') {
34+
return Promise.reject(
35+
new Error(`fatal: unable to access '${remoteUrl}': Could not resolve host: ${remoteUrl}`),
36+
) as unknown as Response<string>;
37+
}
38+
if (!defaultBranch) return Promise.reject(new Error('Bad mock uh oh')) as unknown as Response<string>;
39+
return Promise.resolve(`* remote origin
40+
Fetch URL: ${remoteUrl}
41+
Push URL: ${remoteUrl}
42+
HEAD branch: ${defaultBranch}
43+
`) as unknown as Response<string>;
44+
}
45+
46+
// third call (used to grab remote URLs)
47+
if (arr.length === 1 && arr[0] === '-v') {
48+
if (!remoteUrl) return Promise.reject(new Error('Bad mock uh oh')) as unknown as Response<string>;
49+
return Promise.resolve(`origin ${remoteUrl} (fetch)
50+
origin ${remoteUrl} (push)
51+
`) as unknown as Response<string>;
52+
}
53+
54+
return Promise.reject(new Error('Bad mock uh oh')) as unknown as Response<string>;
55+
});
56+
}
57+
58+
/**
59+
* Helper functions for setting up and tearing down tests for our GitHub Action onboarding.
60+
*
61+
* @see {@link __tests__/lib/createGHA.test.ts}
62+
*/
63+
export const gitMock = {
64+
before: function before(
65+
/** the mock function that should be called in place of `fs.writeFileSync` */
66+
writeFileSyncCb: typeof fs.writeFileSync = () => {},
67+
) {
68+
fs.writeFileSync = vi.fn(writeFileSyncCb);
69+
70+
git.checkIsRepo = vi.fn(() => {
71+
return Promise.resolve(true) as Response<boolean>;
72+
});
73+
74+
git.remote = getGitRemoteMock();
75+
76+
vi.setSystemTime(new Date('2022'));
77+
78+
vi.stubEnv('TEST_RDME_CREATEGHA', 'true');
79+
80+
const spy = vi.spyOn(getPkgVersion, 'getMajorPkgVersion');
81+
spy.mockResolvedValue(7);
82+
},
83+
84+
after: function after() {
85+
configstore.clear();
86+
fs.writeFileSync = fsWriteFileSync;
87+
vi.clearAllMocks();
88+
vi.unstubAllEnvs();
89+
},
90+
};
91+
92+
/**
93+
* Helper functions for setting up tests for simulating a GitHub Actions runner environment
94+
*/
95+
export const githubActionsEnv = {
96+
before: function before() {
97+
vi.resetModules();
98+
99+
// List of all GitHub Actions env variables:
100+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
101+
vi.stubEnv('GITHUB_ACTION', '__repo-owner_name-of-action-repo');
102+
vi.stubEnv('GITHUB_ACTIONS', 'true');
103+
vi.stubEnv('GITHUB_REPOSITORY', 'octocat/Hello-World');
104+
vi.stubEnv('GITHUB_RUN_ATTEMPT', '3');
105+
vi.stubEnv('GITHUB_RUN_ID', '1658821493');
106+
vi.stubEnv('GITHUB_RUN_NUMBER', '3');
107+
vi.stubEnv('GITHUB_SERVER_URL', 'https://github.com');
108+
vi.stubEnv('GITHUB_SHA', 'ffac537e6cbbf934b08745a378932722df287a53');
109+
vi.stubEnv('TEST_RDME_CI', 'true');
110+
vi.stubEnv('TEST_RDME_GHA', 'true');
111+
112+
const ciNameSpy = vi.spyOn(isCI, 'ciName');
113+
ciNameSpy.mockReturnValue('GitHub Actions (test)');
114+
},
115+
116+
after: function after() {
117+
vi.unstubAllEnvs();
118+
vi.resetAllMocks();
119+
},
120+
};

__tests__/helpers/oclif.ts

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ export function setupOclifConfig() {
3939
export function runCommand(Command: CommandClass) {
4040
return async function runCommandAgainstArgs(args?: string[]) {
4141
const oclifConfig = await setupOclifConfig();
42-
// @ts-expect-error this is the pattern recommended by the @oclif/test docs.
43-
// Not sure how to get this working with type generics.
4442
return captureOutput<string>(() => Command.run(args, oclifConfig), { testNodeEnv });
4543
};
4644
}

__tests__/helpers/setup-gha-env.ts

-34
This file was deleted.

__tests__/lib/createGHA.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import prompts from 'prompts';
99
import { describe, beforeEach, afterEach, it, expect, vi, type MockInstance, beforeAll } from 'vitest';
1010

1111
import configstore from '../../src/lib/configstore.js';
12-
import { getConfigStoreKey, getGHAFileName, git } from '../../src/lib/createGHA/index.js';
12+
import { getConfigStoreKey, getGHAFileName } from '../../src/lib/createGHA/index.js';
1313
import { getMajorPkgVersion } from '../../src/lib/getPkg.js';
14-
import { after, before } from '../helpers/get-gha-setup.js';
15-
import getGitRemoteMock from '../helpers/get-git-mock.js';
14+
import { git } from '../../src/lib/git.js';
15+
import { getGitRemoteMock, gitMock } from '../helpers/git-mock.js';
1616
import ghaWorkflowSchema from '../helpers/github-workflow-schema.json' with { type: 'json' };
1717
import { setupOclifConfig } from '../helpers/oclif.js';
1818
import { toBeValidSchema } from '../helpers/vitest.matchers.js';
@@ -36,13 +36,13 @@ describe('#createGHA', () => {
3636
consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
3737
oclifConfig = await setupOclifConfig();
3838

39-
before((fileName, data) => {
39+
gitMock.before((fileName, data) => {
4040
yamlOutput = data;
4141
});
4242
});
4343

4444
afterEach(() => {
45-
after();
45+
gitMock.after();
4646

4747
consoleInfoSpy.mockRestore();
4848
process.chdir(testWorkingDir);

__tests__/lib/fetch.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import pkg from '../../package.json' with { type: 'json' };
55
import DocsUploadCommand from '../../src/commands/docs/upload.js';
66
import { cleanAPIv1Headers, fetchSchema, handleAPIv1Res, readmeAPIv1Fetch } from '../../src/lib/readmeAPIFetch.js';
77
import { getAPIv1Mock, oasFetchMock } from '../helpers/get-api-mock.js';
8+
import { githubActionsEnv } from '../helpers/git-mock.js';
89
import { setupOclifConfig } from '../helpers/oclif.js';
9-
import { after, before } from '../helpers/setup-gha-env.js';
1010

1111
describe('#readmeAPIv1Fetch()', () => {
1212
describe('GitHub Actions environment', () => {
13-
beforeEach(before);
13+
beforeEach(() => {
14+
githubActionsEnv.before();
15+
});
1416

15-
afterEach(after);
17+
afterEach(() => {
18+
githubActionsEnv.after();
19+
});
1620

1721
it('should have correct headers for requests in GitHub Action env', async () => {
1822
const key = 'API_KEY';

0 commit comments

Comments
 (0)