mirror of
https://github.com/nvm-sh/nvm.git
synced 2025-05-10 22:31:51 +00:00
decent optimization; added last of test cases; fixed corner case; added some polish
This commit is contained in:
parent
8221df2a3f
commit
3e2eddd05a
77
nvm.sh
77
nvm.sh
@ -358,13 +358,13 @@ nvm_normalize_semver() {
|
|||||||
s/ ?- ?/ - /g;
|
s/ ?- ?/ - /g;
|
||||||
|
|
||||||
# ' 1 ' => ' 1.x.x '
|
# ' 1 ' => ' 1.x.x '
|
||||||
# ' x ' => ' x.x.x '
|
# ' ~x ' => ' ~x.x.x '
|
||||||
s/ ([0-9]+|x) / \1.x.x /g;
|
s/ (~|\^)?([0-9]+|x) / \1\2.x.x /g;
|
||||||
|
|
||||||
# ' 1.2 ' => ' 1.2.x '
|
# ' 1.2 ' => ' 1.2.x '
|
||||||
# ' 1.x ' => ' 1.x.x '
|
# ' ~1.x ' => ' ~1.x.x '
|
||||||
# ' x.x ' => ' x.x.x '
|
# ' ^x.x ' => ' ^x.x.x '
|
||||||
s/ (([0-9]+|x)\.([0-9]+|x)) / \1.x /g;
|
s/ (~|\^)?(([0-9]+|x)\.([0-9]+|x)) / \1\2.x /g;
|
||||||
|
|
||||||
# ' 1.2.3 - 1.2.4 ' => ' >=1.2.3 <=1.2.4 '
|
# ' 1.2.3 - 1.2.4 ' => ' >=1.2.3 <=1.2.4 '
|
||||||
s/ (([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) ?\- ?(([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) / >=\1 <=\5 /g;
|
s/ (([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) ?\- ?(([0-9]+|x)\.([0-9]+|x)\.([0-9]+|x)) / >=\1 <=\5 /g;
|
||||||
@ -387,9 +387,11 @@ nvm_normalize_semver() {
|
|||||||
# ` ^0.0.1 ` => ` >=0.0.1 <0.0.2 `
|
# ` ^0.0.1 ` => ` >=0.0.1 <0.0.2 `
|
||||||
# ` ^0.1.2 ` => ` >=0.1.2 <0.2.0 `
|
# ` ^0.1.2 ` => ` >=0.1.2 <0.2.0 `
|
||||||
# ` ^1.2.3 ` => ` >=1.2.3 <2.0.0 `
|
# ` ^1.2.3 ` => ` >=1.2.3 <2.0.0 `
|
||||||
|
# ` ^1.0.0 ` => ` >=1.0.0 <2.0.0 `
|
||||||
# ` ~0.0.1 ` => ` >=0.0.1 <0.1.0 `
|
# ` ~0.0.1 ` => ` >=0.0.1 <0.1.0 `
|
||||||
# ` ~0.1.2 ` => ` >=0.1.2 <0.2.0 `
|
# ` ~0.1.2 ` => ` >=0.1.2 <0.2.0 `
|
||||||
# ` ~1.2.3 ` => ` >=1.2.3 <1.3.0 `
|
# ` ~1.2.3 ` => ` >=1.2.3 <1.3.0 `
|
||||||
|
# ` ~1.0.0 ` => ` >=1.0.0 <2.0.0 `
|
||||||
# ` x.x.x ` => ` >0.0.0 `
|
# ` x.x.x ` => ` >0.0.0 `
|
||||||
# ` x.x.1 ` => ` >0.0.0 `
|
# ` x.x.1 ` => ` >0.0.0 `
|
||||||
# ` x.1.x ` => ` >0.0.0 `
|
# ` x.1.x ` => ` >0.0.0 `
|
||||||
@ -438,7 +440,11 @@ nvm_normalize_semver() {
|
|||||||
output=output ">=" version " <" a[1]+1 ".0.0 ";
|
output=output ">=" version " <" a[1]+1 ".0.0 ";
|
||||||
}
|
}
|
||||||
} else if ( match(comparator, /^~/) ) {
|
} else if ( match(comparator, /^~/) ) {
|
||||||
if ( match(comparator, /^~0.0.[0-9]+$/) ) {
|
if ( match(comparator, /^~[0-9]+.0.0$/) ) {
|
||||||
|
version=substr(comparator,2)
|
||||||
|
split(version, a, /\./)
|
||||||
|
output=output ">=" version " <" a[1]+1 ".0.0 "
|
||||||
|
} else if ( match(comparator, /^~0.0.[0-9]+$/) ) {
|
||||||
version=substr(comparator,2)
|
version=substr(comparator,2)
|
||||||
split(version, a, /\./)
|
split(version, a, /\./)
|
||||||
output=output ">=" version " <0." a[2]+1 ".0 "
|
output=output ">=" version " <0." a[2]+1 ".0 "
|
||||||
@ -472,9 +478,9 @@ nvm_normalize_semver() {
|
|||||||
|
|
||||||
if nvm_is_normalized_semver "$validated_semver"; then
|
if nvm_is_normalized_semver "$validated_semver"; then
|
||||||
command printf '%s' "$validated_semver"
|
command printf '%s' "$validated_semver"
|
||||||
else
|
return 0
|
||||||
return 1
|
|
||||||
fi
|
fi
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Given a semver and version list, find the highest compatible version by doing the following:
|
# Given a semver and version list, find the highest compatible version by doing the following:
|
||||||
@ -490,9 +496,9 @@ nvm_interpret_complex_semver() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# For each comparator_set in the semver:
|
# For each comparator_set in the semver:
|
||||||
# - Resolve the comparator_set to its newest compatible version.
|
# - Try to resolve the comparator_set to its newest compatible version.
|
||||||
# - Add the discovered newest compatible version to highest_compatible_versions.
|
# - But stop looking for a compatible version for a comparator_set if the current_version being iterated on is lower than an already found compatible version.
|
||||||
# - Choose the highest version among all the versions collected in highest_compatible_versions.
|
# - If by the end of the algorithm, a version other than 0.0.0 is collected in highest_compatible_version, output that version.
|
||||||
semver=$(command printf '%s' "$semver" | command tr '||' '\n')
|
semver=$(command printf '%s' "$semver" | command tr '||' '\n')
|
||||||
local version_list_copy
|
local version_list_copy
|
||||||
local current_comparator_set
|
local current_comparator_set
|
||||||
@ -500,9 +506,8 @@ nvm_interpret_complex_semver() {
|
|||||||
local current_comparator_set_copy
|
local current_comparator_set_copy
|
||||||
local current_comparator
|
local current_comparator
|
||||||
local stripped_version_from_comparator
|
local stripped_version_from_comparator
|
||||||
local highest_compatible_versions
|
local highest_compatible_version
|
||||||
# TODO make this just always store the highest possible compatible version
|
highest_compatible_version='0.0.0'
|
||||||
highest_compatible_versions=''
|
|
||||||
|
|
||||||
while [ -n "$semver" ]; do
|
while [ -n "$semver" ]; do
|
||||||
version_list_copy=$(command printf '%s' "$version_list")
|
version_list_copy=$(command printf '%s' "$version_list")
|
||||||
@ -511,13 +516,17 @@ nvm_interpret_complex_semver() {
|
|||||||
[ -n "$current_comparator_set" ] || continue
|
[ -n "$current_comparator_set" ] || continue
|
||||||
|
|
||||||
# For each version in the version_list_copy (iterating from newest to oldest):
|
# For each version in the version_list_copy (iterating from newest to oldest):
|
||||||
# - If current_version satisfies all comparators in current_comparator_set, we've found the newest version compatible with all comparators in current current_comparator_set.
|
# - If current_version satisfies all comparators in current_comparator_set, we've found the newest highest version compatible with all comparators in current current_comparator_set.
|
||||||
# - Add discovered version to highest_compatible_versions and stop iterating through versions for current_comparator_set.
|
# - Store the current_version in highest_compatible_version and stop iterating through versions for current_comparator_set.
|
||||||
while [ -n "$version_list_copy" ]; do
|
while [ -n "$version_list_copy" ]; do
|
||||||
current_version=$(command printf '%s' "$version_list_copy" | command tail -n1 | command sed -E 's/^ +//;s/ +$//' | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
|
current_version=$(command printf '%s' "$version_list_copy" | command tail -n1 | command sed -E 's/^ +//;s/ +$//' | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
|
||||||
version_list_copy=$(command printf '%s' "$version_list_copy" | command sed '$d')
|
version_list_copy=$(command printf '%s' "$version_list_copy" | command sed '$d')
|
||||||
[ -n "$current_version" ] || continue
|
[ -n "$current_version" ] || continue
|
||||||
# TODO if current_version is less than the highest version in highest_compatile_versions, no need to continue
|
if [ "$highest_compatible_version" != '0.0.0' ] && nvm_version_greater "$highest_compatible_version" "$current_version"; then
|
||||||
|
# If we previously found a compatible version that is higher than the current_version, there is no need to continue checking versions.
|
||||||
|
version_list_copy=''
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
# For each comparator in the current_comparator_set_copy:
|
# For each comparator in the current_comparator_set_copy:
|
||||||
# - If current_version is compatible with all comparators, we know current_version is the newest compatible version
|
# - If current_version is compatible with all comparators, we know current_version is the newest compatible version
|
||||||
@ -537,7 +546,7 @@ nvm_interpret_complex_semver() {
|
|||||||
# current_comparator is looking for an exact match, and the current_version is the exact match, so this current_comparator is satisfied.
|
# current_comparator is looking for an exact match, and the current_version is the exact match, so this current_comparator is satisfied.
|
||||||
if [ -z "$current_comparator_set_copy" ]; then
|
if [ -z "$current_comparator_set_copy" ]; then
|
||||||
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
||||||
highest_compatible_versions="$highest_compatible_versions $current_version"
|
highest_compatible_version="$current_version"
|
||||||
version_list_copy=''
|
version_list_copy=''
|
||||||
fi
|
fi
|
||||||
elif nvm_version_greater "$stripped_version_from_comparator" "$current_version"; then
|
elif nvm_version_greater "$stripped_version_from_comparator" "$current_version"; then
|
||||||
@ -554,7 +563,7 @@ nvm_interpret_complex_semver() {
|
|||||||
# current_version is less than or equal to the current_comparator version number so this current_comparator is satisfied.
|
# current_version is less than or equal to the current_comparator version number so this current_comparator is satisfied.
|
||||||
if [ -z "$current_comparator_set_copy" ]; then
|
if [ -z "$current_comparator_set_copy" ]; then
|
||||||
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
||||||
highest_compatible_versions="$highest_compatible_versions $current_version"
|
highest_compatible_version="$current_version"
|
||||||
version_list_copy=''
|
version_list_copy=''
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
@ -567,7 +576,7 @@ nvm_interpret_complex_semver() {
|
|||||||
# current_version is greater than or equal to the current_comparator version number so this current_comparator is satisfied.
|
# current_version is greater than or equal to the current_comparator version number so this current_comparator is satisfied.
|
||||||
if [ -z "$current_comparator_set_copy" ]; then
|
if [ -z "$current_comparator_set_copy" ]; then
|
||||||
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
||||||
highest_compatible_versions="$highest_compatible_versions $current_version"
|
highest_compatible_version="$current_version"
|
||||||
version_list_copy=''
|
version_list_copy=''
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
@ -581,7 +590,7 @@ nvm_interpret_complex_semver() {
|
|||||||
# current_version is less than the current_comparator version number so this current_comparator is satisfied.
|
# current_version is less than the current_comparator version number so this current_comparator is satisfied.
|
||||||
if [ -z "$current_comparator_set_copy" ]; then
|
if [ -z "$current_comparator_set_copy" ]; then
|
||||||
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
||||||
highest_compatible_versions="$highest_compatible_versions $current_version"
|
highest_compatible_version="$current_version"
|
||||||
version_list_copy=''
|
version_list_copy=''
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
@ -594,7 +603,7 @@ nvm_interpret_complex_semver() {
|
|||||||
# current_version is greater than the current_comparator version number so this current_comparator is satisfied.
|
# current_version is greater than the current_comparator version number so this current_comparator is satisfied.
|
||||||
if [ -z "$current_comparator_set_copy" ];then
|
if [ -z "$current_comparator_set_copy" ];then
|
||||||
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
# Also, this is the last comparator in the current_comparator_set_copy so we can assume we've found the newest compatible version of the current_comparator_set.
|
||||||
highest_compatible_versions="$highest_compatible_versions $current_version"
|
highest_compatible_version="$current_version"
|
||||||
version_list_copy=''
|
version_list_copy=''
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
@ -610,27 +619,11 @@ nvm_interpret_complex_semver() {
|
|||||||
done # while [ -n "$version_list_copy" ]; do
|
done # while [ -n "$version_list_copy" ]; do
|
||||||
done # while [ -n "$semver" ]; do
|
done # while [ -n "$semver" ]; do
|
||||||
|
|
||||||
# Iterate through each of the versions in highest_compatible_versions, which are the highest versions that satisfy each of the comparator sets.
|
if [ -n "$highest_compatible_version" ] && [ "$highest_compatible_version" != '0.0.0' ]; then
|
||||||
# Since comparator sets are separated by '||', choosing any of the highest versions compatible with any of the comparator_sets would be compatible with the whole semver.
|
|
||||||
# Therefore, we should resolve to the highest version in highest_compatible_versions.
|
|
||||||
local highest_compatible_version
|
|
||||||
local compatible_node_version
|
|
||||||
highest_compatible_version='0.0.0'
|
|
||||||
highest_compatible_versions=$(command printf '%s' "$highest_compatible_versions" | command tr ' ' '\n')
|
|
||||||
while [ -n "$highest_compatible_versions" ]; do
|
|
||||||
compatible_node_version=$(command printf '%s' "$highest_compatible_versions" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
|
|
||||||
highest_compatible_versions=$(command printf '%s' "$highest_compatible_versions" | command tail -n +2)
|
|
||||||
[ -n "$compatible_node_version" ] || continue
|
|
||||||
|
|
||||||
if nvm_version_greater "$compatible_node_version" "$highest_compatible_version"; then
|
|
||||||
highest_compatible_version="$compatible_node_version"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if [ "$highest_compatible_version" != '0.0.0' ]; then
|
|
||||||
command printf '%s' "$highest_compatible_version"
|
command printf '%s' "$highest_compatible_version"
|
||||||
else
|
return 0
|
||||||
return 1
|
|
||||||
fi
|
fi
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Given a semver and version list, optimize discovery of highest compatible version with this function which quickly interprets some common semvers.
|
# Given a semver and version list, optimize discovery of highest compatible version with this function which quickly interprets some common semvers.
|
||||||
@ -677,7 +670,7 @@ nvm_interpret_simple_semver() {
|
|||||||
command printf '%s' "$newest_version_from_list"
|
command printf '%s' "$newest_version_from_list"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
# TODO we know it's not worth doing the complex semver interpretation at this point
|
command printf '%s' 'STOP' # we have determined no node version will be compatible with the semver
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -3,14 +3,14 @@
|
|||||||
\. ../../../nvm.sh
|
\. ../../../nvm.sh
|
||||||
\. ../../generated_semvers.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.
|
||||||
|
|
||||||
# POSITIVE TEST CASES
|
# (TEST 1 POSITIVE TEST CASES)
|
||||||
|
# SEMVERS: valid
|
||||||
|
# PACKAGE.JSON TEMPLATES: invalid
|
||||||
# (TEST SET #1) uses valid TEST_SEMVER's and valid package.json templates
|
|
||||||
test_cases="$VALID_SEMVERS_FOR_PKG_JSON"
|
test_cases="$VALID_SEMVERS_FOR_PKG_JSON"
|
||||||
if [ -z "$test_cases" ]; then
|
if [ -z "$test_cases" ]; then
|
||||||
die 'TEST SET 1 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
die 'TEST 1 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
||||||
fi
|
fi
|
||||||
prev_semver=''
|
prev_semver=''
|
||||||
for template_file_name in package_json_templates/_valid_*; do
|
for template_file_name in package_json_templates/_valid_*; do
|
||||||
@ -21,26 +21,24 @@ for template_file_name in package_json_templates/_valid_*; do
|
|||||||
expectedOutput=$(nvm_trim_and_reduce_whitespace_to_one_space "$semver")
|
expectedOutput=$(nvm_trim_and_reduce_whitespace_to_one_space "$semver")
|
||||||
|
|
||||||
if [ "$prev_semver" = "$semver" ]; then
|
if [ "$prev_semver" = "$semver" ]; then
|
||||||
die "Problem iterating through test_cases (TEST SET #1). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
die "Problem iterating through test_cases (TEST 1). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
||||||
fi
|
fi
|
||||||
prev_semver="$semver"
|
prev_semver="$semver"
|
||||||
|
|
||||||
pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
|
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")
|
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
|
||||||
if [ "$actual_output" != "$expectedOutput" ] || [ -z "$actual_output" ] || [ -z "$pkg_json_contents" ]; then
|
if [ "$actual_output" != "$expectedOutput" ] || [ -z "$actual_output" ] || [ -z "$pkg_json_contents" ]; then
|
||||||
die "'nvm_get_node_from_pkg_json' POSITIVE test case failed (TEST SET #1). Expected '$expectedOutput' but got '$actual_output' when given input '$semver' and template '$template_file_name':\n$pkg_json_contents"
|
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
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# (TEST 2 NEGATIVE TEST CASES)
|
||||||
# NEGATIVE TEST CASES
|
# SEMVERS: valid
|
||||||
|
# PACKAGE.JSON TEMPLATES: invalid
|
||||||
|
|
||||||
# (TEST SET #2) uses valid TEST_SEMVER's but invalid package.json templates
|
|
||||||
test_cases="$VALID_SEMVERS_FOR_PKG_JSON"
|
test_cases="$VALID_SEMVERS_FOR_PKG_JSON"
|
||||||
if [ -z "$test_cases" ]; then
|
if [ -z "$test_cases" ]; then
|
||||||
die 'TEST SET 2 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
die 'TEST 2 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
||||||
fi
|
fi
|
||||||
prev_semver=''
|
prev_semver=''
|
||||||
for template_file_name in package_json_templates/_invalid_*; do
|
for template_file_name in package_json_templates/_invalid_*; do
|
||||||
@ -50,23 +48,25 @@ for template_file_name in package_json_templates/_invalid_*; do
|
|||||||
[ -n "$semver" ] || continue
|
[ -n "$semver" ] || continue
|
||||||
|
|
||||||
if [ "$prev_semver" = "$semver" ]; then
|
if [ "$prev_semver" = "$semver" ]; then
|
||||||
die "Problem iterating through test_cases (TEST SET #2). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
die "Problem iterating through test_cases (TEST 2). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
||||||
fi
|
fi
|
||||||
prev_semver="$semver"
|
prev_semver="$semver"
|
||||||
|
|
||||||
pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
|
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")
|
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
|
||||||
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
|
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
|
||||||
die "'nvm_get_node_from_pkg_json' NEGATIVE test case failed (TEST SET #2). Expected to get empty string but got '$actual_output' when given input template '$template_file_name':\n$pkg_json_contents"
|
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
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
# (TEST SET #3) uses invalid TEST_SEMVER's but valid package.json templates
|
# (TEST 3 NEGATIVE TEST CASES)
|
||||||
|
# SEMVERS: invalid
|
||||||
|
# PACKAGE.JSON TEMPLATES: valid
|
||||||
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
|
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
|
||||||
if [ -z "$test_cases" ]; then
|
if [ -z "$test_cases" ]; then
|
||||||
die 'TEST SET 3 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
die 'TEST 3 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
||||||
fi
|
fi
|
||||||
prev_semver=''
|
prev_semver=''
|
||||||
for template_file_name in package_json_templates/_valid_*; do
|
for template_file_name in package_json_templates/_valid_*; do
|
||||||
@ -76,22 +76,24 @@ for template_file_name in package_json_templates/_valid_*; do
|
|||||||
test_cases=$(echo "$test_cases" | tail -n +2)
|
test_cases=$(echo "$test_cases" | tail -n +2)
|
||||||
|
|
||||||
if [ "$prev_semver" = "$semver" ]; then
|
if [ "$prev_semver" = "$semver" ]; then
|
||||||
die "Problem iterating through test_cases (TEST SET #3). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
die "Problem iterating through test_cases (TEST 3). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
||||||
fi
|
fi
|
||||||
prev_semver=$(printf "%s" "$semver")
|
prev_semver="$semver"
|
||||||
|
|
||||||
pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
|
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")
|
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
|
||||||
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
|
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
|
||||||
die "'nvm_get_node_from_pkg_json' NEGATIVE test case failed (TEST SET #3). Expected to get empty string but got '$actual_output' when given input template '$template_file_name':\n$pkg_json_contents"
|
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
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
# (TEST SET #4) uses invalid TEST_SEMVER's and invalid package.json templates
|
# (TEST 4 NEGATIVE TEST CASES)
|
||||||
|
# SEMVERS: invalid
|
||||||
|
# PACKAGE.JSON TEMPLATES: invalid
|
||||||
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
|
test_cases="$INVALID_SEMVERS_FOR_PKG_JSON"
|
||||||
if [ -z "$test_cases" ]; then
|
if [ -z "$test_cases" ]; then
|
||||||
die 'TEST SET 4 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
die 'TEST 4 for nvm_get_node_from_pkg_json given an empty set of test cases'
|
||||||
fi
|
fi
|
||||||
prev_semver=''
|
prev_semver=''
|
||||||
for template_file_name in package_json_templates/_invalid_*; do
|
for template_file_name in package_json_templates/_invalid_*; do
|
||||||
@ -101,14 +103,14 @@ for template_file_name in package_json_templates/_invalid_*; do
|
|||||||
test_cases=$(echo "$test_cases" | tail -n +2)
|
test_cases=$(echo "$test_cases" | tail -n +2)
|
||||||
|
|
||||||
if [ "$prev_semver" = "$semver" ]; then
|
if [ "$prev_semver" = "$semver" ]; then
|
||||||
die "Problem iterating through test_cases (TEST SET #4). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
die "Problem iterating through test_cases (TEST 4). Encountered the same value twice in a row. prev_semver='$prev_semver' semver='$semver'.\n"
|
||||||
fi
|
fi
|
||||||
prev_semver=$(printf "%s" "$semver")
|
prev_semver="$semver"
|
||||||
|
|
||||||
pkg_json_contents=$(sed "s/NODE_SEMVER/$semver/g" "$template_file_name" | tail -n +3)
|
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")
|
actual_output=$(nvm_get_node_from_pkg_json "$pkg_json_contents")
|
||||||
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
|
if [ "$actual_output" != "" ] || [ -z "$semver" ] || [ -z "$pkg_json_contents" ]; then
|
||||||
die "'nvm_get_node_from_pkg_json' NEGATIVE test case failed (TEST SET #4). Expected to get empty string but got '$actual_output' when given input template '$template_file_name':\n$pkg_json_contents"
|
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
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
@ -3,19 +3,13 @@
|
|||||||
\. ../../../nvm.sh
|
\. ../../../nvm.sh
|
||||||
\. ../../generated_semvers.sh
|
\. ../../generated_semvers.sh
|
||||||
|
|
||||||
# nvm_is_normalized_semver validates that given semvers adhere to the following grammer
|
# 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.
|
||||||
#
|
|
||||||
# semver ::= comparator_set ( ' || ' comparator_set )*
|
|
||||||
# comparator_set ::= comparator ( ' ' comparator )*
|
|
||||||
# comparator ::= ( '<' | '<=' | '>' | '>=' | '' ) [0-9]+ '.' [0-9]+ '.' [0-9]+
|
|
||||||
|
|
||||||
# POSITIVE TEST CASES
|
# POSITIVE TEST CASES
|
||||||
|
|
||||||
positive_test_cases="$VALID_NORMALIZED_SEMVERS"
|
positive_test_cases="$VALID_NORMALIZED_SEMVERS"
|
||||||
if [ -z "$positive_test_cases" ]; then
|
if [ -z "$positive_test_cases" ]; then
|
||||||
die 'positive test cases are empty'
|
die 'positive test cases are empty'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
prev_semver=''
|
prev_semver=''
|
||||||
while [ -n "$positive_test_cases" ]; do
|
while [ -n "$positive_test_cases" ]; do
|
||||||
semver=$(echo "$positive_test_cases" | head -n1)
|
semver=$(echo "$positive_test_cases" | head -n1)
|
||||||
@ -30,12 +24,10 @@ while [ -n "$positive_test_cases" ]; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# NEGATIVE TEST CASES
|
# NEGATIVE TEST CASES
|
||||||
|
|
||||||
negative_test_cases=$(printf "%s\n%s" "$VALID_NON_NORMALIZED_SEMVERS" "$INVALID_SEMVERS_FOR_PKG_JSON")
|
negative_test_cases=$(printf "%s\n%s" "$VALID_NON_NORMALIZED_SEMVERS" "$INVALID_SEMVERS_FOR_PKG_JSON")
|
||||||
if [ -z "$negative_test_cases" ]; then
|
if [ -z "$negative_test_cases" ]; then
|
||||||
die 'negative test cases are empty'
|
die 'negative test cases are empty'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
prev_semver='initialized to non empty string'
|
prev_semver='initialized to non empty string'
|
||||||
while [ -n "$negative_test_cases" ]; do
|
while [ -n "$negative_test_cases" ]; do
|
||||||
semver=$(echo "$negative_test_cases" | head -n1)
|
semver=$(echo "$negative_test_cases" | head -n1)
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
\. ../../../nvm.sh
|
\. ../../../nvm.sh
|
||||||
\. ../../generated_semvers.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 1: Validate that for already normalized semvers, nvm_normalize_semver outputs the same semver.
|
||||||
test_cases="$VALID_NORMALIZED_SEMVERS"
|
test_cases="$VALID_NORMALIZED_SEMVERS"
|
||||||
if [ -z "$test_cases" ]; then
|
if [ -z "$test_cases" ]; then
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
\. ../../../nvm.sh
|
\. ../../../nvm.sh
|
||||||
\. ../../generated_semvers.sh
|
\. ../../generated_semvers.sh
|
||||||
|
|
||||||
normalized_semver_regexp='^( ?(<|<=|>|>=)?[0-9]+\.[0-9]+\.[0-9]+)+( \|\| ( ?(<|<=|>|>=)?[0-9]+\.[0-9]+\.[0-9]+)+)*$'
|
normalized_semver_regexp='^( ?(<|<=|>|>=)?[0-9]+\.[0-9]+\.[0-9]+)+( \|\| ( ?(<|<=|>|>=)?[0-9]+\.[0-9]+\.[0-9]+)+)*$'
|
||||||
|
|
||||||
# POSITIVE TEST CASES
|
# 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"
|
test_cases="$VALID_NORMALIZED_SEMVERS"
|
||||||
while [ -n "$test_cases" ]; do
|
while [ -n "$test_cases" ]; do
|
||||||
string=$(echo "$test_cases" | head -n1)
|
string=$(echo "$test_cases" | head -n1)
|
||||||
@ -17,10 +17,8 @@ while [ -n "$test_cases" ]; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# NEGATIVE TEST CASES
|
# NEGATIVE TEST CASES
|
||||||
|
|
||||||
# string:regexp
|
# string:regexp
|
||||||
test_cases=$(printf "%s\n%s" "$VALID_NON_NORMALIZED_SEMVERS" "$INVALID_SEMVERS_FOR_PKG_JSON")
|
test_cases=$(printf "%s\n%s" "$VALID_NON_NORMALIZED_SEMVERS" "$INVALID_SEMVERS_FOR_PKG_JSON")
|
||||||
|
|
||||||
while [ -n "$test_cases" ]; do
|
while [ -n "$test_cases" ]; do
|
||||||
string=$(echo "$test_cases" | head -n1)
|
string=$(echo "$test_cases" | head -n1)
|
||||||
if [ -z "$normalized_semver_regexp" ] || nvm_string_contains_regexp "$string" "$normalized_semver_regexp"; then
|
if [ -z "$normalized_semver_regexp" ] || nvm_string_contains_regexp "$string" "$normalized_semver_regexp"; then
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
\. ../../../nvm.sh
|
||||||
die () { printf "$@" ; exit 1; }
|
die () { printf "$@" ; exit 1; }
|
||||||
|
|
||||||
\. ../../../nvm.sh
|
# 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
|
# Test cases that have no new lines
|
||||||
# input:expected_output
|
# input:expected_output
|
||||||
@ -26,27 +27,27 @@ done
|
|||||||
|
|
||||||
# Test cases that have new lines
|
# Test cases that have new lines
|
||||||
expected_output='1 2 3'
|
expected_output='1 2 3'
|
||||||
new_line_1=' 1
|
test_case_with_new_line_1=' 1
|
||||||
2 3 '
|
2 3 '
|
||||||
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$new_line_1")
|
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$test_case_with_new_line_1")
|
||||||
if [ "$actual_output" != "$expected_output" ]; then
|
if [ "$actual_output" != "$expected_output" ]; then
|
||||||
die "nvm_trim_and_reduce_whitespace_to_one_space test with new_line_1 failed. Actual output: '$actual_output' Expected output: '$expected_output'"
|
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
|
fi
|
||||||
|
|
||||||
new_line_2=' 1 2
|
test_case_with_new_line_2=' 1 2
|
||||||
3 '
|
3 '
|
||||||
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$new_line_2")
|
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$test_case_with_new_line_2")
|
||||||
if [ "$actual_output" != "$expected_output" ]; then
|
if [ "$actual_output" != "$expected_output" ]; then
|
||||||
die "nvm_trim_and_reduce_whitespace_to_one_space test with new_line_2 failed. Actual output: '$actual_output' Expected output: '$expected_output'"
|
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
|
fi
|
||||||
|
|
||||||
new_line_3=' 1
|
test_case_with_new_line_3=' 1
|
||||||
|
|
||||||
2
|
2
|
||||||
3'
|
3'
|
||||||
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$new_line_3")
|
actual_output=$(nvm_trim_and_reduce_whitespace_to_one_space "$test_case_with_new_line_3")
|
||||||
if [ "$actual_output" != "$expected_output" ]; then
|
if [ "$actual_output" != "$expected_output" ]; then
|
||||||
die "nvm_trim_and_reduce_whitespace_to_one_space test with new_line_3 failed. Actual output: '$actual_output' Expected output: '$expected_output'"
|
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
|
fi
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ VALID_NON_NORMALIZED_SEMVER_OPERATORS='v
|
|||||||
# Versions (stripped of any operators) that are considered valid inputs to the semver interpretation logic.
|
# Versions (stripped of any operators) that are considered valid inputs to the semver interpretation logic.
|
||||||
VALID_NORMALIZED_VERSIONS='4.1.0
|
VALID_NORMALIZED_VERSIONS='4.1.0
|
||||||
0.12.18
|
0.12.18
|
||||||
0.11.16
|
8.0.0
|
||||||
6.11.4
|
6.11.4
|
||||||
10.0.0'
|
10.0.0'
|
||||||
|
|
||||||
@ -62,8 +62,6 @@ asdf
|
|||||||
1111
|
1111
|
||||||
1 1
|
1 1
|
||||||
1.
|
1.
|
||||||
1.1
|
|
||||||
1.*
|
|
||||||
1.2
|
1.2
|
||||||
11.222
|
11.222
|
||||||
1.2.a
|
1.2.a
|
||||||
@ -71,8 +69,6 @@ asdf
|
|||||||
1.x.x
|
1.x.x
|
||||||
11.22.a
|
11.22.a
|
||||||
=1.2.3
|
=1.2.3
|
||||||
~1.2.3
|
|
||||||
^1.2.3
|
|
||||||
1.1.1 2.2.2
|
1.1.1 2.2.2
|
||||||
>1.1.1 <1.1.0
|
>1.1.1 <1.1.0
|
||||||
1.2 - 1.3
|
1.2 - 1.3
|
||||||
@ -81,9 +77,7 @@ asdf
|
|||||||
1.2.3 - 1.2.4
|
1.2.3 - 1.2.4
|
||||||
1.2.3-1.2.4
|
1.2.3-1.2.4
|
||||||
1.2 1.3
|
1.2 1.3
|
||||||
1 2
|
10 20
|
||||||
1.2.3||1.2.4
|
|
||||||
1.2||1.3
|
|
||||||
1||2
|
1||2
|
||||||
>1000
|
>1000
|
||||||
<0"
|
<0"
|
||||||
@ -97,12 +91,23 @@ VALID_NORMALIZED_COMPLEX_SEMVERS='10.3.0 || 8.1.1 || 4.1.0
|
|||||||
|
|
||||||
# Valid semvers that should resolve to a node version but need to be validated/normalized before interpretting.
|
# Valid semvers that should resolve to a node version but need to be validated/normalized before interpretting.
|
||||||
VALID_NON_NORMALIZED_COMPLEX_SEMVERS='x
|
VALID_NON_NORMALIZED_COMPLEX_SEMVERS='x
|
||||||
|
10
|
||||||
|
~10
|
||||||
|
^10
|
||||||
X
|
X
|
||||||
*
|
*
|
||||||
x.x
|
x.x
|
||||||
X.X
|
X.X
|
||||||
*.*
|
*.*
|
||||||
|
10.x
|
||||||
|
~10.x
|
||||||
|
^10.x
|
||||||
|
10.X.X
|
||||||
|
~10.X.X
|
||||||
|
^10.X.X
|
||||||
x.x.x
|
x.x.x
|
||||||
|
~X.X.X
|
||||||
|
^X.X.X
|
||||||
X.X.X
|
X.X.X
|
||||||
x.X.*
|
x.X.*
|
||||||
*.x.X
|
*.x.X
|
||||||
|
@ -29,14 +29,23 @@ while [ -n "$test_cases" ]; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# TODO add more test cases here
|
|
||||||
# TODO get the following semvers working: ^7 ^7.x ^7.0 ~8 ~8.0
|
|
||||||
# Verify actual outputs given some inputs
|
# Verify actual outputs given some inputs
|
||||||
# input:expected_output
|
# input:expected_output
|
||||||
test_cases="*:$NEWEST_NODE_VERSION
|
test_cases="*:$NEWEST_NODE_VERSION
|
||||||
|
^5:$NEWEST_NODE_VERSION_5
|
||||||
|
^5.0:$NEWEST_NODE_VERSION_5
|
||||||
|
^5.x:$NEWEST_NODE_VERSION_5
|
||||||
|
^5.X:$NEWEST_NODE_VERSION_5
|
||||||
|
^5.*:$NEWEST_NODE_VERSION_5
|
||||||
|
~5:$NEWEST_NODE_VERSION_5
|
||||||
|
~5.0:$NEWEST_NODE_VERSION_5
|
||||||
|
~5.x:$NEWEST_NODE_VERSION_5
|
||||||
|
~5.X:$NEWEST_NODE_VERSION_5
|
||||||
|
~5.*:$NEWEST_NODE_VERSION_5
|
||||||
5:$NEWEST_NODE_VERSION_5
|
5:$NEWEST_NODE_VERSION_5
|
||||||
x:$NEWEST_NODE_VERSION
|
x:$NEWEST_NODE_VERSION
|
||||||
X:$NEWEST_NODE_VERSION
|
X:$NEWEST_NODE_VERSION
|
||||||
|
x.x.x:$NEWEST_NODE_VERSION
|
||||||
0.12.18:0.12.18
|
0.12.18:0.12.18
|
||||||
0.11.16:0.11.16
|
0.11.16:0.11.16
|
||||||
7.0.0||7.2.1:7.2.1
|
7.0.0||7.2.1:7.2.1
|
||||||
|
Loading…
Reference in New Issue
Block a user