From 8ef65e1a67003d5e21faaf002e820d380c50b7a6 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sat, 21 May 2022 06:55:21 +0000 Subject: [PATCH 1/4] - Allow setting the CONFIG_PATH argument via the COMPLETELY_CONFIG_PATH environment variable --- lib/completely/commands/base.rb | 12 +++++++---- lib/completely/commands/generate.rb | 10 ++++----- lib/completely/commands/init.rb | 2 +- lib/completely/commands/preview.rb | 6 +++--- lib/completely/commands/test.rb | 2 +- lib/completely/templates/template.erb | 6 +++++- spec/approvals/cli/generate/help | 3 ++- spec/approvals/cli/generated-script | 21 +++++++++++++++---- spec/approvals/cli/generated-script-alt | 21 +++++++++++++++---- spec/approvals/cli/generated-wrapped-script | 21 +++++++++++++++---- spec/approvals/completions/function | 16 +++++++++++--- spec/approvals/completions/script | 16 +++++++++++--- spec/approvals/completions/script-only-spaces | 11 ++++++++-- spec/approvals/completions/script-with-debug | 1 + 14 files changed, 112 insertions(+), 36 deletions(-) diff --git a/lib/completely/commands/base.rb b/lib/completely/commands/base.rb index a17fd45..aa0d844 100644 --- a/lib/completely/commands/base.rb +++ b/lib/completely/commands/base.rb @@ -5,15 +5,15 @@ module Commands class Base < MisterBin::Command class << self - def config_path_usage + def param_config_path param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]" end - def function_usage + def option_function option "-f --function NAME", "Modify the name of the function in the generated script" end - def debug_usage + def environment_debug environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested" end end @@ -29,7 +29,11 @@ def completions end def config_path - args['CONFIG_PATH'] || 'completely.yaml' + args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml' + end + + def config_basename + File.basename config_path, File.extname(config_path) end def syntax_warning diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 5bb49ac..3d635aa 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -8,12 +8,12 @@ class Generate < Base usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]" usage "completely generate (-h|--help)" - function_usage + option_function option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script" - config_path_usage - param "OUTPUT_PATH", "Path to the output bash script [default: completely.bash]" - debug_usage + param_config_path + param "OUTPUT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension" + environment_debug def run wrap = args['--wrap'] @@ -30,7 +30,7 @@ def wrapper_function(wrapper_name) end def output_path - @output_path ||= args['OUTPUT_PATH'] || "completely.bash" + @output_path ||= args['OUTPUT_PATH'] || "#{config_basename}.bash" end end diff --git a/lib/completely/commands/init.rb b/lib/completely/commands/init.rb index 277dde5..9b080e4 100644 --- a/lib/completely/commands/init.rb +++ b/lib/completely/commands/init.rb @@ -8,7 +8,7 @@ class Init < Base usage "completely init [CONFIG_PATH]" usage "completely init (-h|--help)" - config_path_usage + param_config_path def run if File.exist? config_path diff --git a/lib/completely/commands/preview.rb b/lib/completely/commands/preview.rb index 127f453..4bd334b 100644 --- a/lib/completely/commands/preview.rb +++ b/lib/completely/commands/preview.rb @@ -8,9 +8,9 @@ class Preview < Base usage "completely preview [CONFIG_PATH --function NAME]" usage "completely preview (-h|--help)" - function_usage - config_path_usage - debug_usage + option_function + param_config_path + environment_debug def run puts script diff --git a/lib/completely/commands/test.rb b/lib/completely/commands/test.rb index 98c8fbc..3805c16 100644 --- a/lib/completely/commands/test.rb +++ b/lib/completely/commands/test.rb @@ -21,7 +21,7 @@ class Test < Base environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file" environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script. If not set, the basename of the script path will be used, prefixed by an underscore" - debug_usage + environment_debug example %q[completely test "mygit pu"] example %q[completely test "mygit pull "] diff --git a/lib/completely/templates/template.erb b/lib/completely/templates/template.erb index 4e0accf..f8c6ec2 100644 --- a/lib/completely/templates/template.erb +++ b/lib/completely/templates/template.erb @@ -7,6 +7,7 @@ <%= function_name %>() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" @@ -20,7 +21,10 @@ case "$compline" in % patterns.each do |pattern| % next if pattern.empty? - <%= pattern.case_string %>) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;; + <%= pattern.case_string %>) + COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) + ;; + % end esac } && diff --git a/spec/approvals/cli/generate/help b/spec/approvals/cli/generate/help index 472b766..afd2989 100644 --- a/spec/approvals/cli/generate/help +++ b/spec/approvals/cli/generate/help @@ -20,7 +20,8 @@ Parameters: Path to the YAML configuration file [default: completely.yaml] OUTPUT_PATH - Path to the output bash script [default: completely.bash] + Path to the output bash script. When not provided, the name of the input + file will be used with a .bash extension Environment Variables: COMPLETELY_DEBUG diff --git a/spec/approvals/cli/generated-script b/spec/approvals/cli/generated-script index 9949d20..5b880e0 100644 --- a/spec/approvals/cli/generated-script +++ b/spec/approvals/cli/generated-script @@ -7,14 +7,27 @@ _mygit_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;; - 'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;; - *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;; + 'status'*) + COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + ;; + + 'commit'*) + COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + ;; + esac } && complete -F _mygit_completions mygit diff --git a/spec/approvals/cli/generated-script-alt b/spec/approvals/cli/generated-script-alt index 8f83859..1a4866b 100644 --- a/spec/approvals/cli/generated-script-alt +++ b/spec/approvals/cli/generated-script-alt @@ -7,14 +7,27 @@ _mycomps() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;; - 'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;; - *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;; + 'status'*) + COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + ;; + + 'commit'*) + COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + ;; + esac } && complete -F _mycomps mygit diff --git a/spec/approvals/cli/generated-wrapped-script b/spec/approvals/cli/generated-wrapped-script index 87db078..ffd1c2a 100644 --- a/spec/approvals/cli/generated-wrapped-script +++ b/spec/approvals/cli/generated-wrapped-script @@ -8,14 +8,27 @@ give_comps() { echo $'_mygit_completions() {' echo $' local cur compline' echo $' _init_completion -s || return' + echo $'' echo $' cur=${COMP_WORDS[COMP_CWORD]}' echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"' echo $'' echo $' case "$compline" in' - echo $' \'status\'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;' - echo $' \'commit\'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;' - echo $' \'init\'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;' - echo $' *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;' + echo $' \'status\'*)' + echo $' COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' \'commit\'*)' + echo $' COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' \'init\'*)' + echo $' COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' *)' + echo $' COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))' + echo $' ;;' + echo $'' echo $' esac' echo $'} &&' echo $'complete -F _mygit_completions mygit' diff --git a/spec/approvals/completions/function b/spec/approvals/completions/function index 8880eff..9bad5f3 100644 --- a/spec/approvals/completions/function +++ b/spec/approvals/completions/function @@ -8,13 +8,23 @@ send_completions() { echo $'_completely_completions() {' echo $' local cur compline' echo $' _init_completion -s || return' + echo $'' echo $' cur=${COMP_WORDS[COMP_CWORD]}' echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"' echo $'' echo $' case "$compline" in' - echo $' \'generate\'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;' - echo $' \'init\'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;' - echo $' *) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;' + echo $' \'generate\'*)' + echo $' COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' \'init\'*)' + echo $' COMPREPLY=($(compgen -W "--help" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' *)' + echo $' COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur"))' + echo $' ;;' + echo $'' echo $' esac' echo $'} &&' echo $'complete -F _completely_completions completely' diff --git a/spec/approvals/completions/script b/spec/approvals/completions/script index 841c744..ec156c2 100644 --- a/spec/approvals/completions/script +++ b/spec/approvals/completions/script @@ -7,13 +7,23 @@ _completely_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; - *) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;; + 'generate'*) + COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -W "--help" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) + ;; + esac } && complete -F _completely_completions completely diff --git a/spec/approvals/completions/script-only-spaces b/spec/approvals/completions/script-only-spaces index ecdf919..a7b1dfb 100644 --- a/spec/approvals/completions/script-only-spaces +++ b/spec/approvals/completions/script-only-spaces @@ -7,12 +7,19 @@ _completely_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; + 'generate'*) + COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -W "--help" -- "$cur")) + ;; + esac } && complete -F _completely_completions completely diff --git a/spec/approvals/completions/script-with-debug b/spec/approvals/completions/script-with-debug index e5741a9..045ab32 100644 --- a/spec/approvals/completions/script-with-debug +++ b/spec/approvals/completions/script-with-debug @@ -7,6 +7,7 @@ _completely_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" From 531d4d4b18dc02446cc295d9967ca01f1ccbb71e Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sat, 21 May 2022 07:10:23 +0000 Subject: [PATCH 2/4] add specs for COMPLETELY_CONFIG_PATH --- lib/completely/commands/base.rb | 6 +++++- lib/completely/commands/generate.rb | 2 ++ lib/completely/commands/init.rb | 1 + lib/completely/commands/preview.rb | 1 + lib/completely/commands/test.rb | 2 +- spec/approvals/cli/generate/custom-path-env | 1 + spec/approvals/cli/generate/help | 4 ++++ spec/approvals/cli/init/custom-path-env | 1 + spec/approvals/cli/init/help | 5 +++++ spec/approvals/cli/preview/help | 4 ++++ spec/completely/commands/generate_spec.rb | 20 ++++++++++++++++++++ spec/completely/commands/init_spec.rb | 15 +++++++++++++++ spec/completely/commands/preview_spec.rb | 16 ++++++++++++++++ spec/completely/completions_spec.rb | 4 ++-- 14 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 spec/approvals/cli/generate/custom-path-env create mode 100644 spec/approvals/cli/init/custom-path-env diff --git a/lib/completely/commands/base.rb b/lib/completely/commands/base.rb index aa0d844..55f558a 100644 --- a/lib/completely/commands/base.rb +++ b/lib/completely/commands/base.rb @@ -6,13 +6,17 @@ class Base < MisterBin::Command class << self def param_config_path - param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]" + param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable" end def option_function option "-f --function NAME", "Modify the name of the function in the generated script" end + def environment_config_path + environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" + end + def environment_debug environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested" end diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 3d635aa..50a8ea3 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -13,6 +13,8 @@ class Generate < Base param_config_path param "OUTPUT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension" + + environment_config_path environment_debug def run diff --git a/lib/completely/commands/init.rb b/lib/completely/commands/init.rb index 9b080e4..4116ab2 100644 --- a/lib/completely/commands/init.rb +++ b/lib/completely/commands/init.rb @@ -9,6 +9,7 @@ class Init < Base usage "completely init (-h|--help)" param_config_path + environment_config_path def run if File.exist? config_path diff --git a/lib/completely/commands/preview.rb b/lib/completely/commands/preview.rb index 4bd334b..f4b0123 100644 --- a/lib/completely/commands/preview.rb +++ b/lib/completely/commands/preview.rb @@ -10,6 +10,7 @@ class Preview < Base option_function param_config_path + environment_config_path environment_debug def run diff --git a/lib/completely/commands/test.rb b/lib/completely/commands/test.rb index 3805c16..26ed8f6 100644 --- a/lib/completely/commands/test.rb +++ b/lib/completely/commands/test.rb @@ -18,7 +18,7 @@ class Test < Base param "COMPLINE", "The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space." - environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" + environment_config_path environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file" environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script. If not set, the basename of the script path will be used, prefixed by an underscore" environment_debug diff --git a/spec/approvals/cli/generate/custom-path-env b/spec/approvals/cli/generate/custom-path-env new file mode 100644 index 0000000..050d17b --- /dev/null +++ b/spec/approvals/cli/generate/custom-path-env @@ -0,0 +1 @@ +Saved hello.bash diff --git a/spec/approvals/cli/generate/help b/spec/approvals/cli/generate/help index afd2989..2939bbf 100644 --- a/spec/approvals/cli/generate/help +++ b/spec/approvals/cli/generate/help @@ -18,12 +18,16 @@ Options: Parameters: CONFIG_PATH Path to the YAML configuration file [default: completely.yaml] + Can also be set by an environment variable OUTPUT_PATH Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension Environment Variables: + COMPLETELY_CONFIG_PATH + Path to a completely configuration file [default: completely.yaml] + COMPLETELY_DEBUG It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a diff --git a/spec/approvals/cli/init/custom-path-env b/spec/approvals/cli/init/custom-path-env new file mode 100644 index 0000000..a0bec5d --- /dev/null +++ b/spec/approvals/cli/init/custom-path-env @@ -0,0 +1 @@ +Saved spec/tmp/hello.yml diff --git a/spec/approvals/cli/init/help b/spec/approvals/cli/init/help index 06c0db8..5665f0a 100644 --- a/spec/approvals/cli/init/help +++ b/spec/approvals/cli/init/help @@ -11,3 +11,8 @@ Options: Parameters: CONFIG_PATH Path to the YAML configuration file [default: completely.yaml] + Can also be set by an environment variable + +Environment Variables: + COMPLETELY_CONFIG_PATH + Path to a completely configuration file [default: completely.yaml] diff --git a/spec/approvals/cli/preview/help b/spec/approvals/cli/preview/help index 0f226ea..b7f3024 100644 --- a/spec/approvals/cli/preview/help +++ b/spec/approvals/cli/preview/help @@ -14,8 +14,12 @@ Options: Parameters: CONFIG_PATH Path to the YAML configuration file [default: completely.yaml] + Can also be set by an environment variable Environment Variables: + COMPLETELY_CONFIG_PATH + Path to a completely configuration file [default: completely.yaml] + COMPLETELY_DEBUG It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index ef7ceab..0f86a63 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -25,6 +25,26 @@ end end + context "with COMPLETELY_CONFIG_PATH env var" do + before do + reset_tmp_dir + system "cp lib/completely/templates/sample.yaml spec/tmp/hello.yml" + system "rm -f completely.yaml" + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' + end + + after do + ENV['COMPLETELY_CONFIG_PATH'] = nil + system "rm -f hello.bash" + end + + it "generates the bash script to completely.bash" do + expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env') + expect(File.read "hello.bash").to match_approval('cli/generated-script') + end + end + + context "with CONFIG_PATH OUTPUT_PATH" do before { reset_tmp_dir } diff --git a/spec/completely/commands/init_spec.rb b/spec/completely/commands/init_spec.rb index b3b24d6..983082c 100644 --- a/spec/completely/commands/init_spec.rb +++ b/spec/completely/commands/init_spec.rb @@ -29,6 +29,21 @@ end end + context "with COMPLETELY_CONFIG_PATH env var" do + before do + reset_tmp_dir + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' + end + + after { ENV['COMPLETELY_CONFIG_PATH'] = nil } + + it "creates a new sample file with the requested name" do + expect { subject.run %w[init] } + .to output_approval('cli/init/custom-path-env') + expect(File.read 'spec/tmp/hello.yml').to eq sample + end + end + context "when the config file already exists" do before { system "cp lib/completely/templates/sample.yaml completely.yaml" } after { system "rm -f completely.yaml" } diff --git a/spec/completely/commands/preview_spec.rb b/spec/completely/commands/preview_spec.rb index 771cd21..60bc8f5 100644 --- a/spec/completely/commands/preview_spec.rb +++ b/spec/completely/commands/preview_spec.rb @@ -25,6 +25,22 @@ end end + context "with COMPLETELY_CONFIG_PATH env var" do + before do + reset_tmp_dir + system "cp lib/completely/templates/sample.yaml spec/tmp/hello.yml" + system "rm -f completely.yaml" + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' + end + + after { ENV['COMPLETELY_CONFIG_PATH'] = nil } + + it "outputs the generated script to STDOUT" do + expect { subject.run %w[preview] } + .to output_approval('cli/generated-script') + end + end + context "with an invalid configuration" do it "outputs a warning to STDERR" do expect { subject.run %w[preview spec/fixtures/broken.yaml] } diff --git a/spec/completely/completions_spec.rb b/spec/completely/completions_spec.rb index 0f9673f..0044cb6 100644 --- a/spec/completely/completions_spec.rb +++ b/spec/completely/completions_spec.rb @@ -41,7 +41,7 @@ end end - context "when COMPLETELY_DEBUG is set", :focus do + context "when COMPLETELY_DEBUG is set" do before { ENV['COMPLETELY_DEBUG'] = '1' } after { ENV['COMPLETELY_DEBUG'] = nil } @@ -59,7 +59,7 @@ end end - describe '#tester', :focus do + describe '#tester' do it "returns a Tester object" do expect(subject.tester).to be_a Tester end From a484ff4b4bf828629270061549c8dee123aab016 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sat, 21 May 2022 07:24:19 +0000 Subject: [PATCH 3/4] - Allow setting the SCRIPT_PATH argument via the COMPLETELY_SCRIPT_PATH environment variable --- lib/completely/commands/base.rb | 6 +++++- lib/completely/commands/generate.rb | 13 +++++-------- spec/approvals/cli/generate/help | 8 ++++++-- spec/completely/commands/generate_spec.rb | 3 +-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/completely/commands/base.rb b/lib/completely/commands/base.rb index 55f558a..69f1139 100644 --- a/lib/completely/commands/base.rb +++ b/lib/completely/commands/base.rb @@ -33,7 +33,11 @@ def completions end def config_path - args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml' + @config_path ||= args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml' + end + + def script_path + @script_path ||= args['SCRIPT_PATH'] || ENV['COMPLETELY_SCRIPT_PATH'] || "#{config_basename}.bash" end def config_basename diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 50a8ea3..088f9cd 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -5,23 +5,24 @@ module Commands class Generate < Base help "Generate the bash completion script to a file" - usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]" + usage "completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]" usage "completely generate (-h|--help)" option_function option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script" param_config_path - param "OUTPUT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension" + param "SCRIPT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable" environment_config_path + environment "COMPLETELY_SCRIPT_PATH", "Path to the output bash script" environment_debug def run wrap = args['--wrap'] output = wrap ? wrapper_function(wrap) : script - File.write output_path, output - say "Saved !txtpur!#{output_path}" + File.write script_path, output + say "Saved !txtpur!#{script_path}" syntax_warning unless completions.valid? end @@ -31,10 +32,6 @@ def wrapper_function(wrapper_name) completions.wrapper_function wrapper_name end - def output_path - @output_path ||= args['OUTPUT_PATH'] || "#{config_basename}.bash" - end - end end end diff --git a/spec/approvals/cli/generate/help b/spec/approvals/cli/generate/help index 2939bbf..ccd8ac2 100644 --- a/spec/approvals/cli/generate/help +++ b/spec/approvals/cli/generate/help @@ -1,7 +1,7 @@ Generate the bash completion script to a file Usage: - completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME] + completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME] completely generate (-h|--help) Options: @@ -20,14 +20,18 @@ Parameters: Path to the YAML configuration file [default: completely.yaml] Can also be set by an environment variable - OUTPUT_PATH + SCRIPT_PATH Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension + Can also be set by an environment variable Environment Variables: COMPLETELY_CONFIG_PATH Path to a completely configuration file [default: completely.yaml] + COMPLETELY_SCRIPT_PATH + Path to the output bash script + COMPLETELY_DEBUG It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index 0f86a63..ecde134 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -44,8 +44,7 @@ end end - - context "with CONFIG_PATH OUTPUT_PATH" do + context "with CONFIG_PATH SCRIPT_PATH" do before { reset_tmp_dir } it "generates the bash script to the specified path" do From daf27bef3affcf5ea2bb8ab508d99625bb0eb9d3 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sat, 21 May 2022 07:27:31 +0000 Subject: [PATCH 4/4] add specs for COMPLETELY_SCRIPT_PATH --- spec/approvals/cli/generate/custom-path-env2 | 1 + spec/completely/commands/generate_spec.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 spec/approvals/cli/generate/custom-path-env2 diff --git a/spec/approvals/cli/generate/custom-path-env2 b/spec/approvals/cli/generate/custom-path-env2 new file mode 100644 index 0000000..2acdbda --- /dev/null +++ b/spec/approvals/cli/generate/custom-path-env2 @@ -0,0 +1 @@ +Saved spec/tmp/tada.bash diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index ecde134..dfa4bc3 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -38,12 +38,30 @@ system "rm -f hello.bash" end - it "generates the bash script to completely.bash" do + it "generates the bash script to hello.bash" do expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env') expect(File.read "hello.bash").to match_approval('cli/generated-script') end end + context "with COMPLETELY_SCRIPT_PATH env var" do + let(:outfile) { 'spec/tmp/tada.bash' } + + before do + reset_tmp_dir + ENV['COMPLETELY_SCRIPT_PATH'] = outfile + end + + after do + ENV['COMPLETELY_SCRIPT_PATH'] = nil + end + + it "generates the bash script to the requested path" do + expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env2') + expect(File.read outfile).to match_approval('cli/generated-script') + end + end + context "with CONFIG_PATH SCRIPT_PATH" do before { reset_tmp_dir }