From 8048e6445657470cc2971d2cccda30eedbbc26a8 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Wed, 6 Aug 2025 08:23:25 +0000 Subject: [PATCH 1/9] - Add ability to override raw input --- examples/hooks/src/initialize.sh | 6 ++++++ lib/bashly/libraries/hooks/initialize.sh | 6 ++++++ lib/bashly/views/command/master_script.gtx | 6 ++---- lib/bashly/views/command/start.gtx | 8 ++++++++ spec/approvals/examples/stacktrace | 8 ++++---- spec/bashly/integration/examples_spec.rb | 14 +++++++++++++- spec/bashly/script/wrapper_spec.rb | 2 +- 7 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 lib/bashly/views/command/start.gtx diff --git a/examples/hooks/src/initialize.sh b/examples/hooks/src/initialize.sh index 30b092bd..b04f74fc 100644 --- a/examples/hooks/src/initialize.sh +++ b/examples/hooks/src/initialize.sh @@ -3,4 +3,10 @@ ## Any code here will be placed inside the `initialize()` function and called ## before running anything else. ## +## - The raw command line arguments are available to you here as "$@" +## - If you wish to override the raw arguments before they are processed, +## simply define an array named `input_override`: +## +## declare -g -a input_override=("modified" "args") +## ## You can safely delete this file if you do not need it. diff --git a/lib/bashly/libraries/hooks/initialize.sh b/lib/bashly/libraries/hooks/initialize.sh index 30b092bd..b04f74fc 100644 --- a/lib/bashly/libraries/hooks/initialize.sh +++ b/lib/bashly/libraries/hooks/initialize.sh @@ -3,4 +3,10 @@ ## Any code here will be placed inside the `initialize()` function and called ## before running anything else. ## +## - The raw command line arguments are available to you here as "$@" +## - If you wish to override the raw arguments before they are processed, +## simply define an array named `input_override`: +## +## declare -g -a input_override=("modified" "args") +## ## You can safely delete this file if you do not need it. diff --git a/lib/bashly/views/command/master_script.gtx b/lib/bashly/views/command/master_script.gtx index bdfdbb78..0b5eccd4 100644 --- a/lib/bashly/views/command/master_script.gtx +++ b/lib/bashly/views/command/master_script.gtx @@ -15,11 +15,9 @@ > if Settings.enabled? :sourcing > if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then - > {{ Settings.function_name :initialize }} - > {{ Settings.function_name :run }} "$@" + = render(:start).indent 2 > fi else - > {{ Settings.function_name :initialize }} - > {{ Settings.function_name :run }} "$@" + = render :start end > diff --git a/lib/bashly/views/command/start.gtx b/lib/bashly/views/command/start.gtx new file mode 100644 index 00000000..bc37cc5e --- /dev/null +++ b/lib/bashly/views/command/start.gtx @@ -0,0 +1,8 @@ += view_marker + +> {{ Settings.function_name :initialize }} "$@" +> if declare -p input_override &>/dev/null; then +> {{ Settings.function_name :run }} "${input_override[@]}" +> else +> {{ Settings.function_name :run }} "$@" +> fi diff --git a/spec/approvals/examples/stacktrace b/spec/approvals/examples/stacktrace index 1b4e3915..84ebc903 100644 --- a/spec/approvals/examples/stacktrace +++ b/spec/approvals/examples/stacktrace @@ -47,9 +47,9 @@ Examples: + ./download something ./download: line 15: no_such_command: command not found -./download:15 in `root_command`: no_such_command +./download: in `root_command`: no_such_command Stack trace: - from ./download:15 in `root_command` - from ./download:259 in `run` - from ./download:265 in `main` + from ./download: in `root_command` + from ./download: in `run` + from ./download: in `main` diff --git a/spec/bashly/integration/examples_spec.rb b/spec/bashly/integration/examples_spec.rb index 7dc6ddca..3c0ba725 100644 --- a/spec/bashly/integration/examples_spec.rb +++ b/spec/bashly/integration/examples_spec.rb @@ -24,6 +24,12 @@ # Allow up to a certain string distance from the approval text in CI leeway = ENV['CI'] ? 40 : 0 + # For certain examples, allow some exceptions (replacements) since they + # are too volatile (e.g. line number changes) + exceptions = { + 'examples/stacktrace' => [/download:\d+/, 'download:'] + } + test_cases.each do |example| approval_name = example.gsub 'spec/fixtures/workspaces', 'fixtures' @@ -41,7 +47,13 @@ # - The "+ ..." shell messages driven by `set -x` have no space # - The order of our `inspect_args` sometimes differs # - The result of the `deps` array sometimes differs - expect(output).to match_approval(approval_name).diff(leeway) + # In addition, for some examples we allow exceptions, so the below is + # just a more granular version of: + # expect(output).to match_approval(approval_name).diff(leeway).except(*except) + match_output = match_approval(approval_name).diff(leeway) + except = exceptions[example] + match_output = match_output.except(*except) if except + expect(output).to match_output end end end diff --git a/spec/bashly/script/wrapper_spec.rb b/spec/bashly/script/wrapper_spec.rb index 74484a43..c21bcb54 100644 --- a/spec/bashly/script/wrapper_spec.rb +++ b/spec/bashly/script/wrapper_spec.rb @@ -11,7 +11,7 @@ lines = subject.code.split "\n" expect(lines[0..13].join("\n")).to match_approval('script/wrapper/code') .except(/\d+\.\d+\.\d+(\.rc\d)?/) - expect(lines[-2]).to eq ' run "$@"' + expect(lines[-3]).to eq ' run "$@"' end end From 78bd62dd0a7bb810a647ec9dc50ecf88cefedf63 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Wed, 6 Aug 2025 08:26:48 +0000 Subject: [PATCH 2/9] update before hook documentation comment --- lib/bashly/libraries/hooks/before.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/bashly/libraries/hooks/before.sh b/lib/bashly/libraries/hooks/before.sh index b246442c..cc4fbcc0 100644 --- a/lib/bashly/libraries/hooks/before.sh +++ b/lib/bashly/libraries/hooks/before.sh @@ -3,6 +3,9 @@ ## Any code here will be placed inside a `before_hook()` function and called ## before running any command (but after processing its arguments). ## +## - The processed args are available to you here as `$args` and `$extra_args` +## - The raw input array is also available in read-only mode as `$input` +## ## You can safely delete this file if you do not need it. echo "==[ Before Hook Called ]==" inspect_args \ No newline at end of file From 45d18e82d849a4486582c505d1b0ae632fe904a0 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Wed, 6 Aug 2025 08:42:46 +0000 Subject: [PATCH 3/9] change input override condition for shellcheck compliance --- lib/bashly/views/command/start.gtx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bashly/views/command/start.gtx b/lib/bashly/views/command/start.gtx index bc37cc5e..38aa842c 100644 --- a/lib/bashly/views/command/start.gtx +++ b/lib/bashly/views/command/start.gtx @@ -1,7 +1,7 @@ = view_marker > {{ Settings.function_name :initialize }} "$@" -> if declare -p input_override &>/dev/null; then +> if [[ -v input_override ]]; then > {{ Settings.function_name :run }} "${input_override[@]}" > else > {{ Settings.function_name :run }} "$@" From fdadcfc12727452d432b69ce0a6bd0597109b032 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Wed, 6 Aug 2025 19:05:18 +0000 Subject: [PATCH 4/9] reduce render-mandoc approval noise --- examples/hooks/src/before.sh | 3 +++ examples/render-mandoc/docs/download.1 | 2 +- examples/render-mandoc/docs/download.md | 2 +- spec/approvals/examples/render-mandoc | 2 +- spec/bashly/integration/examples_spec.rb | 3 ++- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/hooks/src/before.sh b/examples/hooks/src/before.sh index b246442c..cc4fbcc0 100644 --- a/examples/hooks/src/before.sh +++ b/examples/hooks/src/before.sh @@ -3,6 +3,9 @@ ## Any code here will be placed inside a `before_hook()` function and called ## before running any command (but after processing its arguments). ## +## - The processed args are available to you here as `$args` and `$extra_args` +## - The raw input array is also available in read-only mode as `$input` +## ## You can safely delete this file if you do not need it. echo "==[ Before Hook Called ]==" inspect_args \ No newline at end of file diff --git a/examples/render-mandoc/docs/download.1 b/examples/render-mandoc/docs/download.1 index 3303044c..94d4cf11 100644 --- a/examples/render-mandoc/docs/download.1 +++ b/examples/render-mandoc/docs/download.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 3.2 .\" -.TH "download" "1" "August 2025" "Version 0.1.0" "Sample application" +.TH "download" "1" "January 2026" "Version 0.1.0" "Sample application" .SH NAME \f[B]download\f[R] \- Sample application .SH SYNOPSIS diff --git a/examples/render-mandoc/docs/download.md b/examples/render-mandoc/docs/download.md index b4f08c4a..77792c34 100644 --- a/examples/render-mandoc/docs/download.md +++ b/examples/render-mandoc/docs/download.md @@ -1,6 +1,6 @@ % download(1) Version 0.1.0 | Sample application % Lana Lang -% August 2025 +% January 2026 NAME ================================================== diff --git a/spec/approvals/examples/render-mandoc b/spec/approvals/examples/render-mandoc index fca583fc..f09a7397 100644 --- a/spec/approvals/examples/render-mandoc +++ b/spec/approvals/examples/render-mandoc @@ -44,4 +44,4 @@ ISSUE TRACKER AUTHORS Lana Lang. -Version 0.1.0 August 2025 download(1) +