New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix CLI to avoid waiting for stdin without any input and avoid different outputs #7131
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"stylelint": minor | ||
--- | ||
|
||
Changed: CLI to avoid waiting for stdin without any input |
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,5 @@ | ||
--- | ||
"stylelint": patch | ||
--- | ||
|
||
Fixed: CLI to avoid different outputs on empty files and empty stdin |
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 |
---|---|---|
|
@@ -183,24 +183,22 @@ describe('buildCLI', () => { | |
}); | ||
|
||
describe('CLI', () => { | ||
beforeAll(() => { | ||
beforeEach(() => { | ||
jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
jest.spyOn(process.stdout, 'write').mockImplementation(() => {}); | ||
jest.spyOn(process.stderr, 'write').mockImplementation(() => {}); | ||
jest.spyOn(process, 'stdin', 'get').mockImplementation(() => Readable.from([Buffer.from('')])); | ||
jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
process.exitCode = undefined; | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
it('no arguments', async () => { | ||
jest.spyOn(process, 'stdin', 'get').mockImplementationOnce(() => ({ isTTY: true })); | ||
|
||
it('basic', async () => { | ||
await cli([]); | ||
|
||
expect(process.exit).toHaveBeenCalledWith(0); | ||
|
@@ -223,7 +221,7 @@ describe('CLI', () => { | |
it('--version', async () => { | ||
await cli(['--version']); | ||
|
||
expect(process.exitCode).toBeUndefined(); | ||
expect(process.exit).toHaveBeenCalledWith(0); | ||
|
||
expect(console.log).toHaveBeenCalledTimes(1); | ||
expect(console.log).toHaveBeenCalledWith(expect.stringContaining(pkg.version)); | ||
|
@@ -303,19 +301,46 @@ describe('CLI', () => { | |
); | ||
}); | ||
|
||
it('--stdin', async () => { | ||
it('--stdin with any input from stdin', async () => { | ||
jest.spyOn(process, 'stdin', 'get').mockImplementationOnce(() => Readable.from([])); | ||
|
||
await cli(['--stdin', '--config', fixturesPath('config-no-empty-source.json')]); | ||
|
||
expect(process.exitCode).toBe(2); | ||
|
||
expect(process.stdout.write).not.toHaveBeenCalled(); | ||
expect(process.stderr.write).toHaveBeenCalledTimes(1); | ||
expect(process.stderr.write).toHaveBeenNthCalledWith( | ||
1, | ||
expect(process.stderr.write).toHaveBeenCalledWith( | ||
expect.stringContaining('Unexpected empty source'), | ||
); | ||
}); | ||
|
||
it('--stdin with no input from stdin', async () => { | ||
jest.spyOn(process, 'stdin', 'get').mockImplementationOnce(() => ({ isTTY: true })); | ||
|
||
await cli(['--stdin']); | ||
|
||
expect(process.exitCode).toBe(1); | ||
|
||
expect(process.stdout.write).not.toHaveBeenCalled(); | ||
expect(process.stderr.write).toHaveBeenCalledTimes(1); | ||
expect(process.stderr.write).toHaveBeenCalledWith( | ||
expect.stringContaining('You must pass stylelint a `files` glob or a `code` string'), | ||
); | ||
}); | ||
|
||
it('empty input from stdin without --stdin', async () => { | ||
jest.spyOn(process, 'stdin', 'get').mockImplementation(() => Readable.from([])); | ||
|
||
await cli(['--config', fixturesPath('config-no-empty-source.json')]); | ||
|
||
expect(process.exitCode).toBe(2); | ||
|
||
expect(process.stdout.write).not.toHaveBeenCalled(); | ||
expect(process.stderr.write).toHaveBeenCalledTimes(1); | ||
expect(process.stderr.write).toHaveBeenCalledWith(expect.stringMatching('no-empty-source')); | ||
}); | ||
|
||
it('exits with non zero on unfound module in config', async () => { | ||
await cli([ | ||
'--config', | ||
|
@@ -365,8 +390,8 @@ describe('CLI', () => { | |
|
||
expect(process.exitCode).toBeUndefined(); | ||
|
||
expect(process.stdout.write).toHaveBeenCalledTimes(0); | ||
expect(process.stderr.write).toHaveBeenCalledTimes(0); | ||
expect(process.stdout.write).not.toHaveBeenCalled(); | ||
expect(process.stderr.write).not.toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] To clarify the difference from |
||
}); | ||
|
||
it('--quiet-deprecation-warnings', async () => { | ||
|
@@ -379,7 +404,7 @@ describe('CLI', () => { | |
|
||
expect(process.exitCode).toBe(2); | ||
|
||
expect(process.stdout.write).toHaveBeenCalledTimes(0); | ||
expect(process.stdout.write).not.toHaveBeenCalled(); | ||
expect(process.stderr.write).toHaveBeenCalledTimes(1); | ||
expect(process.stderr.write.mock.calls[0][0]).toEqual( | ||
expect.not.stringContaining('The "color-hex-case" rule is deprecated.'), | ||
|
@@ -418,7 +443,6 @@ describe('CLI', () => { | |
expect(process.exitCode).toBe(2); | ||
|
||
expect(process.stdout.write).not.toHaveBeenCalled(); | ||
expect(process.stdout.write).toHaveBeenCalledTimes(0); | ||
expect(process.stderr.write).toHaveBeenCalledTimes(1); | ||
expect(stripAnsi(process.stderr.write.mock.calls[0][0])).toContain( | ||
'Invalid option "--globby-options". The value "wrong" is not valid JSON object.', | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note] To avoid effects on each
it
.