Skip to content

Commit 40b0894

Browse files
authored
Merge pull request #395 from DannyBen/add/completions-uninstaller
Add `bashly completions --uninstall` command
2 parents e3a482a + 0c293ac commit 40b0894

File tree

7 files changed

+50
-10
lines changed

7 files changed

+50
-10
lines changed

bashly.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
1616
s.required_ruby_version = '>= 3.0'
1717

1818
s.add_runtime_dependency 'colsole', '>= 0.8.1', '< 2'
19-
s.add_runtime_dependency 'completely', '~> 0.6'
19+
s.add_runtime_dependency 'completely', '~> 0.6.1'
2020
s.add_runtime_dependency 'filewatcher', '~> 2.0'
2121
s.add_runtime_dependency 'gtx', '~> 0.1'
2222
s.add_runtime_dependency 'lp', '~> 0.2'

lib/bashly/commands/completions.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ class Completions < Base
44
summary 'Install bash completions for bashly itself'
55
help 'Display the bash completions script or install it directly to your bash completions directory'
66

7-
usage 'bashly completions [--install]'
7+
usage 'bashly completions [--install --uninstall]'
88
usage 'bashly completions (-h|--help)'
99

1010
option '-i --install', 'Install the completions script to your bash completions directory'
11+
option '-u --uninstall', 'Uninstall the completions script from your bash completions directory'
1112

1213
def run
1314
if args['--install']
1415
install_completions
16+
elsif args['--uninstall']
17+
uninstall_completions
1518
else
1619
puts script
1720
end
@@ -25,14 +28,22 @@ def installer
2528

2629
def install_completions
2730
success = installer.install force: true
28-
raise Error, "Failed running command:\nnb`#{installer.command_string}`" unless success
31+
raise Error, "Failed running command:\nnb`#{installer.install_command_string}`" unless success
2932

30-
say 'Completions installed.'
33+
say 'Completions installed'
3134
say "Source: m`#{installer.script_path}`"
3235
say "Target: m`#{installer.target_path}`"
3336
say 'Restart your session for the changes to take effect'
3437
end
3538

39+
def uninstall_completions
40+
success = installer.uninstall
41+
raise Error, "Failed running command:\nnb`#{installer.uninstall_command_string}`" unless success
42+
43+
say 'Completions uninstalled'
44+
say 'Restart your session for the changes to take effect'
45+
end
46+
3647
def script_path
3748
@script_path ||= asset('completions/bashly-completions.bash')
3849
end

spec/approvals/cli/completions/help

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ Display the bash completions script or install it directly to your bash
44
completions directory
55

66
Usage:
7-
bashly completions [--install]
7+
bashly completions [--install --uninstall]
88
bashly completions (-h|--help)
99

1010
Options:
1111
-i --install
1212
Install the completions script to your bash completions directory
1313

14+
-u --uninstall
15+
Uninstall the completions script from your bash completions directory
16+
1417
-h --help
1518
Show this help
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Completions installed.
1+
Completions installed
22
Source: some-script-path
33
Target: some-target-path
44
Restart your session for the changes to take effect
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Completions uninstalled
2+
Restart your session for the changes to take effect
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#<Bashly::Error: Failed running command:
2+
nb`rm -f some files`>

spec/bashly/commands/completions_spec.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
let(:completions_script) { File.read completions_path }
99
let :mock_installer do
1010
instance_double Completely::Installer,
11-
install: true,
12-
target_path: 'some-target-path',
13-
script_path: 'some-script-path',
14-
command_string: 'cp source target'
11+
install: true,
12+
uninstall: true,
13+
target_path: 'some-target-path',
14+
script_path: 'some-script-path',
15+
install_command_string: 'cp source target',
16+
uninstall_command_string: 'rm -f some files'
1517
end
1618

1719
describe '#installer' do
@@ -55,4 +57,24 @@
5557
end
5658
end
5759
end
60+
61+
context 'with --uninstall' do
62+
it 'uninstalls the completions script from all completions directories' do
63+
allow(subject).to receive(:installer).and_return mock_installer
64+
65+
expect { subject.execute %w[completions --uninstall] }
66+
.to output_approval('cli/completions/uninstall')
67+
end
68+
69+
context 'when the installer fails' do
70+
it 'raises an error' do
71+
allow(subject).to receive(:installer).and_return mock_installer
72+
allow(mock_installer).to receive(:uninstall).and_return(false)
73+
74+
expect { subject.execute %w[completions --uninstall] }
75+
.to raise_approval('cli/completions/uninstall-error')
76+
.diff(leeway)
77+
end
78+
end
79+
end
5880
end

0 commit comments

Comments
 (0)