From 14f3f2494601be0e17bc4659db820a3cd93e4465 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Fri, 13 Oct 2023 17:43:57 -0400 Subject: [PATCH 01/19] ! r Extract function: main --- bash/verify.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bash/verify.sh b/bash/verify.sh index 1857c99..c645a2e 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -3,6 +3,7 @@ default_diff_tool="code --diff" diff_tool=$default_diff_tool +function main(){ while getopts ":r:t:d:" opt; do case $opt in d) diff_tool=$OPTARG;; @@ -33,3 +34,6 @@ diff -q "$received" "$approved" > /dev/null \ $diff_tool "$received" "$approved" fi; false) +} + +main From 22abf52cbd18cde9063b14fea8130b5fa37be637 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Fri, 13 Oct 2023 17:50:22 -0400 Subject: [PATCH 02/19] ! r Extract function: compare_and_approve --- bash/verify.sh | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index c645a2e..f22e6d9 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -25,15 +25,20 @@ 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" /dev/null \ + && (echo "test passed"; rm "$received") \ + || (echo "test failed"; + if [ -e /dev/tty ]; then + $diff_tool "$received" "$approved" Date: Fri, 13 Oct 2023 17:57:11 -0400 Subject: [PATCH 03/19] . r Whitespace --- bash/verify.sh | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index f22e6d9..3c5b72c 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -4,31 +4,31 @@ default_diff_tool="code --diff" diff_tool=$default_diff_tool function main(){ -while getopts ":r:t:d:" opt; do - case $opt in - d) diff_tool=$OPTARG;; - r) received_text=$OPTARG;; - t) test_name=$OPTARG;; - \?) echo "Invalid option: -$OPTARG" >&2;; - esac -done - -received="$test_name.received" -approved="$test_name.approved" - -if [ "$received_text" == "" ]; -then - cat - > "$received" -else - echo "$received_text" > "$received" -fi - -touch "$approved" - -compare_and_approve - + while getopts ":r:t:d:" opt; do + case $opt in + d) diff_tool=$OPTARG;; + r) received_text=$OPTARG;; + t) test_name=$OPTARG;; + \?) echo "Invalid option: -$OPTARG" >&2;; + esac + done + + received="$test_name.received" + approved="$test_name.approved" + + if [ "$received_text" == "" ]; + then + cat - > "$received" + else + echo "$received_text" > "$received" + fi + + touch "$approved" + + compare_and_approve } + function compare_and_approve(){ diff -q "$received" "$approved" > /dev/null \ && (echo "test passed"; rm "$received") \ From 9db16f1d546f59c3df96a2b195683df484f95e1d Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Fri, 13 Oct 2023 18:00:13 -0400 Subject: [PATCH 04/19] ! r Extract functions: pass(), fail() --- bash/verify.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index 3c5b72c..6cf424d 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -28,11 +28,19 @@ function main(){ compare_and_approve } +function pass(){ + echo "test passed" +} + + +function fail(){ + echo "test failed" +} function compare_and_approve(){ diff -q "$received" "$approved" > /dev/null \ - && (echo "test passed"; rm "$received") \ - || (echo "test failed"; + && (pass; rm "$received") \ + || (fail; if [ -e /dev/tty ]; then $diff_tool "$received" "$approved" Date: Fri, 13 Oct 2023 18:20:09 -0400 Subject: [PATCH 05/19] ! r Remove duplicate (dead) code --- bash/verify.sh | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index 6cf424d..fd6db72 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -3,19 +3,20 @@ default_diff_tool="code --diff" diff_tool=$default_diff_tool -function main(){ - while getopts ":r:t:d:" opt; do - case $opt in +while getopts ":r:t:d:" opt; do + case $opt in d) diff_tool=$OPTARG;; r) received_text=$OPTARG;; t) test_name=$OPTARG;; \?) echo "Invalid option: -$OPTARG" >&2;; - esac - done + esac +done + +received="$test_name.received" +approved="$test_name.approved" - received="$test_name.received" - approved="$test_name.approved" +function main(){ if [ "$received_text" == "" ]; then cat - > "$received" @@ -41,12 +42,20 @@ function compare_and_approve(){ diff -q "$received" "$approved" > /dev/null \ && (pass; rm "$received") \ || (fail; - if [ -e /dev/tty ]; then - $diff_tool "$received" "$approved" /dev/null \ + && (pass; rm "$received") \ + || (fail_and_diff) +} + + main From 2c15f8604a823a21ca636f3e750eedc1e984a2ab Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Fri, 13 Oct 2023 18:21:48 -0400 Subject: [PATCH 06/19] ! r Extract function: fail_and_diff() --- bash/verify.sh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index fd6db72..9d5822a 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -38,16 +38,14 @@ function fail(){ echo "test failed" } -function compare_and_approve(){ - diff -q "$received" "$approved" > /dev/null \ - && (pass; rm "$received") \ - || (fail; +function fail_and_diff(){ + fail; if [ -e /dev/tty ]; then $diff_tool "$received" "$approved" Date: Fri, 13 Oct 2023 18:24:35 -0400 Subject: [PATCH 07/19] ! r Extract function: pass_and_rm() --- bash/verify.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bash/verify.sh b/bash/verify.sh index 9d5822a..1392f3c 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -49,9 +49,14 @@ function fail_and_diff(){ } +function pass_and_rm(){ + pass; rm "$received" +} + + function compare_and_approve(){ diff -q "$received" "$approved" > /dev/null \ - && (pass; rm "$received") \ + && (pass_and_rm) \ || (fail_and_diff) } From ebf764c53f1453304dd6966585df95a3a68edbc3 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Fri, 13 Oct 2023 18:39:51 -0400 Subject: [PATCH 08/19] ! r Extract function: warn() --- bash/verify.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bash/verify.sh b/bash/verify.sh index 1392f3c..8fc13f7 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -3,12 +3,18 @@ default_diff_tool="code --diff" diff_tool=$default_diff_tool + +function warn(){ + echo "$*" >&2 +} + + while getopts ":r:t:d:" opt; do case $opt in d) diff_tool=$OPTARG;; r) received_text=$OPTARG;; t) test_name=$OPTARG;; - \?) echo "Invalid option: -$OPTARG" >&2;; + \?) warn "Invalid option: -$OPTARG";; esac done From f8b73af8ff4efbb7c3519c33d3c347f342fdb646 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Fri, 13 Oct 2023 18:43:10 -0400 Subject: [PATCH 09/19] ^ r Rename function: pass_and_rm_received() --- bash/verify.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index 8fc13f7..7749807 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -35,6 +35,7 @@ function main(){ compare_and_approve } + function pass(){ echo "test passed" } @@ -55,14 +56,15 @@ function fail_and_diff(){ } -function pass_and_rm(){ - pass; rm "$received" +function pass_and_rm_received(){ + pass; + rm "$received" } function compare_and_approve(){ diff -q "$received" "$approved" > /dev/null \ - && (pass_and_rm) \ + && (pass_and_rm_recieved) \ || (fail_and_diff) } From b7de3489c4e0b3872355f6a55d2c786497adae92 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Thu, 19 Oct 2023 21:47:35 -0400 Subject: [PATCH 10/19] ^ f Default test_name to 'unspecified_test_name" if not specified --- bash/test.sh | 2 ++ bash/unspecified_test_name.approved | 1 + bash/verify.sh | 34 ++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 bash/unspecified_test_name.approved diff --git a/bash/test.sh b/bash/test.sh index e51a4b3..1e54654 100755 --- a/bash/test.sh +++ b/bash/test.sh @@ -8,3 +8,5 @@ echo "" echo "test: fails and triggers diff tool" ./verify.sh -t test3 -d diff <<< "test 3 receives this input" echo "" + +./verify.sh -d meld <<<"Input to test without a name" diff --git a/bash/unspecified_test_name.approved b/bash/unspecified_test_name.approved new file mode 100644 index 0000000..5fd22a6 --- /dev/null +++ b/bash/unspecified_test_name.approved @@ -0,0 +1 @@ +Input to test without a name diff --git a/bash/verify.sh b/bash/verify.sh index 7749807..8ac4a39 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -9,18 +9,30 @@ function warn(){ } -while getopts ":r:t:d:" opt; do +while getopts ":r:t:d:D" opt; do case $opt in d) diff_tool=$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" +function debug_arguments(){ + cat < /dev/null \ - && (pass_and_rm_recieved) \ - || (fail_and_diff) + && (pass_and_rm_received $1) \ + || (fail_and_diff $1) } From ed14b510ac27c364dfaa30c01399dce799d95e88 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Mon, 20 Jan 2025 20:36:38 -0500 Subject: [PATCH 11/19] ! f Logic to choose difftool from 'git difftool' or 'code' --- bash/verify.sh | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index 8ac4a39..d05c218 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -1,8 +1,23 @@ #!/bin/bash - -default_diff_tool="code --diff" -diff_tool=$default_diff_tool - +set -euo pipefail + +true <&2 @@ -11,7 +26,7 @@ function warn(){ while getopts ":r:t:d:D" opt; do case $opt in - d) diff_tool=$OPTARG;; + d) difftool=$OPTARG;; r) received_text=$OPTARG;; t) test_name=$OPTARG;; D) debug_without_compare_and_approve="true";; @@ -29,13 +44,13 @@ approved="$test_name.approved" function debug_arguments(){ cat < "$received" else @@ -44,7 +59,7 @@ function main(){ touch "$approved" - if [[ -n "$debug_without_compare_and_approve" ]] + if [[ -n "${debug_without_compare_and_approve=}" ]] then debug_arguments; else @@ -54,7 +69,7 @@ function main(){ function pass(){ - echo "${1:unnamed-test} passed" + echo "${1:=unnamed-test} passed" } @@ -64,11 +79,11 @@ function fail(){ function fail_and_diff(){ - fail $1; + if [ -e /dev/tty ]; then - $diff_tool "$received" "$approved" Date: Mon, 20 Jan 2025 23:47:43 -0500 Subject: [PATCH 12/19] ! f Allow variable to be unbound: 'test_name' --- bash/verify.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash/verify.sh b/bash/verify.sh index d05c218..daaf444 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -34,7 +34,7 @@ while getopts ":r:t:d:D" opt; do esac done -if [[ "${test_name}" == '' ]] +if [[ "${test_name=}" == '' ]] then test_name="unspecified_test_name" fi From 6532a99873088334fcf680532d379594a750d901 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Mon, 3 Feb 2025 14:02:59 -0500 Subject: [PATCH 13/19] @ t New test: diff_tool_selection --- bash/diff_tool_selection.approved | 0 bash/test.sh | 8 ++++++++ 2 files changed, 8 insertions(+) create mode 100644 bash/diff_tool_selection.approved diff --git a/bash/diff_tool_selection.approved b/bash/diff_tool_selection.approved new file mode 100644 index 0000000..e69de29 diff --git a/bash/test.sh b/bash/test.sh index 1e54654..5f0fabd 100755 --- a/bash/test.sh +++ b/bash/test.sh @@ -10,3 +10,11 @@ echo "test: fails and triggers diff tool" echo "" ./verify.sh -d meld <<<"Input to test without a name" + +cat < Date: Mon, 3 Feb 2025 14:19:43 -0500 Subject: [PATCH 14/19] . @ New file: TODO.md --- TODO.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 TODO.md 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 +} +``` From d3e3c2d962c0687a69eaf8e692cdda60495dd130 Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Mon, 3 Feb 2025 20:54:55 -0500 Subject: [PATCH 15/19] . r Extract tests into separate functions. --- bash/test.sh | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/bash/test.sh b/bash/test.sh index 5f0fabd..4b43fde 100755 --- a/bash/test.sh +++ b/bash/test.sh @@ -1,20 +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 +} -./verify.sh -d meld <<<"Input to test without a name" +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 "" +} -cat < Date: Mon, 3 Feb 2025 20:58:38 -0500 Subject: [PATCH 16/19] . f Whitespace --- bash/verify.sh | 73 +++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index daaf444..0f55c67 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -12,94 +12,83 @@ 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"; + difftool="git difftool --tool ${git_difftool} --no-index" elif [[ -n "${vs_code_path}" ]]; then - difftool="${vs_code_path} --diff" + difftool="${vs_code_path} --diff" else difftool="" fi -function warn(){ +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";; + 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 +if [[ "${test_name=}" == '' ]]; then test_name="unspecified_test_name" fi received="$test_name.received" approved="$test_name.approved" -function debug_arguments(){ +function debug_arguments() { cat < "$received" +function main() { + if [ "${received_text=}" == "" ]; then + cat - >"$received" else - echo "$received_text" > "$received" + echo "$received_text" >"$received" fi - + touch "$approved" - - if [[ -n "${debug_without_compare_and_approve=}" ]] - then - debug_arguments; + + if [[ -n "${debug_without_compare_and_approve=}" ]]; then + debug_arguments else - compare_and_approve $test_name; + compare_and_approve "$test_name" fi } - -function pass(){ - echo "${1:=unnamed-test} passed" +function pass() { + echo "${1:=unnamed-test} passed" } - -function fail(){ - echo "${1:unnamed-test} failed" +function fail() { + echo "${1:unnamed-test} failed" } - -function fail_and_diff(){ +function fail_and_diff() { if [ -e /dev/tty ]; then $difftool "$received" "$approved" /dev/null \ - && (pass_and_rm_received $1) \ - || (fail_and_diff $1) +function compare_and_approve() { + diff -q "$received" "$approved" >/dev/null && + (pass_and_rm_received $1) || + (fail_and_diff $1) } - main From d49fd56418223ab3d04b8c2e42f814807175c8fd Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Mon, 3 Feb 2025 21:00:26 -0500 Subject: [PATCH 17/19] _ _ Approve all tests --- bash/{diff_tool_selection.approved => test5.approved} | 0 bash/test_command_line_parser.approved | 0 bash/test_diff_tool_selection.approved | 5 +++++ unspecified_test_name.approved | 0 4 files changed, 5 insertions(+) rename bash/{diff_tool_selection.approved => test5.approved} (100%) create mode 100644 bash/test_command_line_parser.approved create mode 100644 bash/test_diff_tool_selection.approved create mode 100644 unspecified_test_name.approved diff --git a/bash/diff_tool_selection.approved b/bash/test5.approved similarity index 100% rename from bash/diff_tool_selection.approved rename to bash/test5.approved diff --git a/bash/test_command_line_parser.approved b/bash/test_command_line_parser.approved new file mode 100644 index 0000000..e69de29 diff --git a/bash/test_diff_tool_selection.approved b/bash/test_diff_tool_selection.approved new file mode 100644 index 0000000..b137e55 --- /dev/null +++ b/bash/test_diff_tool_selection.approved @@ -0,0 +1,5 @@ +# Logic for diff tool +# 1. --tool argument takes top priority. +# 2. If no --tool is specified, GIT_DIFF_TOOL is checked next. +# 3. If GIT_DIFF_TOOL is unset, git config --get diff.tool is used. +# 4. If neither is set, Git falls back to its internal diff. diff --git a/unspecified_test_name.approved b/unspecified_test_name.approved new file mode 100644 index 0000000..e69de29 From 2ec8ad5451e2ae87374d1a0a207042f71ee9b09b Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Mon, 3 Feb 2025 21:08:25 -0500 Subject: [PATCH 18/19] ! r Convert short-circuit logic to if/then/else. This changed the logic. Not sure if it broke something. --- bash/verify.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index 0f55c67..8f7ec36 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -86,9 +86,12 @@ function pass_and_rm_received() { } function compare_and_approve() { - diff -q "$received" "$approved" >/dev/null && - (pass_and_rm_received $1) || - (fail_and_diff $1) + # NB: I forget why branches are in subshells. It may no longer be necessary. + if diff -q "$received" "$approved" >/dev/null; then + (pass_and_rm_received "$1"); + else + (fail_and_diff "$1") + fi } main From 2faef7dc8ac43eeb9615f0fb00226cfe1858ccdc Mon Sep 17 00:00:00 2001 From: "Michael R. Wolf" Date: Mon, 3 Feb 2025 21:19:10 -0500 Subject: [PATCH 19/19] _ _ Quotes to preserve whitespace in variable expansion. --- bash/verify.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bash/verify.sh b/bash/verify.sh index 8f7ec36..58dc826 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -71,7 +71,6 @@ function fail() { } function fail_and_diff() { - if [ -e /dev/tty ]; then $difftool "$received" "$approved" /dev/null; then (pass_and_rm_received "$1"); else