Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test command output and allow multiple complines in one run #41

Merged
merged 1 commit into from
Nov 29, 2022
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
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--require spec_helper
--color
--format documentation
--fail-fast
6 changes: 0 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,3 @@ AllCops:
Naming/AccessorMethodName:
Exclude:
- 'lib/completely/tester.rb'

# Allow long lines in specs and commands
Layout/LineLength:
Exclude:
- 'spec/**/*'
- 'lib/completely/commands/**/*'
15 changes: 11 additions & 4 deletions lib/completely/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ module Commands
class Base < MisterBin::Command
class << self
def param_config_path
param 'CONFIG_PATH', "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
param 'CONFIG_PATH', <<~USAGE
Path to the YAML configuration file [default: completely.yaml].
Can also be set by an environment variable.
USAGE
end

def option_function
option '-f --function NAME', 'Modify the name of the function in the generated script'
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]'
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'
environment 'COMPLETELY_DEBUG', 'If 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

Expand Down
11 changes: 8 additions & 3 deletions lib/completely/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ class Generate < Base
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'
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\nCan also be set by an environment variable"
param 'OUTPUT_PATH', <<~USAGE
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.
USAGE

environment_config_path
environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script'
environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script.'
environment_debug

def run
Expand Down
49 changes: 34 additions & 15 deletions lib/completely/commands/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,55 @@ module Commands
class Test < Base
summary 'Test completions'

help 'This command can be used to test that your completions script responds with the right completions. It works by reading your completely.yaml file, generating a completions script, and generating a temporary testing script.'
help 'This command can be used to test that your completions script responds with ' \
'the right completions. It works by reading your completely.yaml file, generating ' \
'a completions script, and generating a temporary testing script.'

usage 'completely test [--keep] COMPLINE'
usage 'completely test [--keep] COMPLINE...'
usage 'completely test (-h|--help)'

option '-k --keep', 'Keep the temporary testing script in the current directory'
option '-k --keep', 'Keep the temporary testing script in the current directory.'

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.'
param 'COMPLINE', 'One or more commands 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_debug

example 'completely test "mygit pu"'
example 'completely test "mygit pull "'
example 'completely test "mygit "'
example 'completely test --keep "mygit status "'
example 'completely test "mygit status --" "mygit init "'

def run
puts tester.test(compline).join "\n"

if args['--keep']
File.write 'completely-tester.sh', tester_script
puts 'saved completely-tester.sh'
complines.each_with_index do |compline, i|
show_compline compline, filename: "completely-tester-#{i + 1}.sh"
end

syntax_warning unless completions.valid?
end

private

def compline
args['COMPLINE']
def show_compline(compline, filename: nil)
filename ||= 'completely-tester.sh'
say "!txtblu!$ !txtgrn!#{compline}!txtrst!<tab>"
puts tester.test(compline).join "\n"
puts

return unless keep

File.write filename, tester_script(compline)
say "Saved !txtpur!#{filename}"
end

def complines
@complines ||= args['COMPLINE']
end

def keep
@keep ||= args['--keep']
end

def completions
Expand All @@ -45,8 +64,8 @@ def tester
@tester ||= completions.tester
end

def tester_script
@tester_script ||= tester.tester_script compline
def tester_script(compline)
tester.tester_script compline
end
end
end
Expand Down
23 changes: 12 additions & 11 deletions spec/approvals/cli/generate/help
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@ Usage:

Options:
-f --function NAME
Modify the name of the function in the generated script
Modify the name of the function in the generated script.

-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
useful if you wish to embed it directly in your script.

-h --help
Show this help

Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
Can also be set by an environment variable
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
Can also be set by an environment variable
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]
Path to a completely configuration file [default: completely.yaml].

COMPLETELY_OUTPUT_PATH
Path to the output bash script
Path to the output bash script.

COMPLETELY_DEBUG
It not empty, the generated script will include an additional debugging
If 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
completion is requested.
6 changes: 3 additions & 3 deletions spec/approvals/cli/init/help
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Options:

Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
Can also be set by an environment variable
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]
Path to a completely configuration file [default: completely.yaml].
12 changes: 6 additions & 6 deletions spec/approvals/cli/preview/help
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ Usage:

Options:
-f --function NAME
Modify the name of the function in the generated script
Modify the name of the function in the generated script.

-h --help
Show this help

Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
Can also be set by an environment variable
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]
Path to a completely configuration file [default: completely.yaml].

COMPLETELY_DEBUG
It not empty, the generated script will include an additional debugging
If 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
completion is requested.
73 changes: 73 additions & 0 deletions spec/approvals/cli/test/completely-tester-1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
if [[ -n $ZSH_VERSION ]]; then
autoload -U +X bashcompinit && bashcompinit
autoload -U +X compinit && compinit
fi

# === COMPLETION SCRIPT START ===

# mygit completion -*- shell-script -*-

# This bash completions script was generated by
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_mygit_completions_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

_mygit_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${compwords[*]}"

case "$compline" in
'status'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
;;

'commit'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )
;;

'init'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )
;;

*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )
;;

esac
} &&
complete -F _mygit_completions mygit

# ex: filetype=sh


# === COMPLETION SCRIPT END ===

COMP_WORDS=( mygit -- )
COMP_LINE="mygit --"
COMP_POINT=${#COMP_LINE}
COMP_CWORD=1

_mygit_completions
for suggestion in "${COMPREPLY[@]}"; do
echo "$suggestion"
done

73 changes: 73 additions & 0 deletions spec/approvals/cli/test/completely-tester-2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
if [[ -n $ZSH_VERSION ]]; then
autoload -U +X bashcompinit && bashcompinit
autoload -U +X compinit && compinit
fi

# === COMPLETION SCRIPT START ===

# mygit completion -*- shell-script -*-

# This bash completions script was generated by
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_mygit_completions_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

_mygit_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${compwords[*]}"

case "$compline" in
'status'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
;;

'commit'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )
;;

'init'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )
;;

*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )
;;

esac
} &&
complete -F _mygit_completions mygit

# ex: filetype=sh


# === COMPLETION SCRIPT END ===

COMP_WORDS=( mygit st )
COMP_LINE="mygit st"
COMP_POINT=${#COMP_LINE}
COMP_CWORD=1

_mygit_completions
for suggestion in "${COMPREPLY[@]}"; do
echo "$suggestion"
done

2 changes: 2 additions & 0 deletions spec/approvals/cli/test/comps-custom-config
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
$ play co<tab>
command
conquer

2 changes: 2 additions & 0 deletions spec/approvals/cli/test/comps-default
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
$ mygit --<tab>
--help
--version

Loading