diff --git a/README.md b/README.md index 6d9343d..ce4a13c 100644 --- a/README.md +++ b/README.md @@ -217,8 +217,7 @@ these (whichever exists): You can use the built in completions script tester by running `completely test`. -This command lets you test any completions script, whether it was generated by -completely or not. +This command lets you test completions for your completions script. In addition, you can set the `COMPLETELY_DEBUG` environment variable to any value in order to generate scripts with some additional debugging functionality. Run diff --git a/lib/completely/commands/base.rb b/lib/completely/commands/base.rb index 69f1139..425e2d4 100644 --- a/lib/completely/commands/base.rb +++ b/lib/completely/commands/base.rb @@ -36,8 +36,8 @@ def config_path @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" + def output_path + @output_path ||= args['OUTPUT_PATH'] || ENV['COMPLETELY_OUTPUT_PATH'] || "#{config_basename}.bash" end def config_basename diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 088f9cd..7302f08 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -5,24 +5,24 @@ module Commands class Generate < Base help "Generate the bash completion script to a file" - usage "completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]" + usage "completely generate [CONFIG_PATH OUTPUT_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 "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" + 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\nCan also be set by an environment variable" environment_config_path - environment "COMPLETELY_SCRIPT_PATH", "Path to the output bash script" + environment "COMPLETELY_OUTPUT_PATH", "Path to the output bash script" environment_debug def run wrap = args['--wrap'] output = wrap ? wrapper_function(wrap) : script - File.write script_path, output - say "Saved !txtpur!#{script_path}" + File.write output_path, output + say "Saved !txtpur!#{output_path}" syntax_warning unless completions.valid? end diff --git a/lib/completely/commands/test.rb b/lib/completely/commands/test.rb index 26ed8f6..fdfad84 100644 --- a/lib/completely/commands/test.rb +++ b/lib/completely/commands/test.rb @@ -9,8 +9,6 @@ class Test < Base This command can be used to test that any completion script (either generated by compeltely or not) responds with the right completions. In order to test on a completely configuration file other than 'completely.yaml', set the COMPLETELY_CONFIG_PATH environemnt variable. - - In order to test on an arbitrary completions script, set the COMPLETELY_SCRIPT_PATH and optionally the COMPLETELY_SCRIPT_FUNCTION environment variables. EOF usage "completely test COMPLINE" @@ -19,21 +17,12 @@ 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_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 example %q[completely test "mygit pu"] example %q[completely test "mygit pull "] - example <<~EOF - COMPLETELY_SCRIPT_PATH=/usr/share/bash-completion/completions/chown \\ - completely test "chown --" - EOF - - attr_reader :config, :script_path, :script_function def run - set_vars puts tester.test(compline).join "\n" end @@ -44,26 +33,10 @@ def compline end def tester - if config - completions = Completions.load config - completions.tester - else - Tester.new script_path: script_path, function_name: script_function - end + completions = Completions.load config_path + completions.tester end - def set_vars - if ENV['COMPLETELY_CONFIG_PATH'] - @config = ENV['COMPLETELY_CONFIG_PATH'] - elsif ENV['COMPLETELY_SCRIPT_PATH'] - @script_path = ENV['COMPLETELY_SCRIPT_PATH'] - @script_function = ENV['COMPLETELY_SCRIPT_FUNCTION'] || "_#{File.basename(script_path)}" - elsif File.exist? 'completely.yaml' - @config = 'completely.yaml' - else - raise "Please set the proper environment variables or run in a folder with completely.yaml" - end - end end end end diff --git a/lib/completely/templates/template.erb b/lib/completely/templates/template.erb index f8c6ec2..67fe0fb 100644 --- a/lib/completely/templates/template.erb +++ b/lib/completely/templates/template.erb @@ -5,11 +5,8 @@ # Modifying it manually is not recommended <%= function_name %>() { - local cur compline - _init_completion -s || return - - cur=${COMP_WORDS[COMP_CWORD]} - compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" % if ENV['COMPLETELY_DEBUG'] if [[ -n "$COMPLETELY_DEBUG" ]]; then diff --git a/lib/completely/templates/tester-template.erb b/lib/completely/templates/tester-template.erb index 805efcf..85749a5 100644 --- a/lib/completely/templates/tester-template.erb +++ b/lib/completely/templates/tester-template.erb @@ -3,7 +3,6 @@ # END OF COMPLETION SCRIPT -source /usr/share/bash-completion/bash_completion COMP_WORDS=( <%= compline %> ) COMP_LINE="<%= compline %>" COMP_POINT=${#COMP_LINE} diff --git a/spec/approvals/cli/generate/help b/spec/approvals/cli/generate/help index ccd8ac2..9a3555d 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 SCRIPT_PATH --function NAME --wrap NAME] + completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME] completely generate (-h|--help) Options: @@ -20,7 +20,7 @@ Parameters: Path to the YAML configuration file [default: completely.yaml] Can also be set by an environment variable - SCRIPT_PATH + OUTPUT_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 @@ -29,7 +29,7 @@ Environment Variables: COMPLETELY_CONFIG_PATH Path to a completely configuration file [default: completely.yaml] - COMPLETELY_SCRIPT_PATH + COMPLETELY_OUTPUT_PATH Path to the output bash script COMPLETELY_DEBUG diff --git a/spec/approvals/cli/generated-script b/spec/approvals/cli/generated-script index 5b880e0..7c5816e 100644 --- a/spec/approvals/cli/generated-script +++ b/spec/approvals/cli/generated-script @@ -5,11 +5,8 @@ # Modifying it manually is not recommended _mygit_completions() { - local cur compline - _init_completion -s || return - - cur=${COMP_WORDS[COMP_CWORD]} - compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in 'status'*) diff --git a/spec/approvals/cli/generated-script-alt b/spec/approvals/cli/generated-script-alt index 1a4866b..d1f8061 100644 --- a/spec/approvals/cli/generated-script-alt +++ b/spec/approvals/cli/generated-script-alt @@ -5,11 +5,8 @@ # Modifying it manually is not recommended _mycomps() { - local cur compline - _init_completion -s || return - - cur=${COMP_WORDS[COMP_CWORD]} - compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in 'status'*) diff --git a/spec/approvals/cli/generated-wrapped-script b/spec/approvals/cli/generated-wrapped-script index ffd1c2a..6e66271 100644 --- a/spec/approvals/cli/generated-wrapped-script +++ b/spec/approvals/cli/generated-wrapped-script @@ -6,11 +6,8 @@ give_comps() { echo $'# Modifying it manually is not recommended' echo $'' 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 $' local cur=${COMP_WORDS[COMP_CWORD]}' + echo $' local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"' echo $'' echo $' case "$compline" in' echo $' \'status\'*)' diff --git a/spec/approvals/cli/test/error b/spec/approvals/cli/test/error index 96dc780..e4c6bf6 100644 --- a/spec/approvals/cli/test/error +++ b/spec/approvals/cli/test/error @@ -1 +1 @@ -# \ No newline at end of file +# \ No newline at end of file diff --git a/spec/approvals/cli/test/help b/spec/approvals/cli/test/help index cb00f87..c613f2a 100644 --- a/spec/approvals/cli/test/help +++ b/spec/approvals/cli/test/help @@ -6,10 +6,6 @@ compeltely or not) responds with the right completions. In order to test on a completely configuration file other than 'completely.yaml', set the COMPLETELY_CONFIG_PATH environemnt variable. -In order to test on an arbitrary completions script, set the -COMPLETELY_SCRIPT_PATH and optionally the COMPLETELY_SCRIPT_FUNCTION environment -variables. - Usage: completely test COMPLINE completely test (-h|--help) @@ -29,14 +25,6 @@ Environment Variables: COMPLETELY_CONFIG_PATH Path to a completely configuration file [default: completely.yaml] - COMPLETELY_SCRIPT_PATH - Path to a completions script. When set, this script will be tested instead - of the completely configuration file - - 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 - 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 @@ -45,5 +33,3 @@ Environment Variables: Examples: completely test "mygit pu" completely test "mygit pull " - COMPLETELY_SCRIPT_PATH=/usr/share/bash-completion/completions/chown \ - completely test "chown --" diff --git a/spec/approvals/completions/function b/spec/approvals/completions/function index 9bad5f3..be0046a 100644 --- a/spec/approvals/completions/function +++ b/spec/approvals/completions/function @@ -6,11 +6,8 @@ send_completions() { echo $'# Modifying it manually is not recommended' echo $'' 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 $' local cur=${COMP_WORDS[COMP_CWORD]}' + echo $' local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"' echo $'' echo $' case "$compline" in' echo $' \'generate\'*)' diff --git a/spec/approvals/completions/script b/spec/approvals/completions/script index ec156c2..1ff89e5 100644 --- a/spec/approvals/completions/script +++ b/spec/approvals/completions/script @@ -5,11 +5,8 @@ # Modifying it manually is not recommended _completely_completions() { - local cur compline - _init_completion -s || return - - cur=${COMP_WORDS[COMP_CWORD]} - compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in 'generate'*) diff --git a/spec/approvals/completions/script-only-spaces b/spec/approvals/completions/script-only-spaces index a7b1dfb..92a49ef 100644 --- a/spec/approvals/completions/script-only-spaces +++ b/spec/approvals/completions/script-only-spaces @@ -5,11 +5,8 @@ # Modifying it manually is not recommended _completely_completions() { - local cur compline - _init_completion -s || return - - cur=${COMP_WORDS[COMP_CWORD]} - compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in 'generate'*) diff --git a/spec/approvals/completions/script-with-debug b/spec/approvals/completions/script-with-debug index 045ab32..017f691 100644 --- a/spec/approvals/completions/script-with-debug +++ b/spec/approvals/completions/script-with-debug @@ -5,11 +5,8 @@ # Modifying it manually is not recommended _completely_completions() { - local cur compline - _init_completion -s || return - - cur=${COMP_WORDS[COMP_CWORD]} - compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" if [[ -n "$COMPLETELY_DEBUG" ]]; then echo "compline: '$compline'" > 'completely-debug.txt' diff --git a/spec/approvals/tester/script b/spec/approvals/tester/script index 15f445b..a1c46a9 100644 --- a/spec/approvals/tester/script +++ b/spec/approvals/tester/script @@ -3,7 +3,6 @@ # END OF COMPLETION SCRIPT -source /usr/share/bash-completion/bash_completion COMP_WORDS=( cli co ) COMP_LINE="cli co" COMP_POINT=${#COMP_LINE} diff --git a/spec/approvals/tester/script_path b/spec/approvals/tester/script_path index 79e257e..5b98250 100644 --- a/spec/approvals/tester/script_path +++ b/spec/approvals/tester/script_path @@ -3,7 +3,6 @@ source "" # END OF COMPLETION SCRIPT -source /usr/share/bash-completion/bash_completion COMP_WORDS=( cli co ) COMP_LINE="cli co" COMP_POINT=${#COMP_LINE} diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index dfa4bc3..e60c86c 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -44,16 +44,16 @@ end end - context "with COMPLETELY_SCRIPT_PATH env var" do + context "with COMPLETELY_OUTPUT_PATH env var" do let(:outfile) { 'spec/tmp/tada.bash' } before do reset_tmp_dir - ENV['COMPLETELY_SCRIPT_PATH'] = outfile + ENV['COMPLETELY_OUTPUT_PATH'] = outfile end after do - ENV['COMPLETELY_SCRIPT_PATH'] = nil + ENV['COMPLETELY_OUTPUT_PATH'] = nil end it "generates the bash script to the requested path" do @@ -62,7 +62,7 @@ end end - context "with CONFIG_PATH SCRIPT_PATH" do + context "with CONFIG_PATH OUTPUT_PATH" do before { reset_tmp_dir } it "generates the bash script to the specified path" do diff --git a/spec/completely/commands/test_spec.rb b/spec/completely/commands/test_spec.rb index 4b6fd42..5edd5f0 100644 --- a/spec/completely/commands/test_spec.rb +++ b/spec/completely/commands/test_spec.rb @@ -6,8 +6,6 @@ before do system "cp lib/completely/templates/sample.yaml completely.yaml" ENV['COMPLETELY_CONFIG_PATH'] = nil - ENV['COMPLETELY_SCRIPT_PATH'] = nil - ENV['COMPLETELY_SCRIPT_FUNCTION'] = nil end after { system "rm -f completely.yaml" } @@ -45,18 +43,6 @@ end end - context "when COMPLETELY_SCRIPT_PATH and COMPLETELY_SCRIPT_FUNCTION are set" do - before do - ENV['COMPLETELY_SCRIPT_PATH'] = "/usr/share/bash-completion/completions/apt" - ENV['COMPLETELY_SCRIPT_FUNCTION'] = "_apt" - end - - it "tests against this script" do - expect { subject.run ["test", "apt up"] } - .to output_approval('cli/test/comps-apt') - end - end - context "when there is no compeltely.yaml or any environment instructions" do before { system "rm -f completely.yaml" } diff --git a/spec/completely/tester_spec.rb b/spec/completely/tester_spec.rb index 9e532f4..d29074d 100644 --- a/spec/completely/tester_spec.rb +++ b/spec/completely/tester_spec.rb @@ -7,6 +7,12 @@ let(:fixture) { 'default' } let(:compline) { "cli co" } + before :all do + # Create an up to date fixture + comps = Completions.load "spec/fixtures/tester/default.yaml" + File.write "spec/fixtures/tester/default.bash", comps.script + end + describe "#tester_script" do it "sources the script using its absolute path" do expect(subject.tester_script compline).to match %r[source "/.*spec/fixtures/tester/default.bash"] @@ -18,7 +24,7 @@ end end - describe '#test' do + describe '#test', :focus do it "returns an array with completions" do expect(subject.test compline).to eq ["command", "conquer"] end diff --git a/spec/fixtures/tester/default.bash b/spec/fixtures/tester/default.bash index 02e3186..8a5d834 100644 --- a/spec/fixtures/tester/default.bash +++ b/spec/fixtures/tester/default.bash @@ -5,16 +5,26 @@ # Modifying it manually is not recommended _cli_completions() { - local cur comp_line - _init_completion -s || return - cur=${COMP_WORDS[COMP_CWORD]} - comp_line="${COMP_WORDS[@]:1}" - - case "$comp_line" in - 'command childcommand'*) COMPREPLY=($(compgen -W "--quiet --verbose -q -v" -- "$cur")) ;; - 'command subcommand'*) COMPREPLY=($(compgen -W "--force --quiet" -- "$cur")) ;; - 'command'*) COMPREPLY=($(compgen -W "subcommand childcommand" -- "$cur")) ;; - ''*) COMPREPLY=($(compgen -W "--help --version command conquer" -- "$cur")) ;; + local cur=${COMP_WORDS[COMP_CWORD]} + local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" + + case "$compline" in + 'command childcommand'*) + COMPREPLY=($(compgen -W "--quiet --verbose -q -v" -- "$cur")) + ;; + + 'command subcommand'*) + COMPREPLY=($(compgen -W "--force --quiet" -- "$cur")) + ;; + + 'command'*) + COMPREPLY=($(compgen -W "subcommand childcommand" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version command conquer" -- "$cur")) + ;; + esac } && complete -F _cli_completions cli diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b6c83b5..c75d815 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,11 @@ ENV['COLUMNS'] = '80' ENV['LINES'] = '30' +# Just in case the developer's environment contains these, we don't need them +ENV['COMPLETELY_DEBUG'] = nil +ENV['COMPLETELY_OUTPUT_PATH'] = nil +ENV['COMPLETELY_CONFIG_PATH'] = nil + def reset_tmp_dir system 'rm -rf spec/tmp/*' system 'mkdir -p spec/tmp'