Skip to content

Commit

Permalink
refactor: generateTasks doesn't calculate gitDir itself
Browse files Browse the repository at this point in the history
  • Loading branch information
Iiro Jäppinen authored and okonet committed Jun 6, 2019
1 parent 8921989 commit defcdfc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/generateTasks.js
Expand Up @@ -4,17 +4,15 @@ const path = require('path')
const micromatch = require('micromatch')
const pathIsInside = require('path-is-inside')
const { getConfig } = require('./getConfig')
const resolveGitDir = require('./resolveGitDir')

const debug = require('debug')('lint-staged:gen-tasks')

module.exports = async function generateTasks(config, stagedRelFiles) {
module.exports = async function generateTasks(config, gitDir, stagedRelFiles) {
debug('Generating linter tasks')

const normalizedConfig = getConfig(config) // Ensure we have a normalized config
const { linters, globOptions, ignore } = normalizedConfig

const gitDir = await resolveGitDir()
const cwd = process.cwd()
const stagedFiles = stagedRelFiles.map(file => path.resolve(gitDir, file))

Expand Down
2 changes: 1 addition & 1 deletion src/runAll.js
Expand Up @@ -34,7 +34,7 @@ module.exports = async function runAll(config) {
const filenames = files.map(file => file.filename)
debug('Loaded list of staged files in git:\n%O', filenames)

const tasks = (await generateTasks(config, filenames)).map(task => ({
const tasks = (await generateTasks(config, gitDir, filenames)).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: async () =>
new Listr(
Expand Down
34 changes: 24 additions & 10 deletions test/generateTasks.spec.js
Expand Up @@ -53,6 +53,7 @@ describe('generateTasks', () => {
{
'*.js': 'lint'
},
workDir,
['test.js']
)
const commands = result.map(match => match.commands)
Expand All @@ -66,6 +67,7 @@ describe('generateTasks', () => {
'*.js': 'lint'
}
},
workDir,
['test.js']
)
const commands = result.map(match => match.commands)
Expand All @@ -79,6 +81,7 @@ describe('generateTasks', () => {
'*': 'lint'
}
},
workDir,
files
)
task.fileList.forEach(file => {
Expand All @@ -94,6 +97,7 @@ describe('generateTasks', () => {
},
relative: true
},
workDir,
files
)
task.fileList.forEach(file => {
Expand All @@ -103,8 +107,7 @@ describe('generateTasks', () => {

it('should not match non-children files', async () => {
const relPath = path.join(process.cwd(), '..')
resolveGitDir.mockResolvedValueOnce(relPath)
const result = await generateTasks({ ...config }, files)
const result = await generateTasks({ ...config }, relPath, files)
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
Expand All @@ -114,7 +117,7 @@ describe('generateTasks', () => {
})

it('should return an empty file list for linters with no matches.', async () => {
const result = await generateTasks(config, files)
const result = await generateTasks(config, workDir, files)

result.forEach(task => {
if (task.commands === 'unknown-js') {
Expand All @@ -126,7 +129,7 @@ describe('generateTasks', () => {
})

it('should match pattern "*.js"', async () => {
const result = await generateTasks(config, files)
const result = await generateTasks(config, workDir, files)
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
Expand All @@ -142,7 +145,11 @@ describe('generateTasks', () => {
})

it('should match pattern "*.js" and return relative path', async () => {
const result = await generateTasks(Object.assign({}, config, { relative: true }), files)
const result = await generateTasks(
Object.assign({}, config, { relative: true }),
workDir,
files
)
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
Expand All @@ -158,7 +165,7 @@ describe('generateTasks', () => {
})

it('should match pattern "**/*.js"', async () => {
const result = await generateTasks(config, files)
const result = await generateTasks(config, workDir, files)
const linter = result.find(item => item.pattern === '**/*.js')
expect(linter).toEqual({
pattern: '**/*.js',
Expand All @@ -174,7 +181,11 @@ describe('generateTasks', () => {
})

it('should match pattern "**/*.js" with relative path', async () => {
const result = await generateTasks(Object.assign({}, config, { relative: true }), files)
const result = await generateTasks(
Object.assign({}, config, { relative: true }),
workDir,
files
)
const linter = result.find(item => item.pattern === '**/*.js')
expect(linter).toEqual({
pattern: '**/*.js',
Expand All @@ -190,7 +201,7 @@ describe('generateTasks', () => {
})

it('should match pattern "deeper/*.js"', async () => {
const result = await generateTasks(config, files)
const result = await generateTasks(config, workDir, files)
const linter = result.find(item => item.pattern === 'deeper/*.js')
expect(linter).toEqual({
pattern: 'deeper/*.js',
Expand All @@ -200,7 +211,7 @@ describe('generateTasks', () => {
})

it('should match pattern ".hidden/*.js"', async () => {
const result = await generateTasks(config, files)
const result = await generateTasks(config, workDir, files)
const linter = result.find(item => item.pattern === '.hidden/*.js')
expect(linter).toEqual({
pattern: '.hidden/*.js',
Expand All @@ -210,7 +221,7 @@ describe('generateTasks', () => {
})

it('should match pattern "*.{css,js}"', async () => {
const result = await generateTasks(config, files)
const result = await generateTasks(config, workDir, files)
const linter = result.find(item => item.pattern === '*.{css,js}')
expect(linter).toEqual({
pattern: '*.{css,js}',
Expand Down Expand Up @@ -241,6 +252,7 @@ describe('generateTasks', () => {
'TeSt.*': 'lint'
}
},
workDir,
files
)
const linter = result.find(item => item.pattern === 'TeSt.*')
Expand All @@ -261,6 +273,7 @@ describe('generateTasks', () => {
ignore: ['**/ignore/**', '**/ignore.*'],
linters: { [pattern]: commands }
},
workDir,
['ignore/me.js', 'ignore.me.js', 'cool/js.js']
)
expect(result[0]).toEqual({
Expand All @@ -279,6 +292,7 @@ describe('generateTasks', () => {
'../outside/*.js': 'my-cmd'
}
},
workDir,
['root.js', 'prj/test.js', 'outside/test.js', 'outside/test2.js']
)

Expand Down

0 comments on commit defcdfc

Please sign in to comment.