-
-
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.
feat: Allow config to be provided via command-line (#304)
Configuration file can be provided via a -c or --config commandline parameter. This allows users to be explicit with where the configuration lives rather than relying on cosmiconfig path search.
- Loading branch information
1 parent
eacb3d2
commit 54809ae
Showing
8 changed files
with
107 additions
and
15 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
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,12 @@ | ||
const actual = require.requireActual('cosmiconfig') | ||
const mock = jest.genMockFromModule('cosmiconfig') | ||
|
||
function cosmiconfig(name, options) { | ||
if (options.configPath) { | ||
return actual(name, options) | ||
} | ||
|
||
return mock(name, options) | ||
} | ||
|
||
module.exports = jest.fn(cosmiconfig) |
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,6 @@ | ||
{ | ||
"verbose": true, | ||
"linters": { | ||
"*": "mytask" | ||
} | ||
} |
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,38 +1,66 @@ | ||
import { makeConsoleMock } from 'consolemock' | ||
import cosmiconfig from 'cosmiconfig' | ||
import path from 'path' | ||
import lintStaged from '../src/index' | ||
|
||
const replaceSerializer = (from, to) => ({ | ||
test: val => typeof val === 'string' && from.test(val), | ||
print: val => val.replace(from, to) | ||
}) | ||
|
||
jest.mock('cosmiconfig') | ||
|
||
describe('lintStaged', () => { | ||
let logger | ||
|
||
beforeEach(() => { | ||
console = makeConsoleMock() | ||
logger = makeConsoleMock() | ||
}) | ||
|
||
it('should ouput config in verbose mode', async () => { | ||
it('should output config in verbose mode', async () => { | ||
const config = { | ||
verbose: true, | ||
linters: { | ||
'*': 'mytask' | ||
} | ||
} | ||
cosmiconfig.mockImplementationOnce(() => Promise.resolve({ config })) | ||
await lintStaged() | ||
expect(console.printHistory()).toMatchSnapshot() | ||
await lintStaged(logger) | ||
expect(logger.printHistory()).toMatchSnapshot() | ||
}) | ||
|
||
it('should not output config in non verbose mode', async () => { | ||
const config = { | ||
'*': 'mytask' | ||
} | ||
cosmiconfig.mockImplementationOnce(() => Promise.resolve({ config })) | ||
await lintStaged() | ||
expect(console.printHistory()).toMatchSnapshot() | ||
await lintStaged(logger) | ||
expect(logger.printHistory()).toMatchSnapshot() | ||
}) | ||
|
||
it('should load config file when specified', async () => { | ||
await lintStaged(logger, path.join(__dirname, '__mocks__', 'my-config.json')) | ||
expect(logger.printHistory()).toMatchSnapshot() | ||
}) | ||
|
||
it('should print helpful error message when config file is not found', async () => { | ||
cosmiconfig.mockImplementationOnce(() => Promise.resolve(null)) | ||
await lintStaged() | ||
expect(console.printHistory()).toMatchSnapshot() | ||
await lintStaged(logger) | ||
expect(logger.printHistory()).toMatchSnapshot() | ||
}) | ||
|
||
it('should print helpful error message when explicit config file is not found', async () => { | ||
const nonExistentConfig = 'fake-config-file.yml' | ||
|
||
// Serialize Windows, Linux and MacOS paths consistently | ||
expect.addSnapshotSerializer( | ||
replaceSerializer( | ||
/Error: ENOENT: no such file or directory, open '([^']+)'/, | ||
`Error: ENOENT: no such file or directory, open '${nonExistentConfig}'` | ||
) | ||
) | ||
|
||
await lintStaged(logger, nonExistentConfig) | ||
expect(logger.printHistory()).toMatchSnapshot() | ||
}) | ||
}) |