Skip to content

Commit

Permalink
test: add e2e test for linting difficult filename with ESLint + Prett…
Browse files Browse the repository at this point in the history
…ier (#1413)
  • Loading branch information
iiroj committed May 10, 2024
1 parent 28cc901 commit 1353320
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .changeset/three-dogs-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
7 changes: 7 additions & 0 deletions test/__utils__/getRepoRootPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const THIS_FILE_DIR = path.dirname(fileURLToPath(import.meta.url))

/** @returns the path to the `lint-staged` repo root */
export const getRepoRootPath = () => path.join(THIS_FILE_DIR, '../../')
5 changes: 2 additions & 3 deletions test/e2e/__utils__/getLintStagedExecutor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { execaCommand } from 'execa'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
import { getRepoRootPath } from '../../__utils__/getRepoRootPath.js'

const lintStagedBin = path.resolve(__dirname, '../../../bin/lint-staged.js')
const lintStagedBin = path.resolve(getRepoRootPath(), 'bin/lint-staged.js')

/**
* @param {string} cwd
Expand Down
34 changes: 34 additions & 0 deletions test/e2e/special-characters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import path from 'node:path'

import { withGitIntegration } from '../integration/__utils__/withGitIntegration.js'
import * as fileFixtures from '../integration/__fixtures__/files.js'
import { getRepoRootPath } from '../__utils__/getRepoRootPath.js'

import { getLintStagedExecutor } from './__utils__/getLintStagedExecutor.js'

describe('lint-staged', () => {
const REPO_ESLINT_CONFIG = path.join(getRepoRootPath(), '.eslintrc.json')

const BASIC_CONFIG = {
'*.js': [`eslint --config ${REPO_ESLINT_CONFIG} --fix`, 'prettier --write'],
}

test.each([['$test.js'], ['[test].js'], ['(test).js']])(
'supports linting "%s" with ESLint + Prettier',
async (filename) =>
withGitIntegration(async ({ cwd, execGit, readFile, writeFile }) => {
const lintStaged = getLintStagedExecutor(cwd)

await writeFile('.lintstagedrc.json', JSON.stringify(BASIC_CONFIG))

await writeFile(filename, fileFixtures.uglyJS)
await execGit(['add', filename])

const result = await lintStaged()

expect(result.exitCode).toEqual(0)

expect(await readFile(filename)).toEqual(fileFixtures.prettyJS)
})
)
})
9 changes: 4 additions & 5 deletions test/unit/index2.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { jest } from '@jest/globals'
import makeConsoleMock from 'consolemock'

import { getRepoRootPath } from '../__utils__/getRepoRootPath.js'

import { getMockListr2 } from './__utils__/getMockListr2.js'

const { Listr } = await getMockListr2()

const __dirname = path.dirname(fileURLToPath(import.meta.url))

const MOCK_CONFIG_FILE = path.join(__dirname, '__mocks__', 'my-config.json')
const MOCK_STAGED_FILE = path.resolve(__dirname, '__mocks__', 'sample.js')
const MOCK_CONFIG_FILE = path.join(getRepoRootPath(), 'test/__mocks__/my-config.json')
const MOCK_STAGED_FILE = path.join(getRepoRootPath(), 'test/__mocks__/sample.js')

jest.unstable_mockModule('../../lib/execGit.js', () => ({
execGit: jest.fn(async () => {
Expand Down
3 changes: 1 addition & 2 deletions test/unit/resolveGitRepo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { fileURLToPath } from 'node:url'
import { normalizePath } from '../../lib/normalizePath.js'
import { determineGitDir, resolveGitRepo } from '../../lib/resolveGitRepo.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const __dirname = path.dirname(fileURLToPath(import.meta.url))

describe('resolveGitRepo', () => {
it('should resolve to current working dir when .git is in the same dir', async () => {
Expand Down

0 comments on commit 1353320

Please sign in to comment.