eltro/test/cli.test.mjs

288 lines
9.7 KiB
JavaScript
Raw Normal View History

import t from '../lib/eltro.mjs'
2020-03-31 17:27:36 +00:00
import assert from '../lib/assert.mjs'
import { CLI, getFiles, fileMatches } from '../lib/cli.mjs'
t.describe('CLI', function() {
2020-03-31 17:27:36 +00:00
let cli = new CLI()
t.test('#constructor() give default options', function() {
let cliTest = new CLI()
assert.strictEqual(cliTest.reporter, 'list')
assert.strictEqual(cliTest.ignoreOnly, false)
assert.strictEqual(cliTest.timeout, 2000)
assert.deepEqual(cliTest.targets, ['test/**'])
assert.deepEqual(cliTest.files, [])
assert.notOk(cliTest.errored)
2020-03-31 17:27:36 +00:00
})
/*****************************************
* #parseOptions()
*****************************************/
t.describe('#parseOptions()', function() {
t.test('should not do anything if no options', function() {
2020-03-31 17:27:36 +00:00
cli.reporter = 'list'
cli.parseOptions([])
assert.strictEqual(cli.reporter, 'list')
assert.notOk(cli.errored)
})
t.test('should support overriding reporter with shorthand option', function() {
2020-03-31 17:27:36 +00:00
cli.reporter = 'list'
cli.parseOptions(['-r', 'dot'])
assert.strictEqual(cli.reporter, 'dot')
assert.notOk(cli.errored)
})
t.test('should support overriding reporter with long option', function() {
2020-03-31 17:27:36 +00:00
cli.reporter = 'list'
cli.parseOptions(['--reporter', 'dot'])
assert.strictEqual(cli.reporter, 'dot')
assert.notOk(cli.errored)
})
t.test('should support enabling ignore-only long option', function() {
cli.ignoreOnly = false
cli.parseOptions(['--ignore-only', '-r', 'dot'])
assert.strictEqual(cli.ignoreOnly, true)
assert.notOk(cli.errored)
})
2020-03-31 17:27:36 +00:00
t.test('should support reporter list', function() {
2020-03-31 17:27:36 +00:00
cli.reporter = 'list'
cli.parseOptions(['-r', 'list'])
assert.strictEqual(cli.reporter, 'list')
assert.notOk(cli.errored)
})
t.test('should mark errored if missing reporter', function() {
2020-03-31 17:27:36 +00:00
cli.parseOptions(['--reporter'])
assert.ok(cli.errored)
})
t.test('should mark errored if invalid reporter', function() {
2020-03-31 17:27:36 +00:00
cli.parseOptions(['--reporter', 'test'])
assert.ok(cli.errored)
})
t.test('should support overriding timeout with shorthand option', function() {
cli.timeout = 2000
cli.parseOptions(['-t', '1000'])
assert.strictEqual(cli.timeout, 1000)
assert.notOk(cli.errored)
})
t.test('should support overriding timeout with long option', function() {
cli.timeout = 2000
cli.parseOptions(['--timeout', '250'])
assert.strictEqual(cli.timeout, 250)
assert.notOk(cli.errored)
})
t.test('should mark errored if missing timeout', function() {
cli.parseOptions(['--timeout'])
assert.ok(cli.errored)
})
t.test('should mark errored if invalid timeout', function() {
cli.parseOptions(['--timeout', 'test'])
assert.ok(cli.errored)
})
t.test('should add file to targets', function() {
2020-03-31 17:27:36 +00:00
cli.parseOptions(['test'])
assert.deepEqual(cli.targets, ['test'])
assert.notOk(cli.errored)
})
t.test('should add file to targets no matter where it is', function() {
2020-03-31 17:27:36 +00:00
cli.parseOptions(['test', '-r', 'list', 'test2'])
assert.deepEqual(cli.targets, ['test', 'test2'])
assert.notOk(cli.errored)
})
t.test('should default add test to target if no target', function() {
2020-03-31 17:27:36 +00:00
cli.parseOptions(['-r', 'list'])
assert.deepEqual(cli.targets, ['test/**'])
assert.notOk(cli.errored)
})
2020-04-01 15:46:11 +00:00
t.test('should mark errored if invalid shorthand option', function() {
2020-04-01 15:46:11 +00:00
cli.parseOptions(['-A'])
assert.ok(cli.errored)
})
t.test('should mark errored if invalid longhand option', function() {
2020-04-01 15:46:11 +00:00
cli.parseOptions(['--asdf'])
assert.ok(cli.errored)
})
2020-03-31 17:27:36 +00:00
})
/*****************************************
* #processTargets()
*****************************************/
t.describe('#processTargets()', function() {
t.test('should mark errored if empty', async function() {
cli.targets = ['test/testtree/folder1/*.txt']
2020-03-31 17:27:36 +00:00
await cli.processTargets()
assert.strictEqual(cli.files.length, 0)
assert.ok(cli.errored)
})
t.test('should support direct file path if exists', async function() {
cli.targets = ['test/testtree/folder1/sampletest1.temp.mjs']
2020-03-31 17:27:36 +00:00
await cli.processTargets()
assert.strictEqual(cli.files.length, 1)
assert.strictEqual(cli.files[0], 'test/testtree/folder1/sampletest1.temp.mjs')
2020-03-31 17:27:36 +00:00
})
t.test('should return all files in a directory', async function() {
cli.targets = ['test/testtree/folder1/']
2020-03-31 17:27:36 +00:00
await cli.processTargets()
assert.strictEqual(cli.files.length, 2)
cli.files.sort()
assert.strictEqual(cli.files[0], 'test/testtree/folder1/sampletest1.temp.mjs')
assert.strictEqual(cli.files[1], 'test/testtree/folder1/sampletest2.temp.mjs')
2020-03-31 17:27:36 +00:00
})
t.test('should support start as folder substitute', async function() {
cli.targets = ['*/testtree/folder1/']
2020-03-31 17:27:36 +00:00
await cli.processTargets()
assert.strictEqual(cli.files.length, 2)
cli.files.sort()
assert.strictEqual(cli.files[0], 'test/testtree/folder1/sampletest1.temp.mjs')
assert.strictEqual(cli.files[1], 'test/testtree/folder1/sampletest2.temp.mjs')
2020-03-31 17:27:36 +00:00
})
t.test('should support grabbing only files in folder', async function() {
2020-03-31 17:27:36 +00:00
cli.targets = ['test/*']
await cli.processTargets()
assert.ok(cli.files.length)
for (let i = 0; i < cli.files.length; i++) {
assert.notOk(cli.files[i].match(/\/folder1\//))
assert.notOk(cli.files[i].match(/\/folder2\//))
}
})
t.test('should support grabbing only pattern files in folder', async function() {
2020-03-31 17:27:36 +00:00
cli.targets = ['test/*.test.mjs']
await cli.processTargets()
assert.ok(cli.files.length)
for (let i = 0; i < cli.files.length; i++) {
assert.notOk(cli.files[i].match(/\/folder1\//))
assert.notOk(cli.files[i].match(/\/folder2\//))
}
})
t.test('should support multiple star pattern', async function() {
cli.targets = ['test/testtree/*/*.mjs']
2020-03-31 17:27:36 +00:00
await cli.processTargets()
console.log(cli.files)
2020-03-31 17:27:36 +00:00
assert.strictEqual(cli.files.length, 4)
cli.files.sort()
assert.deepEqual(cli.files, [
'test/testtree/folder1/sampletest1.temp.mjs',
'test/testtree/folder1/sampletest2.temp.mjs',
'test/testtree/folder2/sampletest3.temp.mjs',
'test/testtree/folder2/sampletest4.temp.mjs',
2020-03-31 17:27:36 +00:00
])
cli.targets = ['test/testtree/*/sampletest*.mjs']
2020-03-31 17:27:36 +00:00
await cli.processTargets()
assert.strictEqual(cli.files.length, 4)
cli.files.sort()
assert.deepEqual(cli.files, [
'test/testtree/folder1/sampletest1.temp.mjs',
'test/testtree/folder1/sampletest2.temp.mjs',
'test/testtree/folder2/sampletest3.temp.mjs',
'test/testtree/folder2/sampletest4.temp.mjs',
2020-03-31 17:27:36 +00:00
])
})
t.test('should support double star pattern', async function() {
2020-03-31 17:27:36 +00:00
cli.targets = ['test/**/*.mjs']
await cli.processTargets()
assert.ok(cli.files.length)
let found = {
sampletest1: false,
sampletest2: false,
sampletest3: false,
sampletest4: false,
sampletest5: false,
cli: false
}
for (let i = 0; i < cli.files.length; i++) {
found.sampletest1 = found.sampletest1 || cli.files[i] === 'test/testtree/folder1/sampletest1.temp.mjs'
found.sampletest2 = found.sampletest2 || cli.files[i] === 'test/testtree/folder1/sampletest2.temp.mjs'
found.sampletest3 = found.sampletest3 || cli.files[i] === 'test/testtree/folder2/sampletest3.temp.mjs'
found.sampletest4 = found.sampletest4 || cli.files[i] === 'test/testtree/folder2/sampletest4.temp.mjs'
found.sampletest5 = found.sampletest5 || cli.files[i] === 'test/testtree/folder2/sampletest5.temp.txt'
found.cli = found.cli || cli.files[i] === 'test/cli.test.mjs'
2020-03-31 17:27:36 +00:00
}
assert.deepEqual(found, {
sampletest1: true,
sampletest2: true,
sampletest3: true,
sampletest4: true,
sampletest5: false,
cli: true
})
})
t.test('should support double star pattern end', async function() {
2020-03-31 17:27:36 +00:00
cli.targets = ['test/**']
await cli.processTargets()
assert.ok(cli.files.length)
let found = {
sampletest1: false,
sampletest2: false,
sampletest3: false,
sampletest4: false,
sampletest5: false,
cli: false
}
for (let i = 0; i < cli.files.length; i++) {
found.sampletest1 = found.sampletest1 || cli.files[i] === 'test/testtree/folder1/sampletest1.temp.mjs'
found.sampletest2 = found.sampletest2 || cli.files[i] === 'test/testtree/folder1/sampletest2.temp.mjs'
found.sampletest3 = found.sampletest3 || cli.files[i] === 'test/testtree/folder2/sampletest3.temp.mjs'
found.sampletest4 = found.sampletest4 || cli.files[i] === 'test/testtree/folder2/sampletest4.temp.mjs'
found.sampletest5 = found.sampletest5 || cli.files[i] === 'test/testtree/folder2/sampletest5.temp.txt'
found.cli = found.cli || cli.files[i] === 'test/cli.test.mjs'
2020-03-31 17:27:36 +00:00
}
assert.deepEqual(found, {
sampletest1: true,
sampletest2: true,
sampletest3: true,
sampletest4: true,
sampletest5: true,
cli: true
})
})
})
2020-03-31 17:27:36 +00:00
})
t.test('#fileMatches() should support filename matching with glob pattern', async function() {
2020-03-31 17:27:36 +00:00
assert.ok(fileMatches('bla.test.mjs', '*.mjs'))
assert.ok(fileMatches('bla.test.mjs', '*test.mjs'))
assert.ok(fileMatches('bla.test.mjs', 'bla*.mjs'))
assert.notOk(fileMatches('bla.test.mjs', 'bla*.js'))
assert.notOk(fileMatches('bla.test.mjs', '*.js'))
assert.notOk(fileMatches('bla.test.mjs', 'blas*.js'))
})