Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAyush Rawal <royalrawal.2001@gmail.com>2021-09-11 11:06:09 +0300
committerGar <gar+gh@danger.computer>2021-09-15 00:07:34 +0300
commitb807cd62eabe337e3243415c9870ea36d9289e12 (patch)
treecb54fb077bd4fd016bcb844db98d2eb58f19a6ec
parentb4aac345b0a7cdec4d713c5be4daea37330b2b26 (diff)
fix(search): return valid json for no results
PR-URL: https://github.com/npm/cli/pull/3738 Credit: @AyushRawal Close: #3738 Reviewed-by: @wraithgar
-rw-r--r--lib/search/format-package-stream.js2
-rw-r--r--test/lib/search.js31
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/search/format-package-stream.js b/lib/search/format-package-stream.js
index c88df5eb4..fb7d81856 100644
--- a/lib/search/format-package-stream.js
+++ b/lib/search/format-package-stream.js
@@ -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()
}
}
diff --git a/test/lib/search.js b/test/lib/search.js
index 510a470f4..55b584b8a 100644
--- a/test/lib/search.js
+++ b/test/lib/search.js
@@ -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,