|
| 1 | +import { randomUUID } from 'crypto'; |
| 2 | +import { $ as chainableExeca } from 'execa'; |
| 3 | +import fsp from 'fs/promises'; |
| 4 | +import { after, before, beforeEach, describe, it, mock } from 'node:test'; |
| 5 | +import { EOL, tmpdir } from 'os'; |
| 6 | +import path from 'path'; |
| 7 | +import { GitClient } from './git_client.js'; |
| 8 | +import { GithubClient } from './github_client.js'; |
| 9 | +import { NpmClient } from './npm_client.js'; |
| 10 | +import { |
| 11 | + readPackageJson, |
| 12 | + writePackageJson, |
| 13 | +} from './package-json/package_json.js'; |
| 14 | +import { DependabotVersionUpdateHandler } from './dependabot_version_update_handler.js'; |
| 15 | +import assert from 'assert'; |
| 16 | + |
| 17 | +const originalEnv = process.env; |
| 18 | + |
| 19 | +/** |
| 20 | + * This test suite is more of an integration test than a unit test. |
| 21 | + * It uses the real file system and git repo but mocks the GitHub API client and GitHub context |
| 22 | + */ |
| 23 | +void describe('dependabot version update handler', async () => { |
| 24 | + let testWorkingDir: string; |
| 25 | + let gitClient: GitClient; |
| 26 | + let npmClient: NpmClient; |
| 27 | + |
| 28 | + let cantaloupePackageName: string; |
| 29 | + let cantaloupePackagePath: string; |
| 30 | + let platypusPackageName: string; |
| 31 | + let platypusPackagePath: string; |
| 32 | + |
| 33 | + let baseRef: string; |
| 34 | + |
| 35 | + const pullRequestBody = 'Bumps testDep from 1.0.0 to 1.1.0.'; |
| 36 | + |
| 37 | + before(async () => { |
| 38 | + process.env.GITHUB_TOKEN = 'testToken'; |
| 39 | + }); |
| 40 | + |
| 41 | + after(async () => { |
| 42 | + process.env = originalEnv; |
| 43 | + }); |
| 44 | + |
| 45 | + beforeEach(async ({ name: testName }) => { |
| 46 | + // create temp dir |
| 47 | + const shortId = randomUUID().split('-')[0]; |
| 48 | + const testNameNormalized = testName.slice(0, 15).replaceAll(/\s/g, ''); |
| 49 | + testWorkingDir = await fsp.mkdtemp( |
| 50 | + path.join(tmpdir(), `${testNameNormalized}-${shortId}`) |
| 51 | + ); |
| 52 | + console.log(testWorkingDir); |
| 53 | + |
| 54 | + gitClient = new GitClient(testWorkingDir); |
| 55 | + npmClient = new NpmClient(null, testWorkingDir); |
| 56 | + |
| 57 | + const $ = chainableExeca({ stdio: 'inherit', cwd: testWorkingDir }); |
| 58 | + |
| 59 | + // converting to lowercase because npm init creates packages with all lowercase |
| 60 | + cantaloupePackageName = |
| 61 | + `${testNameNormalized}-cantaloupe-${shortId}`.toLocaleLowerCase(); |
| 62 | + platypusPackageName = |
| 63 | + `${testNameNormalized}-platypus-${shortId}`.toLocaleLowerCase(); |
| 64 | + |
| 65 | + cantaloupePackagePath = path.join( |
| 66 | + testWorkingDir, |
| 67 | + 'packages', |
| 68 | + cantaloupePackageName |
| 69 | + ); |
| 70 | + platypusPackagePath = path.join( |
| 71 | + testWorkingDir, |
| 72 | + 'packages', |
| 73 | + platypusPackageName |
| 74 | + ); |
| 75 | + |
| 76 | + await gitClient.init(); |
| 77 | + await gitClient.switchToBranch('main'); |
| 78 | + await npmClient.init(); |
| 79 | + |
| 80 | + await npmClient.initWorkspacePackage(cantaloupePackageName); |
| 81 | + await setPackageToPublic(cantaloupePackagePath); |
| 82 | + |
| 83 | + await npmClient.initWorkspacePackage(platypusPackageName); |
| 84 | + await setPackageToPublic(platypusPackagePath); |
| 85 | + |
| 86 | + await npmClient.install(['@changesets/cli']); |
| 87 | + await setPackageDependencies(cantaloupePackagePath, { testDep: '^1.0.0' }); |
| 88 | + await setPackageDependencies(platypusPackagePath, { testDep: '^1.0.0' }); |
| 89 | + |
| 90 | + await $`npx changeset init`; |
| 91 | + await gitClient.commitAllChanges('Initial setup'); |
| 92 | + baseRef = await gitClient.getHashForCurrentCommit(); |
| 93 | + }); |
| 94 | + |
| 95 | + void it('can generate changeset with version updates', async () => { |
| 96 | + const githubClient = new GithubClient('garbage'); |
| 97 | + const labelPullRequestMocked = mock.method( |
| 98 | + githubClient, |
| 99 | + 'labelPullRequest', |
| 100 | + async () => {} |
| 101 | + ); |
| 102 | + const gitPushMocked = mock.method(gitClient, 'push', async () => {}); |
| 103 | + const ghContextMocked = { |
| 104 | + eventName: '', |
| 105 | + sha: '', |
| 106 | + ref: '', |
| 107 | + workflow: '', |
| 108 | + action: '', |
| 109 | + actor: '', |
| 110 | + job: '', |
| 111 | + runNumber: 0, |
| 112 | + runId: 0, |
| 113 | + apiUrl: '', |
| 114 | + serverUrl: '', |
| 115 | + graphqlUrl: '', |
| 116 | + payload: { |
| 117 | + pull_request: { |
| 118 | + number: 1, |
| 119 | + body: pullRequestBody, |
| 120 | + }, |
| 121 | + }, |
| 122 | + issue: { |
| 123 | + owner: '', |
| 124 | + repo: '', |
| 125 | + number: 0, |
| 126 | + }, |
| 127 | + repo: { |
| 128 | + owner: '', |
| 129 | + repo: '', |
| 130 | + }, |
| 131 | + }; |
| 132 | + |
| 133 | + // Update package.json files for both packages and commit to match what Dependabot will do for a version update PR |
| 134 | + await gitClient.switchToBranch('dependabot/test_update'); |
| 135 | + await setPackageDependencies(cantaloupePackagePath, { testDep: '^1.1.0' }); |
| 136 | + await setPackageDependencies(platypusPackagePath, { testDep: '^1.1.0' }); |
| 137 | + await gitClient.commitAllChanges('Bump dependencies'); |
| 138 | + const headRef = await gitClient.getHashForCurrentCommit(); |
| 139 | + |
| 140 | + const dependabotVersionUpdateHandler = new DependabotVersionUpdateHandler( |
| 141 | + baseRef, |
| 142 | + headRef, |
| 143 | + gitClient, |
| 144 | + githubClient, |
| 145 | + testWorkingDir, |
| 146 | + ghContextMocked |
| 147 | + ); |
| 148 | + |
| 149 | + await dependabotVersionUpdateHandler.handleVersionUpdate(); |
| 150 | + |
| 151 | + const changesetFilePath = path.join( |
| 152 | + testWorkingDir, |
| 153 | + `.changeset/dependabot-${headRef}.md` |
| 154 | + ); |
| 155 | + |
| 156 | + await assertChangesetFile( |
| 157 | + changesetFilePath, |
| 158 | + [cantaloupePackageName, platypusPackageName], |
| 159 | + pullRequestBody |
| 160 | + ); |
| 161 | + assert.deepEqual(labelPullRequestMocked.mock.calls[0].arguments, [ |
| 162 | + 1, |
| 163 | + ['run-e2e'], |
| 164 | + ]); |
| 165 | + assert.deepEqual(gitPushMocked.mock.callCount(), 1); |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
| 169 | +const setPackageToPublic = async (packagePath: string) => { |
| 170 | + const packageJson = await readPackageJson(packagePath); |
| 171 | + packageJson.publishConfig = { |
| 172 | + access: 'public', |
| 173 | + }; |
| 174 | + await writePackageJson(packagePath, packageJson); |
| 175 | +}; |
| 176 | + |
| 177 | +const setPackageDependencies = async ( |
| 178 | + packagePath: string, |
| 179 | + dependencies: Record<string, string> |
| 180 | +) => { |
| 181 | + const packageJson = await readPackageJson(packagePath); |
| 182 | + packageJson.dependencies = dependencies; |
| 183 | + await writePackageJson(packagePath, packageJson); |
| 184 | +}; |
| 185 | + |
| 186 | +const assertChangesetFile = async ( |
| 187 | + filePath: string, |
| 188 | + packageNames: string[], |
| 189 | + message: string |
| 190 | +) => { |
| 191 | + const changesetFileContent = await fsp.readFile(filePath, 'utf-8'); |
| 192 | + const frontmatterContent = packageNames |
| 193 | + .map((name) => `'${name}': patch`) |
| 194 | + .join(EOL); |
| 195 | + |
| 196 | + const expectedContent = `---${EOL}${frontmatterContent}${EOL}---${EOL}${EOL}${message}${EOL}`; |
| 197 | + |
| 198 | + assert.deepEqual(changesetFileContent, expectedContent); |
| 199 | +}; |
0 commit comments