Skip to content

Commit

Permalink
1.0.14: fixes #5: fixes RangeError with options.absolute as true
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelzhang committed Feb 26, 2019
1 parent 0ffbdec commit 1690391
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glob-gitignore",
"version": "1.0.13",
"version": "1.0.14",
"description": "Extends `glob` with support for filtering files according to gitignore rules and exposes an optional Promise API with NO performance issues",
"main": "src/index.js",
"scripts": {
Expand Down
29 changes: 19 additions & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ const createShouldIgnore = options => {
}

const ig = ignore().add(ignores)
const filter = ig.createFilter()
const _filter = ig.createFilter()

return {
ignores: filepath => {
filepath = relative(filepath, cwd)
if (!filepath) {
return false
}
const filterABS = f => {
const filepath = relative(f, cwd)
if (!filepath) {
return true
}

return !filter(filepath)
},
return _filter(filepath)
}

const filter = options.absolute
? filterABS
: _filter

return {
// Check directories during traversing
ignores: f => !filterABS(f),
// Filter result
filter,
opts
}
Expand Down Expand Up @@ -90,7 +97,8 @@ const createTasks = (patterns, options) => {
if (positivesCount === 1) {
return {
join ([files]) {
// _GlobSync only filters _readdir, so glob results should be filtered again.
// _GlobSync only filters _readdir,
// so glob results should be filtered again.
return files.filter(filter)
},

Expand All @@ -113,6 +121,7 @@ const createTasks = (patterns, options) => {
})

return difference(union(...positives), ...negatives)
// The same reason as above
.filter(filter)
},

Expand Down
156 changes: 103 additions & 53 deletions test/glob-gitignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,38 @@ const CASES = [
}
]

process.chdir(fixture())
const DIR = fixture()
const ABS = f => path.join(DIR, f)

process.chdir(DIR)

const RUNNER = {
glob (patterns, i) {
return glob(patterns, {ignore: i})
glob (patterns, i, options) {
return glob(patterns, Object.assign({ignore: i}, options))
},

sync (patterns, i) {
sync (patterns, i, options) {
try {
return Promise.resolve(sync(patterns, {ignore: i}))
return Promise.resolve(
sync(patterns, Object.assign({ignore: i}, options))
)
} catch (e) {
return Promise.reject(e)
}
},

options_sync (patterns, i) {
options_sync (patterns, i, options) {
try {
return Promise.resolve(glob(patterns, {ignore: i, sync: true}))
return Promise.resolve(
glob(patterns, Object.assign({ignore: i, sync: true}, options))
)
} catch (e) {
return Promise.reject(e)
}
}
}


CASES.forEach(({
const run = ({
// description
d,
// patterns
Expand All @@ -79,56 +85,100 @@ CASES.forEach(({
// error
e,
// only
only,
// options
o
}) => {
['glob', 'sync', 'options_sync'].forEach(type => {
const _test = o === true
}, type,

// Extra config
{
desc = '',
absolute = false
} = {}) => {
const _test = only === true
? test.only
: only === type
? test.only
: o === type
? test.only
: test

_test(`${type}: ${d}`, t =>
RUNNER[type](p, i)
.then(
files => {
if (e) {
t.fail('error expected')
return
}

t.deepEqual(files.sort(), r.sort(), 'fails to compare expected')

// Only race for string pattern.
if (typeof p !== 'string') {
return
}

return new Promise((resolve, reject) => {
vanilla(p, (err, f) => {
if (err) {
return reject(err)
}

const filter = ignore().add(i).createFilter()

t.deepEqual(files.sort(), f.filter(filter).sort(), 'race with node-glob')
resolve()
})
})
},
: test

const extra_desc = desc
? `, ${desc}`
: ''

const options = absolute
? Object.assign({
absolute: true,
o
})
: o

_test(`${type}: ${d}${extra_desc}`, t =>
RUNNER[type](p, i, options)
.then(
files => {
if (e) {
t.fail('error expected')
return
}

err => {
if (!e) {
t.fail('should not fail')
/* eslint no-console: 'off' */
console.error(err)
}
t.deepEqual(
files.sort(),
absolute
? r.sort().map(ABS)
: r.sort(),
'fails to compare expected'
)

// Only race for string pattern.
if (typeof p !== 'string') {
return
}

t.is(err.message, e)
return new Promise((resolve, reject) => {
vanilla(p, (err, f) => {
if (err) {
return reject(err)
}

const filter = ignore().add(i).createFilter()
const globbed_files = f.filter(filter).sort()
const expected = absolute
? globbed_files.map(ABS)
: globbed_files

t.deepEqual(
files.sort(),
expected,
'race with node-glob'
)
resolve()
})
})
},

err => {
if (!e) {
t.fail('should not fail')
/* eslint no-console: 'off' */
console.error(err)
}
)

t.is(err.message, e)
}
)
)
}


CASES.forEach(c => {
['glob', 'sync', 'options_sync'].forEach(type => {
// run(c, type)

// #5
run(c, type, {
desc: 'absolute: true',
absolute: true
})
})
})

Expand Down

0 comments on commit 1690391

Please sign in to comment.