|
| 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 | +}; |
0 commit comments