Skip to content

Commit

Permalink
fix(CLI): --list-different: error out there are differences (#78)
Browse files Browse the repository at this point in the history
* docs(contributor): new contributor

* fix(cli): --list-different: error out when contents has changed
  • Loading branch information
moretti authored and Kent C. Dodds committed Jun 14, 2017
1 parent b116bec commit 7db596b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Expand Up @@ -149,6 +149,17 @@
"contributions": [
"doc"
]
},
{
"login": "moretti",
"name": "Paolo Moretti",
"avatar_url": "https://avatars3.githubusercontent.com/u/1186409?v=3",
"profile": "http://stackoverflow.com/users/63011",
"contributions": [
"bug",
"code",
"test"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -9,7 +9,7 @@ CLI for [`prettier-eslint`][prettier-eslint]
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-16-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -223,7 +223,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Code") [📖](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/3266363?v=3" width="100px;"/><br /><sub>Adam Harris</sub>](https://github.com/aharris88)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=aharris88 "Code") [📖](https://github.com/prettier/prettier-eslint-cli/commits?author=aharris88 "Documentation") [👀](#review-aharris88 "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/622118?v=3" width="100px;"/><br /><sub>Eric McCormick</sub>](https://ericmccormick.io)<br />[👀](#review-edm00se "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/12389411?v=3" width="100px;"/><br /><sub>Joel Sequeira</sub>](https://github.com/joelseq)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=joelseq "Documentation") | [<img src="https://avatars.githubusercontent.com/u/103008?v=3" width="100px;"/><br /><sub>Frank Taillandier</sub>](https://frank.taillandier.me)<br /> | [<img src="https://avatars3.githubusercontent.com/u/292365?v=3" width="100px;"/><br /><sub>Adam Stankiewicz</sub>](http://sheerun.net)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=sheerun "Code") | [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=spudly "Code") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars0.githubusercontent.com/u/11560964?v=3" width="100px;"/><br /><sub>Gandem</sub>](https://github.com/Gandem)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Tests") | [<img src="https://avatars0.githubusercontent.com/u/129991?v=3" width="100px;"/><br /><sub>Matteo Ronchi</sub>](https://github.com/cef62)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Acef62 "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=cef62 "Code") | [<img src="https://avatars2.githubusercontent.com/u/61787?v=3" width="100px;"/><br /><sub>Benoît Zugmeyer</sub>](https://github.com/BenoitZugmeyer)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Tests") | [<img src="https://avatars0.githubusercontent.com/u/5038030?v=3" width="100px;"/><br /><sub>Charlike Mike Reagent</sub>](https://i.am.charlike.online)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Tests") | [<img src="https://avatars0.githubusercontent.com/u/10954870?v=3" width="100px;"/><br /><sub>Dion Dirza</sub>](https://github.com/diondirza)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Adiondirza "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/3297808?v=3" width="100px;"/><br /><sub>mrm007</sub>](https://github.com/mrm007)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Amrm007 "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=mrm007 "Code") | [<img src="https://avatars0.githubusercontent.com/u/193238?v=3" width="100px;"/><br /><sub>Jack Franklin</sub>](http://www.jackfranklin.co.uk)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=jackfranklin "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/17342435?v=3" width="100px;"/><br /><sub>Ryan Zimmerman</sub>](http://www.ryanzim.com)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=RyanZim "Documentation") |
| [<img src="https://avatars0.githubusercontent.com/u/17342435?v=3" width="100px;"/><br /><sub>Ryan Zimmerman</sub>](http://www.ryanzim.com)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=RyanZim "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/1186409?v=3" width="100px;"/><br /><sub>Paolo Moretti</sub>](http://stackoverflow.com/users/63011)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Amoretti "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=moretti "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=moretti "Tests") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand Down
1 change: 1 addition & 0 deletions src/format-files.js
Expand Up @@ -201,6 +201,7 @@ function formatFile(filePath, prettierESLintOptions, cliOptions) {
} else if (cliOptions.listDifferent) {
format$ = format$.map(info => {
if (!info.unchanged) {
process.exitCode = 1
console.log(info.filePath)
}
return info
Expand Down
59 changes: 42 additions & 17 deletions src/format-files.test.js
Expand Up @@ -18,6 +18,10 @@ beforeEach(() => {
fsMock.readFile.mockClear()
})

afterEach(() => {
process.exitCode = 0
})

test('sanity test', async () => {
const globs = ['src/**/1*.js', 'src/**/2*.js']
await formatFiles({_: globs})
Expand Down Expand Up @@ -234,24 +238,45 @@ test('will not blow up if an .eslintignore cannot be found', async () => {
}
})

test('will list different files', async () => {
await formatFiles({
_: ['src/**/1*.js', 'src/**/no-change*.js'],
listDifferent: true,
describe('listDifferent', () => {
test('will list different files', async () => {
await formatFiles({
_: ['src/**/1*.js', 'src/**/no-change*.js'],
listDifferent: true,
})
expect(fsMock.readFile).toHaveBeenCalledTimes(7)
expect(fsMock.writeFile).toHaveBeenCalledTimes(0)

const unchangedOutput = expect.stringMatching(/3.*files were.*unchanged/)
const successOutput = expect.stringMatching(/success.*4.*files/)
expect(console.error).toHaveBeenCalledTimes(2)
expect(console.error).toHaveBeenCalledWith(unchangedOutput)
expect(console.error).toHaveBeenCalledWith(successOutput)

const path =
'/Users/fredFlintstone/Developer/top-secret/footless-carriage/'
expect(console.log).toHaveBeenCalledTimes(4)
expect(console.log).toHaveBeenCalledWith(`${path}stop/log.js`)
expect(console.log).toHaveBeenCalledWith(`${path}stop/index.js`)
expect(console.log).toHaveBeenCalledWith(`${path}index.js`)
expect(console.log).toHaveBeenCalledWith(`${path}start.js`)
})
expect(fsMock.readFile).toHaveBeenCalledTimes(7)
expect(fsMock.writeFile).toHaveBeenCalledTimes(0)

const unchangedOutput = expect.stringMatching(/3.*files were.*unchanged/)
const successOutput = expect.stringMatching(/success.*4.*files/)
expect(console.error).toHaveBeenCalledTimes(2)
expect(console.error).toHaveBeenCalledWith(unchangedOutput)
expect(console.error).toHaveBeenCalledWith(successOutput)
test('will error out when contents did change', async () => {
const fileGlob = 'src/**/1*.js'
await formatFiles({
_: [fileGlob],
listDifferent: true,
})
expect(process.exitCode).toBe(1)
})

const path = '/Users/fredFlintstone/Developer/top-secret/footless-carriage/'
expect(console.log).toHaveBeenCalledTimes(4)
expect(console.log).toHaveBeenCalledWith(`${path}stop/log.js`)
expect(console.log).toHaveBeenCalledWith(`${path}stop/index.js`)
expect(console.log).toHaveBeenCalledWith(`${path}index.js`)
expect(console.log).toHaveBeenCalledWith(`${path}start.js`)
test('wont error out when contents did not change', async () => {
const fileGlob = 'no-change/*.js'
await formatFiles({
_: [fileGlob],
listDifferent: true,
})
expect(process.exitCode).toBe(0)
})
})

0 comments on commit 7db596b

Please sign in to comment.