Skip to content

Commit

Permalink
test: Add shell test workflow, bats tests, and test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 18, 2024
1 parent eca1fed commit 0922f09
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 38 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/shell-test.yml
Original file line number Diff line number Diff line change
@@ -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
113 changes: 113 additions & 0 deletions install.bats
Original file line number Diff line number Diff line change
@@ -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"
}
}
82 changes: 44 additions & 38 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -465,5 +469,7 @@ main() {
}

# Execute main installation
main "$@"
if [ "${TEST_MODE:-}" != "true" ]; then
main "$@"
fi

0 comments on commit 0922f09

Please sign in to comment.