Skip to content

Commit 950853d

Browse files
committed
test plugin in subshell and from isolated asdf
The subshell sources the isolated asdf environment, making the asdf command available. In some plugin tests we were having failures do to the asdf bin not set on PATH.
1 parent c060b4c commit 950853d

1 file changed

Lines changed: 74 additions & 59 deletions

File tree

‎lib/commands/plugin-test.sh‎

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,98 @@
1-
fail_test() {
2-
echo "FAILED: $1"
3-
rm -rf "$ASDF_DIR"
4-
exit 1
5-
}
61

72
plugin_test_command() {
8-
ASDF_DIR=$(mktemp -dt asdf.XXXX)
9-
export ASDF_DIR
10-
git clone https://github.com/asdf-vm/asdf.git "$ASDF_DIR"
113

124
local plugin_name=$1
135
local plugin_url=$2
146
local plugin_command="${*:3}"
157

16-
if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then
17-
fail_test "please provide a plugin name and url"
18-
fi
8+
local exit_code
9+
local TEST_DIR
1910

20-
if ! (asdf plugin-add "$plugin_name" "$plugin_url"); then
21-
fail_test "could not install $plugin_name from $plugin_url"
22-
fi
11+
TEST_DIR=$(mktemp -dt asdf.XXXX)
12+
git clone "$ASDF_DIR/.git" "$TEST_DIR"
2313

24-
if ! (asdf plugin-list | grep "^$plugin_name$" > /dev/null); then
25-
fail_test "$plugin_name was not properly installed"
26-
fi
14+
fail_test() {
15+
echo "FAILED: $1"
16+
rm -rf "$TEST_DIR"
17+
exit 1
18+
}
2719

28-
local versions
29-
# shellcheck disable=SC2046
30-
if ! read -r -a versions <<< $(asdf list-all "$plugin_name"); then
31-
fail_test "list-all exited with an error"
32-
fi
20+
plugin_test() {
21+
export ASDF_DIR=$TEST_DIR
3322

34-
if [ ${#versions} -eq 0 ]; then
35-
fail_test "list-all did not return any version"
36-
fi
23+
# shellcheck disable=SC1090
24+
source "$ASDF_DIR/asdf.sh"
3725

38-
local latest_version
39-
latest_version=${versions[${#versions[@]} - 1]}
26+
if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then
27+
fail_test "please provide a plugin name and url"
28+
fi
4029

41-
if ! (asdf install "$plugin_name" "$latest_version"); then
42-
fail_test "install exited with an error"
43-
fi
30+
if ! (asdf plugin-add "$plugin_name" "$plugin_url"); then
31+
fail_test "could not install $plugin_name from $plugin_url"
32+
fi
4433

45-
cd "$ASDF_DIR" || fail_test "could not cd $ASDF_DIR"
34+
if ! (asdf plugin-list | grep "^$plugin_name$" > /dev/null); then
35+
fail_test "$plugin_name was not properly installed"
36+
fi
4637

47-
if ! (asdf local "$plugin_name" "$latest_version"); then
48-
fail_test "install did not add the requested version"
49-
fi
5038

51-
if ! (asdf reshim "$plugin_name"); then
52-
fail_test "could not reshim plugin"
53-
fi
39+
local versions
40+
# shellcheck disable=SC2046
41+
if ! read -r -a versions <<< $(asdf list-all "$plugin_name"); then
42+
fail_test "list-all exited with an error"
43+
fi
44+
45+
if [ ${#versions} -eq 0 ]; then
46+
fail_test "list-all did not return any version"
47+
fi
48+
49+
local latest_version
50+
latest_version=${versions[${#versions[@]} - 1]}
51+
52+
if ! (asdf install "$plugin_name" "$latest_version"); then
53+
fail_test "install exited with an error"
54+
fi
55+
56+
cd "$TEST_DIR" || fail_test "could not cd $TEST_DIR"
57+
58+
if ! (asdf local "$plugin_name" "$latest_version"); then
59+
fail_test "install did not add the requested version"
60+
fi
5461

55-
if [ -n "$plugin_command" ]; then
56-
(PATH="$ASDF_DIR/bin:$ASDF_DIR/shims:$PATH" "$plugin_command")
57-
local exit_code
58-
exit_code=$?
59-
if [ $exit_code != 0 ]; then
60-
fail_test "$plugin_command failed with exit code $?"
62+
if ! (asdf reshim "$plugin_name"); then
63+
fail_test "could not reshim plugin"
6164
fi
62-
fi
6365

64-
# Assert the scripts in bin are executable by asdf
65-
for filename in "$ASDF_DIR/plugins/$plugin_name/bin"/*
66-
do
67-
if [ ! -x "$filename" ]; then
68-
fail_test "Incorrect permissions on $filename. Must be executable by asdf"
66+
if [ -n "$plugin_command" ]; then
67+
$plugin_command
68+
exit_code=$?
69+
if [ $exit_code != 0 ]; then
70+
fail_test "$plugin_command failed with exit code $?"
71+
fi
6972
fi
70-
done
7173

72-
# Assert that a license file exists in the plugin repo and is not empty
73-
license_file="$ASDF_DIR/plugins/$plugin_name/LICENSE"
74-
if [ -f "$license_file" ]; then
75-
if [ ! -s "$license_file" ]; then
76-
fail_test "LICENSE file in the plugin repository must not be empty"
74+
# Assert the scripts in bin are executable by asdf
75+
for filename in "$ASDF_DIR/plugins/$plugin_name/bin"/*
76+
do
77+
if [ ! -x "$filename" ]; then
78+
fail_test "Incorrect permissions on $filename. Must be executable by asdf"
79+
fi
80+
done
81+
82+
# Assert that a license file exists in the plugin repo and is not empty
83+
license_file="$ASDF_DIR/plugins/$plugin_name/LICENSE"
84+
if [ -f "$license_file" ]; then
85+
if [ ! -s "$license_file" ]; then
86+
fail_test "LICENSE file in the plugin repository must not be empty"
87+
fi
88+
else
89+
fail_test "LICENSE file must be present in the plugin repository"
7790
fi
78-
else
79-
fail_test "LICENSE file must be present in the plugin repository"
80-
fi
91+
}
8192

82-
rm -rf "$ASDF_DIR"
93+
# run test in a subshell
94+
(plugin_test)
95+
exit_code=$?
96+
rm -rf "$TEST_DIR"
97+
exit $exit_code
8398
}

0 commit comments

Comments
 (0)