Skip to content

Commit f32d447

Browse files
committed
Quiet errors from JSON.sh
1 parent 50844c2 commit f32d447

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

libexec/nodenv-package-json-engine

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extract_version_from_package_json() {
3939
package_json_path="$1"
4040
version_regex='\["engines","node"\][[:space:]]*"([^"]*)"'
4141
# -b -n gives minimal output - see https://github.com/dominictarr/JSON.sh#options
42-
[[ $("$JSON_SH" -b -n < "$package_json_path") =~ $version_regex ]]
42+
[[ $("$JSON_SH" -b -n < "$package_json_path" 2>/dev/null) =~ $version_regex ]]
4343
echo "${BASH_REMATCH[1]}"
4444
}
4545

test/nodenv-package-json-engine.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,22 @@ load test_helper
9090

9191
assert_success 'system'
9292
}
93+
94+
@test 'Does not fail with empty or malformed package.json' {
95+
in_example_package
96+
97+
# empty
98+
touch package.json
99+
run nodenv version-name
100+
assert_success 'system'
101+
102+
# non json
103+
echo "foo" > package.json
104+
run nodenv version-name
105+
assert_success 'system'
106+
107+
# malformed
108+
echo "{" > package.json
109+
run nodenv version-name
110+
assert_success 'system'
111+
}

0 commit comments

Comments
 (0)