|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { mkdtempSync, mkdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { dirname, join } from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import test from 'node:test'; |
| 8 | + |
| 9 | +const script = fileURLToPath(new URL('./check-changeset-coverage.mjs', import.meta.url)); |
| 10 | +const packages = { |
| 11 | + 'packages/browser': 'posthog-js', |
| 12 | + 'packages/rrweb/rrweb': '@posthog/rrweb', |
| 13 | + 'packages/rrweb/types': '@posthog/rrweb-types', |
| 14 | + 'packages/rrweb-extra': 'rrweb-extra', |
| 15 | + 'packages/node': 'posthog-node', |
| 16 | +}; |
| 17 | +const mapping = { releasePackagePaths: { 'packages/rrweb/': 'posthog-js' } }; |
| 18 | + |
| 19 | +function report(t, { files = [], declared = [], config } = {}) { |
| 20 | + const cwd = realpathSync(mkdtempSync(join(tmpdir(), 'changeset-hygiene-'))); |
| 21 | + t.after(() => rmSync(cwd, { recursive: true, force: true })); |
| 22 | + function write(path, content) { |
| 23 | + mkdirSync(dirname(join(cwd, path)), { recursive: true }); |
| 24 | + writeFileSync(join(cwd, path), content); |
| 25 | + } |
| 26 | + const git = (...args) => execFileSync('git', args, { cwd, stdio: 'pipe' }); |
| 27 | + git('init', '-b', 'main'); |
| 28 | + git('config', 'user.email', 'test@example.com'); |
| 29 | + git('config', 'user.name', 'Test'); |
| 30 | + git('config', 'commit.gpgsign', 'false'); |
| 31 | + git('config', 'core.hooksPath', '/dev/null'); |
| 32 | + // Stub only workspace discovery. Exercise the real script, git diff and frontmatter parser. |
| 33 | + write( |
| 34 | + 'bin/pnpm', |
| 35 | + `#!/bin/sh\nprintf '%s\\n' '${JSON.stringify( |
| 36 | + Object.entries(packages).map(([path, name]) => ({ path: join(cwd, path), name })), |
| 37 | + )}'\n`, |
| 38 | + ); |
| 39 | + execFileSync('chmod', ['+x', join(cwd, 'bin/pnpm')]); |
| 40 | + if (config) write('.changeset/hygiene.json', JSON.stringify(config)); |
| 41 | + git('add', '.'); |
| 42 | + git('commit', '-m', 'base'); |
| 43 | + git('update-ref', 'refs/remotes/origin/main', 'HEAD'); |
| 44 | + for (const file of files) write(file, 'changed\n'); |
| 45 | + if (declared.length) { |
| 46 | + write( |
| 47 | + '.changeset/change.md', |
| 48 | + `---\n${declared.map((n) => `'${n}': patch`).join('\n')}\n---\n\nChange\n`, |
| 49 | + ); |
| 50 | + } |
| 51 | + git('add', '.'); |
| 52 | + git('commit', '--allow-empty', '-m', 'change'); |
| 53 | + return execFileSync(process.execPath, [script], { |
| 54 | + cwd, |
| 55 | + encoding: 'utf8', |
| 56 | + env: { ...process.env, BASE_REF: 'main', PATH: `${cwd}/bin:${process.env.PATH}` }, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +test('rrweb-only changes are covered by a browser changeset', (t) => { |
| 61 | + assert.equal( |
| 62 | + report(t, { |
| 63 | + files: ['packages/rrweb/rrweb/src/index.ts', 'packages/rrweb/types/src/index.ts'], |
| 64 | + declared: ['posthog-js'], |
| 65 | + config: mapping, |
| 66 | + }), |
| 67 | + 'body=\n', |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +test('rrweb changes without a changeset request only the browser package', (t) => { |
| 72 | + const body = report(t, { files: ['packages/rrweb/rrweb/src/index.ts'], config: mapping }); |
| 73 | + assert.match(body, /`posthog-js` is modified but this PR has no changeset/); |
| 74 | + assert.doesNotMatch(body, /@posthog\/rrweb/); |
| 75 | +}); |
| 76 | + |
| 77 | +test('an rrweb changeset does not satisfy the browser release requirement', (t) => { |
| 78 | + const body = report(t, { |
| 79 | + files: ['packages/rrweb/rrweb/src/index.ts'], |
| 80 | + declared: ['@posthog/rrweb'], |
| 81 | + config: mapping, |
| 82 | + }); |
| 83 | + assert.match(body, /"posthog-js": patch/); |
| 84 | + assert.match( |
| 85 | + body, |
| 86 | + /\*\*Declared in a changeset but no source files modified:\*\*\n- `@posthog\/rrweb`/, |
| 87 | + ); |
| 88 | +}); |
| 89 | + |
| 90 | +test('unmapped packages still need their own changesets', (t) => { |
| 91 | + const body = report(t, { |
| 92 | + files: ['packages/rrweb/rrweb/src/index.ts', 'packages/node/src/index.ts'], |
| 93 | + declared: ['posthog-js'], |
| 94 | + config: mapping, |
| 95 | + }); |
| 96 | + assert.match(body, /`posthog-node` is modified but not declared/); |
| 97 | + assert.doesNotMatch(body, /@posthog\/rrweb/); |
| 98 | +}); |
| 99 | + |
| 100 | +test('path matching respects directory boundaries', (t) => { |
| 101 | + const body = report(t, { files: ['packages/rrweb-extra/src/index.ts'], config: mapping }); |
| 102 | + assert.match(body, /`rrweb-extra` is modified/); |
| 103 | + assert.doesNotMatch(body, /posthog-js/); |
| 104 | +}); |
| 105 | + |
| 106 | +test('the most specific mapping wins, with or without trailing slashes', (t) => { |
| 107 | + assert.equal( |
| 108 | + report(t, { |
| 109 | + files: ['packages/rrweb/rrweb/src/index.ts', 'packages/rrweb/types/src/index.ts'], |
| 110 | + declared: ['posthog-js', 'posthog-node'], |
| 111 | + config: { |
| 112 | + releasePackagePaths: { |
| 113 | + 'packages/rrweb': 'posthog-js', |
| 114 | + 'packages/rrweb/types/': 'posthog-node', |
| 115 | + }, |
| 116 | + }, |
| 117 | + }), |
| 118 | + 'body=\n', |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +test('changelog and manifest changes remain ignored under mapped paths', (t) => { |
| 123 | + assert.equal( |
| 124 | + report(t, { |
| 125 | + files: ['packages/rrweb/rrweb/CHANGELOG.md', 'packages/rrweb/types/package.json'], |
| 126 | + config: mapping, |
| 127 | + }), |
| 128 | + 'body=\n', |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +test('no config preserves normal workspace coverage', (t) => { |
| 133 | + assert.equal( |
| 134 | + report(t, { |
| 135 | + files: ['packages/rrweb/rrweb/src/index.ts'], |
| 136 | + declared: ['@posthog/rrweb'], |
| 137 | + }), |
| 138 | + 'body=\n', |
| 139 | + ); |
| 140 | +}); |
| 141 | + |
| 142 | +test('transitive re-export config still works', (t) => { |
| 143 | + assert.equal( |
| 144 | + report(t, { |
| 145 | + files: ['packages/node/src/index.ts'], |
| 146 | + declared: ['posthog-node', 'posthog-js'], |
| 147 | + config: { transitiveReExports: { 'posthog-node': ['posthog-js'] } }, |
| 148 | + }), |
| 149 | + 'body=\n', |
| 150 | + ); |
| 151 | +}); |
| 152 | + |
| 153 | +test('browser and bundled source changes share one changeset', (t) => { |
| 154 | + assert.equal( |
| 155 | + report(t, { |
| 156 | + files: ['packages/browser/src/index.ts', 'packages/rrweb/rrweb/src/index.ts'], |
| 157 | + declared: ['posthog-js'], |
| 158 | + config: mapping, |
| 159 | + }), |
| 160 | + 'body=\n', |
| 161 | + ); |
| 162 | +}); |
| 163 | + |
| 164 | +for (const [path, target] of [ |
| 165 | + ['packages/rrweb/', 'typo'], |
| 166 | + ['', 'posthog-js'], |
| 167 | + ['../packages/rrweb', 'posthog-js'], |
| 168 | +]) { |
| 169 | + test(`invalid mapping ${path} -> ${target} keeps workspace coverage`, (t) => { |
| 170 | + assert.equal( |
| 171 | + report(t, { |
| 172 | + files: ['packages/rrweb/rrweb/src/index.ts'], |
| 173 | + declared: ['@posthog/rrweb'], |
| 174 | + config: { releasePackagePaths: { [path]: target } }, |
| 175 | + }), |
| 176 | + 'body=\n', |
| 177 | + ); |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +test('browser changesets without source changes are still reported as extra', (t) => { |
| 182 | + assert.match( |
| 183 | + report(t, { declared: ['posthog-js'], config: mapping }), |
| 184 | + /Changeset declares `posthog-js` but no source files in that package changed/, |
| 185 | + ); |
| 186 | +}); |
0 commit comments