diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..d512570 --- /dev/null +++ b/TODO.md @@ -0,0 +1,39 @@ +# Todo + +- diff-tool selection is consistent with `git help difftool` +- Command line args are consistent with `git dif` + Use -t and --tool, NOT -d + See: git difftool --tool-help + +# Starter code + +```bash +get_git_difftool() { + # Check if --tool argument is provided in command line arguments + while [[ $# -gt 0 ]]; do + case "$1" in + -t|--tool) + echo "$2" + return 0 + ;; + *) shift ;; + esac + done + + # Check if GIT_DIFF_TOOL environment variable is set + if [[ -n "$GIT_DIFF_TOOL" ]]; then + echo "$GIT_DIFF_TOOL" + return 0 + fi + + # Fallback to the configured diff.tool in git config + git_config_tool="$(git config --get diff.tool)" + if [[ -n "$git_config_tool" ]]; then + echo "$git_config_tool" + return 0 + fi + + # Default return if no tool is set (to avoid empty return) + echo "default-diff-tool" # Replace this with the actual default tool if needed +} +``` diff --git a/bash/test.sh b/bash/test.sh index e51a4b3..4b43fde 100755 --- a/bash/test.sh +++ b/bash/test.sh @@ -1,10 +1,46 @@ #!/bin/bash -echo "test: pass" -./verify.sh -t test1 <<< "test 1 approves this message +function test1() { + test_name="${FUNCNAME[0]}" + echo "test: pass" + ./verify.sh -t "$test_name" <<<"test 1 approves this message line 2" -echo "" + echo "" +} -echo "test: fails and triggers diff tool" -./verify.sh -t test3 -d diff <<< "test 3 receives this input" -echo "" +function test2() { + true +} + +function test3() { + test_name="${FUNCNAME[0]}" + echo "test: fails and triggers diff tool" + ./verify.sh -t "$test_name" -d diff <<<"test 3 receives this input" + echo "" +} + +function test4() { + test_name="${FUNCNAME[0]}" + ./verify.sh -d meld <<<"Input to test without a name" +} + +function test_diff_tool_selection() { + test_name="${FUNCNAME[0]}" + cat <&2;; - esac +git_difftool="$(git config --get diff.tool)" +vs_code_path=$(command -v code) + +if [[ -n "${git_difftool}" ]] && command -v "${git_difftool}"; then + difftool="git difftool --tool ${git_difftool} --no-index" +elif [[ -n "${vs_code_path}" ]]; then + difftool="${vs_code_path} --diff" +else + difftool="" +fi + +function warn() { + echo "$*" >&2 +} + +while getopts ":r:t:d:D" opt; do + case $opt in + d) difftool=$OPTARG ;; + r) received_text=$OPTARG ;; + t) test_name=$OPTARG ;; + D) debug_without_compare_and_approve="true" ;; + \?) warn "Invalid option: -$OPTARG" ;; + esac done +if [[ "${test_name=}" == '' ]]; then + test_name="unspecified_test_name" +fi + received="$test_name.received" approved="$test_name.approved" -if [ "$received_text" == "" ]; -then - cat - > "$received" -else - echo "$received_text" > "$received" -fi +function debug_arguments() { + cat <"$received" + else + echo "$received_text" >"$received" + fi + + touch "$approved" + + if [[ -n "${debug_without_compare_and_approve=}" ]]; then + debug_arguments + else + compare_and_approve "$test_name" + fi +} + +function pass() { + echo "${1:=unnamed-test} passed" +} + +function fail() { + echo "${1:unnamed-test} failed" +} + +function fail_and_diff() { + if [ -e /dev/tty ]; then + $difftool "$received" "$approved" /dev/null; then + (pass_and_rm_received "$1"); + else + (fail_and_diff "$1") + fi +} -touch "$approved" - -diff -q "$received" "$approved" > /dev/null \ - && (echo "test passed"; rm "$received") \ - || (echo "test failed"; - if [ -e /dev/tty ]; then - $diff_tool "$received" "$approved"