Skip to content

Commit be76843

Browse files
authored
Merge pull request #25 from DannyBen/add/tester-keep
Allow keeping the test script with `--keep`
2 parents 18232cf + 20d9b15 commit be76843

File tree

6 files changed

+95
-16
lines changed

6 files changed

+95
-16
lines changed

lib/completely/commands/test.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ module Commands
55
class Test < Base
66
summary "Test completions"
77

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.
8+
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."
109

11-
In order to test on a completely configuration file other than 'completely.yaml', set the COMPLETELY_CONFIG_PATH environemnt variable.
12-
EOF
13-
14-
usage "completely test COMPLINE"
10+
usage "completely test [--keep] COMPLINE"
1511
usage "completely test (-h|--help)"
1612

13+
option "-k --keep", "Keep the temporary testing script in the current directory"
14+
1715
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."
1816

1917
environment_config_path
@@ -24,6 +22,10 @@ class Test < Base
2422

2523
def run
2624
puts tester.test(compline).join "\n"
25+
if args['--keep']
26+
File.write 'completely-tester.sh', tester_script
27+
puts 'saved completely-tester.sh'
28+
end
2729
end
2830

2931
private
@@ -32,9 +34,16 @@ def compline
3234
args['COMPLINE']
3335
end
3436

37+
def completions
38+
@completions ||= Completions.load config_path
39+
end
40+
3541
def tester
36-
completions = Completions.load config_path
37-
completions.tester
42+
@tester ||= completions.tester
43+
end
44+
45+
def tester_script
46+
@tester_script ||= tester.tester_script compline
3847
end
3948

4049
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
if [[ -n $ZSH_VERSION ]]; then
3+
autoload -U +X bashcompinit && bashcompinit
4+
autoload -U +X compinit && compinit
5+
fi
6+
7+
# === COMPLETION SCRIPT START ===
8+
9+
# mygit completion -*- shell-script -*-
10+
11+
# This bash completions script was generated by
12+
# completely (https://github.com/dannyben/completely)
13+
# Modifying it manually is not recommended
14+
15+
_mygit_completions() {
16+
local cur=${COMP_WORDS[COMP_CWORD]}
17+
local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
18+
19+
case "$compline" in
20+
'status'*)
21+
COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))
22+
;;
23+
24+
'commit'*)
25+
COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))
26+
;;
27+
28+
'init'*)
29+
COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))
30+
;;
31+
32+
*)
33+
COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))
34+
;;
35+
36+
esac
37+
} &&
38+
complete -F _mygit_completions mygit
39+
40+
# ex: filetype=sh
41+
42+
43+
# === COMPLETION SCRIPT END ===
44+
45+
COMP_WORDS=( mygit status -- )
46+
COMP_LINE="mygit status --"
47+
COMP_POINT=${#COMP_LINE}
48+
COMP_CWORD=2
49+
50+
_mygit_completions
51+
echo "${COMPREPLY[*]}"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--help
2+
--verbose
3+
--branch
4+
saved completely-tester.sh

spec/approvals/cli/test/help

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
Test completions
22

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.
3+
This command can be used to test that your completions script responds with the
4+
right completions. It works by reading your completely.yaml file, generating a
5+
completions script, and generating a temporary testing script.
86

97
Usage:
10-
completely test COMPLINE
8+
completely test [--keep] COMPLINE
119
completely test (-h|--help)
1210

1311
Options:
12+
-k --keep
13+
Keep the temporary testing script in the current directory
14+
1415
-h --help
1516
Show this help
1617

spec/approvals/cli/test/usage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Usage:
2-
completely test COMPLINE
2+
completely test [--keep] COMPLINE
33
completely test (-h|--help)

spec/completely/commands/test_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@
3030
end
3131
end
3232

33+
context "with --keep COMPLINE" do
34+
before { system "rm -f #{filename}" }
35+
after { system "rm -f #{filename}" }
36+
37+
let(:filename) { 'completely-tester.sh' }
38+
39+
it "copies the test script to the current directory" do
40+
expect { subject.run ["test", "--keep", "mygit status --"] }
41+
.to output_approval('cli/test/comps-default-keep')
42+
43+
expect(File.read filename).to match_approval('cli/test/completely-tester.sh')
44+
end
45+
end
46+
3347
context "when COMPLETELY_CONFIG_PATH is set" do
3448
before do
3549
reset_tmp_dir
@@ -43,7 +57,7 @@
4357
end
4458
end
4559

46-
context "when there is no compeltely.yaml or any environment instructions" do
60+
context "when there is no compeltely.yaml or COMPLETELY_CONFIG_PATH" do
4761
before { system "rm -f completely.yaml" }
4862

4963
it "fails gracefully" do

0 commit comments

Comments
 (0)