diff --git a/cli/src/test/all-tests-functions.sh b/cli/src/test/all-tests-functions.sh index fff32b027..ff296172e 100644 --- a/cli/src/test/all-tests-functions.sh +++ b/cli/src/test/all-tests-functions.sh @@ -43,22 +43,40 @@ function doDownloadSnapshot () { local url # Change OS type based on github workflow matrix.os name local osType - if [ "${MATRIX_OS}" == "windows-latest" ]; then - osType="windows-x64" - elif [ "${MATRIX_OS}" == "ubuntu-latest" ]; then - osType="linux-x64" - elif [ "${MATRIX_OS}" == "macos-latest" ]; then - osType="mac-arm64" - elif [ "${MATRIX_OS}" == "macos-13" ]; then - osType="mac-x64" - fi - url=$(grep "href=\"https://.*${osType}.tar.gz" "$pageHtmlLocal" | grep -o "https://.*${osType}.tar.gz" | cut -f1 -d"\"") + osType=$(doGetOsType) + url=$(grep "href=\"https://.*${osType}.tar.gz" "$pageHtmlLocal" | grep -o "https://[^\"]*${osType}.tar.gz" | head -1) echo "Trying to download IDEasy for OS: ${osType} from: ${url} to: ${IDEASY_COMPRESSED_FILE:?} ..." curl -o "${IDEASY_COMPRESSED_FILE:?}" "$url" rm "${pageHtmlLocal:?}" fi } +function doGetOsType() { + local osType + case "$OSTYPE" in + msys*|cygwin*|win32*) + osType="windows-x64" + ;; + linux*|gnu*) + osType="linux-x64" + ;; + darwin*|macos*) + # Try to distinguish between Apple Silicon and Intel Macs + if sysctl -n machdep.cpu.brand_string 2>/dev/null | grep -qi "Apple"; then + osType="mac-arm64" + else + osType="mac-x64" + fi + ;; + *) + echo "Unknown OSTYPE: $OSTYPE. Falling back to windows (most common developer machine)." >&2 + osType="windows-x64" + ;; + esac + echo "Detected OS type: ${osType}" >&2 + echo "$osType" +} + function doExtract() { echo "Extracting IDEasy archive: ${IDEASY_COMPRESSED_FILE} to: ${IDEASY_DIR}" @@ -86,7 +104,8 @@ function doError() { } function doIsMacOs() { - if [ "${OSTYPE:0:6}" = "darwin" ] + local osType=$(doGetOsType) + if [ "$osType" = "mac-arm64" ] || [ "$osType" = "mac-x64" ] then return fi @@ -94,7 +113,8 @@ function doIsMacOs() { } function doIsWindows() { - if [ "${OSTYPE}" = "cygwin" ] || [ "${OSTYPE}" = "msys" ] + local osType=$(doGetOsType) + if [ "$osType" = "windows-x64" ] then return fi diff --git a/cli/src/test/all-tests.sh b/cli/src/test/all-tests.sh index d3ebb8f31..0b9be1e27 100755 --- a/cli/src/test/all-tests.sh +++ b/cli/src/test/all-tests.sh @@ -8,23 +8,55 @@ touch "${HOME}"/.ide/.license.agreement source "$(dirname "${0}")"/all-tests-functions.sh -MATRIX_OS="$1" +# Remove side-effects +BAK_IDE_ROOT="${IDE_ROOT}" +BAK_PATH="${PATH}" +DEBUG_INTEGRATION_TEST_PREFIX="${HOME}/tmp/ideasy-integration-test-debug" + +# Create backups of shell RC files to prevent destroying user's existing configuration +BAK_BASHRC="" +BAK_ZSHRC="" +if [ -f "$HOME/.bashrc" ]; then + BAK_BASHRC="$HOME/.bashrc.ideasy-test-backup" + cp "$HOME/.bashrc" "$BAK_BASHRC" +fi +if [ -f "$HOME/.zshrc" ]; then + BAK_ZSHRC="$HOME/.zshrc.ideasy-test-backup" + cp "$HOME/.zshrc" "$BAK_ZSHRC" +fi + +trap "export PATH=\"${BAK_PATH}\" && export IDE_ROOT=\"${BAK_IDE_ROOT}\" && rm -rf \"${DEBUG_INTEGRATION_TEST_PREFIX}\" && doRestoreRcFiles && echo \"PATH, IDE_ROOT, and shell RC files restored\"" EXIT + +function doRestoreRcFiles() { + # Restore shell RC files from backups to preserve user's existing configuration + if [ -n "$BAK_BASHRC" ] && [ -f "$BAK_BASHRC" ]; then + mv "$BAK_BASHRC" "$HOME/.bashrc" + echo "Restored ~/.bashrc from backup" + fi + if [ -n "$BAK_ZSHRC" ] && [ -f "$BAK_ZSHRC" ]; then + mv "$BAK_ZSHRC" "$HOME/.zshrc" + echo "Restored ~/.zshrc from backup" + fi +} + +function doResetVariables() { + IDE_HOME="${DEBUG_INTEGRATION_TEST}/home-dir" + export IDE_ROOT="${IDE_HOME}/projects" + IDEASY_DIR="${IDE_ROOT}/_ide" + FUNCTIONS="${IDEASY_DIR}/installation/functions" + IDE="${DEBUG_INTEGRATION_TEST}/home-dir/projects/_ide/bin/${BINARY_FILE_NAME}" + TEST_RESULTS_FILE="${IDE_ROOT}/testResults" +} + # Switch IDEasy binary file name based on github workflow matrix.os name (first argument of all-tests.sh) BINARY_FILE_NAME="ideasy" -if [ "${MATRIX_OS}" == "windows-latest" ]; then +if doIsWindows; then BINARY_FILE_NAME="ideasy.exe" fi START_TIME=$(date '+%Y-%m-%d_%H-%M-%S') - -DEBUG_INTEGRATION_TEST_PREFIX="${HOME}/tmp/ideasy-integration-test-debug" DEBUG_INTEGRATION_TEST="${DEBUG_INTEGRATION_TEST_PREFIX}-${START_TIME}" -IDE_HOME="${DEBUG_INTEGRATION_TEST}/home-dir" -export IDE_ROOT="${IDE_HOME}/projects" -IDEASY_DIR="${IDE_ROOT}/_ide" -FUNCTIONS="${IDEASY_DIR}/installation/functions" -IDE="${DEBUG_INTEGRATION_TEST}/home-dir/projects/_ide/bin/${BINARY_FILE_NAME}" -TEST_RESULTS_FILE="${IDE_ROOT}/testResults" +doResetVariables test_files_directory=$(realpath "$0" | xargs dirname) @@ -35,6 +67,7 @@ total=0 function doTestsInner() { # Note: requires var test_files_directory to be set. for testpath in "${test_files_directory:?}/integration-tests"/*; do + doResetVariables testcase="${testpath/*\//}" echo "Running test #${total}: ${testcase} (${testpath})" @@ -69,8 +102,6 @@ function doDisplayResults() { while read -r line; do echo -e "${line}"; done < "${TEST_RESULTS_FILE}" } - - function doTests () { doTestsInner echo -e "\n*****************************************************" @@ -114,11 +145,22 @@ function main () { # upgrade to latest snapshot echo "Upgrading IDEasy to latest SNAPSHOT" - $IDE -d --batch upgrade --mode=snapshot + $IDE -d --batch upgrade --mode=snapshot || echo "Upgrade failed, continuing with downloaded version" # source functions (resets IDEasy) echo "Sourcing functions to: ${FUNCTIONS}" - source "${FUNCTIONS:?}" + # Add IDE bin to PATH so ideasy command can be found + export PATH="${IDEASY_DIR}/bin:$PATH" + # Try installation path first, then fall back to root + if [ -f "${FUNCTIONS:?}" ]; then + source "${FUNCTIONS:?}" + elif [ -f "${IDEASY_DIR}/functions" ]; then + echo "Using functions from root: ${IDEASY_DIR}/functions" + source "${IDEASY_DIR}/functions" + else + echo "ERROR: Could not find functions file" + exit 1 + fi echo "Checking version after upgrade" ide -v diff --git a/cli/src/test/integration-tests/force-install.sh b/cli/src/test/integration-tests/force-install.sh new file mode 100644 index 000000000..cc0d4d0b3 --- /dev/null +++ b/cli/src/test/integration-tests/force-install.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# Integration test to reproduce the issue where force install (-f) breaks environment +# for subsequent tests when executed in alphabetical order + +echo "Running force install integration test to reproduce environment interference issue" + +# Capture initial environment state +echo "=== Initial Environment State ===" +env | grep -E "IDE|PATH" | sort > /tmp/env_initial.txt +echo "IDE_ROOT: '$IDE_ROOT'" +echo "PATH: '$PATH'" + +# Create a dummy .gitconfig file for testing +touch "$HOME"/.gitconfig + +# Capture environment variables before force install +echo "=== Environment before force install ===" +env | sort > /tmp/env_before_force.txt +echo "Shell RC files before:" +echo "~/.bashrc exists: $(test -f ~/.bashrc && echo "yes" || echo "no")" +if [ -f ~/.bashrc ]; then + echo "Last 20 lines of ~/.bashrc:" + tail -20 ~/.bashrc +fi + +# Run force install to reproduce the issue +echo "=== Running force install ===" +$IDE -f install + +# Capture environment variables after force install +echo "=== Environment after force install ===" +env | sort > /tmp/env_after_force.txt +echo "Shell RC files after:" +echo "~/.bashrc exists: $(test -f ~/.bashrc && echo "yes" || echo "no")" +if [ -f ~/.bashrc ]; then + echo "Last 20 lines of ~/.bashrc:" + tail -20 ~/.bashrc +fi + +# Show the differences +echo "=== Environment variable differences ===" +diff /tmp/env_before_force.txt /tmp/env_after_force.txt || true + +# Test that the git longpaths configuration gets set (original test purpose) +echo "=== Testing git longpaths configuration ===" +if doIsWindows; then + gitconfig_path="$HOME"/.gitconfig + fileContent=$(cat "$gitconfig_path") + assertThat "$fileContent" contains "longpaths" +else + echo "Skipping git longpaths test - only applicable on Windows" +fi + +# Test that subsequent IDE commands still work +echo "=== Testing subsequent IDE commands ===" +ide_version_output=$($IDE -v 2>&1) +if [[ $? -eq 0 ]]; then + echo "SUCCESS: IDE version command works after force install" + echo "Version output: $ide_version_output" +else + echo "FAILURE: IDE version command failed after force install" + echo "Error output: $ide_version_output" +fi + +# Test ide env command +echo "=== Testing ide env command ===" +ide_env_output=$($IDE env --bash 2>&1) +if [[ $? -eq 0 ]]; then + echo "SUCCESS: IDE env command works after force install" + echo "Environment variables count: $(echo "$ide_env_output" | wc -l)" +else + echo "FAILURE: IDE env command failed after force install" + echo "Error output: $ide_env_output" +fi + +# Clean up temporary files +rm -f /tmp/env_initial.txt /tmp/env_before_force.txt /tmp/env_after_force.txt + +echo "Force install test completed"