-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix reading config from stdin, introduced in v14.0.0 (#1317)
- Loading branch information
Showing
8 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createInterface } from 'node:readline' | ||
|
||
/** | ||
* Returns a promise resolving to the first line written to stdin after invoking. | ||
* @warn will never resolve if called after writing to stdin | ||
* | ||
* @returns {Promise<string>} | ||
*/ | ||
export const readStdin = () => { | ||
const readline = createInterface({ input: process.stdin }) | ||
|
||
return new Promise((resolve) => { | ||
readline.prompt() | ||
readline.on('line', (line) => { | ||
readline.close() | ||
resolve(line) | ||
}) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import '../integration/__mocks__/resolveConfig.js' | ||
|
||
import { jest } from '@jest/globals' | ||
|
||
import { withGitIntegration } from '../integration/__utils__/withGitIntegration.js' | ||
import * as fileFixtures from '../integration/__fixtures__/files.js' | ||
import * as configFixtures from '../integration/__fixtures__/configs.js' | ||
|
||
import { getLintStagedExecutor } from './__utils__/getLintStagedExecutor.js' | ||
|
||
jest.setTimeout(20000) | ||
jest.retryTimes(2) | ||
|
||
describe('lint-staged', () => { | ||
test( | ||
'reads config from stdin', | ||
withGitIntegration(async ({ cwd, execGit, readFile, writeFile }) => { | ||
const lintStaged = getLintStagedExecutor(cwd) | ||
|
||
// Stage ugly file | ||
await writeFile('test file.js', fileFixtures.uglyJS) | ||
await execGit(['add', 'test file.js']) | ||
|
||
// Run lint-staged with config from stdin | ||
await lintStaged('-c -', { | ||
input: JSON.stringify(configFixtures.prettierWrite), | ||
}) | ||
|
||
// Nothing was wrong so file was prettified | ||
expect(await readFile('test file.js')).toEqual(fileFixtures.prettyJS) | ||
}) | ||
) | ||
|
||
test( | ||
'fails when stdin config is not valid', | ||
withGitIntegration(async ({ cwd, execGit, readFile, writeFile }) => { | ||
const lintStaged = getLintStagedExecutor(cwd) | ||
|
||
// Stage ugly file | ||
await writeFile('test file.js', fileFixtures.uglyJS) | ||
await execGit(['add', 'test file.js']) | ||
|
||
// Break JSON by removing } from the end | ||
const brokenJSONConfig = JSON.stringify(configFixtures.prettierWrite).replace('"}', '"') | ||
|
||
// Run lint-staged with broken config from stdin | ||
await expect(lintStaged('-c -', { input: brokenJSONConfig })).rejects.toThrowError( | ||
'Failed to read config from stdin' | ||
) | ||
|
||
// File was not edited | ||
expect(await readFile('test file.js')).toEqual(fileFixtures.uglyJS) | ||
}) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { stdin } from 'mock-stdin' | ||
|
||
import { readStdin } from '../../lib/readStdin.js' | ||
|
||
const mockStdin = stdin() | ||
|
||
describe('readStdin', () => { | ||
it('should return stdin', async () => { | ||
const stdinPromise = readStdin() | ||
|
||
mockStdin.send('Hello, world!') | ||
mockStdin.end() | ||
|
||
expect(await stdinPromise).toEqual('Hello, world!') | ||
}) | ||
}) |