Skip to content

Commit 41c7046

Browse files
feat: add install script tests and enhance install-cli.sh for existing installations
Add a scenario for breaking if kosli is installed with eg. homebrew in a different folder.
1 parent 8527a93 commit 41c7046

4 files changed

Lines changed: 164 additions & 5 deletions

File tree

.github/workflows/install-script-tests.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ on:
55
paths:
66
- 'install-cli.sh'
77
- '.github/workflows/install-script-tests.yml'
8+
- 'bin/test_install_script.sh'
9+
- 'bin/test_install_script_over_homebrew.sh'
810
pull_request:
911
paths:
1012
- 'install-cli.sh'
1113
- '.github/workflows/install-script-tests.yml'
14+
- 'bin/test_install_script.sh'
15+
- 'bin/test_install_script_over_homebrew.sh'
1216
workflow_dispatch:
1317

1418
jobs:
@@ -23,10 +27,34 @@ jobs:
2327
- name: Checkout repository
2428
uses: actions/checkout@v5
2529

26-
- name: Run install script
30+
- name: Run install script test
2731
shell: bash
28-
run: bash install-cli.sh --debug --token ${{ secrets.GITHUB_TOKEN }}
32+
run: |
33+
chmod +x install-cli.sh
34+
bash bin/test_install_script.sh
2935
30-
- name: Verify installation
36+
mac-homebrew:
37+
name: Test Homebrew Installation on macOS
38+
runs-on: macos-latest
39+
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v5
43+
- name: install homebrew
44+
shell: bash
45+
run: |
46+
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
47+
48+
- name: Run Homebrew install
49+
shell: bash
50+
run: brew install kosli-cli
51+
52+
- name: Verify Homebrew installation
53+
shell: bash
54+
run: command -v kosli
55+
56+
- name: Run install script test
3157
shell: bash
32-
run: kosli version
58+
run: |
59+
chmod +x install-cli.sh
60+
bash bin/test_install_script_over_homebrew.sh

bin/test_install_script.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# --- Configuration ---
5+
TOKEN=""
6+
7+
# Parse arguments for the test script itself, primarily to pass --token
8+
while [[ "$#" -gt 0 ]]; do
9+
case $1 in
10+
--token)
11+
if [[ -n "$2" && "$2" != --* ]]; then
12+
TOKEN="$2"
13+
shift
14+
else
15+
echo "Error: --token requires a value"
16+
exit 1
17+
fi
18+
;;
19+
*) echo "Unknown parameter: $1"; exit 1 ;;
20+
esac
21+
shift
22+
done
23+
24+
# Helper to construct command
25+
run_install() {
26+
local cmd="./install-cli.sh"
27+
if [ -n "$TOKEN" ]; then
28+
cmd="$cmd --token $TOKEN"
29+
fi
30+
# Add other arguments passed to function
31+
cmd="$cmd $@"
32+
echo "Running: $cmd"
33+
$cmd
34+
}
35+
36+
# Colors for output
37+
GREEN='\033[0;32m'
38+
RED='\033[0;31m'
39+
NC='\033[0m' # No Color
40+
41+
log_info() {
42+
echo -e "${GREEN}[INFO] $1${NC}"
43+
}
44+
45+
log_error() {
46+
echo -e "${RED}[ERROR] $1${NC}"
47+
}
48+
49+
50+
# Test 1: Install specific version
51+
SPECIFIC_VERSION="v2.11.40"
52+
log_info "Test 1: Installing specific version ${SPECIFIC_VERSION}..."
53+
54+
# We pass --version as requested by the user
55+
run_install --version "${SPECIFIC_VERSION}" --debug
56+
57+
if ! command -v kosli &> /dev/null; then
58+
log_error "Kosli CLI not found after installation"
59+
exit 1
60+
fi
61+
62+
INSTALLED_VERSION=$(kosli version | grep -o "v[0-9]\+\.[0-9]\+\.[0-9]\+")
63+
log_info "Installed version: ${INSTALLED_VERSION}"
64+
65+
if [[ "${INSTALLED_VERSION}" == "${SPECIFIC_VERSION}" ]]; then
66+
log_info "✅ Specific version installed successfully"
67+
else
68+
log_info "Expected ${SPECIFIC_VERSION}, got ${INSTALLED_VERSION}"
69+
log_error "❌ Version mismatch"
70+
exit 1
71+
fi
72+
73+
# Test 2: Upgrade to latest version
74+
log_info "Test 2: Upgrading to latest version..."
75+
run_install --debug
76+
77+
LATEST_INSTALLED_VERSION=$(kosli version | grep -o "v[0-9]\+\.[0-9]\+\.[0-9]\+")
78+
log_info "Installed version after update: ${LATEST_INSTALLED_VERSION}"
79+
80+
# Simple check to ensure version changed (assuming latest > specific)
81+
if [[ "${LATEST_INSTALLED_VERSION}" != "${SPECIFIC_VERSION}" ]]; then
82+
log_info "✅ Version updated successfully (from ${SPECIFIC_VERSION} to ${LATEST_INSTALLED_VERSION})"
83+
else
84+
log_error "❌ Version did not update"
85+
exit 1
86+
fi
87+
88+
log_info "🎉 All installation tests passed!"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Attempt to run the install script
4+
./install-cli.sh
5+
6+
# Capture the exit code of the install script
7+
EXIT_CODE=$?
8+
9+
# Check if the exit code is 1 (expected failure due to existing brew installation)
10+
if [ $EXIT_CODE -eq 1 ]; then
11+
echo "Success: install-cli.sh detected existing Homebrew installation and exited with 1."
12+
exit 0
13+
else
14+
echo "Failure: install-cli.sh did not exit with 1. Actual exit code: $EXIT_CODE"
15+
exit 1
16+
fi

