|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Integration tests for multi-install detection and hash verification |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 6 | +PASS=0 |
| 7 | +FAIL=0 |
| 8 | + |
| 9 | +assert_contains() { |
| 10 | + local label="$1" haystack="$2" needle="$3" |
| 11 | + if echo "$haystack" | grep -qF "$needle"; then |
| 12 | + echo " PASS: $label" |
| 13 | + ((PASS++)) || true |
| 14 | + else |
| 15 | + echo " FAIL: $label" |
| 16 | + echo " expected to contain: $needle" |
| 17 | + echo " got: $(echo "$haystack" | head -5)" |
| 18 | + ((FAIL++)) || true |
| 19 | + fi |
| 20 | +} |
| 21 | + |
| 22 | +assert_not_contains() { |
| 23 | + local label="$1" haystack="$2" needle="$3" |
| 24 | + if ! echo "$haystack" | grep -qF "$needle"; then |
| 25 | + echo " PASS: $label" |
| 26 | + ((PASS++)) || true |
| 27 | + else |
| 28 | + echo " FAIL: $label" |
| 29 | + echo " expected NOT to contain: $needle" |
| 30 | + ((FAIL++)) || true |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +echo "=== Test: check_multi_installs helper ===" |
| 35 | + |
| 36 | +# Source capability.sh to get the functions |
| 37 | +. "$DIR/scripts/lib/capability.sh" |
| 38 | + |
| 39 | +# Test: detect_all_installations returns expected format |
| 40 | +echo "--- detect_all_installations format ---" |
| 41 | +output="$(detect_all_installations "bash" "bash" 2>/dev/null || true)" |
| 42 | +if [ -n "$output" ]; then |
| 43 | + # Should have method:path format |
| 44 | + first_line="$(echo "$output" | head -1)" |
| 45 | + if echo "$first_line" | grep -qE '^[a-z_]+(\([^)]*\))?:/.+'; then |
| 46 | + echo " PASS: detect_all_installations returns method:path format" |
| 47 | + ((PASS++)) || true |
| 48 | + else |
| 49 | + echo " FAIL: unexpected format: $first_line" |
| 50 | + ((FAIL++)) || true |
| 51 | + fi |
| 52 | +else |
| 53 | + echo " SKIP: bash not found via type -a (unexpected)" |
| 54 | +fi |
| 55 | + |
| 56 | +echo "=== Test: SHA256 hash comparison in github_release_binary ===" |
| 57 | + |
| 58 | +# Create a mock scenario: two identical files |
| 59 | +tmpdir="$(mktemp -d)" |
| 60 | +echo "identical content" > "$tmpdir/old_binary" |
| 61 | +echo "identical content" > "$tmpdir/new_binary" |
| 62 | + |
| 63 | +OLD_HASH="$(sha256sum "$tmpdir/old_binary" | awk '{print $1}')" |
| 64 | +NEW_HASH="$(sha256sum "$tmpdir/new_binary" | awk '{print $1}')" |
| 65 | + |
| 66 | +if [ "$OLD_HASH" = "$NEW_HASH" ]; then |
| 67 | + echo " PASS: identical files produce matching SHA256" |
| 68 | + ((PASS++)) || true |
| 69 | +else |
| 70 | + echo " FAIL: SHA256 mismatch for identical files" |
| 71 | + ((FAIL++)) || true |
| 72 | +fi |
| 73 | + |
| 74 | +# Different files should NOT match |
| 75 | +echo "different content" > "$tmpdir/new_binary2" |
| 76 | +NEW_HASH2="$(sha256sum "$tmpdir/new_binary2" | awk '{print $1}')" |
| 77 | +if [ "$OLD_HASH" != "$NEW_HASH2" ]; then |
| 78 | + echo " PASS: different files produce different SHA256" |
| 79 | + ((PASS++)) || true |
| 80 | +else |
| 81 | + echo " FAIL: SHA256 matched for different files" |
| 82 | + ((FAIL++)) || true |
| 83 | +fi |
| 84 | + |
| 85 | +rm -rf "$tmpdir" |
| 86 | + |
| 87 | +echo "=== Test: already-current marker file ===" |
| 88 | + |
| 89 | +# Simulate marker creation and cleanup |
| 90 | +mkdir -p /tmp/.cli-audit |
| 91 | +echo "v1.1.0" > /tmp/.cli-audit/test-tool.already-current |
| 92 | + |
| 93 | +if [ -f /tmp/.cli-audit/test-tool.already-current ]; then |
| 94 | + echo " PASS: marker file created" |
| 95 | + ((PASS++)) || true |
| 96 | +else |
| 97 | + echo " FAIL: marker file not created" |
| 98 | + ((FAIL++)) || true |
| 99 | +fi |
| 100 | + |
| 101 | +# Simulate cleanup |
| 102 | +rm -f /tmp/.cli-audit/test-tool.already-current |
| 103 | +if [ ! -f /tmp/.cli-audit/test-tool.already-current ]; then |
| 104 | + echo " PASS: marker file cleaned up" |
| 105 | + ((PASS++)) || true |
| 106 | +else |
| 107 | + echo " FAIL: marker file not cleaned up" |
| 108 | + ((FAIL++)) || true |
| 109 | +fi |
| 110 | + |
| 111 | +rmdir /tmp/.cli-audit 2>/dev/null || true |
| 112 | + |
| 113 | +echo "" |
| 114 | +echo "Results: $PASS passed, $FAIL failed" |
| 115 | +[ "$FAIL" -eq 0 ] && exit 0 || exit 1 |
0 commit comments