Skip to content

Commit 548199e

Browse files
committed
add specs for completions concern
1 parent 2efca29 commit 548199e

File tree

10 files changed

+159
-6
lines changed

10 files changed

+159
-6
lines changed

lib/bashly/commands/add.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class Add < Base
2323
command "yaml", "Add standard functions for reading YAML files to the lib directory."
2424
command "comp", "Generate a bash completions script or function."
2525

26+
example "bashly add strings --force"
27+
example "bashly add comp function"
28+
example "bashly add comp script completions.bash"
29+
2630
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
2731

2832
def strings_command
@@ -111,12 +115,18 @@ def save_comp_yaml(filename = nil)
111115
filename ||= "completions.yaml"
112116
File.write filename, completions.to_yaml
113117
say "created !txtgrn!#{filename}"
118+
say ""
119+
say "This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem."
114120
end
115121

116122
def save_comp_script(filename = nil)
117123
filename ||= "completions.bash"
118124
File.write filename, completions_script
119125
say "created !txtgrn!#{filename}"
126+
say ""
127+
say "To enable completions, run:"
128+
say ""
129+
say " !txtpur!$ source #{filename}"
120130
end
121131

122132
def save_comp_function(name = nil)
@@ -126,8 +136,13 @@ def save_comp_function(name = nil)
126136

127137
FileUtils.mkdir_p target_dir unless Dir.exist? target_dir
128138
File.write filename, completions_function
129-
139+
130140
say "created !txtgrn!#{filename}"
141+
say ""
142+
say "In order to use it in your script, create a command or a flag (for example: !txtgrn!#{command.name} completions!txtrst! or !txtgrn!#{command.name} --completions!txtrst!) that calls the !txtgrn!#{name}!txtrst! function."
143+
say "Your users can then run something like this to enable completions:"
144+
say ""
145+
say " !txtpur!$ eval \"$(#{command.name} completions)\""
131146
end
132147

133148
end

lib/bashly/concerns/completions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def completion_words(with_version: false)
4343
completion_flag_names + completion_actions
4444
)
4545

46-
all.uniq.sort
46+
all.compact.uniq.sort
4747
end
4848

4949
end

lib/bashly/concerns/renderable.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ module Bashly
44
module Renderable
55
def render(view)
66
template = File.read view_path(view)
7-
# TODO: This new format is only supported in Ruby >= 2.6
8-
# So for now, we keep the old deprecated syntax
9-
# ERB.new(template, trim_mode: '%-').result(binding)
10-
ERB.new(template, nil, '%-').result(binding)
7+
ERB.new(template, trim_mode: '%-').result(binding)
118
end
129

1310
def strings

spec/approvals/cli/add/help

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ Parameters:
5353
Environment Variables:
5454
BASHLY_SOURCE_DIR
5555
The path containing the bashly configuration and source files [default: src]
56+
57+
Examples:
58+
bashly add strings --force
59+
bashly add comp function
60+
bashly add comp script completions.bash
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
say:
3+
- "--help"
4+
- "--version"
5+
- "-h"
6+
- "-v"
7+
- goodbye
8+
- hello
9+
say hello:
10+
- "--help"
11+
- "-h"
12+
- world
13+
say hello world:
14+
- "--force"
15+
- "--help"
16+
- "--verbose"
17+
- "-h"
18+
say goodbye:
19+
- "--help"
20+
- "-h"
21+
- universe
22+
say goodbye universe:
23+
- "--color"
24+
- "--help"
25+
- "--verbose"
26+
- "-c"
27+
- "-h"
28+
- "-v"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
custom_name() {
2+
echo $'#!/usr/bin/env bash'
3+
echo $''
4+
echo $'# This bash completions script was generated by'
5+
echo $'# completely (https://github.com/dannyben/completely)'
6+
echo $'# Modifying it manually is not recommended'
7+
echo $'_get_completions() {'
8+
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
9+
echo $''
10+
echo $' case "$COMP_LINE" in'
11+
echo $' \'get\'*) COMPREPLY=($(compgen -W "--force --help --verbose --version -h -v" -- "$cur")) ;;'
12+
echo $' esac'
13+
echo $'}'
14+
echo $''
15+
echo $'complete -F _get_completions get'
16+
}

spec/approvals/completions/script

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# This bash completions script was generated by
4+
# completely (https://github.com/dannyben/completely)
5+
# Modifying it manually is not recommended
6+
_say_completions() {
7+
local cur=${COMP_WORDS[COMP_CWORD]}
8+
9+
case "$COMP_LINE" in
10+
'say goodbye universe'*) COMPREPLY=($(compgen -W "--color --help --verbose -c -h -v" -- "$cur")) ;;
11+
'say hello world'*) COMPREPLY=($(compgen -W "--force --help --verbose -h" -- "$cur")) ;;
12+
'say goodbye'*) COMPREPLY=($(compgen -W "--help -h universe" -- "$cur")) ;;
13+
'say hello'*) COMPREPLY=($(compgen -W "--help -h world" -- "$cur")) ;;
14+
'say'*) COMPREPLY=($(compgen -W "--help --version -h -v goodbye hello" -- "$cur")) ;;
15+
esac
16+
}
17+
18+
complete -F _say_completions say

spec/approvals/completions/simple

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
get:
3+
- "--force"
4+
- "--help"
5+
- "--verbose"
6+
- "--version"
7+
- "-h"
8+
- "-v"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'spec_helper'
2+
3+
describe Models::Command do
4+
let(:fixture) { :completions_simple }
5+
6+
subject do
7+
options = load_fixture('models/commands')[fixture]
8+
described_class.new options
9+
end
10+
11+
describe '#completion_data' do
12+
it "returns a data structure for completely" do
13+
expect(subject.completion_data.to_yaml).to match_approval("completions/simple")
14+
end
15+
end
16+
17+
describe '#completion_function' do
18+
it "returns a bash completion script wrapped in a function" do
19+
expect(subject.completion_function "custom_name")
20+
.to match_approval("completions/function")
21+
end
22+
end
23+
24+
context "with a more complex command" do
25+
let(:fixture) { :completions_advanced }
26+
27+
describe '#completion_data' do
28+
it "returns a data structure for completely" do
29+
expect(subject.completion_data.to_yaml)
30+
.to match_approval("completions/advanced")
31+
end
32+
end
33+
34+
describe '#completion_script' do
35+
it "returns a bash completion script" do
36+
expect(subject.completion_script)
37+
.to match_approval("completions/script")
38+
end
39+
end
40+
end
41+
end

spec/fixtures/models/commands.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,28 @@
127127
catch_all:
128128
label: additional params
129129
help: Any additional argument or flag
130+
131+
:completions_simple:
132+
name: get
133+
134+
flags:
135+
- long: --force
136+
- long: --verbose
137+
138+
:completions_advanced:
139+
name: say
140+
commands:
141+
- name: hello
142+
commands:
143+
- name: world
144+
flags:
145+
- long: --force
146+
- long: --verbose
147+
- name: goodbye
148+
commands:
149+
- name: universe
150+
flags:
151+
- long: --color
152+
short: -c
153+
- long: --verbose
154+
short: -v

0 commit comments

Comments
 (0)