Skip to content
Merged
32 changes: 28 additions & 4 deletions .github/workflows/install-script-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ on:
paths:
- 'install-cli.sh'
- '.github/workflows/install-script-tests.yml'
- 'bin/test_install_script.sh'
- 'bin/test_install_script_over_homebrew.sh'
pull_request:
paths:
- 'install-cli.sh'
- '.github/workflows/install-script-tests.yml'
- 'bin/test_install_script.sh'
- 'bin/test_install_script_over_homebrew.sh'
workflow_dispatch:

jobs:
Expand All @@ -23,10 +27,30 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v5

- name: Run install script
- name: Run install script test
shell: bash
run: bash install-cli.sh --debug --token ${{ secrets.GITHUB_TOKEN }}
run: |
chmod +x install-cli.sh
bash bin/test_install_script.sh --token ${{ secrets.GITHUB_TOKEN }}

- name: Verify installation
mac-homebrew:
name: Test Homebrew Installation on macOS
runs-on: macos-latest

steps:
- name: Checkout repository
uses: actions/checkout@v5

- name: Run Homebrew install
shell: bash
run: brew install kosli-cli

- name: Verify Homebrew installation
shell: bash
run: command -v kosli

- name: Run install script test
shell: bash
run: kosli version
run: |
chmod +x install-cli.sh
bash bin/test_install_script_over_homebrew.sh --token ${{ secrets.GITHUB_TOKEN }}
88 changes: 88 additions & 0 deletions bin/test_install_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash
set -e

# --- Configuration ---
TOKEN=""

# Parse arguments for the test script itself, primarily to pass --token
while [[ "$#" -gt 0 ]]; do
case $1 in
--token)
if [[ -n "$2" && "$2" != --* ]]; then
TOKEN="$2"
shift
else
echo "Error: --token requires a value"
exit 1
fi
;;
*) echo "Unknown parameter: $1"; exit 1 ;;
esac
shift
done

# Helper to construct command
run_install() {
local cmd="./install-cli.sh"
if [ -n "$TOKEN" ]; then
cmd="$cmd --token $TOKEN"
fi
# Add other arguments passed to function
cmd="$cmd $@"
echo "Running: $cmd"
$cmd
}

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

log_info() {
echo -e "${GREEN}[INFO] $1${NC}"
}

log_error() {
echo -e "${RED}[ERROR] $1${NC}"
}


# Test 1: Install specific version
SPECIFIC_VERSION="v2.11.40"
log_info "Test 1: Installing specific version ${SPECIFIC_VERSION}..."

# We pass --version as requested by the user
run_install --version "${SPECIFIC_VERSION}" --debug

if ! command -v kosli &> /dev/null; then
log_error "Kosli CLI not found after installation"
exit 1
fi

INSTALLED_VERSION=$(kosli version -s)
log_info "Installed version: ${INSTALLED_VERSION}"

if [[ "${INSTALLED_VERSION}" == "${SPECIFIC_VERSION}" ]]; then
log_info "✅ Specific version installed successfully"
else
log_info "Expected ${SPECIFIC_VERSION}, got ${INSTALLED_VERSION}"
log_error "❌ Version mismatch"
exit 1
fi

# Test 2: Upgrade to latest version
log_info "Test 2: Upgrading to latest version..."
run_install --debug

LATEST_INSTALLED_VERSION=$(kosli version | grep -o "v[0-9]\+\.[0-9]\+\.[0-9]\+")
log_info "Installed version after update: ${LATEST_INSTALLED_VERSION}"

# Simple check to ensure version changed (assuming latest > specific)
if [[ "${LATEST_INSTALLED_VERSION}" != "${SPECIFIC_VERSION}" ]]; then
log_info "✅ Version updated successfully (from ${SPECIFIC_VERSION} to ${LATEST_INSTALLED_VERSION})"
else
log_error "❌ Version did not update"
exit 1
fi

log_info "🎉 All installation tests passed!"
50 changes: 50 additions & 0 deletions bin/test_install_script_over_homebrew.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# Note: set -e is intentionally omitted here to allow manual exit code checking.

# --- Configuration ---
TOKEN=""

# Parse arguments for the test script itself, primarily to pass --token
while [[ "$#" -gt 0 ]]; do
case $1 in
--token)
if [[ -n "$2" && "$2" != --* ]]; then
TOKEN="$2"
shift
else
echo "Error: --token requires a value"
exit 1
fi
;;
*) echo "Unknown parameter: $1"; exit 1 ;;
esac
shift
done

# Helper to construct command
run_install() {
local cmd="./install-cli.sh"
if [ -n "$TOKEN" ]; then
cmd="$cmd --token $TOKEN"
fi
# Add other arguments passed to function
cmd="$cmd $@"
echo "Running: $cmd"
$cmd
}

# Run the install script
run_install

# Capture the exit code of the install script
EXIT_CODE=$?

# Check if the exit code is 1 (expected failure due to existing brew installation)
if [ $EXIT_CODE -eq 1 ]; then
echo "Success: install-cli.sh detected existing Homebrew installation and exited with 1."
exit 0
else
echo "Failure: install-cli.sh did not exit with 1. Actual exit code: $EXIT_CODE"
exit 1
fi
25 changes: 24 additions & 1 deletion install-cli.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ VERSION=""
FILE_NAME="kosli"
DEBUG=false
GITHUB_TOKEN=""
TARGET_INSTALL_DIR=""

# --- Debug function ---
debug_print() {
Expand Down Expand Up @@ -45,6 +46,22 @@ while [ $# -gt 0 ]; do
esac
done

# --- Check existing installation ---
debug_print "Checking for existing Kosli installation"
if command -v kosli >/dev/null 2>&1; then
if command -v brew >/dev/null 2>&1 && brew list kosli-cli >/dev/null 2>&1; then
echo "Kosli was installed via Homebrew. Please use 'brew upgrade kosli-cli' instead."
exit 1
fi

EXISTING_KOSLI_PATH=$(command -v kosli)
debug_print "Existing Kosli found at: $EXISTING_KOSLI_PATH"
EXISTING_KOSLI_DIR=$(dirname "$EXISTING_KOSLI_PATH")
debug_print "Existing Kosli directory: $EXISTING_KOSLI_DIR"

TARGET_INSTALL_DIR="$EXISTING_KOSLI_DIR"
fi

# --- Version Selection ---
if [ -n "$VERSION" ]; then
echo "Downloading specified version $VERSION of Kosli CLI..."
Expand Down Expand Up @@ -177,8 +194,14 @@ echo "Installing Kosli CLI..."
debug_print "Starting installation process"
debug_print "Current PATH: $PATH"

if [ -n "$TARGET_INSTALL_DIR" ]; then
INSTALL_DIRS="$TARGET_INSTALL_DIR"
else
INSTALL_DIRS="/usr/local/bin /usr/bin /opt/bin"
fi

# Check directories one by one instead of using set --
for dir in "/usr/local/bin" "/usr/bin" "/opt/bin"; do
for dir in $INSTALL_DIRS; do
debug_print "Checking directory: $dir"
# Check if destination directory exists and is in the PATH
if [ -d "$dir" ] && echo "$PATH" | grep -q "$dir"; then
Expand Down
Loading