From 5e07b81de2fa07410e5c2e89dab005f41b6380cc Mon Sep 17 00:00:00 2001 From: rob Date: Fri, 29 Apr 2016 15:38:28 +0100 Subject: [PATCH 1/4] Added mix_path function with unit tests --- .gitignore | 2 ++ constant_testing.sh | 37 ++++++++++++++++++++++++------------- test.sh | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100755 test.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88a9c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +shunit2/ + diff --git a/constant_testing.sh b/constant_testing.sh index 09eb4a2..cda9e82 100755 --- a/constant_testing.sh +++ b/constant_testing.sh @@ -49,6 +49,11 @@ function file_changed { log_failure "Uh-oh, You don't have tests for this do you?" fi } + +mix_path() { + [[ $1 =~ (^.*/)(test|lib)/ ]] + mix_path=${BASH_REMATCH[1]} +} function install_tools { if [ $(dpkg-query -W -f='${Status}' inotify-tools 2>/dev/null | grep -c "ok installed") -eq 0 ]; @@ -57,17 +62,23 @@ function install_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 + 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 +} -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 diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..e1605ac --- /dev/null +++ b/test.sh @@ -0,0 +1,20 @@ +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 +} + +test_mix_path() { + assert_mix_path project/test/the_test.exs project/ + assert_mix_path /dir/project/test/the_test.exs /dir/project/ + assert_mix_path /dir/test/project/test/the_test.exs /dir/test/project/ + assert_mix_path /dir/test/project/test/subdir/the_test.exs /dir/test/project/ + assert_mix_path /dir/lib/project/lib/subdir/the_test.exs /dir/lib/project/ +} + +. shunit2/source/2.1/src/shunit2 From a276215339566ead4dd9f0e2ed1834bc6a8d71d5 Mon Sep 17 00:00:00 2001 From: rob Date: Fri, 29 Apr 2016 16:27:19 +0100 Subject: [PATCH 2/4] Wired in new functions --- constant_testing.sh | 17 ++++++++++------- test.sh | 23 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/constant_testing.sh b/constant_testing.sh index cda9e82..556a27d 100755 --- a/constant_testing.sh +++ b/constant_testing.sh @@ -37,24 +37,26 @@ function log_success { 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" + mix_test "$test_path" } function file_changed { - test_file="./test/${file%.*}_test.exs" - if [ -f $test_file ]; then + 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 } mix_path() { - [[ $1 =~ (^.*/)(test|lib)/ ]] + [[ $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} } - + function install_tools { if [ $(dpkg-query -W -f='${Status}' inotify-tools 2>/dev/null | grep -c "ok installed") -eq 0 ]; then @@ -70,7 +72,8 @@ watch() { inotifywait -m -r -q --exclude '(.swp)' -e close_write ./ | while read path action file; do if [[ "$file" == *.exs || "$file" == *.ex ]]; then - cd $path../ + 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..." diff --git a/test.sh b/test.sh index e1605ac..ff6c6d9 100755 --- a/test.sh +++ b/test.sh @@ -9,12 +9,27 @@ assert_mix_path() { 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 /dir/project/test/the_test.exs /dir/project/ - assert_mix_path /dir/test/project/test/the_test.exs /dir/test/project/ - assert_mix_path /dir/test/project/test/subdir/the_test.exs /dir/test/project/ - assert_mix_path /dir/lib/project/lib/subdir/the_test.exs /dir/lib/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 From 523b62c819909a453e94d961b682636dca1c7dc3 Mon Sep 17 00:00:00 2001 From: rob Date: Fri, 29 Apr 2016 16:31:50 +0100 Subject: [PATCH 3/4] Standardising function syntax --- constant_testing.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/constant_testing.sh b/constant_testing.sh index 556a27d..8a6a74d 100755 --- a/constant_testing.sh +++ b/constant_testing.sh @@ -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=$? @@ -17,30 +17,30 @@ 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 { +test_file_changed() { log_debug "You changed the test file: $path$file so I'm running those tests for you" mix_test "$test_path" } -function file_changed { +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_path" @@ -57,7 +57,7 @@ mix_path() { test_path=${test_path/%.ex/_test.exs} } -function install_tools { +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 From b3ccb2a5a5fc54c79fbcb8f7970941ee64602743 Mon Sep 17 00:00:00 2001 From: rob Date: Sun, 1 May 2016 21:46:49 +0100 Subject: [PATCH 4/4] Better error message if the mix project for a test file cannot be found --- constant_testing.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/constant_testing.sh b/constant_testing.sh index 8a6a74d..2f45666 100755 --- a/constant_testing.sh +++ b/constant_testing.sh @@ -36,8 +36,12 @@ log_success() { } test_file_changed() { - log_debug "You changed the test file: $path$file so I'm running those tests for you" - mix_test "$test_path" + 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 } file_changed() {