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

Run ww_activate script on linux workspace activation #12

Merged
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
57 changes: 24 additions & 33 deletions test_ww
Original file line number Diff line number Diff line change
@@ -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 .
Expand All @@ -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"
echo ""

test "test_can_activate_workspace"
test "test_can_activate_current"
test "test_ww_activate_script_is_called"

echo ""
printGreen "Success"
65 changes: 65 additions & 0 deletions testing/test_tools
Original file line number Diff line number Diff line change
@@ -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}"
}

11 changes: 10 additions & 1 deletion ww
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"
Expand Down