Skip to content

Commit 4b6e3d5

Browse files
committed
add --dry
1 parent 23e99de commit 4b6e3d5

File tree

7 files changed

+46
-18
lines changed

7 files changed

+46
-18
lines changed

lib/completely/commands/install.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,28 @@ class Install < Base
1919
The target filename will be the program name, and sudo will be used if necessary.
2020
HELP
2121

22-
usage 'completely install PROGRAM [SCRIPT_PATH --force]'
22+
usage 'completely install PROGRAM [SCRIPT_PATH --force --dry]'
2323
usage 'completely install (-h|--help)'
2424

2525
option '-f --force', 'Overwrite target file if it exists'
26+
option '-d --dry', 'Show the installation command but do not run it'
2627

2728
param 'PROGRAM', 'Name of the program the completions are for.'
2829
param 'SCRIPT_PATH', 'Path to the source bash script [default: completely.bash].'
2930

3031
def run
3132
bounce
33+
34+
if args['--dry']
35+
puts command.join ' '
36+
return
37+
end
38+
3239
success = system(*command)
3340
raise "Failed running command:\nnb`#{command.join ' '}`" unless success
3441

3542
say "Saved m`#{target_path}`"
43+
say 'You may need to restart your session to test it'
3644
end
3745

3846
private

spec/approvals/cli/install/dry

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sudo cp README.md /usr/share/bash-completion/completions/completely-test

spec/approvals/cli/install/help

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ The target filename will be the program name, and sudo will be used if
1010
necessary.
1111

1212
Usage:
13-
completely install PROGRAM [SCRIPT_PATH --force]
13+
completely install PROGRAM [SCRIPT_PATH --force --dry]
1414
completely install (-h|--help)
1515

1616
Options:
1717
-f --force
1818
Overwrite target file if it exists
1919

20+
-d --dry
21+
Show the installation command but do not run it
22+
2023
-h --help
2124
Show this help
2225

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Saved /usr/share/bash-completion/completions/completely-test
2+
You may need to restart your session to test it
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Saved /usr/share/bash-completion/completions/completely-test
2+
You may need to restart your session to test it

spec/approvals/cli/install/no-args

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Usage:
2-
completely install PROGRAM [SCRIPT_PATH --force]
2+
completely install PROGRAM [SCRIPT_PATH --force --dry]
33
completely install (-h|--help)

spec/completely/commands/install_spec.rb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@
55

66
context 'with --help' do
77
it 'shows long usage' do
8-
expect { subject.execute %w[install --help] }.to output_approval('cli/install/help').diff(10)
8+
expect { subject.execute %w[install --help] }
9+
.to output_approval('cli/install/help').diff(10)
910
end
1011
end
1112

1213
context 'without arguments' do
1314
it 'shows short usage' do
14-
expect { subject.execute %w[install] }.to output_approval('cli/install/no-args')
15+
expect { subject.execute %w[install] }
16+
.to output_approval('cli/install/no-args')
1517
end
1618
end
1719

1820
context 'with only the program name argument' do
1921
context 'when the default script is not found' do
2022
it 'raises an error' do
21-
expect { subject.execute %w[install completely-test] }.to raise_approval('cli/install/missing-script')
23+
expect { subject.execute %w[install completely-test] }
24+
.to raise_approval('cli/install/missing-script')
2225
end
2326
end
2427

2528
context 'when the default script is found' do
26-
let(:expected_args) {
29+
let(:expected_args) do
2730
%w[
2831
sudo
2932
cp
3033
completely.bash
3134
/usr/share/bash-completion/completions/completely-test
3235
]
33-
}
36+
end
3437

3538
before do
3639
reset_tmp_dir
@@ -39,40 +42,51 @@
3942

4043
it 'copies the script' do
4144
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')
45+
allow(subject).to receive(:system).with(*expected_args).and_return true
46+
expect { subject.execute %w[install completely-test] }
47+
.to output_approval('cli/install/install-default')
4448
end
4549
end
4650
end
4751
end
4852

4953
context 'with the program name argument and a script argument' do
50-
let(:expected_args) {
54+
let(:expected_args) do
5155
%w[
5256
sudo
5357
cp
5458
README.md
5559
/usr/share/bash-completion/completions/completely-test
5660
]
57-
}
61+
end
5862

5963
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')
64+
allow(subject).to receive(:system).with(*expected_args).and_return true
65+
expect { subject.execute %w[install completely-test README.md] }
66+
.to output_approval('cli/install/install-specified')
67+
end
68+
end
69+
70+
context 'with --dry' do
71+
it 'shows the command' do
72+
expect { subject.execute %w[install completely-test README.md --dry] }
73+
.to output_approval('cli/install/dry')
6274
end
6375
end
6476

6577
context 'when none of the target directories is found' do
6678
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')
79+
allow(subject).to receive(:completions_path).and_return nil
80+
expect { subject.execute %w[install completely-test README.md] }
81+
.to raise_approval('cli/install/no-completion-targets')
6982
end
7083
end
7184

7285
context 'when the target file exists' do
7386
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')
87+
allow(subject).to receive(:target_exist?).and_return true
88+
expect { subject.execute %w[install completely-test README.md] }
89+
.to raise_approval('cli/install/target-exists')
7690
end
7791
end
7892
end

0 commit comments

Comments
 (0)