/
getStagedFiles.spec.js
79 lines (64 loc) · 2.7 KB
/
getStagedFiles.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import path from 'path'
import normalize from 'normalize-path'
import { getStagedFiles } from '../lib/getStagedFiles'
import { execGit } from '../lib/execGit'
jest.mock('../lib/execGit')
// Windows filepaths
const normalizePath = (input) => normalize(path.resolve('/', input))
describe('getStagedFiles', () => {
afterEach(() => {
jest.clearAllMocks()
})
it('should return array of file names', async () => {
execGit.mockImplementationOnce(async () => 'foo.js\u0000bar.js\u0000')
const staged = await getStagedFiles({ cwd: '/' })
// Windows filepaths
expect(staged).toEqual([normalizePath('/foo.js'), normalizePath('/bar.js')])
expect(execGit).toHaveBeenLastCalledWith(
['diff', '--name-only', '-z', '--diff-filter=ACMR', '--staged'],
{ cwd: '/' }
)
})
it('should return empty array when no staged files', async () => {
execGit.mockImplementationOnce(async () => '')
const staged = await getStagedFiles()
expect(staged).toEqual([])
})
it('should return null in case of error', async () => {
execGit.mockImplementationOnce(async () => {
throw new Error('fatal: not a git repository (or any of the parent directories): .git')
})
const staged = await getStagedFiles({})
expect(staged).toEqual(null)
})
it('should support overriding diff trees with ...', async () => {
execGit.mockImplementationOnce(async () => 'foo.js\u0000bar.js\u0000')
const staged = await getStagedFiles({ cwd: '/', diff: 'master...my-branch' })
// Windows filepaths
expect(staged).toEqual([normalizePath('/foo.js'), normalizePath('/bar.js')])
expect(execGit).toHaveBeenLastCalledWith(
['diff', '--name-only', '-z', '--diff-filter=ACMR', 'master...my-branch'],
{ cwd: '/' }
)
})
it('should support overriding diff trees with multiple args', async () => {
execGit.mockImplementationOnce(async () => 'foo.js\u0000bar.js\u0000')
const staged = await getStagedFiles({ cwd: '/', diff: 'master my-branch' })
// Windows filepaths
expect(staged).toEqual([normalizePath('/foo.js'), normalizePath('/bar.js')])
expect(execGit).toHaveBeenLastCalledWith(
['diff', '--name-only', '-z', '--diff-filter=ACMR', 'master', 'my-branch'],
{ cwd: '/' }
)
})
it('should support overriding diff-filter', async () => {
execGit.mockImplementationOnce(async () => 'foo.js\u0000bar.js\u0000')
const staged = await getStagedFiles({ cwd: '/', diffFilter: 'ACDMRTUXB' })
// Windows filepaths
expect(staged).toEqual([normalizePath('/foo.js'), normalizePath('/bar.js')])
expect(execGit).toHaveBeenLastCalledWith(
['diff', '--name-only', '-z', '--diff-filter=ACDMRTUXB', '--staged'],
{ cwd: '/' }
)
})
})