-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathshellcheck-fixtures.js
67 lines (58 loc) · 1.89 KB
/
shellcheck-fixtures.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
62
63
64
65
66
67
var test = require('tape')
var parse = require('../parser')
var fs = require('fs')
var path = require('path')
var fixturesDir = path.join(__dirname, 'fixtures/shellcheck-tests')
var fixtureDirs = fs.readdirSync(fixturesDir).map(function (dir) {
return path.join(fixturesDir, dir)
})
test('shellcheck fixtures', function(t) {
// allow skipping fixtures entirely
if (process.env.skip_shellcheck_fixtures) {
t.skip('skipping shellcheck fixture tests')
t.end()
return
}
// allow running individual fixtures directly
if (process.argv[1].match(/shellcheck-fixtures\.js$/)
&& process.argv.length > 2) {
fixtureDirs = process.argv.slice(2)
}
t.plan(fixtureDirs.length)
fixtureDirs.forEach(function (fixtureDir) {
var source = fs.readFileSync(path.join(fixtureDir, 'source.sh'), 'utf8')
var error, ast;
try { error = require(path.join(fixtureDir, 'error.json')) } catch (e) {}
if (!error) {
try { ast = require(path.join(fixtureDir, 'ast.json')) } catch (e) {}
}
if (error) {
t.throws(function() {
parse(source)
}, error.message ? new RegExp(error.message) : undefined, fixtureDir + ' errored')
} else if (ast) {
t.deepEqual(parse(source), ast, dir)
} else {
try {
parse(source)
t.pass(fixtureDir.replace(fixturesDir + '/', '') + ': parsed sucessfully')
} catch (e) {
if (e instanceof parse.SyntaxError) {
t.fail('parse failed')
formatParseError(fixtureDir, source, e)
} else {
throw e
}
}
}
})
t.end()
})
function formatParseError (dir, source, err) {
var msg = dir.replace(fixturesDir + '/', '') + ': ' + err.message
var start = Math.max(0, err.location.start.offset - 8)
msg += '\n ' + source.slice(start, start + 10).trim() + '\n'
for (var i = 0; i <= (err.column - start); i++) msg += '-';
msg += '^'
console.error(msg)
}