From 0922f090f93d89edf8aaed6952a7ecbc709bdc7e Mon Sep 17 00:00:00 2001 From: Matty Evans Date: Thu, 19 Dec 2024 07:42:57 +1000 Subject: [PATCH] test: Add shell test workflow, bats tests, and test improvements --- .github/workflows/shell-test.yml | 21 ++++++ install.bats | 113 +++++++++++++++++++++++++++++++ install.sh | 82 +++++++++++----------- 3 files changed, 178 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/shell-test.yml create mode 100644 install.bats diff --git a/.github/workflows/shell-test.yml b/.github/workflows/shell-test.yml new file mode 100644 index 0000000..4af6d08 --- /dev/null +++ b/.github/workflows/shell-test.yml @@ -0,0 +1,21 @@ +name: shell-test + +on: + push: + branches: [ '**' ] + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install bats + run: | + git clone https://github.com/bats-core/bats-core.git + cd bats-core + sudo ./install.sh /usr/local + + - name: Run tests + run: bats *.bats \ No newline at end of file diff --git a/install.bats b/install.bats new file mode 100644 index 0000000..b9600da --- /dev/null +++ b/install.bats @@ -0,0 +1,113 @@ +#!/usr/bin/env bats + +setup() { + # Create temp test directory first + export TEST_DIR="$(mktemp -d)" + [ -d "$TEST_DIR" ] || fail "Failed to create temp directory" + + # Set test environment + export TEST_MODE=true + export CONTRIBUTOOR_PATH="$TEST_DIR/.contributoor" + export VERSION="1.0.0" + export INSTALL_MODE="docker" + export CUSTOM_PATH="" + + # Source the script without running main + source './install.sh' +} + +teardown() { + # Clean up test directory if it exists + [ -d "$TEST_DIR" ] && rm -rf "$TEST_DIR" +} + +@test "detect_architecture returns valid architecture" { + run detect_architecture + [ "$status" -eq 0 ] + [[ "$output" =~ ^(amd64|arm64)$ ]] +} + +@test "detect_platform returns valid platform" { + run detect_platform + [ "$status" -eq 0 ] + [[ "$output" =~ ^(linux|darwin)$ ]] +} + +@test "setup directories creates expected structure" { + run mkdir -p "$CONTRIBUTOOR_PATH" + [ "$status" -eq 0 ] + + run mkdir -p "$CONTRIBUTOOR_PATH/bin" + [ "$status" -eq 0 ] + + run mkdir -p "$CONTRIBUTOOR_PATH/logs" + [ "$status" -eq 0 ] + + [ -d "$CONTRIBUTOOR_PATH" ] + [ -d "$CONTRIBUTOOR_PATH/bin" ] + [ -d "$CONTRIBUTOOR_PATH/logs" ] +} + +@test "update_config_file creates valid yaml" { + # Setup directory first + mkdir -p "$CONTRIBUTOOR_PATH" + + local config_file="$CONTRIBUTOOR_PATH/config.yaml" + + # Create initial config with some custom settings + cat > "$config_file" << EOF +customSetting: value +anotherSetting: true +version: old-version +contributoorDirectory: /old/path +runMethod: old-method +yetAnotherSetting: 123 +EOF + + # Debug output + echo "Initial config:" + cat "$config_file" + + run update_config_file "$config_file" + echo "Status: $status" + echo "Output: $output" + + [ "$status" -eq 0 ] + [ -f "$config_file" ] + + # Debug final config + echo "Final config:" + cat "$config_file" + + # Verify custom settings were preserved first + run grep "customSetting: value" "$config_file" + [ "$status" -eq 0 ] + + run grep "anotherSetting: true" "$config_file" + [ "$status" -eq 0 ] + + run grep "yetAnotherSetting: 123" "$config_file" + [ "$status" -eq 0 ] + + # Now verify our updated settings + run grep -F "version: $VERSION" "$config_file" + [ "$status" -eq 0 ] || { + echo "Expected version: $VERSION" + echo "Config contents:" + cat "$config_file" + } + + run grep -F "contributoorDirectory: $CONTRIBUTOOR_PATH" "$config_file" + [ "$status" -eq 0 ] || { + echo "Expected path: $CONTRIBUTOOR_PATH" + echo "Config contents:" + cat "$config_file" + } + + run grep -F "runMethod: $INSTALL_MODE" "$config_file" + [ "$status" -eq 0 ] || { + echo "Expected mode: $INSTALL_MODE" + echo "Config contents:" + cat "$config_file" + } +} \ No newline at end of file diff --git a/install.sh b/install.sh index c5fdc03..982520f 100755 --- a/install.sh +++ b/install.sh @@ -374,8 +374,10 @@ main() { # Installation path progress 3 "Installation path" - printf "\nWhere would you like to install contributoor? [${COLOR_CYAN}~/.contributoor${COLOR_RESET}]: " - read -r CUSTOM_PATH + if [ "${TEST_MODE:-}" != "true" ]; then + printf "\nWhere would you like to install contributoor? [${COLOR_CYAN}~/.contributoor${COLOR_RESET}]: " + read -r CUSTOM_PATH + fi if [ -n "$CUSTOM_PATH" ]; then CONTRIBUTOOR_PATH="$CUSTOM_PATH" CONTRIBUTOOR_BIN="$CONTRIBUTOOR_PATH/bin" @@ -389,41 +391,43 @@ main() { CONTRIBUTOOR_URL="https://github.com/ethpandaops/contributoor/releases/download/v${VERSION}/contributoor_${VERSION}_${PLATFORM}_${ARCH}.tar.gz" # Installation mode selection - selected=1 - trap 'tput cnorm' EXIT - tput civis - while true; do - clear - print_logo - progress 1 "Detecting platform..." - success "$PLATFORM ($ARCH)" - progress 2 "Determining latest version" - success "Using version: $VERSION" - progress 3 "Installation path" - success "Using path: $CONTRIBUTOOR_PATH" - progress 4 "Select installation mode" - printf "\n %s Docker (${COLOR_CYAN}recommended${COLOR_RESET})\n" "$([ "$selected" = 1 ] && echo ">" || echo " ")" - printf " %s Binary\n" "$([ "$selected" = 2 ] && echo ">" || echo " ")" - printf "\nUse arrow keys (↑/↓) or j/k to select, Enter to confirm\n" - - read -r -n1 key - case "$key" in - A|k) [ "$selected" -gt 1 ] && selected=$((selected - 1)) ;; - B|j) [ "$selected" -lt 2 ] && selected=$((selected + 1)) ;; - "") - tput cnorm - printf "Selected: " - if [ "$selected" = 1 ]; then - INSTALL_MODE="docker" - printf "${COLOR_GREEN}Docker${COLOR_RESET}" - else - INSTALL_MODE="binary" - printf "${COLOR_GREEN}Binary${COLOR_RESET}" - fi - break - ;; - esac - done + if [ "${TEST_MODE:-}" != "true" ]; then + selected=1 + trap 'tput cnorm' EXIT + tput civis + while true; do + clear + print_logo + progress 1 "Detecting platform..." + success "$PLATFORM ($ARCH)" + progress 2 "Determining latest version" + success "Using version: $VERSION" + progress 3 "Installation path" + success "Using path: $CONTRIBUTOOR_PATH" + progress 4 "Select installation mode" + printf "\n %s Docker (${COLOR_CYAN}recommended${COLOR_RESET})\n" "$([ "$selected" = 1 ] && echo ">" || echo " ")" + printf " %s Binary\n" "$([ "$selected" = 2 ] && echo ">" || echo " ")" + printf "\nUse arrow keys (↑/↓) or j/k to select, Enter to confirm\n" + + read -r -n1 key + case "$key" in + A|k) [ "$selected" -gt 1 ] && selected=$((selected - 1)) ;; + B|j) [ "$selected" -lt 2 ] && selected=$((selected + 1)) ;; + "") + tput cnorm + printf "Selected: " + if [ "$selected" = 1 ]; then + INSTALL_MODE="docker" + printf "${COLOR_GREEN}Docker${COLOR_RESET}" + else + INSTALL_MODE="binary" + printf "${COLOR_GREEN}Binary${COLOR_RESET}" + fi + break + ;; + esac + done + fi # Directory setup progress 5 "Setting up directories" @@ -465,5 +469,7 @@ main() { } # Execute main installation -main "$@" +if [ "${TEST_MODE:-}" != "true" ]; then + main "$@" +fi