import assert from '@remix-run/assert' import * as cp from 'node:child_process' import * as fs from 'node:fs' import * as os from 'node:os' import * as path from 'node:path' import { describe, it, test } from '@remix-run/test' import { findVersionIntroductionCommit, getLocalTagTarget, parsePrNumber } from './git.ts' describe('parsePrNumber', () => { it('recognizes squash merge subjects', () => { assert.equal(parsePrNumber('Fix a bug (#123)'), 123) }) it('recognizes merge commit subjects', () => { assert.equal(parsePrNumber('Merge pull request #456 from example/fix'), 456) }) it('ignores ordinary issue references', () => { assert.equal(parsePrNumber('Fix #123'), null) assert.equal(parsePrNumber('Follow up on (#123) with more changes'), null) assert.equal(parsePrNumber('Fix a bug'), null) }) }) function execGit(args: string[], cwd: string): string { return cp.execFileSync('git', args, { cwd, stdio: 'pipe', encoding: 'utf-8' }).trim() } test('findVersionIntroductionCommit returns the commit where the target version was introduced', () => { let tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'remix-git-test-')) let packageJsonPath = 'packages/example/package.json' let absolutePackageJsonPath = path.join(tempDir, packageJsonPath) try { execGit(['init'], tempDir) execGit(['config', 'user.name', 'Test Bot'], tempDir) execGit(['config', 'user.email', 'test@example.com'], tempDir) fs.mkdirSync(path.dirname(absolutePackageJsonPath), { recursive: true }) fs.writeFileSync( absolutePackageJsonPath, `${JSON.stringify({ name: '@remix-run/example', version: '1.0.0' }, null, 2)}\n`, ) execGit(['add', '.'], tempDir) execGit(['commit', '-m', 'initial release'], tempDir) let initialCommit = execGit(['rev-parse', 'HEAD'], tempDir) fs.writeFileSync(path.join(tempDir, 'README.md'), 'hello\n') execGit(['add', 'README.md'], tempDir) execGit(['commit', '-m', 'docs change'], tempDir) fs.writeFileSync( absolutePackageJsonPath, `${JSON.stringify({ name: '@remix-run/example', version: '1.1.0' }, null, 2)}\n`, ) execGit(['add', packageJsonPath], tempDir) execGit(['commit', '-m', 'bump version'], tempDir) let versionBumpCommit = execGit(['rev-parse', 'HEAD'], tempDir) fs.writeFileSync( absolutePackageJsonPath, `${JSON.stringify( { name: '@remix-run/example', version: '1.1.0', description: 'metadata only', }, null, 2, )}\n`, ) execGit(['add', packageJsonPath], tempDir) execGit(['commit', '-m', 'metadata tweak'], tempDir) assert.equal( findVersionIntroductionCommit(packageJsonPath, '1.1.0', tempDir), versionBumpCommit, ) assert.equal(findVersionIntroductionCommit(packageJsonPath, '1.0.0', tempDir), initialCommit) assert.equal(findVersionIntroductionCommit(packageJsonPath, '9.9.9', tempDir), null) execGit(['tag', 'example@1.1.0', versionBumpCommit], tempDir) assert.equal(getLocalTagTarget('example@1.1.0', tempDir), versionBumpCommit) } finally { fs.rmSync(tempDir, { recursive: true, force: true }) } })