Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/completely/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/completely/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 2 additions & 29 deletions lib/completely/commands/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand All @@ -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
7 changes: 2 additions & 5 deletions lib/completely/templates/template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/completely/templates/tester-template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 3 additions & 3 deletions spec/approvals/cli/generate/help
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions spec/approvals/cli/generated-script
Original file line number Diff line number Diff line change
Expand Up @@ -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'*)
Expand Down
7 changes: 2 additions & 5 deletions spec/approvals/cli/generated-script-alt
Original file line number Diff line number Diff line change
Expand Up @@ -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'*)
Expand Down
7 changes: 2 additions & 5 deletions spec/approvals/cli/generated-wrapped-script
Original file line number Diff line number Diff line change
Expand Up @@ -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\'*)'
Expand Down
2 changes: 1 addition & 1 deletion spec/approvals/cli/test/error
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#<RuntimeError: Please set the proper environment variables or run in a folder with completely.yaml>
#<Errno::ENOENT: No such file or directory @ rb_sysopen - completely.yaml>
14 changes: 0 additions & 14 deletions spec/approvals/cli/test/help
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 --"
7 changes: 2 additions & 5 deletions spec/approvals/completions/function
Original file line number Diff line number Diff line change
Expand Up @@ -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\'*)'
Expand Down
7 changes: 2 additions & 5 deletions spec/approvals/completions/script
Original file line number Diff line number Diff line change
Expand Up @@ -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'*)
Expand Down
7 changes: 2 additions & 5 deletions spec/approvals/completions/script-only-spaces
Original file line number Diff line number Diff line change
Expand Up @@ -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'*)
Expand Down
7 changes: 2 additions & 5 deletions spec/approvals/completions/script-with-debug
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 0 additions & 1 deletion spec/approvals/tester/script
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 0 additions & 1 deletion spec/approvals/tester/script_path
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source "<path removed>"

# END OF COMPLETION SCRIPT

source /usr/share/bash-completion/bash_completion
COMP_WORDS=( cli co )
COMP_LINE="cli co"
COMP_POINT=${#COMP_LINE}
Expand Down
8 changes: 4 additions & 4 deletions spec/completely/commands/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 0 additions & 14 deletions spec/completely/commands/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }

Expand Down
8 changes: 7 additions & 1 deletion spec/completely/tester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand Down
Loading