Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#583: First prototype of integration tests in bash. #752

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions cli/src/test/integration-tests-bash/all-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash
#set -e
#set -xe

source "$(dirname "${0}")"/functions-test.sh

START_TIME=$(date '+%Y-%m-%d_%H-%M-%S')

DEBUG_INTEGRATION_TEST_PREFIX="/c/tmp/ideasy-integration-test-debug"
DEBUG_INTEGRATION_TEST="${DEBUG_INTEGRATION_TEST_PREFIX}-${START_TIME}"
HOME="${DEBUG_INTEGRATION_TEST}/home-dir"
IDE_ROOT="${HOME}/projects"
IDEASY_DIR="${IDE_ROOT}/_ide"
IDE="${DEBUG_INTEGRATION_TEST}/home-dir/projects/_ide/bin/ide"
TEST_RESULTS_FILE="${IDE_ROOT}/testResults"

test_files_directory=$(realpath $0 | xargs dirname)

success=0
failure=0
total=0

function doTestsInner() {
# Note: requires var test_files_directory to be set.
echo "Enter doTestsInner..."
echo $PWD
local test_files_prefix="integration-test"
for testpath in "${test_files_directory}"/"${test_files_prefix}"-*
do
testcase="${testpath/*\//}"
echo "Running test #${total}: ${testcase} (${testpath})"
# Every test runs in its own environment.
# Need to return back from created environment (integration test might have changed pwd).
cd "${IDE_ROOT}"

integration_test_result=0

# Following line adds a trap to ERR signal: Whenever error (some
# conditions apply; see documentation) set integration_test_result=1.
trap 'doWarning "A non handled error in integration test ocurred.";integration_test_result=1' ERR

# "${testpath}" $IDE || integration_test_result=1
#source "${testpath}" || integration_test_result=1
#source "${testpath}" || echo "WHY IS IT RETURNING 0 ALWAYS?"
#integration_test_result=$(source "${testpath}")
source "${testpath}"

# Remove trap
# Disable errexit in case it was turned on in a test script
trap - ERR
set +e

echo "RESULTS:"
echo $integration_test_result
if [ "${integration_test_result}" == 0 ]
then
doSuccess "[SUCCESS] Succeeded running test #${total}: ${testcase}"
let "success++"
echo "\033[92m[SUCCESS] Succeeded running test #${total}: ${testcase}\033[39m" >> ${TEST_RESULTS_FILE}
else
doError "[ERROR] Failed running test #${total}: ${testcase} - exit code ${integration_test_result}"
let "failure++"
echo "\033[91m[ERROR] Failed running test #${total}: ${testcase} - exit code ${integration_test_result}\033[39m" >> ${TEST_RESULTS_FILE}
fi
let "total++"
done
}

function doDisplayResults() {
cat ${TEST_RESULTS_FILE} | while read -r line; do echo -e "${line}"; done
}



function doTests () {
doTestsInner
echo -e "\n*****************************************************"
echo "Executed #${total} test(s), #${success} succeeded and #${failure} failed"
echo -e "*****************************************************\n"
if [ "${failure}" == 0 ]
then
doSuccess "All test succeeded. Fine!"
doDisplayResults
else
doWarning "There are test failures! Please check the logs and fix errors.\n"
doDisplayResults
exit 1
fi
exit 0
}

function main () {

echo "Will run integration tests from dir: ${test_files_directory}"

# rm -rf "${DEBUG_INTEGRATION_TEST_PREFIX}"
# Only need to mkdir once:
mkdir -p "${IDEASY_DIR}"


# TODO remove logs
echo "IDE_ROOT is: $IDE_ROOT"
echo "My PWD is: $PWD"
echo "My ~/ is:" ~/

cd "${IDE_ROOT}/_ide"


# Will call IDEasy with variable
#ide="${IDE_ROOT}/_ide/bin/ide"
#alias ide="source ${IDE_ROOT}/_ide/bin/ide"
# TODO REMOVE? We will simulate having a virtual home.
#no need to: cd "${HOME}"
#echo -e "echo \"Tmp home dir's bashrc loading...\"" >> ~/.bashrc
#echo -e "alias ide=\"source ${PWD}/bin/ide\"" >> ~/.bashrc
# echo -e 'export IDE_ROOT="$(pwd)"' >> ~/.bashrc
#source ~/.bashrc


# Determine IDEasy release to use for testing (default: downloads latest release)
# NOTE: For debugging purposes, if you want to avoid download time, you can
# uncomment var SNAPSHOT, set it to a local compressed IDEasy release and
# give it to 'doDownloadSnapshot' as first argument..
local SNAPSHOT="/c/Users/nmollers/Downloads/ide-cli-2024.10.001-beta-20241029.023922-8-windows-x64.tar.gz"
if [ $SNAPSHOT != "" ]
then
doDownloadSnapshot $SNAPSHOT
else
doDownloadSnapshot
fi
# Extract IDEasy and setup
doExtract

# source ./bin/ide
cd "${IDE_ROOT}"
doTests

echo "DONE"
exit 0
}

main

#doDownloadSnapshot "/c/Users/nmollers/Downloads/ide-cli-2024.10.001-beta-20241029.023922-8-windows-x64.tar.gz"
#doDownloadSnapshot

6 changes: 6 additions & 0 deletions cli/src/test/integration-tests-bash/debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

