Files
lint-staged/test/integration/partially-staged-changes.test.js

131 lines
5.4 KiB
JavaScript

import { describe, test } from 'vitest'
import * as configFixtures from './__fixtures__/configs.js'
import * as fileFixtures from './__fixtures__/files.js'
import { addConfigFileSerializer } from './__utils__/addConfigFileSerializer.js'
import { withGitIntegration } from './__utils__/withGitIntegration.js'
addConfigFileSerializer()
describe('lint-staged', () => {
test(
'commits partial change from partially staged file when no errors from linter',
withGitIntegration(async ({ appendFile, execGit, expect, gitCommit, readFile }) => {
await appendFile('.lintstagedrc.json', JSON.stringify(configFixtures.oxfmtListDifferent))
// Stage pretty file
await appendFile('test.js', fileFixtures.prettyJS)
await execGit(['add', 'test.js'])
// Edit pretty file but do not stage changes
const appended = `\nconsole.log("test");\n`
await appendFile('test.js', appended)
const output = await gitCommit()
expect(output).toMatch('Hiding unstaged changes to partially staged files')
expect(output).toMatch('Staging changes from tasks…')
expect(output).toMatch('Restoring unstaged changes…')
// Nothing is wrong, so a new commit is created and file is pretty
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('test')
// Latest commit contains pretty file
// `git show` strips empty line from here here
expect(await execGit(['show', 'HEAD:test.js'])).toEqual(fileFixtures.prettyJS.trim())
// Since edit was not staged, the file is still modified
const status = await execGit(['status'])
expect(status).toMatch('modified: test.js')
expect(status).toMatch('no changes added to commit')
expect(await readFile('test.js')).toEqual(fileFixtures.prettyJS + appended)
})
)
test(
'commits partial change from partially staged file when no errors from linter and linter modifies file',
withGitIntegration(async ({ appendFile, execGit, expect, gitCommit, readFile }) => {
await appendFile('.lintstagedrc.json', JSON.stringify(configFixtures.oxfmtWrite))
// Stage ugly file
await appendFile('test.js', fileFixtures.uglyJS)
await execGit(['add', 'test.js'])
// Edit ugly file but do not stage changes
const appended = '\n\nconsole.log("test");\n'
await appendFile('test.js', appended)
await gitCommit()
// Nothing is wrong, so a new commit is created and file is pretty
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('test')
// Latest commit contains pretty file
// `git show` strips empty line from here here
expect(await execGit(['show', 'HEAD:test.js'])).toEqual(fileFixtures.prettyJS.trim())
// Nothing is staged
const status = await execGit(['status'])
expect(status).toMatch('modified: test.js')
expect(status).toMatch('no changes added to commit')
// File is pretty, and has been edited
expect(await readFile('test.js')).toEqual(fileFixtures.prettyJS + appended)
})
)
test(
'fails to commit partial change from partially staged file when errors from linter',
withGitIntegration(async ({ appendFile, execGit, expect, gitCommit, readFile }) => {
await appendFile('.lintstagedrc.json', JSON.stringify(configFixtures.oxfmtListDifferent))
// Stage ugly file
await appendFile('test.js', fileFixtures.uglyJS)
await execGit(['add', 'test.js'])
// Edit ugly file but do not stage changes
const appended = '\nconsole.log("test");\n'
await appendFile('test.js', appended)
const status = await execGit(['status'])
// Run lint-staged with `oxfmt --list-different` to break the linter
await expect(gitCommit(configFixtures.oxfmtListDifferent)).rejects.toThrow(
'Reverting to original state because of errors'
)
// Something was wrong so the repo is returned to original state
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('initial commit')
expect(await execGit(['status'])).toEqual(status)
expect(await readFile('test.js')).toEqual(fileFixtures.uglyJS + appended)
})
)
test(
'fails to commit partial change from partially staged file when errors from linter and linter modifies files',
withGitIntegration(async ({ appendFile, execGit, expect, gitCommit, readFile }) => {
await appendFile('.lintstagedrc.json', JSON.stringify(configFixtures.oxfmtWrite))
// Add unfixable file to commit so `oxfmt --write` breaks
await appendFile('test.js', fileFixtures.invalidJS)
await execGit(['add', 'test.js'])
// Edit unfixable file but do not stage changes
const appended = '\nconsole.log("test");\n'
await appendFile('test.js', appended)
const status = await execGit(['status'])
await expect(gitCommit()).rejects.toThrow('Reverting to original state because of errors')
// Something was wrong so the repo is returned to original state
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('initial commit')
expect(await execGit(['status'])).toEqual(status)
expect(await readFile('test.js')).toEqual(fileFixtures.invalidJS + appended)
})
)
})