Skip to content

Commit 08e2c43

Browse files
committed
add specs for completions command
1 parent 3f4b1a3 commit 08e2c43

File tree

9 files changed

+98
-30
lines changed

9 files changed

+98
-30
lines changed

lib/bashly/commands/completions.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def run
2020
private
2121

2222
def install_completions
23-
raise Error, "Cannot find completions directory" unless compdir
23+
raise Error, 'Cannot find completions directory' unless compdir
2424

2525
target = "#{compdir}/bashly"
2626

2727
say "Installing completions to m`#{target}`"
28-
command = %Q[cp "#{completions_path}" "#{target}"]
28+
command = %[cp "#{completions_path}" "#{target}"]
2929
command = "sudo #{command}" unless root_user?
3030
system command
3131

@@ -45,21 +45,24 @@ def completions_script
4545
end
4646

4747
def compdir
48-
@compdir ||= begin
49-
candidates = [
50-
'/usr/share/bash-completion/completions',
51-
'/usr/local/etc/bash_completion.d'
52-
]
53-
54-
candidates.each { |dir| return dir if Dir.exist? dir }
55-
nil
56-
end
48+
@compdir ||= compdir!
49+
end
50+
51+
def compdir!
52+
compdir_candidates.each { |dir| return dir if Dir.exist? dir }
53+
nil
54+
end
55+
56+
def compdir_candidates
57+
@compdir_candidates ||= [
58+
'/usr/share/bash-completion/completions',
59+
'/usr/local/etc/bash_completion.d',
60+
]
5761
end
5862

5963
def root_user?
60-
Process.uid == 0
64+
Process.uid.zero?
6165
end
6266
end
63-
6467
end
6568
end

lib/bashly/completions/completely.yaml.gtx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ end
6161
libs.each do |lib|
6262
= "- #{lib}"
6363
end
64-
6564
>
6665
> bashly a: *add
6766
>
@@ -72,15 +71,15 @@ end
7271
docs.each do |doc|
7372
= "- #{doc}"
7473
end
75-
74+
>
7675
> bashly completions: &completions
7776
= help_flags
7877
> - --install
7978
> - -i
8079
>
8180
> bashly c: *completions
82-
81+
>
8382
> bashly shell: &shell
8483
= help_flags
8584
>
86-
> bashly s: *shell
85+
> bashly s: *shell

spec/approvals/cli/commands

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Bashly - Bash CLI Generator
22

33
Commands:
4-
init Initialize a new workspace
5-
preview Generate the bash script to STDOUT
6-
validate Scan the configuration file for errors
7-
generate Generate the bash script and required files
8-
add Add extra features and customization to your script
9-
doc Show bashly reference documentation
10-
shell Start an interactive bashly shell
4+
init Initialize a new workspace
5+
preview Generate the bash script to STDOUT
6+
validate Scan the configuration file for errors
7+
generate Generate the bash script and required files
8+
add Add extra features and customization to your script
9+
doc Show bashly reference documentation
10+
completions Install bash completions for bashly itself
11+
shell Start an interactive bashly shell
1112

1213
Help: bashly COMMAND --help
1314
Docs: https://bashly.dannyb.co
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Install bash completions for bashly itself
2+
3+
Display the bash completions script or install it directly to your bash
4+
completions directory
5+
6+
Usage:
7+
bashly completions [--install]
8+
bashly completions (-h|--help)
9+
10+
Options:
11+
-i --install
12+
Install the completions script to your bash completions directory
13+
14+
-h --help
15+
Show this help
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Installing completions to /tmp/bashly
2+
Restart your session for the changes to take effect.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::Error: Cannot find completions directory>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Installing completions to /tmp/bashly
2+
Restart your session for the changes to take effect.

spec/approvals/cli/shell/boot

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Bashly - Bash CLI Generator
22

33
Commands:
4-
init Initialize a new workspace
5-
preview Generate the bash script to STDOUT
6-
validate Scan the configuration file for errors
7-
generate Generate the bash script and required files
8-
add Add extra features and customization to your script
9-
doc Show bashly reference documentation
4+
init Initialize a new workspace
5+
preview Generate the bash script to STDOUT
6+
validate Scan the configuration file for errors
7+
generate Generate the bash script and required files
8+
add Add extra features and customization to your script
9+
doc Show bashly reference documentation
10+
completions Install bash completions for bashly itself
1011

1112
Help: bashly COMMAND --help
1213
Docs: https://bashly.dannyb.co
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
describe Commands::Completions do
4+
subject { described_class.new }
5+
6+
let(:completions_path) { File.expand_path 'lib/bashly/completions/bashly-completions.bash' }
7+
let(:completions_script) { File.read completions_path }
8+
9+
context 'with --help' do
10+
it 'shows long usage' do
11+
expect { subject.execute %w[completions --help] }.to output_approval('cli/completions/help')
12+
end
13+
end
14+
15+
context 'without arguments' do
16+
it 'shows the completions script' do
17+
expect { subject.execute %w[completions] }.to output(completions_script).to_stdout
18+
end
19+
end
20+
21+
context 'with --install' do
22+
it 'installs the completions script to the completions directory' do
23+
allow(subject).to receive(:compdir_candidates).and_return ['/tmp']
24+
expect(subject).to receive(:system).with(%Q[sudo cp "#{completions_path}" "/tmp/bashly"])
25+
expect { subject.execute %w[completions --install] }.to output_approval('cli/completions/install')
26+
end
27+
28+
context 'when the completions directory is not found' do
29+
it 'raises an error' do
30+
allow(Dir).to receive(:exist?).twice.and_return(false)
31+
expect { subject.execute %w[completions --install] }.to raise_approval('cli/completions/install-error')
32+
end
33+
end
34+
35+
context 'when running as root' do
36+
it 'installs the completions script to the completions directory without sudo' do
37+
allow(subject).to receive(:compdir_candidates).and_return ['/tmp']
38+
allow(subject).to receive(:root_user?).and_return true
39+
expect(subject).to receive(:system).with(%Q[cp "#{completions_path}" "/tmp/bashly"])
40+
expect { subject.execute %w[completions --install] }.to output_approval('cli/completions/install-root')
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)