Skip to content

Commit 5bf70ba

Browse files
committed
add specs
1 parent 2fbd879 commit 5bf70ba

File tree

9 files changed

+137
-9
lines changed

9 files changed

+137
-9
lines changed

lib/completely/commands/install.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33
module Completely
44
module Commands
55
class Install < Base
6-
help 'Install a bash completion script'
6+
TARGETS = %W[
7+
/usr/share/bash-completion/completions
8+
/usr/local/etc/bash_completion.d
9+
#{Dir.home}/.bash_completion.d
10+
]
11+
12+
summary 'Install a bash completion script'
13+
14+
help <<~HELP
15+
This command will copy the specified file to one of the following directories:
16+
17+
#{TARGETS.map { |c| " - #{c}" }.join "\n"}
18+
19+
The target filename will be the program name, and sudo will be used if necessary.
20+
HELP
721

822
usage 'completely install PROGRAM [SCRIPT_PATH --force]'
923
usage 'completely install (-h|--help)'
@@ -32,11 +46,15 @@ def bounce
3246
raise "Cannot find script: m`#{script_path}`"
3347
end
3448

35-
if File.exist?(target_path) && !args['--force']
49+
if target_exist? && !args['--force']
3650
raise "File exists: m`#{target_path}`\nUse nb`--force` to overwrite"
3751
end
3852
end
3953

54+
def target_exist?
55+
File.exist? target_path
56+
end
57+
4058
def command
4159
result = root? ? [] : %w[sudo]
4260
result + %W[cp #{script_path} #{target_path}]
@@ -59,13 +77,8 @@ def completions_path
5977
end
6078

6179
def completions_path!
62-
candidates = %w[
63-
/usr/share/bash-completion/completions
64-
/usr/local/etc/bash_completion.d
65-
]
66-
67-
candidates.each do |candidate|
68-
return candidate if Dir.exist? candidate
80+
TARGETS.each do |tarnet|
81+
return tarnet if Dir.exist? tarnet
6982
end
7083

7184
nil

spec/approvals/cli/install/help

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Install a bash completion script
2+
3+
This command will copy the specified file to one of the following directories:
4+
5+
- /usr/share/bash-completion/completions
6+
- /usr/local/etc/bash_completion.d
7+
- /home/vagrant/.bash_completion.d
8+
9+
The target filename will be the program name, and sudo will be used if
10+
necessary.
11+
12+
Usage:
13+
completely install PROGRAM [SCRIPT_PATH --force]
14+
completely install (-h|--help)
15+
16+
Options:
17+
-f --force
18+
Overwrite target file if it exists
19+
20+
-h --help
21+
Show this help
22+
23+
Parameters:
24+
PROGRAM
25+
Name of the program the completions are for.
26+
27+
SCRIPT_PATH
28+
Path to the source bash script [default: completely.bash].
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Saved /usr/share/bash-completion/completions/completely-test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Saved /usr/share/bash-completion/completions/completely-test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<RuntimeError: Cannot find script: m`completely.bash`>

spec/approvals/cli/install/no-args

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Usage:
2+
completely install PROGRAM [SCRIPT_PATH --force]
3+
completely install (-h|--help)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<RuntimeError: Cannot determine system completions directory>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#<RuntimeError: File exists: m`/usr/share/bash-completion/completions/completely-test`
2+
Use nb`--force` to overwrite>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'spec_helper'
2+
3+
describe Commands::Install do
4+
subject { described_class.new }
5+
6+
context 'with --help' do
7+
it 'shows long usage' do
8+
expect { subject.execute %w[install --help] }.to output_approval('cli/install/help')
9+
end
10+
end
11+
12+
context 'without arguments' do
13+
it 'shows short usage' do
14+
expect { subject.execute %w[install] }.to output_approval('cli/install/no-args')
15+
end
16+
end
17+
18+
context 'with only the program name argument' do
19+
context 'when the default script is not found' do
20+
it 'raises an error' do
21+
expect { subject.execute %w[install completely-test] }.to raise_approval('cli/install/missing-script')
22+
end
23+
end
24+
25+
context 'when the default script is found' do
26+
let(:expected_args) {
27+
%w[
28+
sudo
29+
cp
30+
completely.bash
31+
/usr/share/bash-completion/completions/completely-test
32+
]
33+
}
34+
35+
before do
36+
reset_tmp_dir
37+
File.write 'spec/tmp/completely.bash', 'not-important'
38+
end
39+
40+
it 'copies the script' do
41+
Dir.chdir 'spec/tmp' do
42+
expect(subject).to receive(:system).with(*expected_args).and_return true
43+
expect { subject.execute %W[install completely-test] }.to output_approval('cli/install/install-default')
44+
end
45+
end
46+
end
47+
end
48+
49+
context 'with the program name argument and a script argument' do
50+
let(:expected_args) {
51+
%w[
52+
sudo
53+
cp
54+
README.md
55+
/usr/share/bash-completion/completions/completely-test
56+
]
57+
}
58+
59+
it 'copies the script' do
60+
expect(subject).to receive(:system).with(*expected_args).and_return true
61+
expect { subject.execute %W[install completely-test README.md] }.to output_approval('cli/install/install-specified')
62+
end
63+
end
64+
65+
context 'when none of the target directories is found' do
66+
it 'raises an error' do
67+
expect(subject).to receive(:completions_path).and_return nil
68+
expect { subject.execute %W[install completely-test README.md] }.to raise_approval('cli/install/no-completion-targets')
69+
end
70+
end
71+
72+
context 'when the target file exists' do
73+
it 'raises an error' do
74+
expect(subject).to receive(:target_exist?).and_return true
75+
expect { subject.execute %W[install completely-test README.md] }.to raise_approval('cli/install/target-exists')
76+
end
77+
end
78+
end

0 commit comments

Comments
 (0)