Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use package.json engines semver expression #1535

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ indent_size = false

[Makefile]
indent_style = tab

[test/fast/Unit tests/package_json_templates/*]
indent_style = unset

[test/generated_semvers.sh]
indent_style = unset
indent_size = unset

[test/fast/Unit tests/nvm_trim_and_reduce_whitespace_to_one_space]
indent_style = unset
indent_size = unset

536 changes: 531 additions & 5 deletions nvm.sh

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions test/fast/Unit tests/nvm_get_node_from_pkg_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/sh

\. ../../../nvm.sh
\. ../../generated_semvers.sh

# This test suite validates the behavior of extracting the semver from a package.json's engine.node value given valid/invalid semvers and valid/invalid json structures.

# (TEST 1 POSITIVE TEST CASES)
# SEMVERS: valid
# PACKAGE.JSON TEMPLATES: invalid
test_cases="$VALID_SEMVERS_FOR_PKG_JSON"
if [ -z "$test_cases" ]; then
die 'TEST 1 for nvm_get_node_from_pkg_json given an empty set of test cases'
fi
prev_semver=''
for template_file_name in package_json_templates/_valid_*; do
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
test_cases=$(echo "$test_cases" | tail -n +2)
[ -n "$semver" ] || continue
expectedOutput=$(nvm_trim_and_reduce_whitespace_to_one_space "$semver")

if [ "$prev_semver" = "$semver" ]; then
die "Problem iterating through test_cases (TEST 1). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
fi
prev_semver="$semver"

pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
if [ "$actual_output" != "$expectedOutput" ] || [ -z "$actual_output" ] || [ -z "$pkg_json_contents" ]; then
die "'nvm_get_node_from_pkg_json' POSITIVE test case failed (TEST 1). Expected '$expectedOutput' but got '$actual_output' when given input '$semver' and template '$template_file_name':\n$pkg_json_contents"
fi
done
done

# (TEST 2 NEGATIVE TEST CASES)
# SEMVERS: valid
# PACKAGE.JSON TEMPLATES: invalid
test_cases="$VALID_SEMVERS_FOR_PKG_JSON"
if [ -z "$test_cases" ]; then
die 'TEST 2 for nvm_get_node_from_pkg_json given an empty set of test cases'
fi
prev_semver=''
for template_file_name in package_json_templates/_invalid_*; do
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
test_cases=$(echo "$test_cases" | tail -n +2)
[ -n "$semver" ] || continue

if [ "$prev_semver" = "$semver" ]; then
die "Problem iterating through test_cases (TEST 2). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
fi
prev_semver="$semver"

pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
die "'nvm_get_node_from_pkg_json' NEGATIVE test case failed (TEST 2). Expected to get empty string but got '$actual_output' when given input template '$template_file_name':\n$pkg_json_contents"
fi
done
done


# (TEST 3 NEGATIVE TEST CASES)
# SEMVERS: invalid
# PACKAGE.JSON TEMPLATES: valid
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
if [ -z "$test_cases" ]; then
die 'TEST 3 for nvm_get_node_from_pkg_json given an empty set of test cases'
fi
prev_semver=''
for template_file_name in package_json_templates/_valid_*; do
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
[ -n "$semver" ] || continue
test_cases=$(echo "$test_cases" | tail -n +2)

if [ "$prev_semver" = "$semver" ]; then
die "Problem iterating through test_cases (TEST 3). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
fi
prev_semver="$semver"

pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
die "'nvm_get_node_from_pkg_json' NEGATIVE test case failed (TEST 3). Expected to get empty string but got '$actual_output' when given input template '$template_file_name':\n$pkg_json_contents"
fi
done
done

# (TEST 4 NEGATIVE TEST CASES)
# SEMVERS: invalid
# PACKAGE.JSON TEMPLATES: invalid
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
if [ -z "$test_cases" ]; then
die 'TEST 4 for nvm_get_node_from_pkg_json given an empty set of test cases'
fi
prev_semver=''
for template_file_name in package_json_templates/_invalid_*; do
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
[ -n "$semver" ] || continue
test_cases=$(echo "$test_cases" | tail -n +2)

if [ "$prev_semver" = "$semver" ]; then
die "Problem iterating through test_cases (TEST 4). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
fi
prev_semver="$semver"

pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
die "'nvm_get_node_from_pkg_json' NEGATIVE test case failed (TEST 4). Expected to get empty string but got '$actual_output' when given input template '$template_file_name':\n$pkg_json_contents"
fi
done
done
exit 0

44 changes: 44 additions & 0 deletions test/fast/Unit tests/nvm_is_normalized_semver
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh

\. ../../../nvm.sh
\. ../../generated_semvers.sh

# nvm_is_normalized_semver validates that a given semver adheres to a particular grammar. See grammar with definition of function nvm_is_normalized_semver() in nvm.sh.

# POSITIVE TEST CASES
positive_test_cases="$VALID_NORMALIZED_SEMVERS"
if [ -z "$positive_test_cases" ]; then
die 'positive test cases are empty'
fi
prev_semver=''
while [ -n "$positive_test_cases" ]; do
semver=$(echo "$positive_test_cases" | head -n1)
if [ -z "$semver" ] || ! nvm_is_normalized_semver "$semver"; then
die "nvm_is_normalized_semver POSITIVE test case failed. semver: '$semver'.\n"
fi
if [ "$prev_semver" = "$semver" ]; then
die "something is wrong. positive test cases received the same test case twice in a row. semver: '$semver'"
fi
prev_semver="$semver"
positive_test_cases=$(echo "$positive_test_cases" | tail -n +2)
done

# NEGATIVE TEST CASES
negative_test_cases=$(printf "%s\n%s" "$VALID_NON_NORMALIZED_SEMVERS" "$INVALID_SEMVERS_FOR_PKG_JSON")
if [ -z "$negative_test_cases" ]; then
die 'negative test cases are empty'
fi
prev_semver='initialized to non empty string'
while [ -n "$negative_test_cases" ]; do
semver=$(echo "$negative_test_cases" | head -n1)
if nvm_is_normalized_semver "$semver"; then
die "nvm_is_normalized_semver NEGATIVE test case failed. semver: '$semver'.\n"
fi
if [ "$prev_semver" = "$semver" ]; then
die "something is wrong. negative test cases received the same test case twice in a row. semver: '$semver'"
fi
prev_semver="$semver"
negative_test_cases=$(echo "$negative_test_cases" | tail -n +2)
done
exit 0

102 changes: 102 additions & 0 deletions test/fast/Unit tests/nvm_normalize_semver
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh

\. ../../../nvm.sh
\. ../../generated_semvers.sh

# This test suite validates the behavior of normalizing a semver from its raw form to a specific grammar. See the grammar defined with nvm_is_normalized_semver() in nvm.sh.

# TEST 1: Validate that for already normalized semvers, nvm_normalize_semver outputs the same semver.
test_cases="$VALID_NORMALIZED_SEMVERS"
if [ -z "$test_cases" ]; then
die 'nvm_normalize_semver (TEST 1) was given an empty set of test cases'
fi
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
actual_output=$(nvm_normalize_semver "$semver")
if [ -z "$semver" ] || [ "$semver" != "$actual_output" ]; then
die "nvm_normalize_semver (TEST 1) test case failed. Expected output: '$semver'. Actual output: '$actual_output'. Input: '$semver'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done


# TEST 2: Validate that non normalized valid semvers produce a normalized result
test_cases="$VALID_NON_NORMALIZED_SEMVERS"
if [ -z "$test_cases" ]; then
die 'nvm_normalize_semver (TEST 2) was given an empty set of test cases'
fi
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
actual_output=$(nvm_normalize_semver "$semver")
if [ -z "$semver" ] || [ -z "$actual_output" ] || [ "$semver" = "$actual_output" ]; then
die "nvm_normalize_semver (TEST 2) test case failed. Expected output: '$semver'. Actual output: '$actual_output'. Input: '$semver'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done

# TEST 3: Validate specific outputs for some specific inputs to nvm_normalize_semver.
# input:expected_output
test_cases="1.2.3:1.2.3
1.2.3 - 1.2.4:>=1.2.3 <=1.2.4
1.2.3-1.2.4:>=1.2.3 <=1.2.4
11.22.33 - 11.22.44:>=11.22.33 <=11.22.44
1.2.x - 1.2.4:>=1.2.0 <=1.2.4
1.2.xx - 1.2.4:
1.2.3 || 1.2.4:1.2.3 || 1.2.4
*:>0.0.0
x:>0.0.0
X:>0.0.0
1:>=1.0.0 <2.0.0
1.2:>=1.2.0 <1.3.0
< 1.2.3:<1.2.3
> 1.2.3:>1.2.3
<= 1.2.3:<=1.2.3
>= 1.2.3:>=1.2.3
= 1.2.3:1.2.3
^0.0.1 ^0.1.2 ^1.2.3:>=0.0.1 <0.0.2 >=0.1.2 <0.2.0 >=1.2.3 <2.0.0
~0.0.1 ~0.1.2 ~1.2.3:>=0.0.1 <0.1.0 >=0.1.2 <0.2.0 >=1.2.3 <1.3.0
~ 1.2.3:>=1.2.3 <1.3.0
^ 1.2.3:>=1.2.3 <2.0.0
1.2.3 || 1.2.4 1.2.5:1.2.3 || 1.2.4 1.2.5
a:
1 || 2 a:
1 || a:
a || 1.2.3:
1.2.?:
^0.0.1:>=0.0.1 <0.0.2
<x.x.x:
>=x.1.1:>0.0.0
>x.1.1:>0.0.0
~x.1.1:>0.0.0
^x.1.1:>0.0.0
11.22.33:11.22.33"

if [ -z "$test_cases" ]; then
die 'nvm_normalize_semver (TEST 3) was given an empty set of test cases'
fi
while [ -n "$test_cases" ]; do
line=$(echo "$test_cases" | head -n1)
input=$(echo "$line" | awk -F: '{ print $1 }')
expected_output=$(echo "$line" | awk -F: '{ print $2 }')
actual_output=$(nvm_normalize_semver "$input")
if [ -z "$input" ] || [ "$actual_output" != "$expected_output" ]; then
die "nvm_normalize_semver (TEST 3) test case failed. Expected output: '$expected_output'. Actual output: '$actual_output'. Input: '$input'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done

# TEST 4: Validate that invalid semvers with invalid characters that shouldn't be retrieved from the package.json produce no result from nvm_normalize_semver
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
if [ -z "$test_cases" ]; then
die 'nvm_normalize_semver (TEST 4) was given an empty set of test cases'
fi
while [ -n "$test_cases" ]; do
semver=$(echo "$test_cases" | head -n1)
actual_output=$(nvm_normalize_semver "$semver")
if [ -z "$semver" ] || [ -n "$actual_output" ]; then
die "nvm_normalize_semver (TEST 4) test case failed. Expected no output but got: '$actual_output'. Input: '$semver'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done
exit 0

30 changes: 30 additions & 0 deletions test/fast/Unit tests/nvm_string_contains_regexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

\. ../../../nvm.sh
\. ../../generated_semvers.sh
normalized_semver_regexp='^( ?(<|<=|>|>=)?[0-9]+\.[0-9]+\.[0-9]+)+( \|\| ( ?(<|<=|>|>=)?[0-9]+\.[0-9]+\.[0-9]+)+)*$'

# Validates the behavior of nvm_string_contains_regexp() which returns 0 if the given string contains the given regular expression.

# POSITIVE TEST CASES
test_cases="$VALID_NORMALIZED_SEMVERS"
while [ -n "$test_cases" ]; do
string=$(echo "$test_cases" | head -n1)
if [ -z "$normalized_semver_regexp" ] || [ -z "$string" ] || ! nvm_string_contains_regexp "$string" "$normalized_semver_regexp"; then
die "nvm_string_contains_regexp POSITIVE test case failed. regexp: '$normalized_semver_regexp'. string: '$string'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done

# NEGATIVE TEST CASES
# string:regexp
test_cases=$(printf "%s\n%s" "$VALID_NON_NORMALIZED_SEMVERS" "$INVALID_SEMVERS_FOR_PKG_JSON")
while [ -n "$test_cases" ]; do
string=$(echo "$test_cases" | head -n1)
if [ -z "$normalized_semver_regexp" ] || nvm_string_contains_regexp "$string" "$normalized_semver_regexp"; then
die "nvm_string_contains_regexp NEGATIVE test case failed. regexp: '$normalized_semver_regexp'. string: '$string'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done
exit 0

53 changes: 53 additions & 0 deletions test/fast/Unit tests/nvm_trim_and_reduce_whitespace_to_one_space
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

\. ../../../nvm.sh
die () { printf "$@" ; exit 1; }

# Validates the behavior of nvm_trim_and_reduce_whitespace_to_one_space() which consolidates all consecutive white space to one space, and then trims any spaces from the ends.

# Test cases that have no new lines
# input:expected_output
test_cases='1:1
1 2 3:1 2 3
1.1 2.2 :1.1 2.2
1.2.3:1.2.3
1 1 :1 1
2 2 :2 2'

while [ -n "$test_cases" ]; do
line=$(echo "$test_cases" | head -n1)
input=$(echo "$line" | awk -F: '{ print $1 }')
expected_output=$(echo "$line" | awk -F: '{ print $2 }')
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$input")
if [ -z "$input" ] || [ -z "$expected_output" ] || [ "$expected_output" != "$actual_output" ]; then
die "nvm_reduce_whitespace_to_one_space test case failed. expected_output: '$expected_output'. actual_output: '$actual_output'.\n"
fi
test_cases=$(echo "$test_cases" | tail -n +2)
done

# Test cases that have new lines
expected_output='1 2 3'
test_case_with_new_line_1=' 1
2 3 '
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$test_case_with_new_line_1")
if [ "$actual_output" != "$expected_output" ]; then
die "nvm_trim_and_reduce_whitespace_to_one_space test with test_case_with_new_line_1 failed. Actual output: '$actual_output' Expected output: '$expected_output'"
fi

test_case_with_new_line_2=' 1 2
3 '
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$test_case_with_new_line_2")
if [ "$actual_output" != "$expected_output" ]; then
die "nvm_trim_and_reduce_whitespace_to_one_space test with test_case_with_new_line_2 failed. Actual output: '$actual_output' Expected output: '$expected_output'"
fi

test_case_with_new_line_3=' 1

2
3'
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$test_case_with_new_line_3")
if [ "$actual_output" != "$expected_output" ]; then
die "nvm_trim_and_reduce_whitespace_to_one_space test with test_case_with_new_line_3 failed. Actual output: '$actual_output' Expected output: '$expected_output'"
fi
exit 0

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
verifies incompatibility with package.json that is missing 'engines' key

{
"name": "fake",
"description": "fake"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
verifies incompatibility with package.json that is missing 'engines.node' key

{
"name": "fake",
"engines": {
"npm": "fake"
},
"description": "fake"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
verifies incompatibility with package.json that is missing quotes around the 'engines'node' value

{
"name": "fake",
"engines": {
"node": NODE_SEMVER
},
"description": "fake"
}
Loading