Skip to content

Commit

Permalink
Merge pull request #73 from OpenVoiceOS/fix/issue_72
Browse files Browse the repository at this point in the history
[scenario] Make sure scenario is valid
  • Loading branch information
goldyfruit authored Apr 25, 2024
2 parents 87b6f57 + 9d705bf commit 7c64863
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tests/bats/scenario.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function setup() {
load ../../utils/constants.sh
load ../../utils/common.sh
LOG_FILE=/tmp/ovos-installer.log
SCENARIO_ALLOWED_OPTIONS=(features channel share_telemetry profile method uninstall rapsberry_pi_tuning)
SCENARIO_ALLOWED_OPTIONS=(features channel share_telemetry profile method uninstall rapsberry_pi_tuning hivemind)
SCENARIO_ALLOWED_FEATURES=(skills gui)
}

Expand All @@ -25,7 +25,7 @@ function setup() {
export -f uname curl
run download_yq
assert_success
unset uname curl
unset -f uname curl
}

@test "function_download_yq_download_linux_amd64" {
Expand All @@ -42,7 +42,7 @@ function setup() {
export -f uname curl
run download_yq
assert_success
unset uname curl
unset -f uname curl
}

@test "function_download_yq_download_linux_arm64" {
Expand All @@ -59,7 +59,7 @@ function setup() {
export -f uname curl
run download_yq
assert_success
unset uname curl
unset -f uname curl
}

@test "function_download_yq_download_linux_arm65" {
Expand All @@ -76,7 +76,7 @@ function setup() {
export -f uname curl
run download_yq
assert_failure
unset uname curl
unset -f uname curl
}

@test "function_detect_scenario_directory_found" {
Expand All @@ -101,6 +101,7 @@ function setup() {

@test "function_detect_scenario_valid" {
RUN_AS_HOME=/home/$USER
ARCH="x86_64"
cat <<EOF >$RUN_AS_HOME/.config/ovos-installer/$SCENARIO_NAME
---
uninstall: false
Expand All @@ -117,6 +118,13 @@ EOF
assert_success
}

@test "function_detect_scenario_not_valid_empty" {
RUN_AS_HOME=/home/$USER
run touch $RUN_AS_HOME/.config/ovos-installer/$SCENARIO_NAME
run detect_scenario
assert_failure
}

@test "function_in_array_found" {
run in_array SCENARIO_ALLOWED_OPTIONS uninstall
assert_success
Expand Down
7 changes: 7 additions & 0 deletions utils/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,16 @@ function detect_scenario() {
download_yq
"$YQ_BINARY_PATH" "$SCENARIO_PATH" &>>"$LOG_FILE"

SCENARIO_NOT_SUPPORTED="false"
# shellcheck source=scenario.sh
source utils/scenario.sh

# Check scenario status
if [ "$SCENARIO_NOT_SUPPORTED" == "true" ]; then
echo "scenario not supported" &>>"$LOG_FILE"
on_error
fi

export SCENARIO_FOUND="true"

if [ -f "$YQ_BINARY_PATH" ]; then
Expand Down
17 changes: 17 additions & 0 deletions utils/scenario.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ if [ -f "$SCENARIO_PATH" ]; then
done < <(
"$YQ_BINARY_PATH" '.features | to_entries | map([.key, .value] | join("=")) | .[]' "$SCENARIO_PATH"
)

# Read all the hivemind options
while IFS="=" read -r key_hivemind value_hivemind; do
hivemind["$key_hivemind"]="$value_hivemind"
done < <(
"$YQ_BINARY_PATH" '.hivemind | to_entries | map([.key, .value] | join("=")) | .[]' "$SCENARIO_PATH"
)

# Make sure the scenario file is not empty
if [ -z "${!options[*]}" ]; then
export SCENARIO_NOT_SUPPORTED="true"
elif [ "${#options[@]}" -lt 7 ]; then
export SCENARIO_NOT_SUPPORTED="true"
fi

# Loop over each options and features
for option in "${!options[@]}"; do
# Ensure the option is supported by the installer
Expand Down Expand Up @@ -63,6 +71,9 @@ if [ -f "$SCENARIO_PATH" ]; then
[ "${features[$feature]}" == "true" ] && FEATURE_GUI="true" || FEATURE_GUI="false"
export FEATURE_GUI
;;
*)
export SCENARIO_NOT_SUPPORTED="true"
;;
esac
fi
done
Expand All @@ -84,6 +95,9 @@ if [ -f "$SCENARIO_PATH" ]; then
password)
[ -n "${hivemind[$hivemind_option]}" ] && export SATELLITE_PASSWORD="${hivemind[$hivemind_option]}"
;;
*)
export SCENARIO_NOT_SUPPORTED="true"
;;
esac
fi
done
Expand All @@ -92,6 +106,9 @@ if [ -f "$SCENARIO_PATH" ]; then
[ "${options[$option]}" == "true" ] && SHARE_TELEMETRY="true" || SHARE_TELEMETRY="false"
export SHARE_TELEMETRY
;;
*)
export SCENARIO_NOT_SUPPORTED="true"
;;
esac
fi
done
Expand Down

0 comments on commit 7c64863

Please sign in to comment.