## source "$(dirname "${0}")"/functions-test

echo my PWD is: $PWD
echo "My ~ (home dir) is " ~/
102 changes: 102 additions & 0 deletions cli/src/test/integration-tests-bash/functions-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash
#set -x
#set -e errexit

WORK_DIR_INTEG_TEST="/c/tmp/ideasy-integration-test-debug/IDEasy_snapshot"
IDEASY_COMPRESSED_NAME="ideasy_latest.tar.gz"
IDEASY_COMPRESSED_FILE="${WORK_DIR_INTEG_TEST}/${IDEASY_COMPRESSED_NAME}"
test_project_name="tmp-integ-test"

function doIdeCreate () {
#TODO: determine the name of the currently executed script
# local project-name="$(dirname "${BASH_SOURCE:-$0}")"
# local project_name="$(basename "${BASH_SOURCE:-$0}")"
# local test_project_name="tmp-integ-test"
# If first argument is given, then it is the url for the ide create command (default is '-').
local settings_url=${1:--}
$IDE create ${test_project_name} ${settings_url}
echo ide create ${test_project_name} ${settings_url}
#TODO: IDE_ROOT ?
# mkdir ${IDE_ROOT}/${test_project_name}
cd "${IDE_ROOT}/${test_project_name}"

echo "${IDE_ROOT}/${test_project_name}"
echo "${test_project_name}"
# TODO: Remove logs
echo "My IDE_ROOT is: ${IDE_ROOT}"
echo "My PWD is: $PWD"
echo "settings-url : ${settings_url}"
}

function doIdeCreateCleanup () {
rm -rf ${IDE_ROOT}/${test_project_name}
}

function doDownloadSnapshot () {
mkdir -p $WORK_DIR_INTEG_TEST
if [ $1 ]
then
if [ -f "${1}" ] && [[ $1 == *.tar.gz ]];
then
echo "Local snapshot given. Copy to directory ${WORK_DIR_INTEG_TEST}"
cp "$1" "${IDEASY_COMPRESSED_FILE}"
else
echo "Expected a file ending with tar.gz - Given: ${1}"
exit 1
fi
else
echo "Will try to download latest IDEasy release..."
local URL_IDEASY_LATEST="https://github.com/devonfw/IDEasy/releases/latest"
local PAGE_HTML_LOCAL="${WORK_DIR_INTEG_TEST}/integ_test_gh_latest.html"

curl -L $URL_IDEASY_LATEST > $PAGE_HTML_LOCAL
# TODO: A bit of a workaround. But works for the time being...
# Note: Explanation for cryptic argument "\"" of 'cut': delimiting char after url link from href is char '"'
local URL=$(cat $PAGE_HTML_LOCAL | grep "href=\"https://.*windows-x64.tar.gz" | grep -o https://.*windows-x64.tar.gz | cut -f1 -d"\"")
curl -o "${IDEASY_COMPRESSED_FILE}" $URL
rm $PAGE_HTML_LOCAL
fi
}


function doExtract() {
echo "${IDE_ROOT}/_ide"
if [ -f "${IDEASY_COMPRESSED_FILE}" ]
then
tar xfz "${IDEASY_COMPRESSED_FILE}" --directory "${IDE_ROOT}/_ide" || exit 1
else
echo "Could not find and extract release ${IDEASY_COMPRESSED_FILE}"
exit 1
fi
}

# $@: success message
function doSuccess() {
echo -e "\033[92m${*}\033[39m"
}

# $@: warning message
function doWarning() {
echo -e "\033[93m${*}\033[39m"
}

# $@: messages to output
function doError() {
echo -e "\033[91m${1}\033[39m"
}

function doIsMacOs() {
if [ "${OSTYPE:0:6}" = "darwin" ]
then
return
fi
return 255
}

function doIsWindows() {
if [ "${OSTYPE}" = "cygwin" ] || [ "${OSTYPE}" = "msys" ]
then
return
fi
return 255
}
31 changes: 31 additions & 0 deletions cli/src/test/integration-tests-bash/integration-test-intellij.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#--- Preamble ---
# Working dir (PWD) is IDE_ROOT (it should contain an ide installation)
# Inherited variables:
# - $IDE := IDEasy ($PWD/_ide/bin/ide)
# - $IDE_ROOT := PWD
# - All functions in functions-test
# Use 'return <EXIT_CODE>' (instead of exit) to signal if test passed/failed.

# For debugging integration scripts (like this script), it is a good idea to
# fail whenever an error ocurred (set -e). When you finished writting the
# test, however, please remove your 'set -e' line, since in production it will
# exit and not run subsequent integration tests.
#set -e

#--- Body ---
echo "My ide $IDE"
echo "MY PWD: $PWD"
#doIdeCreate
echo "MY PWD: $PWD"

doWarning "Fn working?"
#cd asgkagkadgöadjg
#cd agagagadlöm
#which ide | echo
#$IDE gibberish whatever intellij
#cd sometakntnalgnank || return 1
# cd sometakntnalgnank
#echo "Try and fail" && exit 1
echo "All good."
#exit 0

10 changes: 10 additions & 0 deletions cli/src/test/integration-tests-bash/integration-test-npm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#--- Body ---
echo "MY PWD: $PWD"
echo "npm test"
#cd adgnaklgnadgknlan
echo Aha
#doIdeCreate
#ide install npm
#return
return 0

Loading