Skip to content

Commit 1f5723e

Browse files
feat: add install script tests and enhance install-cli.sh for existin… (#670)
* 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. * refactor: remove Homebrew installation step from install script tests * fix: improve existing installation check for Kosli CLI and handle Homebrew installations * fix: remove debug print for existing Kosli installation path * fix: format and improve token parsing in test_install_script.sh * feat: add token support to install script tests for improved authentication * fix: streamline version retrieval in test installation script
1 parent f44c2ab commit 1f5723e

4 files changed

Lines changed: 190 additions & 5 deletions

File tree

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

Lines changed: 28 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,30 @@ 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 --token ${{ secrets.GITHUB_TOKEN }}
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+
44+
- name: Run Homebrew install
45+
shell: bash
46+
run: brew install kosli-cli
47+
48+
- name: Verify Homebrew installation
49+
shell: bash
50+
run: command -v kosli
51+
52+
- name: Run install script test
3153
shell: bash
32-
run: kosli version
54+
run: |
55+
chmod +x install-cli.sh
56+
bash bin/test_install_script_over_homebrew.sh --token ${{ secrets.GITHUB_TOKEN }}

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 -s)
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# Note: set -e is intentionally omitted here to allow manual exit code checking.
4+
5+
# --- Configuration ---
6+
TOKEN=""
7+
8+
# Parse arguments for the test script itself, primarily to pass --token
9+
while [[ "$#" -gt 0 ]]; do
10+
case $1 in
11+
--token)
12+
if [[ -n "$2" && "$2" != --* ]]; then
13+
TOKEN="$2"
14+
shift
15+
else
16+
echo "Error: --token requires a value"
17+
exit 1
18+
fi
19+
;;
20+
*) echo "Unknown parameter: $1"; exit 1 ;;
21+
esac
22+
shift
23+
done
24+
25+
# Helper to construct command
26+
run_install() {
27+
local cmd="./install-cli.sh"
28+
if [ -n "$TOKEN" ]; then
29+
cmd="$cmd --token $TOKEN"
30+
fi
31+
# Add other arguments passed to function
32+
cmd="$cmd $@"
33+
echo "Running: $cmd"
34+
$cmd
35+
}
36+
37+
# Run the install script
38+
run_install
39+
40+
# Capture the exit code of the install script
41+
EXIT_CODE=$?
42+
43+
# Check if the exit code is 1 (expected failure due to existing brew installation)
44+
if [ $EXIT_CODE -eq 1 ]; then
45+
echo "Success: install-cli.sh detected existing Homebrew installation and exited with 1."
46+
exit 0
47+
else
48+
echo "Failure: install-cli.sh did not exit with 1. Actual exit code: $EXIT_CODE"
49+
exit 1
50+
fi

install-cli.sh

100644100755
Lines changed: 24 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,22 @@ 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+
if command -v brew >/dev/null 2>&1 && brew list kosli-cli >/dev/null 2>&1; then
53+
echo "Kosli was installed via Homebrew. Please use 'brew upgrade kosli-cli' instead."
54+
exit 1
55+
fi
56+
57+
EXISTING_KOSLI_PATH=$(command -v kosli)
58+
debug_print "Existing Kosli found at: $EXISTING_KOSLI_PATH"
59+
EXISTING_KOSLI_DIR=$(dirname "$EXISTING_KOSLI_PATH")
60+
debug_print "Existing Kosli directory: $EXISTING_KOSLI_DIR"
61+
62+
TARGET_INSTALL_DIR="$EXISTING_KOSLI_DIR"
63+
fi
64+
4865
# --- Version Selection ---
4966
if [ -n "$VERSION" ]; then
5067
echo "Downloading specified version $VERSION of Kosli CLI..."
@@ -177,8 +194,14 @@ echo "Installing Kosli CLI..."
177194
debug_print "Starting installation process"
178195
debug_print "Current PATH: $PATH"
179196

197+
if [ -n "$TARGET_INSTALL_DIR" ]; then
198+
INSTALL_DIRS="$TARGET_INSTALL_DIR"
199+
else
200+
INSTALL_DIRS="/usr/local/bin /usr/bin /opt/bin"
201+
fi
202+
180203
# Check directories one by one instead of using set --
181-
for dir in "/usr/local/bin" "/usr/bin" "/opt/bin"; do
204+
for dir in $INSTALL_DIRS; do
182205
debug_print "Checking directory: $dir"
183206
# Check if destination directory exists and is in the PATH
184207
if [ -d "$dir" ] && echo "$PATH" | grep -q "$dir"; then

0 commit comments

Comments
 (0)