install-cli.sh

100644100755
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ VERSION=""
1111
FILE_NAME="kosli"
1212
DEBUG=false
1313
GITHUB_TOKEN=""
14+
TARGET_INSTALL_DIR=""
1415

1516
# --- Debug function ---
1617
debug_print() {
@@ -45,6 +46,26 @@ while [ $# -gt 0 ]; do
4546
esac
4647
done
4748

49+
# --- Check existing installation ---
50+
debug_print "Checking for existing Kosli installation"
51+
if command -v kosli >/dev/null 2>&1; then
52+
EXISTING_KOSLI_PATH=$(command -v kosli)
53+
debug_print "Existing Kosli found at: $EXISTING_KOSLI_PATH"
54+
EXISTING_KOSLI_DIR=$(dirname "$EXISTING_KOSLI_PATH")
55+
debug_print "Existing Kosli directory: $EXISTING_KOSLI_DIR"
56+
57+
case "$EXISTING_KOSLI_DIR" in
58+
"/usr/local/bin" | "/usr/bin" | "/opt/bin")
59+
TARGET_INSTALL_DIR="$EXISTING_KOSLI_DIR"
60+
debug_print "Found existing Kosli installation in standard location: $TARGET_INSTALL_DIR"
61+
;;
62+
*)
63+
echo "Kosli found but was installed in another way in $EXISTING_KOSLI_PATH. Please uninstall before running this script to avoid multiple versions present"
64+
exit 1
65+
;;
66+
esac
67+
fi
68+
4869
# --- Version Selection ---
4970
if [ -n "$VERSION" ]; then
5071
echo "Downloading specified version $VERSION of Kosli CLI..."
@@ -177,8 +198,14 @@ echo "Installing Kosli CLI..."
177198
debug_print "Starting installation process"
178199
debug_print "Current PATH: $PATH"
179200

201+
if [ -n "$TARGET_INSTALL_DIR" ]; then
202+
INSTALL_DIRS="$TARGET_INSTALL_DIR"
203+
else
204+
INSTALL_DIRS="/usr/local/bin /usr/bin /opt/bin"
205+
fi
206+
180207
# Check directories one by one instead of using set --
181-
for dir in "/usr/local/bin" "/usr/bin" "/opt/bin"; do
208+
for dir in $INSTALL_DIRS; do
182209
debug_print "Checking directory: $dir"
183210
# Check if destination directory exists and is in the PATH
184211
if [ -d "$dir" ] && echo "$PATH" | grep -q "$dir"; then

0 commit comments

Comments
 (0)