From 20c7344aca83022d981e76aad765caa93f64b5ef Mon Sep 17 00:00:00 2001 From: Noah Negin-Ulster Date: Thu, 9 Mar 2023 18:20:56 -0500 Subject: [PATCH] refactor: improve test coverage and checkout command --- e2e-tests/tests/issues/issue-601.test.ts | 17 ++++++++++++++--- packages/changelog/src/prependChangelogFile.ts | 4 +++- packages/git/src/gitCommands.ts | 7 +++++-- packages/publish/src/commitPublishChanges.ts | 18 +++++++++--------- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/e2e-tests/tests/issues/issue-601.test.ts b/e2e-tests/tests/issues/issue-601.test.ts index fd7cfac5..8d15b6e1 100644 --- a/e2e-tests/tests/issues/issue-601.test.ts +++ b/e2e-tests/tests/issues/issue-601.test.ts @@ -61,6 +61,12 @@ describe('Issue #601', () => { if (error2) console.error(error2) expect(error2).toBeUndefined() + // At this point we expected change 2 followed by change 1 in the changelog + let remoteChangelog = ( + await exec('git cat-file blob origin/main:packages/pkg-1/CHANGELOG.md') + ).stdout + expect(remoteChangelog).toMatch(/change 2.*change 1/s) + // Switch back to change_3 which is now "out of sync" with main await exec('git checkout main') await exec('git reset --hard change_3') @@ -68,7 +74,7 @@ describe('Issue #601', () => { // If we attempt to publish, we'll have a conflict with the CHANGELOG.md file // since our change_3 will only have change 1 and change 3 and will be missing change 2. // We'll validate this: - let remoteChangelog = ( + remoteChangelog = ( await exec('git cat-file blob origin/main:packages/pkg-1/CHANGELOG.md') ).stdout expect(remoteChangelog).toEqual(expect.stringContaining('change 1')) @@ -85,9 +91,14 @@ describe('Issue #601', () => { remoteChangelog = ( await exec('git cat-file blob origin/main:packages/pkg-1/CHANGELOG.md') ).stdout - expect(remoteChangelog).toEqual( - expect.stringMatching('change 1.*change 2.*change 3'), + expect(remoteChangelog).toMatch(/change 3.*change 2.*change 1/s) + expect((await (await exec('git describe --abbrev=0')).stdout).trim()).toBe( + 'pkg-1@0.3.0', ) + + // NOTE: the hard reset we do disassociates the git tag with the HEAD of main. + // This causes the change_3 monodeploy run to include changes 2 and 3. This is + // just a quirk of the test scenario. }, }), ) diff --git a/packages/changelog/src/prependChangelogFile.ts b/packages/changelog/src/prependChangelogFile.ts index c0349218..5fc7bb3b 100644 --- a/packages/changelog/src/prependChangelogFile.ts +++ b/packages/changelog/src/prependChangelogFile.ts @@ -93,7 +93,9 @@ const prependChangelogFile = async ({ try { await gitCheckout({ files: [changelogGlob] }, { cwd: config.cwd, context }) } catch { - logging.debug('Force refreshing changelogs failed.', { report: context.report }) + logging.debug('Force refreshing changelogs failed. Ignoring.', { + report: context.report, + }) } } } diff --git a/packages/git/src/gitCommands.ts b/packages/git/src/gitCommands.ts index ecbe4406..8aea0465 100644 --- a/packages/git/src/gitCommands.ts +++ b/packages/git/src/gitCommands.ts @@ -17,11 +17,14 @@ export const gitCheckout = async ( { files }: { files: string[] }, { cwd, context }: { cwd: string; context?: YarnContext }, ): Promise => { - const { stdout: branch } = await git('rev-parse --abbrev-ref --symbolic-full-name @{u}', { + const { stdout: branch } = await git('rev-parse --abbrev-ref --symbolic-full-name @\\{u\\}', { + cwd, + context, + }) + await git(`checkout ${branch.trim()} -- ${files.map((f) => `"${f}"`).join(' ')}`, { cwd, context, }) - await git(`checkout ${branch.trim()} -- ${files.join(' ')}`, { cwd, context }) } export const gitResolveSha = async ( diff --git a/packages/publish/src/commitPublishChanges.ts b/packages/publish/src/commitPublishChanges.ts index 4c208d2b..1d1e4ec7 100644 --- a/packages/publish/src/commitPublishChanges.ts +++ b/packages/publish/src/commitPublishChanges.ts @@ -47,15 +47,6 @@ export const createPublishCommit = async ({ } } - if (config.git.tag && gitTags?.size) { - // Tag commit - await createReleaseGitTags({ - config, - context, - gitTags, - }) - } - if (config.git.push && config.autoCommit) { await gitPull({ cwd: config.cwd, @@ -66,6 +57,15 @@ export const createPublishCommit = async ({ }) } + if (config.git.tag && gitTags?.size) { + // Tag commit + await createReleaseGitTags({ + config, + context, + gitTags, + }) + } + return { headSha: await gitResolveSha('HEAD', { cwd: config.cwd, context }) } }