Files
lint-staged/test/unit/getStagedFiles.spec.js

130 lines
4.2 KiB
JavaScript

import path from 'node:path'
import { afterEach, describe, it, vi } from 'vitest'
import { normalizePath } from '../../lib/normalizePath.js'
vi.mock('../../lib/execGit.js', () => ({
execGit: vi.fn(async () => ''),
}))
const { execGit } = await import('../../lib/execGit.js')
const { getStagedFiles } = await import('../../lib/getStagedFiles.js')
// Windows filepaths
const normalizeWindowsPath = (input) => normalizePath(path.resolve('/', input))
describe('getStagedFiles', () => {
afterEach(() => {
vi.clearAllMocks()
})
it('should return array of file names', async ({ expect }) => {
execGit.mockImplementationOnce(
async () =>
':000000 100644 0000000 0000000 A\u0000foo.js\u0000:000000 100644 0000000 0000000 A\u0000bar.js\u0000'
)
const staged = await getStagedFiles({ cwd: '/' })
// Windows filepaths
expect(staged).toEqual([
{ filepath: normalizeWindowsPath('/foo.js'), status: 'A' },
{ filepath: normalizeWindowsPath('/bar.js'), status: 'A' },
])
expect(execGit).toHaveBeenCalledExactlyOnceWith(
['diff', '--diff-filter=ACMR', '--staged', '--raw', '-z'],
{ cwd: '/' }
)
})
it('should allow colons in file names', async ({ expect }) => {
execGit.mockImplementationOnce(
async () =>
':000000 100644 0000000 0000000 A\u0000foo.js\u0000:000000 100644 0000000 0000000 A\u0000bar:qux.js\u0000'
)
const staged = await getStagedFiles({ cwd: '/' })
// Windows filepaths
expect(staged).toEqual([
{ filepath: normalizeWindowsPath('/foo.js'), status: 'A' },
{ filepath: normalizeWindowsPath('/bar:qux.js'), status: 'A' },
])
expect(execGit).toHaveBeenCalledExactlyOnceWith(
['diff', '--diff-filter=ACMR', '--staged', '--raw', '-z'],
{ cwd: '/' }
)
})
it('should return empty array when no staged files', async ({ expect }) => {
const staged = await getStagedFiles()
expect(staged).toEqual([])
})
it('should return null in case of error', async ({ expect }) => {
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 ({ expect }) => {
execGit.mockImplementationOnce(
async () =>
':000000 100644 0000000 0000000 A\u0000foo.js\u0000:000000 100644 0000000 0000000 A\u0000bar.js\u0000'
)
const staged = await getStagedFiles({ cwd: '/', diff: 'main...my-branch' })
// Windows filepaths
expect(staged).toEqual([
{ filepath: normalizeWindowsPath('/foo.js'), status: 'A' },
{ filepath: normalizeWindowsPath('/bar.js'), status: 'A' },
])
expect(execGit).toHaveBeenCalledExactlyOnceWith(
['diff', '--diff-filter=ACMR', 'main...my-branch', '--raw', '-z'],
{ cwd: '/' }
)
})
it('should support overriding diff trees with multiple args', async ({ expect }) => {
execGit.mockImplementationOnce(
async () =>
':000000 100644 0000000 0000000 A\u0000foo.js\u0000:000000 100644 0000000 0000000 A\u0000bar.js\u0000'
)
const staged = await getStagedFiles({ cwd: '/', diff: 'main my-branch' })
// Windows filepaths
expect(staged).toEqual([
{ filepath: normalizeWindowsPath('/foo.js'), status: 'A' },
{ filepath: normalizeWindowsPath('/bar.js'), status: 'A' },
])
expect(execGit).toHaveBeenCalledExactlyOnceWith(
['diff', '--diff-filter=ACMR', 'main', 'my-branch', '--raw', '-z'],
{ cwd: '/' }
)
})
it('should support overriding diff-filter', async ({ expect }) => {
execGit.mockImplementationOnce(
async () =>
':000000 100644 0000000 0000000 A\u0000foo.js\u0000:000000 100644 0000000 0000000 A\u0000bar.js\u0000'
)
const staged = await getStagedFiles({ cwd: '/', diffFilter: 'ACDMRTUXB' })
// Windows filepaths
expect(staged).toEqual([
{ filepath: normalizeWindowsPath('/foo.js'), status: 'A' },
{ filepath: normalizeWindowsPath('/bar.js'), status: 'A' },
])
expect(execGit).toHaveBeenCalledExactlyOnceWith(
['diff', '--diff-filter=ACDMRTUXB', '--staged', '--raw', '-z'],
{ cwd: '/' }
)
})
})