|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { mkdtemp, writeFile, rm } from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { execFile } from 'node:child_process'; |
| 6 | +import { promisify } from 'node:util'; |
| 7 | + |
| 8 | +import { createBranchAndCommit } from '../src/git'; |
| 9 | + |
| 10 | +const execFileAsync = promisify(execFile); |
| 11 | + |
| 12 | +async function runGit(args: string[], cwd: string): Promise<string> { |
| 13 | + const { stdout } = await execFileAsync('git', args, { cwd }); |
| 14 | + return stdout.trim(); |
| 15 | +} |
| 16 | + |
| 17 | +describe('createBranchAndCommit', () => { |
| 18 | + let repoDir: string; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + repoDir = await mkdtemp(path.join(tmpdir(), 'git-branch-')); |
| 22 | + await runGit(['init'], repoDir); |
| 23 | + await runGit(['config', 'user.name', 'Test User'], repoDir); |
| 24 | + await runGit(['config', 'user.email', '[email protected]'], repoDir); |
| 25 | + |
| 26 | + const filePath = path.join(repoDir, 'README.md'); |
| 27 | + await writeFile(filePath, 'initial\n'); |
| 28 | + await runGit(['add', 'README.md'], repoDir); |
| 29 | + await runGit(['commit', '-m', 'Initial commit'], repoDir); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + await rm(repoDir, { recursive: true, force: true }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('creates branch and commits staged changes', async () => { |
| 37 | + const filePath = path.join(repoDir, 'Dockerfile'); |
| 38 | + await writeFile(filePath, 'FROM python:3.11.8-slim\n'); |
| 39 | + |
| 40 | + const result = await createBranchAndCommit({ |
| 41 | + repoPath: repoDir, |
| 42 | + track: '3.11', |
| 43 | + files: ['Dockerfile'], |
| 44 | + commitMessage: 'chore: bump python to 3.11.9', |
| 45 | + authorName: 'Python Bot', |
| 46 | + |
| 47 | + }); |
| 48 | + |
| 49 | + expect(result.branch).toBe('chore/bump-python-3.11'); |
| 50 | + expect(result.commitCreated).toBe(true); |
| 51 | + expect(result.filesCommitted).toEqual(['Dockerfile']); |
| 52 | + |
| 53 | + const currentBranch = await runGit(['rev-parse', '--abbrev-ref', 'HEAD'], repoDir); |
| 54 | + expect(currentBranch).toBe('chore/bump-python-3.11'); |
| 55 | + |
| 56 | + const lastCommit = await runGit(['log', '-1', '--pretty=%s'], repoDir); |
| 57 | + expect(lastCommit).toBe('chore: bump python to 3.11.9'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('skips commit when no changes staged', async () => { |
| 61 | + const result = await createBranchAndCommit({ |
| 62 | + repoPath: repoDir, |
| 63 | + track: '3.12', |
| 64 | + files: ['README.md'], |
| 65 | + commitMessage: 'chore: noop commit', |
| 66 | + }); |
| 67 | + |
| 68 | + expect(result.commitCreated).toBe(false); |
| 69 | + expect(result.filesCommitted).toEqual([]); |
| 70 | + |
| 71 | + const currentBranch = await runGit(['rev-parse', '--abbrev-ref', 'HEAD'], repoDir); |
| 72 | + expect(currentBranch).toBe('chore/bump-python-3.12'); |
| 73 | + |
| 74 | + const status = await runGit(['status', '--short'], repoDir); |
| 75 | + expect(status).toBe(''); |
| 76 | + }); |
| 77 | +}); |
0 commit comments