Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
234 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const mockStream = () => ({ | ||
once: jest.fn(), | ||
on: jest.fn(), | ||
removeListener: jest.fn(), | ||
pipe: jest.fn(), | ||
}); | ||
|
||
const mockExeca = jest.fn().mockReturnValue({ | ||
stdout: mockStream(), | ||
stderr: mockStream(), | ||
kill: () => {}, | ||
}); | ||
|
||
const mockExecaSync = jest.fn().mockReturnValue({ | ||
stdout: '', | ||
stderr: '', | ||
kill: () => {}, | ||
}); | ||
|
||
module.exports = mockExeca; | ||
module.exports.sync = mockExecaSync; |
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,20 @@ | ||
const prettierMock = { | ||
format: jest.fn().mockImplementation(input => 'formatted:' + input), | ||
resolveConfig: { | ||
sync: jest.fn().mockImplementation(file => ({ file })), | ||
}, | ||
getSupportInfo: jest.fn().mockReturnValue({ | ||
languages: [ | ||
{ | ||
name: 'JavaScript', | ||
extensions: ['.js'], | ||
}, | ||
{ | ||
name: 'Markdown', | ||
extensions: ['.md'], | ||
}, | ||
], | ||
}), | ||
}; | ||
|
||
module.exports = prettierMock; |
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
it('has not tests', () => { | ||
// yet... | ||
import mock from 'mock-fs'; | ||
|
||
import prettyQuick from '..'; | ||
|
||
jest.mock('execa'); | ||
|
||
afterEach(() => mock.restore()); | ||
|
||
test('throws an error when no vcs is found', () => { | ||
mock({ | ||
'root/README.md': '', | ||
}); | ||
|
||
expect(() => prettyQuick('root')).toThrowError( | ||
'Unable to detect a source control manager.' | ||
); | ||
}); |
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,162 @@ | ||
import mock from 'mock-fs'; | ||
import execa from 'execa'; | ||
import fs from 'fs'; | ||
|
||
import prettyQuick from '..'; | ||
|
||
jest.mock('execa'); | ||
|
||
afterEach(() => { | ||
mock.restore(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const mockGitFs = () => { | ||
mock({ | ||
'root/.git': {}, | ||
'root/foo.js': 'foo()', | ||
'root/bar.md': '# foo', | ||
}); | ||
execa.sync.mockImplementation((command, args) => { | ||
if (command !== 'git') { | ||
throw new Error(`unexpected command: ${command}`); | ||
} | ||
switch (args[0]) { | ||
case 'ls-files': | ||
return { stdout: '' }; | ||
case 'diff': | ||
return { stdout: './foo.js\n' + './bar.md\n' }; | ||
case 'add': | ||
return { stdout: '' }; | ||
default: | ||
throw new Error(`unexpected arg0: ${args[0]}`); | ||
} | ||
}); | ||
}; | ||
|
||
describe('with git', () => { | ||
test('calls `git merge-base`', () => { | ||
mock({ | ||
'root/.git': {}, | ||
}); | ||
|
||
prettyQuick('root'); | ||
|
||
expect(execa.sync).toHaveBeenCalledWith( | ||
'git', | ||
['merge-base', 'HEAD', 'master'], | ||
{ cwd: 'root' } | ||
); | ||
}); | ||
|
||
test('with --staged does NOT call `git merge-base`', () => { | ||
mock({ | ||
'root/.git': {}, | ||
}); | ||
|
||
prettyQuick('root'); | ||
|
||
expect(execa.sync).not.toHaveBeenCalledWith('git', [ | ||
'merge-base', | ||
'HEAD', | ||
'master', | ||
]); | ||
}); | ||
|
||
test('calls `git diff --name-only` with revision', () => { | ||
mock({ | ||
'root/.git': {}, | ||
}); | ||
|
||
prettyQuick('root', { since: 'banana' }); | ||
|
||
expect(execa.sync).toHaveBeenCalledWith( | ||
'git', | ||
['diff', '--name-only', '--diff-filter=ACMRTUB', 'banana'], | ||
{ cwd: 'root' } | ||
); | ||
}); | ||
|
||
test('calls `git ls-files`', () => { | ||
mock({ | ||
'root/.git': {}, | ||
}); | ||
|
||
prettyQuick('root', { since: 'banana' }); | ||
|
||
expect(execa.sync).toHaveBeenCalledWith( | ||
'git', | ||
['ls-files', '--others', '--exclude-standard'], | ||
{ cwd: 'root' } | ||
); | ||
}); | ||
|
||
test('calls onFoundSinceRevision with return value from `git merge-base`', () => { | ||
const onFoundSinceRevision = jest.fn(); | ||
|
||
mock({ | ||
'root/.git': {}, | ||
}); | ||
execa.sync.mockReturnValue({ stdout: 'banana' }); | ||
|
||
prettyQuick('root', { onFoundSinceRevision }); | ||
|
||
expect(onFoundSinceRevision).toHaveBeenCalledWith('git', 'banana'); | ||
}); | ||
|
||
test('calls onFoundChangedFiles with changed files', () => { | ||
const onFoundChangedFiles = jest.fn(); | ||
mockGitFs(); | ||
|
||
prettyQuick('root', { since: 'banana', onFoundChangedFiles }); | ||
|
||
expect(onFoundChangedFiles).toHaveBeenCalledWith(['./foo.js', './bar.md']); | ||
}); | ||
|
||
test('calls onWriteFile with changed files', () => { | ||
const onWriteFile = jest.fn(); | ||
mockGitFs(); | ||
|
||
prettyQuick('root', { since: 'banana', onWriteFile }); | ||
|
||
expect(onWriteFile).toHaveBeenCalledWith('./foo.js'); | ||
expect(onWriteFile).toHaveBeenCalledWith('./bar.md'); | ||
}); | ||
|
||
test('writes formatted files to disk', () => { | ||
const onWriteFile = jest.fn(); | ||
|
||
mockGitFs(); | ||
|
||
prettyQuick('root', { since: 'banana', onWriteFile }); | ||
|
||
expect(fs.readFileSync('root/foo.js', 'utf8')).toEqual('formatted:foo()'); | ||
expect(fs.readFileSync('root/bar.md', 'utf8')).toEqual('formatted:# foo'); | ||
}); | ||
|
||
test('with --staged stages changed files', () => { | ||
mockGitFs(); | ||
|
||
prettyQuick('root', { since: 'banana', staged: true }); | ||
|
||
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './foo.js'], { | ||
cwd: 'root', | ||
}); | ||
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './bar.md'], { | ||
cwd: 'root', | ||
}); | ||
}); | ||
|
||
test('without --staged does NOT stage changed files', () => { | ||
mockGitFs(); | ||
|
||
prettyQuick('root', { since: 'banana' }); | ||
|
||
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.js'], { | ||
cwd: 'root', | ||
}); | ||
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], { | ||
cwd: 'root', | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,20 +1,22 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { resolveConfig, format } from 'prettier'; | ||
import { join } from 'path'; | ||
|
||
export default (files, { config, onWriteFile }) => { | ||
for (const file of files) { | ||
export default (directory, files, { config, onWriteFile } = {}) => { | ||
for (const relative of files) { | ||
const file = join(directory, relative); | ||
const options = resolveConfig.sync(file, { config }); | ||
const input = readFileSync(file, 'utf8'); | ||
const output = format( | ||
input, | ||
Object.assign(options, { | ||
Object.assign({}, options, { | ||
filepath: file, | ||
}) | ||
); | ||
|
||
if (output !== input) { | ||
writeFileSync(file, output); | ||
onWriteFile && onWriteFile(file); | ||
onWriteFile && onWriteFile(relative); | ||
} | ||
} | ||
}; |
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