Skip to content

Commit

Permalink
fix(search): return valid json for no results
Browse files Browse the repository at this point in the history
PR-URL: #3738
Credit: @AyushRawal
Close: #3738
Reviewed-by: @wraithgar
  • Loading branch information
AyushRawal authored and wraithgar committed Sep 14, 2021
1 parent b4aac34 commit b807cd6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/search/format-package-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JSONOutputStream extends Minipass {
}

end () {
super.write(this._didFirst ? ']\n' : '\n]\n')
super.write(this._didFirst ? ']\n' : '\n[]\n')
super.end()
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ t.test('search <name> --json', (t) => {
src.end()
})

t.test('search <invalid-module> --json', (t) => {
const src = new Minipass()
src.objectMode = true

npm.flatOptions.json = true
config.json = true
const libnpmsearch = {
stream () {
return src
},
}

const Search = t.mock('../../lib/search.js', {
...mocks,
libnpmsearch,
})
const search = new Search(npm)

search.exec(['foo'], (err) => {
if (err)
throw err

t.equal(result, '\n[]\n', 'should have expected empty square brackets')

config.json = false
t.end()
})

src.end()
})

t.test('search <name> --searchexclude --searchopts', t => {
npm.flatOptions.search = {
...flatOptions.search,
Expand Down

0 comments on commit b807cd6

Please sign in to comment.