-
-
Notifications
You must be signed in to change notification settings - Fork 412
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
remove gitDir config option #327
Changes from 7 commits
bf7dd09
3673070
f27d20d
daad31d
ff34559
f145865
8d9a79c
904ed83
0e174ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const findParentDir = require('find-parent-dir') | ||
|
||
module.exports = function resolveGitDir(gitDir) { | ||
return gitDir ? path.resolve(gitDir) : process.cwd() | ||
module.exports = function resolveGitDir() { | ||
return findParentDir.sync(process.cwd(), '.git') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import path from 'path' | ||
import os from 'os' | ||
import generateTasks from '../src/generateTasks' | ||
import resolveGitDir from '../src/resolveGitDir' | ||
|
||
const files = [ | ||
'test.js', | ||
|
@@ -22,10 +23,12 @@ const files = [ | |
'.hidden/test.txt' | ||
] | ||
|
||
// Mocks get hoisted | ||
jest.mock('../src/resolveGitDir.js') | ||
const workDir = path.join(os.tmpdir(), 'tmp-lint-staged') | ||
resolveGitDir.mockReturnValue(workDir) | ||
|
||
const config = { | ||
gitDir: workDir, | ||
linters: { | ||
'*.js': 'root-js', | ||
'**/*.js': 'any-js', | ||
|
@@ -37,6 +40,14 @@ const config = { | |
} | ||
|
||
describe('generateTasks', () => { | ||
beforeEach(() => { | ||
jest.spyOn(process, 'cwd').mockReturnValue(workDir) | ||
}) | ||
|
||
afterEach(() => { | ||
process.cwd.mockRestore() | ||
}) | ||
|
||
it('should work with simple configuration', () => { | ||
const result = generateTasks( | ||
{ | ||
|
@@ -77,8 +88,21 @@ describe('generateTasks', () => { | |
}) | ||
}) | ||
|
||
it('should not match non-children files', () => { | ||
const relPath = path.resolve(path.join(process.cwd(), '..')) | ||
resolveGitDir.mockReturnValueOnce(relPath) | ||
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. @okonet check for filtering out files that aren't in the |
||
const result = generateTasks(Object.assign({}, config), files) | ||
const linter = result.find(item => item.pattern === '*.js') | ||
expect(linter).toEqual({ | ||
pattern: '*.js', | ||
commands: 'root-js', | ||
fileList: [] | ||
}) | ||
}) | ||
|
||
it('should return an empty file list for linters with no matches.', () => { | ||
const result = generateTasks(config, files) | ||
|
||
result.forEach(task => { | ||
if (task.commands === 'unknown-js') { | ||
expect(task.fileList.length).toEqual(0) | ||
|
@@ -88,23 +112,6 @@ describe('generateTasks', () => { | |
}) | ||
}) | ||
|
||
it('should match pattern "*.js" for relative path', () => { | ||
const relPath = path.resolve(path.join(process.cwd(), '..')) | ||
const result = generateTasks(Object.assign({}, config, { gitDir: '..' }), files) | ||
const linter = result.find(item => item.pattern === '*.js') | ||
expect(linter).toEqual({ | ||
pattern: '*.js', | ||
commands: 'root-js', | ||
fileList: [ | ||
`${relPath}/test.js`, | ||
`${relPath}/deeper/test.js`, | ||
`${relPath}/deeper/test2.js`, | ||
`${relPath}/even/deeper/test.js`, | ||
`${relPath}/.hidden/test.js` | ||
].map(path.normalize) | ||
}) | ||
}) | ||
|
||
it('should match pattern "*.js"', () => { | ||
const result = generateTasks(config, files) | ||
const linter = result.find(item => item.pattern === '*.js') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,24 +105,6 @@ describe('getConfig', () => { | |
) | ||
}) | ||
|
||
it('should set gitDir', () => { | ||
expect(getConfig({})).toEqual( | ||
expect.objectContaining({ | ||
gitDir: '.' | ||
}) | ||
) | ||
|
||
expect( | ||
getConfig({ | ||
gitDir: '../path' | ||
}) | ||
).toEqual( | ||
expect.objectContaining({ | ||
gitDir: '../path' | ||
}) | ||
) | ||
}) | ||
|
||
it('should set linters', () => { | ||
expect(getConfig()).toEqual( | ||
expect.objectContaining({ | ||
|
@@ -192,7 +174,6 @@ describe('getConfig', () => { | |
const src = { | ||
concurrent: false, | ||
chunkSize: 2, | ||
gitDir: '/to', | ||
globOptions: { | ||
matchBase: false, | ||
dot: true | ||
|
@@ -216,21 +197,31 @@ describe('validateConfig', () => { | |
it('should throw and should print validation errors for invalid config', () => { | ||
const invalidAdvancedConfig = { | ||
foo: false, | ||
chunkSize: 'string', | ||
gitDir: 111 | ||
chunkSize: 'string' | ||
} | ||
expect(() => validateConfig(getConfig(invalidAdvancedConfig))).toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
it('should not throw and should print validation warnings for mixed config', () => { | ||
const invalidMixedConfig = { | ||
gitDir: './path/to/packagejson/', | ||
concurrent: false, | ||
'*.js': ['eslint --fix', 'git add'] | ||
} | ||
expect(() => validateConfig(getConfig(invalidMixedConfig))).not.toThrow() | ||
expect(console.printHistory()).toMatchSnapshot() | ||
}) | ||
|
||
it('should not throw and should print validation warnings for old config', () => { | ||
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. Can we change this test title to |
||
const invalidMixedConfig = { | ||
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. Rename variable to |
||
gitDir: '../', | ||
linters: { | ||
'*.js': ['eslint --fix', 'git add'] | ||
} | ||
} | ||
expect(() => validateConfig(getConfig(invalidMixedConfig))).not.toThrow() | ||
expect(console.printHistory()).toMatchSnapshot() | ||
}) | ||
|
||
it('should not throw and should print nothing for simple valid config', () => { | ||
const validSimpleConfig = { | ||
'*.js': ['eslint --fix', 'git add'] | ||
|
@@ -241,7 +232,7 @@ describe('validateConfig', () => { | |
|
||
it('should not throw and should print nothing for advanced valid config', () => { | ||
const validAdvancedConfig = { | ||
gitDir: '.', | ||
concurrent: false, | ||
linters: { | ||
'*.js': ['eslint --fix', 'git add'] | ||
} | ||
|
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.
@okonet Does the formatting for this text seem a bit odd to you?