-
Notifications
You must be signed in to change notification settings - Fork 17
refactor: phase 1 shell dedup foundation #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
e8d7e9e
4d1afd8
22fbd6e
a1d72f3
66889b5
a4d24ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/bin/bash | ||
| # Shared counter helpers for check/report style shell scripts. | ||
|
|
||
| bb_counter_reset_many() { | ||
| local counter_name | ||
| for counter_name in "$@"; do | ||
| local -n counter_ref="$counter_name" | ||
| counter_ref=0 | ||
| done | ||
| } | ||
|
|
||
| bb_counter_inc() { | ||
| local counter_name="$1" | ||
| local -n counter_ref="$counter_name" | ||
| counter_ref=$((counter_ref + 1)) | ||
| } | ||
|
|
||
| bb_summary_print_header() { | ||
| echo "Summary" | ||
| echo "───────" | ||
| } | ||
|
|
||
| bb_summary_print_item() { | ||
| local icon="$1" | ||
| local label="$2" | ||
| local value="$3" | ||
|
|
||
| printf " %s %-9s %s\n" "$icon" "$label:" "$value" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #!/bin/bash | ||
| # Tests for bin/lib/check-report-common.sh | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| # shellcheck source=bin/lib/check-report-common.sh | ||
| source "$SCRIPT_DIR/check-report-common.sh" | ||
|
|
||
| TOTAL=0 | ||
| PASSED=0 | ||
| FAILED=0 | ||
|
|
||
| run_test() { | ||
| local name="$1" | ||
| shift | ||
| local out | ||
|
|
||
| TOTAL=$((TOTAL + 1)) | ||
| printf " %-45s " "$name" | ||
|
|
||
| out="$(mktemp /tmp/baudbot-check-report-common-test-output.XXXXXX)" | ||
| if "$@" >"$out" 2>&1; then | ||
| echo "✓" | ||
| PASSED=$((PASSED + 1)) | ||
| else | ||
| echo "✗ FAILED" | ||
| tail -40 "$out" | sed 's/^/ /' | ||
| FAILED=$((FAILED + 1)) | ||
| fi | ||
| rm -f "$out" | ||
| } | ||
|
|
||
| test_reset_many_sets_zero() { | ||
| ( | ||
| set -euo pipefail | ||
| a=7 | ||
| b=3 | ||
| c=9 | ||
| bb_counter_reset_many a b c | ||
| [ "$a" -eq 0 ] | ||
| [ "$b" -eq 0 ] | ||
| [ "$c" -eq 0 ] | ||
| ) | ||
| } | ||
|
|
||
| test_inc_increments_named_counter() { | ||
| ( | ||
| set -euo pipefail | ||
| value=0 | ||
| bb_counter_inc value | ||
| bb_counter_inc value | ||
| [ "$value" -eq 2 ] | ||
| ) | ||
| } | ||
|
|
||
| test_summary_helpers_render_rows() { | ||
| ( | ||
| set -euo pipefail | ||
| local out | ||
| out="$(mktemp /tmp/check-report-summary.XXXXXX)" | ||
| trap 'rm -f "$out"' EXIT | ||
|
|
||
| { | ||
| bb_summary_print_header | ||
| bb_summary_print_item "✅" "Pass" "3" | ||
| } >"$out" | ||
|
|
||
| grep -q '^Summary$' "$out" | ||
| grep -q '✅ Pass:' "$out" | ||
| ) | ||
| } | ||
|
|
||
| echo "=== check-report-common tests ===" | ||
| echo "" | ||
|
|
||
| run_test "reset many sets counters to zero" test_reset_many_sets_zero | ||
| run_test "increment updates named counter" test_inc_increments_named_counter | ||
| run_test "summary helpers render rows" test_summary_helpers_render_rows | ||
|
|
||
| echo "" | ||
| echo "=== $PASSED/$TOTAL passed, $FAILED failed ===" | ||
|
|
||
| if [ "$FAILED" -gt 0 ]; then | ||
| exit 1 | ||
| fi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #!/bin/bash | ||
| # Shared runtime helpers for release update/rollback scripts. | ||
| # | ||
| # Expects caller to provide: | ||
| # - log() and die() functions | ||
| # - restart_baudbot_service_if_active() | ||
| # - json_get_string_stdin() | ||
| # - BAUDBOT_AGENT_USER and BAUDBOT_AGENT_HOME | ||
|
|
||
| bb_run_release_override_cmd() { | ||
| local description="$1" | ||
| local command="$2" | ||
| local env_array_name="$3" | ||
|
|
||
| [ -n "$command" ] || return 1 | ||
|
|
||
| local -n env_ref="$env_array_name" | ||
| log "running $description override" | ||
| env "${env_ref[@]}" bash -lc "$command" | ||
| } | ||
|
|
||
| bb_run_release_restart_and_health() { | ||
| local restart_cmd="$1" | ||
| local skip_restart="$2" | ||
| local health_cmd="$3" | ||
| local env_array_name="$4" | ||
|
|
||
| if [ -n "$restart_cmd" ]; then | ||
| bb_run_release_override_cmd "restart" "$restart_cmd" "$env_array_name" | ||
| elif [ "$skip_restart" = "1" ]; then | ||
| log "skipping restart" | ||
| else | ||
| restart_baudbot_service_if_active | ||
| fi | ||
|
|
||
| if [ -n "$health_cmd" ]; then | ||
| bb_run_release_override_cmd "health" "$health_cmd" "$env_array_name" | ||
| fi | ||
| } | ||
|
|
||
| bb_verify_deployed_release_sha() { | ||
| local expected_sha="$1" | ||
| local skip_version_check="$2" | ||
| local verified_label="${3:-}" | ||
|
|
||
| if [ "$skip_version_check" = "1" ]; then | ||
| return 0 | ||
| fi | ||
|
|
||
| if [ "$(id -u)" -ne 0 ]; then | ||
| log "non-root run: skipping deployed version verification" | ||
| return 0 | ||
| fi | ||
|
|
||
| if ! id "$BAUDBOT_AGENT_USER" >/dev/null 2>&1; then | ||
| log "agent user '$BAUDBOT_AGENT_USER' missing; skipping deployed version verification" | ||
| return 0 | ||
| fi | ||
|
|
||
| local version_file="$BAUDBOT_AGENT_HOME/.pi/agent/baudbot-version.json" | ||
| local deployed_sha | ||
|
|
||
| deployed_sha="$(sudo -u "$BAUDBOT_AGENT_USER" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "sha" 2>/dev/null || true)" | ||
|
|
||
| if [ -z "$deployed_sha" ]; then | ||
| die "deployed version file missing or unreadable: $version_file" | ||
| fi | ||
|
|
||
| if [ "$deployed_sha" != "$expected_sha" ]; then | ||
| die "deployed sha mismatch (expected $expected_sha, got $deployed_sha)" | ||
| fi | ||
|
|
||
| if [ -n "$verified_label" ]; then | ||
| log "deployed version verified: $verified_label" | ||
| fi | ||
| } | ||
|
Comment on lines
+1
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding unit tests for Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! Prompt To Fix With AIThis is a comment left during a code review.
Path: bin/lib/release-runtime-common.sh
Line: 1-74
Comment:
Consider adding unit tests for `release-runtime-common.sh`. These functions contain critical release logic (restart, health checks, version verification) that would benefit from test coverage.
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added coverage in a new bin/lib/release-runtime-common.test.sh and wired it into test/shell-scripts.test.mjs. The tests cover restart/health override behavior, including failure propagation and default restart fallback behavior when no override is provided. Responded by dev-agent using openai/gpt-5. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| #!/bin/bash | ||
| # Tests for bin/lib/release-runtime-common.sh | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| # shellcheck source=bin/lib/release-runtime-common.sh | ||
| source "$SCRIPT_DIR/release-runtime-common.sh" | ||
|
|
||
| TOTAL=0 | ||
| PASSED=0 | ||
| FAILED=0 | ||
|
|
||
| run_test() { | ||
| local name="$1" | ||
| shift | ||
| local out | ||
|
|
||
| TOTAL=$((TOTAL + 1)) | ||
| printf " %-45s " "$name" | ||
|
|
||
| out="$(mktemp /tmp/baudbot-release-runtime-common-test-output.XXXXXX)" | ||
| if "$@" >"$out" 2>&1; then | ||
| echo "✓" | ||
| PASSED=$((PASSED + 1)) | ||
| else | ||
| echo "✗ FAILED" | ||
| tail -40 "$out" | sed 's/^/ /' | ||
| FAILED=$((FAILED + 1)) | ||
| fi | ||
| rm -f "$out" | ||
| } | ||
|
|
||
| TEST_LOGS=() | ||
| RESTART_CALLS=0 | ||
|
|
||
| log() { | ||
| TEST_LOGS+=("$*") | ||
| } | ||
|
|
||
| die() { | ||
| echo "die:$*" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| restart_baudbot_service_if_active() { | ||
| RESTART_CALLS=$((RESTART_CALLS + 1)) | ||
| } | ||
|
|
||
| json_get_string_stdin() { | ||
| local _key="$1" | ||
| cat | ||
| } | ||
|
|
||
| reset_state() { | ||
| TEST_LOGS=() | ||
| RESTART_CALLS=0 | ||
| } | ||
|
|
||
| test_restart_override_failure_does_not_fallback() { | ||
| ( | ||
| set -euo pipefail | ||
| reset_state | ||
| # shellcheck disable=SC2034 # consumed via nameref in bb_run_release_restart_and_health | ||
| local hook_env=("X_TEST=1") | ||
|
|
||
| set +e | ||
| bb_run_release_restart_and_health "false" "0" "" hook_env | ||
| rc=$? | ||
| set -e | ||
|
|
||
| [ "$rc" -ne 0 ] | ||
| [ "$RESTART_CALLS" -eq 0 ] | ||
| ) | ||
| } | ||
|
|
||
| test_no_restart_override_uses_default_restart() { | ||
| ( | ||
| set -euo pipefail | ||
| reset_state | ||
| local hook_env=() | ||
|
|
||
| bb_run_release_restart_and_health "" "0" "" hook_env | ||
| [ "$RESTART_CALLS" -eq 1 ] | ||
| ) | ||
| } | ||
|
|
||
| test_skip_restart_logs_and_does_not_restart() { | ||
| ( | ||
| set -euo pipefail | ||
| reset_state | ||
| local hook_env=() | ||
|
|
||
| bb_run_release_restart_and_health "" "1" "" hook_env | ||
| [ "$RESTART_CALLS" -eq 0 ] | ||
| ) | ||
| } | ||
|
|
||
| test_health_override_failure_propagates() { | ||
| ( | ||
| set -euo pipefail | ||
| reset_state | ||
| # shellcheck disable=SC2034 # consumed via nameref in bb_run_release_restart_and_health | ||
| local hook_env=("X_TEST=1") | ||
|
|
||
| set +e | ||
| bb_run_release_restart_and_health "" "1" "false" hook_env | ||
| rc=$? | ||
| set -e | ||
|
|
||
| [ "$rc" -ne 0 ] | ||
| ) | ||
| } | ||
|
|
||
| echo "=== release-runtime-common tests ===" | ||
| echo "" | ||
|
|
||
| run_test "restart override failure does not fallback" test_restart_override_failure_does_not_fallback | ||
| run_test "default restart runs without override" test_no_restart_override_uses_default_restart | ||
| run_test "skip restart avoids default restart" test_skip_restart_logs_and_does_not_restart | ||
| run_test "health override failure propagates" test_health_override_failure_propagates | ||
|
|
||
| echo "" | ||
| echo "=== $PASSED/$TOTAL passed, $FAILED failed ===" | ||
|
|
||
| if [ "$FAILED" -gt 0 ]; then | ||
| exit 1 | ||
| fi |
Uh oh!
There was an error while loading. Please reload this page.