Skip to content

Commit 5a58625

Browse files
committed
Improve error handling when reading config file
1 parent 8060f23 commit 5a58625

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,17 @@ Hook.prototype.initialize = function initialize () {
184184
this.root = this.root.stdout.toString().trim()
185185

186186
if (fs.existsSync(path.join(this.root, '.pre-commit.json'))) {
187-
let preCommitConfig
187+
let configFile
188188

189189
try {
190-
const raw = fs.readFileSync(path.join(this.root, '.pre-commit.json'), 'utf-8').toString()
191-
preCommitConfig = JSON.parse(raw)
192-
} catch (err) {}
190+
const rawText = fs.readFileSync(path.join(this.root, '.pre-commit.json'), 'utf-8').toString()
191+
configFile = JSON.parse(rawText)
192+
} catch (e) {
193+
return this.log(this.format(Hook.log.preCommitConfig, e.message), 1)
194+
}
193195

194-
if (typeof preCommitConfig === 'object' && !Array.isArray(preCommitConfig)) {
195-
this.configFile = preCommitConfig
196+
if (typeof configFile === 'object' && !Array.isArray(configFile)) {
197+
this.configFile = configFile
196198
}
197199
}
198200

@@ -298,6 +300,14 @@ Hook.log = {
298300
'Skipping the pre-commit hook.'
299301
].join('\n'),
300302

303+
preCommitConfig: [
304+
'Received an error while parsing or locating the `.pre-commit.json` file:',
305+
'',
306+
' %s',
307+
'',
308+
'Skipping the pre-commit hook.'
309+
].join('\n'),
310+
301311
run: [
302312
'We have nothing pre-commit hooks to run. Either you\'re missing the `scripts`',
303313
'in your `package.json` or have configured pre-commit to run nothing.',

test.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ t.test('pre-commit', function (t) {
2626
})
2727

2828
t.test('#parse', function (t) {
29-
t.plan(8)
29+
t.plan(9)
3030

3131
let hook
3232

@@ -143,7 +143,7 @@ t.test('pre-commit', function (t) {
143143
t.strictSame(typeof hook.config.run, 'undefined')
144144
})
145145

146-
t.test('overrides the `pre-commit` config property in package.json with the config inside pre-commit.json if it exists', function (t) {
146+
t.test('overrides the `pre-commit` config property in package.json with the config inside `.pre-commit.json` if it exists', function (t) {
147147
t.plan(1)
148148

149149
const Hook = proxyquire('.', {
@@ -163,6 +163,40 @@ t.test('pre-commit', function (t) {
163163
// ----
164164
t.same(hook.config.run, ['lint', 'bench'])
165165
})
166+
167+
t.test('should properly handle errors while trying to read and parse the contents of `.pre-commit.json`', function (t) {
168+
t.plan(4)
169+
170+
let Hook = proxyquire('.', {
171+
fs: {
172+
existsSync () {
173+
return true
174+
},
175+
readFileSync () {
176+
throw new Error()
177+
}
178+
}
179+
})
180+
181+
hook = new Hook(exit)
182+
183+
Hook = proxyquire('.', {
184+
fs: {
185+
existsSync () { return true },
186+
readFileSync () {
187+
return Buffer.from('{ "bad": [json }')
188+
}
189+
}
190+
})
191+
192+
hook = new Hook(exit)
193+
194+
// *****************
195+
function exit (code, lines) {
196+
t.not(lines.length, 0)
197+
t.equal(code, 1)
198+
}
199+
})
166200
})
167201

168202
t.test('#log', function (t) {

0 commit comments

Comments
 (0)