Skip to content
This repository has been archived by the owner on Aug 17, 2019. It is now read-only.

Commit

Permalink
fixup! fix: strip BOM when reading JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
rmg committed Sep 5, 2017
1 parent 54cf9de commit 0ceb034
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Installer {
}
}
module.exports = Installer
module.exports._readJson = readJson

function readJson (jsonPath, name, ignoreMissing) {
return readFileAsync(path.join(jsonPath, name), 'utf8')
Expand Down
6 changes: 6 additions & 0 deletions test/lib/package-json-with-bom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "strong-spawn-npm",
"version": "1.0.0",
"description": "Reliably spawn npm™ on any platform",
"homepage": "https://github.com/strongloop/strong-spawn-npm"
}
19 changes: 19 additions & 0 deletions test/specs/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const fs = require('fs')
const path = require('path')
const test = require('tap').test
const requireInject = require('require-inject')
const fixtureHelper = require('../lib/fixtureHelper.js')
Expand Down Expand Up @@ -295,3 +297,20 @@ test('skips lifecycle scripts with ignoreScripts is set', t => {
t.end()
})
})

test('handles JSON docs that contain a BOM', t => {
t.plan(2)
const Installer = requireInject('../../index.js', {/* just don't want to cache */})
const bomJSON = 'package-json-with-bom.json'
const bomJSONDir = path.resolve(__dirname, '../lib')
const actualJSON = {
name: 'strong-spawn-npm',
version: '1.0.0',
description: 'Reliably spawn npm™ on any platform',
homepage: 'https://github.com/strongloop/strong-spawn-npm'
}
// ensure that the file does indeed fail to be parsed by JSON.parse
t.throws(() => JSON.parse(fs.readFileSync(path.join(bomJSONDir, bomJSON), 'utf8')),
{message: 'Unexpected token \uFEFF in JSON at position 0'})
return Installer._readJson(bomJSONDir, bomJSON).then(obj => t.match(obj, actualJSON))
})

0 comments on commit 0ceb034

Please sign in to comment.