Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
shunit2/

74 changes: 46 additions & 28 deletions constant_testing.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash

function run_all {
run_all() {
log_info "$path$file changed... running all the tests from: `pwd`"
mix_test
}

function mix_test {
mix_test() {
log_info "Running: mix test $1"
output=`mix test $1`
result=$?
Expand All @@ -17,57 +17,75 @@ function mix_test {
fi
}

function log_info {
log_info() {
echo -e "\e[35m$1\e[39m"
}

function log_debug {
log_debug() {
if [[ $debug ]]; then
echo -e "\e[33m$1\e[39m"
fi
}

function log_failure {
log_failure() {
echo -e "\e[31m$1\e[39m"
}

function log_success {
log_success() {
echo -e "\e[32m$1\e[39m"
}

function test_file_changed {
log_debug "You changed the test file: $path$file so I'm running those tests for you"
mix_test "test/$file"
test_file_changed() {
if [ "$mix_path" == "" ]; then
log_failure "Cannot find a mix project for: $path$file"
else
log_debug "You changed the test file: $path$file so I'm running those tests for you"
mix_test "$test_path"
fi
}

function file_changed {
test_file="./test/${file%.*}_test.exs"
if [ -f $test_file ]; then
file_changed() {
if [ -f $test_path ]; then
log_debug "You changed the file: $path$file so I'll run the tests for that"
mix_test "$test_file"
mix_test "$test_path"
else
log_failure "Uh-oh, You don't have tests for this do you?"
fi
}

function install_tools {

mix_path() {
[[ $1 =~ (^.*/)((test|lib)/.*$) ]]
mix_path=${BASH_REMATCH[1]}
file_path=${BASH_REMATCH[2]}
test_path=${file_path/#lib/test}
test_path=${test_path/%.ex/_test.exs}
}

install_tools() {
if [ $(dpkg-query -W -f='${Status}' inotify-tools 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
sudo apt-get install inotify-tools
fi
}

install_tools
log_info "I'm going to watch you work... in a creepy way"
if [[ $1 == "--debug" ]]; then debug=1; fi
watch() {
install_tools
log_info "I'm going to watch you work... in a creepy way"
[[ $1 == "--debug" ]] && debug=1

inotifywait -m -r -q --exclude '(.swp)' -e close_write ./ |
while read path action file; do
if [[ "$file" == *.exs || "$file" == *.ex ]]; then
mix_path "$path$file"
cd $mix_path
if [[ "$file" == *_test.exs ]]; then test_file_changed
elif [[ "$file" == *.ex ]]; then file_changed; fi
log_info "I'm watching you..."
cd - > /dev/null
fi
done
}

inotifywait -m -r -q --exclude '(.swp)' -e close_write ./ |
while read path action file; do
if [[ "$file" == *.exs || "$file" == *.ex ]]; then
cd $path../
if [[ "$file" == *_test.exs ]]; then test_file_changed
elif [[ "$file" == *.ex ]]; then file_changed; fi
log_info "I'm watching you..."
cd - > /dev/null
fi
done
if [ "$1" != "test" ]; then
watch
fi
35 changes: 35 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
if [ ! -d "shunit2" ]; then
git clone https://github.com/kward/shunit2.git
fi

. constant_testing.sh test

assert_mix_path() {
mix_path $1
assertEquals $2 $mix_path
}

assert_test_path() {
mix_path $1
assertEquals $2 $test_path
}

test_mix_path() {
assert_mix_path project/test/the_test.exs project/
assert_mix_path /src/project/test/the_test.exs /src/project/
assert_mix_path /src/test/project/test/the_test.exs /src/test/project/
assert_mix_path /src/test/project/test/subdir/the_test.exs /src/test/project/

assert_mix_path /src/lib/project/lib/subfolder/the_test.exs /src/lib/project/
}

test_test_path() {
assert_test_path project/test/the_test.exs test/the_test.exs
assert_test_path project/test/subfolder/the_test.exs test/subfolder/the_test.exs

assert_test_path project/lib/module.ex test/module_test.exs
assert_test_path project/lib/subfolder/module.ex test/subfolder/module_test.exs
assert_test_path lib/project/lib/subfolder/lib.ex test/subfolder/lib_test.exs
}

. shunit2/source/2.1/src/shunit2