From 7ebdf6e7c66a63595d9af7a4c94a61f377261877 Mon Sep 17 00:00:00 2001 From: Lucas Vinicius Amaral de Oliveira Date: Wed, 17 Jan 2024 17:59:51 -0300 Subject: [PATCH] Run ww_activate script on linux workspace activation On Linux, this script was already being created for this purpose, but it was not being executed. On Windows it was working correctly. --- test_ww | 57 +++++++++++++++++----------------------- testing/test_tools | 65 ++++++++++++++++++++++++++++++++++++++++++++++ ww | 11 +++++++- 3 files changed, 99 insertions(+), 34 deletions(-) create mode 100644 testing/test_tools diff --git a/test_ww b/test_ww index 8e3ca86..53eea2a 100755 --- a/test_ww +++ b/test_ww @@ -1,45 +1,22 @@ #!/bin/bash - -assert_equal() { - if [ $1 != $2 ]; then - errorOut "Assertion failed: $1 is not equal to $2'" - fi -} +# load test tools +. ./testing/test_tools test_can_activate_workspace() { - echo "" - echo "Testing ${FUNCNAME[0]}" - echo "" - - local test_dir=$(pwd) - - mkdir $test_dir/temp-ww-workdir - export WW_DEFAULT_PATH=$test_dir/temp-ww-workdir - + # create and activate workspace 'ws1' $test_dir/ww -c ws1 source ./ww ws1 # check that the correct workspace was activated assert_equal $WW_CURRENT_WORKSPACE ws1 assert_equal $CONDA_ENVS_PATH $test_dir/temp-ww-workdir/ws1/envs - - cd $test_dir - rm -rf temp-ww-workdir } -test_can_activate_current() { - echo "" - echo "Testing ${FUNCNAME[0]}" - echo "" - local test_dir=$(pwd) - - mkdir $test_dir/temp-ww-workdir - export WW_DEFAULT_PATH=$test_dir/temp-ww-workdir - +test_can_activate_current() { $test_dir/ww -c ws2 - cd ./temp-ww-workdir/ws2/Projects + cd $WW_DEFAULT_PATH/ws2/Projects local cwd_before=$(pwd) source $test_dir/ww . @@ -51,12 +28,26 @@ test_can_activate_current() { # check that the correct workspace was activated assert_equal $WW_CURRENT_WORKSPACE ws2 assert_equal $CONDA_ENVS_PATH $test_dir/temp-ww-workdir/ws2/envs +} - cd $test_dir - rm -rf temp-ww-workdir +test_ww_activate_script_is_called() { + $test_dir/ww -c ws1 + + echo "export TEMP_TEST_VAR=1" >> $WW_DEFAULT_PATH/ws1/ww_activate.sh + + source ./ww ws1 + + # check that the correct workspace was activated + assert_equal $WW_CURRENT_WORKSPACE ws1 + assert_equal $TEMP_TEST_VAR 1 } echo "Testing file test_ww" -test_can_activate_workspace -test_can_activate_current -echo "Success" \ No newline at end of file +echo "" + +test "test_can_activate_workspace" +test "test_can_activate_current" +test "test_ww_activate_script_is_called" + +echo "" +printGreen "Success" diff --git a/testing/test_tools b/testing/test_tools new file mode 100644 index 0000000..95d1758 --- /dev/null +++ b/testing/test_tools @@ -0,0 +1,65 @@ +#!/bin/bash +#enable colors on CI +export TERM=xterm-color + +test() { + local test_function=$1 + + echo -n "Testing ${test_function} ... " + + setup + + # run test function + output=$($test_function) + failed=$? + + teardown + + if [ $failed != 0 ]; then + printRed "Failed" + echo "" + echo "------------ Test Output ------------" + echo "$test_function:" + echo "$output" + exit 1 + fi + + printGreen "Success" +} + +setup() { + test_dir=$(pwd) + + rm -rf temp-ww-workdir + mkdir $test_dir/temp-ww-workdir + export WW_DEFAULT_PATH=$test_dir/temp-ww-workdir +} + +teardown() { + cd $test_dir + rm -rf temp-ww-workdir +} + +assert_equal() { + if [ $1 != $2 ]; then + testErrorOut "Assertion failed: $1 is not equal to $2'" + fi +} + +testErrorOut() { + printRed "ERROR: $@" + exit 1 +} + +printGreen() { + green=`tput setaf 2` + reset=`tput sgr0` + echo "${green}$1${reset}" +} + +printRed() { + red=`tput setaf 1` + reset=`tput sgr0` + echo "${red}$1${reset}" +} + diff --git a/ww b/ww index a3cf1e6..a2b81d8 100755 --- a/ww +++ b/ww @@ -36,6 +36,12 @@ activate() { if [[ -n $WW_CONDA_PKGS_PATH ]]; then export CONDA_PKGS_PATH=$WW_CONDA_PKGS_PATH fi + + # Run ww_activate.sh script if one exists + ACTIVATE_SCRIPT="${workspace}/ww_activate.sh" + if [ -f "$ACTIVATE_SCRIPT" ]; then + . "$ACTIVATE_SCRIPT" + fi } activate_current() { @@ -60,7 +66,10 @@ create() { mkdir -p "${_NEW_WORKSPACE}/${WW_PROJECTS_SUBDIR}" CONDA_ROOT=$(conda info --root) - cp "${CONDA_ROOT}/.condarc" "${_NEW_WORKSPACE}/.condarc" + ROOT_CONDARC="${CONDA_ROOT}/.condarc" + if [ -f "$ROOT_CONDARC" ]; then + cp "${ROOT_CONDARC}" "${_NEW_WORKSPACE}/.condarc" + fi ACTIVATE_SCRIPT="${_NEW_WORKSPACE}/ww_activate.sh" touch "$ACTIVATE_SCRIPT"