@@ -8,5 +8,93 @@ source dev-container-features-test-lib
88# Definition specific tests
99check " version" gh --version
1010
11+ find_version_from_git_tags () {
12+ local variable_name=$1
13+ local requested_version=${! variable_name}
14+ if [ " ${requested_version} " = " none" ]; then return ; fi
15+ local repository=$2
16+ local prefix=${3:- " tags/v" }
17+ local separator=${4:- " ." }
18+ local last_part_optional=${5:- " false" }
19+ if [ " $( echo " ${requested_version} " | grep -o " ." | wc -l) " != " 2" ]; then
20+ local escaped_separator=${separator// ./ \\ .}
21+ local last_part
22+ if [ " ${last_part_optional} " = " true" ]; then
23+ last_part=" (${escaped_separator} [0-9]+)?"
24+ else
25+ last_part=" ${escaped_separator} [0-9]+"
26+ fi
27+ local regex=" ${prefix} \\ K[0-9]+${escaped_separator} [0-9]+${last_part} $"
28+ local version_list=" $( git ls-remote --tags ${repository} | grep -oP " ${regex} " | tr -d ' ' | tr " ${separator} " " ." | sort -rV) "
29+ if [ " ${requested_version} " = " latest" ] || [ " ${requested_version} " = " current" ] || [ " ${requested_version} " = " lts" ]; then
30+ declare -g ${variable_name} =" $( echo " ${version_list} " | head -n 1) "
31+ else
32+ set +e
33+ declare -g ${variable_name} =" $( echo " ${version_list} " | grep -E -m 1 " ^${requested_version// ./ \\ .} ([\\ .\\ s]|$)" ) "
34+ set -e
35+ fi
36+ fi
37+ if [ -z " ${! variable_name} " ] || ! echo " ${version_list} " | grep " ^${! variable_name// ./ \\ .} $" > /dev/null 2>&1 ; then
38+ echo -e " Invalid ${variable_name} value: ${requested_version} \nValid values:\n${version_list} " >&2
39+ exit 1
40+ fi
41+ echo " ${variable_name} =${! variable_name} "
42+ }
43+
44+ find_prev_version_from_git_tags () {
45+ local variable_name=$1
46+ local current_version=${! variable_name}
47+ local repository=$2
48+ # Normally a "v" is used before the version number, but support alternate cases
49+ local prefix=${3:- " tags/v" }
50+ # Some repositories use "_" instead of "." for version number part separation, support that
51+ local separator=${4:- " ." }
52+ # Some tools release versions that omit the last digit (e.g. go)
53+ local last_part_optional=${5:- " false" }
54+ # Some repositories may have tags that include a suffix (e.g. actions/node-versions)
55+ local version_suffix_regex=$6
56+ # Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
57+ set +e
58+ major=" $( echo " ${current_version} " | grep -oE ' ^[0-9]+' || echo ' ' ) "
59+ minor=" $( echo " ${current_version} " | grep -oP ' ^[0-9]+\.\K[0-9]+' || echo ' ' ) "
60+ breakfix=" $( echo " ${current_version} " | grep -oP ' ^[0-9]+\.[0-9]+\.\K[0-9]+' 2> /dev/null || echo ' ' ) "
61+
62+ if [ " ${minor} " = " 0" ] && [ " ${breakfix} " = " 0" ]; then
63+ (( major= major- 1 ))
64+ declare -g ${variable_name} =" ${major} "
65+ # Look for latest version from previous major release
66+ find_version_from_git_tags " ${variable_name} " " ${repository} " " ${prefix} " " ${separator} " " ${last_part_optional} "
67+ # Handle situations like Go's odd version pattern where "0" releases omit the last part
68+ elif [ " ${breakfix} " = " " ] || [ " ${breakfix} " = " 0" ]; then
69+ (( minor= minor- 1 ))
70+ declare -g ${variable_name} =" ${major} .${minor} "
71+ # Look for latest version from previous minor release
72+ find_version_from_git_tags " ${variable_name} " " ${repository} " " ${prefix} " " ${separator} " " ${last_part_optional} "
73+ else
74+ (( breakfix= breakfix- 1 ))
75+ if [ " ${breakfix} " = " 0" ] && [ " ${last_part_optional} " = " true" ]; then
76+ declare -g ${variable_name} =" ${major} .${minor} "
77+ else
78+ declare -g ${variable_name} =" ${major} .${minor} .${breakfix} "
79+ fi
80+ fi
81+ }
82+
83+ CLI_VERSION=" 2.20.0"
84+ find_prev_version_from_git_tags CLI_VERSION https://github.com/cli/cli
85+ check " pre-version-to-2.20.0" bash -c " echo ${CLI_VERSION} | grep '2.19.0'"
86+
87+ CLI_VERSION=" 2.18.1"
88+ find_prev_version_from_git_tags CLI_VERSION https://github.com/cli/cli
89+ check " pre-version-to-2.18.1" bash -c " echo ${CLI_VERSION} | grep '2.18.0'"
90+
91+ CLI_VERSION=" 2.15.0"
92+ find_prev_version_from_git_tags CLI_VERSION https://github.com/cli/cli
93+ check " pre-version-to-2.15.0" bash -c " echo ${CLI_VERSION} | grep '2.14.7'"
94+
95+ CLI_VERSION=" 2.0.0"
96+ find_prev_version_from_git_tags CLI_VERSION https://github.com/cli/cli
97+ check " pre-version-to-2.0.0" bash -c " echo ${CLI_VERSION} | grep '1.14.0'"
98+
1199# Report result
12100reportResults
0 commit comments