Skip to content

Commit

Permalink
test: adjust tests for Windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 13, 2021
1 parent 533c43c commit 3a35037
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
15 changes: 3 additions & 12 deletions test/gitWorkflow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { writeFile } from '../lib/file'
import GitWorkflow from '../lib/gitWorkflow'
import { getInitialState } from '../lib/state'
import { createTempDir } from './utils/tempDir'
import { isWindowsActions } from './utils/gitHubActions'
import { normalizeWindowsNewlines } from './utils/crossPlatform'

jest.mock('../lib/file.js')
jest.unmock('execa')
Expand Down Expand Up @@ -177,17 +177,8 @@ describe('gitWorkflow', () => {
const ctx = getInitialState()
await gitWorkflow.hideUnstagedChanges(ctx)

if (isWindowsActions()) {
/**
* @todo `git mv` in GitHub Windows runners seem to remove
* the ending line terminator in this case.
*/
const received = await readFile('TEST.md')
const normalized = received.trimEnd() + `\n`
expect(normalized).toStrictEqual(origContent)
} else {
expect(await readFile('TEST.md')).toStrictEqual(origContent)
}
/** @todo `git mv` in GitHub Windows runners seem to add `\r\n` newlines in this case. */
expect(normalizeWindowsNewlines(await readFile('TEST.md'))).toStrictEqual(origContent)
})
})

Expand Down
10 changes: 7 additions & 3 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import execGitBase from '../lib/execGit'
import lintStaged from '../lib/index'
import { replaceSerializer } from './utils/replaceSerializer'
import { createTempDir } from './utils/tempDir'
import { isWindowsActions, normalizeWindowsNewlines } from './utils/crossPlatform'

jest.setTimeout(20000)

Expand All @@ -36,7 +37,7 @@ let cwd
// Get file content, coercing Windows `\r\n` newlines to `\n`
const readFile = async (filename, dir = cwd) => {
const file = await fs.readFile(path.resolve(dir, filename), { encoding: 'utf-8' })
return file.replace(/(\r\n|\r|\n)/gm, '\n')
return normalizeWindowsNewlines(file)
}

// Append to file, creating if it doesn't exist
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('lint-staged', () => {
await execGit('init')
await execGit(['config', 'user.name', '"test"'])
await execGit(['config', 'user.email', '"test@test.com"'])
if (isWindowsActions()) await execGit(['config', 'core.autocrlf', 'input'])
await appendFile('README.md', '# Test\n')
await execGit(['add', 'README.md'])
await execGit(['commit', '-m initial commit'])
Expand Down Expand Up @@ -224,11 +226,12 @@ describe('lint-staged', () => {
await execGit(['add', 'test.js'])

// Edit pretty file but do not stage changes
const appended = '\nconsole.log("test");\n'
const appended = `\nconsole.log("test");\n`
await appendFile('test.js', appended)

// Run lint-staged with `prettier --list-different` and commit pretty file
await gitCommit({ config: { '*.js': 'prettier --list-different' } })

expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG [STARTED] Preparing...
Expand Down Expand Up @@ -261,7 +264,8 @@ describe('lint-staged', () => {
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(testJsFilePretty + appended)
/** @todo `git` in GitHub Windows runners seem to add `\r\n` newlines in this case. */
expect(normalizeWindowsNewlines(await readFile('test.js'))).toEqual(testJsFilePretty + appended)
})

it('Should commit partial change from partially staged file when no errors from linter and linter modifies file', async () => {
Expand Down
8 changes: 8 additions & 0 deletions test/utils/crossPlatform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Whether the current environment is a GitHub Actions runner under Windows */
export const isWindowsActions = () => {
const { GITHUB_ACTIONS, RUNNER_OS } = process.env
return GITHUB_ACTIONS === 'true' && RUNNER_OS === 'Windows'
}

/** Replace Windows `\r\n` newlines with `\n` */
export const normalizeWindowsNewlines = (input) => input.replace(/(\r\n|\r|\n)/gm, '\n')
4 changes: 0 additions & 4 deletions test/utils/gitHubActions.js

This file was deleted.

0 comments on commit 3a35037

Please sign in to comment.