Skip to content

Commit 09e91ee

Browse files
committed
- Add tester CLI command
1 parent 986f4d1 commit 09e91ee

File tree

11 files changed

+195
-1
lines changed

11 files changed

+195
-1
lines changed

lib/completely/cli.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'completely/commands/init'
33
require 'completely/commands/preview'
44
require 'completely/commands/generate'
5+
require 'completely/commands/test'
56

67
module Completely
78
class CLI
@@ -13,6 +14,7 @@ def self.runner
1314
runner.route 'init', to: Commands::Init
1415
runner.route 'preview', to: Commands::Preview
1516
runner.route 'generate', to: Commands::Generate
17+
runner.route 'test', to: Commands::Test
1618

1719
runner
1820
end

lib/completely/commands/preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Preview < Base
88
usage "completely preview [CONFIG_PATH --function NAME]"
99
usage "completely preview (-h|--help)"
1010

11-
function_usage
11+
function_usage
1212
config_path_usage
1313

1414
def run

lib/completely/commands/test.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'completely/commands/base'
2+
3+
module Completely
4+
module Commands
5+
class Test < Base
6+
summary "Test completions"
7+
8+
help <<~EOF
9+
This command can be used to test that any completion script (either generated by compeltely or not) responds with the right completions.
10+
11+
In order to test on a completely configuration file other than 'completely.yaml', set the COMPLETELY_CONFIG_PATH environemnt variable.
12+
13+
In order to test on an arbitrary completions script, set the COMPLETELY_SCRIPT_PATH and COMPLETELY_SCRIPT_FUNCTION environment variables.
14+
EOF
15+
16+
usage "completely test COMPLINE"
17+
usage "completely test (-h|--help)"
18+
19+
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."
20+
21+
environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
22+
environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file"
23+
environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script"
24+
25+
example %q[completely test "mygit pu"]
26+
example %q[completely test "mygit pull "]
27+
example <<~EOF
28+
COMPLETELY_SCRIPT_PATH=/usr/share/bash-completion/completions/git \\
29+
COMPLETELY_SCRIPT_FUNCTION=_git \\
30+
completely test "git pu"
31+
EOF
32+
33+
attr_reader :config, :script_path, :script_function
34+
35+
def run
36+
set_vars
37+
puts tester.test(compline).join "\n"
38+
end
39+
40+
private
41+
42+
def compline
43+
args['COMPLINE']
44+
end
45+
46+
def tester
47+
if config
48+
completions = Completions.load config
49+
completions.tester
50+
else
51+
Tester.new script: File.read(script_path), function_name: ENV['COMPLETELY_SCRIPT_FUNCTION']
52+
end
53+
end
54+
55+
def set_vars
56+
if ENV['COMPLETELY_CONFIG_PATH']
57+
@config = ENV['COMPLETELY_CONFIG_PATH']
58+
elsif ENV['COMPLETELY_SCRIPT_PATH'] and ENV['COMPLETELY_SCRIPT_FUNCTION']
59+
@script_path = ENV['COMPLETELY_SCRIPT_PATH']
60+
@script_function = ENV['COMPLETELY_SCRIPT_FUNCTION']
61+
elsif File.exist? 'completely.yaml'
62+
@config = 'completely.yaml'
63+
else
64+
raise "Please set the proper environment variables or run in a folder with completely.yaml"
65+
end
66+
end
67+
end
68+
end
69+
end

spec/approvals/cli/commands

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Commands:
44
init Create a new sample YAML configuration file
55
preview Generate the bash completion script to STDOUT
66
generate Generate the bash completion script to a file
7+
test Test completions
78

89
Run completely COMMAND --help for more information
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
command
2+
conquer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--help
2+
--version

spec/approvals/cli/test/comps-git

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pull
2+
push

spec/approvals/cli/test/error

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<RuntimeError: Please set the proper environment variables or run in a folder with completely.yaml>

spec/approvals/cli/test/help

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Test completions
2+
3+
This command can be used to test that any completion script (either generated by
4+
compeltely or not) responds with the right completions.
5+
6+
In order to test on a completely configuration file other than
7+
'completely.yaml', set the COMPLETELY_CONFIG_PATH environemnt variable.
8+
9+
In order to test on an arbitrary completions script, set the
10+
COMPLETELY_SCRIPT_PATH and COMPLETELY_SCRIPT_FUNCTION environment variables.
11+
12+
Usage:
13+
completely test COMPLINE
14+
completely test (-h|--help)
15+
16+
Options:
17+
-h --help
18+
Show this help
19+
20+
Parameters:
21+
COMPLINE
22+
The command to test completions for. This will be handled as if a TAB was
23+
pressed immediately at the end of it, so the last word is considered the
24+
active cursor. If you wish to complete for the next word instead, end your
25+
command with a space.
26+
27+
Environment Variables:
28+
COMPLETELY_CONFIG_PATH
29+
Path to a completely configuration file [default: completely.yaml]
30+
31+
COMPLETELY_SCRIPT_PATH
32+
Path to a completions script. When set, this script will be tested instead
33+
of the completely configuration file
34+
35+
COMPLETELY_SCRIPT_FUNCTION
36+
The main completion function to call when using a custom script
37+
38+
Examples:
39+
completely test "mygit pu"
40+
completely test "mygit pull "
41+
COMPLETELY_SCRIPT_PATH=/usr/share/bash-completion/completions/git \
42+
COMPLETELY_SCRIPT_FUNCTION=_git \
43+
completely test "git pu"

spec/approvals/cli/test/usage

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Usage:
2+
completely test COMPLINE
3+
completely test (-h|--help)

0 commit comments

Comments
 (0)