forked from beakerbrowser/datignore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
61 lines (52 loc) · 1.9 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const datignore = require('./index')
const test = require('ava')
const anymatch = require('anymatch')
const TEST_PATHS = [
'/foo.txt',
'/bar.txt',
'/baz.txt',
'/.git',
'/.dat',
'/sub/foo.txt',
'/sub/bar.txt',
'/sub/baz.txt'
]
test('Default rules for empty string', t => {
t.deepEqual(datignore.toAnymatchRules(''), ['/.git', '/.dat'])
})
test('Default rules for bad params', t => {
t.deepEqual(datignore.toAnymatchRules(), ['/.git', '/.dat'])
t.deepEqual(datignore.toAnymatchRules(false), ['/.git', '/.dat'])
})
test('One rule', t => {
t.deepEqual(datignore.toAnymatchRules('/foo.txt'), ['/foo.txt', '/.git', '/.dat'])
runAnymatches(t, '/foo.txt', ['/foo.txt', '/.git', '/.dat'])
})
test('Two rules', t => {
t.deepEqual(datignore.toAnymatchRules('/foo.txt\n/bar.txt'), ['/foo.txt', '/bar.txt', '/.git', '/.dat'])
runAnymatches(t, '/foo.txt\n/bar.txt', ['/foo.txt', '/bar.txt', '/.git', '/.dat'])
})
test('Empty newlines are ignored', t => {
t.deepEqual(datignore.toAnymatchRules('/foo.txt\n\n\n\n/bar.txt\n'), ['/foo.txt', '/bar.txt', '/.git', '/.dat'])
})
test('Carraige returns are ignored', t => {
t.deepEqual(datignore.toAnymatchRules('/foo.txt\r\n/bar.txt\r\n'), ['/foo.txt', '/bar.txt', '/.git', '/.dat'])
})
test('Rules without a preceding slash are given syntax to match anywhere in the folders', t => {
t.deepEqual(datignore.toAnymatchRules('foo.txt\nbar.txt'), ['**/foo.txt', '**/bar.txt', '/.git', '/.dat'])
runAnymatches(t, 'foo.txt\nbar.txt', ['/foo.txt', '/bar.txt', '/.git', '/.dat', '/sub/foo.txt', '/sub/bar.txt'])
})
function runAnymatches (t, str, matches) {
var rules = datignore.toAnymatchRules(str)
for (let p of TEST_PATHS) {
if (anymatch(rules, p)) {
if (!matches.includes(p)) {
t.fail(`${p} should not have matched ${str}`)
}
} else {
if (matches.includes(p)) {
t.fail(`${p} should have matched ${str}`)
}
}
}
}