Skip to content

Commit 8f89b98

Browse files
committed
chore: wip
1 parent 3766ad9 commit 8f89b98

File tree

3 files changed

+590
-128
lines changed

3 files changed

+590
-128
lines changed

test/cli.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { afterEach, beforeEach, describe, expect, it, spyOn } from 'bun:test'
2+
import { execSync } from 'node:child_process'
3+
import fs from 'node:fs'
4+
import os from 'node:os'
5+
import path from 'node:path'
6+
7+
describe('gitlint CLI', () => {
8+
let tempDir: string
9+
let mockConsoleError: ReturnType<typeof spyOn>
10+
let mockConsoleLog: ReturnType<typeof spyOn>
11+
12+
beforeEach(() => {
13+
// Create temp directory for test files
14+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gitlint-test-'))
15+
16+
// Mock console output to avoid polluting test output
17+
mockConsoleError = spyOn(console, 'error').mockImplementation(() => {})
18+
mockConsoleLog = spyOn(console, 'log').mockImplementation(() => {})
19+
})
20+
21+
afterEach(() => {
22+
// Clean up temp directory
23+
try {
24+
fs.rmSync(tempDir, { recursive: true, force: true })
25+
}
26+
catch (_error) {
27+
// Ignore errors during cleanup
28+
}
29+
30+
// Restore mocks
31+
mockConsoleError.mockRestore()
32+
mockConsoleLog.mockRestore()
33+
})
34+
35+
// Helper to run CLI with given arguments
36+
function runCLI(args: string[] = [], input?: string): string {
37+
const binPath = path.resolve('bin/gitlint')
38+
const cmd = `${binPath} ${args.join(' ')}`
39+
40+
try {
41+
return execSync(cmd, {
42+
encoding: 'utf8',
43+
input,
44+
stdio: ['pipe', 'pipe', 'pipe'],
45+
})
46+
}
47+
catch (error: any) {
48+
return error.stdout || error.stderr || error.message
49+
}
50+
}
51+
52+
it('should show help when no arguments are provided', () => {
53+
// Run CLI with --help flag
54+
const output = runCLI(['--help'])
55+
56+
// Check that the output contains expected help text
57+
expect(output).toContain('gitlint')
58+
expect(output).toContain('Usage')
59+
expect(output).toContain('Commands')
60+
expect(output).toContain('Options')
61+
})
62+
63+
it('should validate a valid commit message from stdin', () => {
64+
const validMessage = 'feat: add new feature'
65+
66+
try {
67+
runCLI([], validMessage)
68+
// If we get here, it means the command succeeded (exit code 0)
69+
// This is expected for a valid message
70+
}
71+
catch (_error) {
72+
// Should not throw for a valid message
73+
expect('No error').toBe('Error occurred')
74+
}
75+
})
76+
77+
it('should fail validation for an invalid commit message', () => {
78+
const invalidMessage = 'Invalid commit message'
79+
80+
// Instead of checking for an error being thrown directly,
81+
// we just check the output content
82+
const output = runCLI([], invalidMessage)
83+
expect(output).toContain('validation failed') // Error message contains this text
84+
})
85+
86+
it('should validate a commit message from a file', () => {
87+
// Create a test file with a valid commit message
88+
const commitFile = path.join(tempDir, 'COMMIT_EDITMSG')
89+
const validMessage = 'feat: add new feature'
90+
fs.writeFileSync(commitFile, validMessage)
91+
92+
// Run gitlint with the file
93+
const output = runCLI([commitFile])
94+
95+
// Should exit cleanly for a valid message (no specific output to check)
96+
// We just verify it doesn't fail
97+
expect(output).not.toContain('validation failed')
98+
})
99+
100+
it('should work with the --edit flag for git hook integration', () => {
101+
// Create a test file simulating git's COMMIT_EDITMSG
102+
const commitFile = path.join(tempDir, 'COMMIT_EDITMSG')
103+
const validMessage = 'feat: add new feature'
104+
fs.writeFileSync(commitFile, validMessage)
105+
106+
// Run gitlint with --edit flag
107+
const output = runCLI(['--edit', commitFile])
108+
109+
// Should exit cleanly for a valid message
110+
expect(output).not.toContain('validation failed')
111+
})
112+
113+
it('should show version information', () => {
114+
const output = runCLI(['version'])
115+
116+
// Should contain a version number in the output
117+
expect(output).toMatch(/\d+\.\d+\.\d+/) // Matches semver-like output
118+
})
119+
120+
it('should handle verbose output mode', () => {
121+
const validMessage = 'feat: add new feature'
122+
123+
// Run with verbose flag
124+
const output = runCLI(['--verbose'], validMessage)
125+
126+
// Should contain extra output in verbose mode
127+
expect(output).toContain('passed')
128+
})
129+
})

0 commit comments

Comments
 (0)