|
3 | 3 | describe Commands::Completions do
|
4 | 4 | subject { described_class.new }
|
5 | 5 |
|
| 6 | + let(:leeway) { RUBY_VERSION < '3.2.0' ? 0 : 5 } |
6 | 7 | let(:completions_path) { File.expand_path 'lib/bashly/completions/bashly-completions.bash' }
|
7 | 8 | let(:completions_script) { File.read completions_path }
|
| 9 | + let :mock_installer do |
| 10 | + 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' |
| 15 | + end |
| 16 | + |
| 17 | + describe '#installer' do |
| 18 | + it 'returns a properly configured Completely::Installer instance' do |
| 19 | + expect(subject.installer).to be_a Completely::Installer |
| 20 | + expect(subject.installer.program).to eq 'bashly' |
| 21 | + expect(subject.installer.script_path).to end_with '/bashly-completions.bash' |
| 22 | + end |
| 23 | + end |
8 | 24 |
|
9 | 25 | context 'with --help' do
|
10 | 26 | it 'shows long usage' do
|
11 |
| - expect { subject.execute %w[completions --help] }.to output_approval('cli/completions/help') |
| 27 | + expect { subject.execute %w[completions --help] } |
| 28 | + .to output_approval('cli/completions/help') |
12 | 29 | end
|
13 | 30 | end
|
14 | 31 |
|
15 | 32 | context 'without arguments' do
|
16 | 33 | it 'shows the completions script' do
|
17 |
| - expect { subject.execute %w[completions] }.to output(completions_script).to_stdout |
| 34 | + expect { subject.execute %w[completions] } |
| 35 | + .to output(completions_script).to_stdout |
18 | 36 | end
|
19 | 37 | end
|
20 | 38 |
|
21 | 39 | context 'with --install' do
|
22 | 40 | 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(%[sudo cp "#{completions_path}" "/tmp/bashly"]) |
25 |
| - expect { subject.execute %w[completions --install] }.to output_approval('cli/completions/install') |
| 41 | + allow(subject).to receive(:installer).and_return mock_installer |
| 42 | + |
| 43 | + expect { subject.execute %w[completions --install] } |
| 44 | + .to output_approval('cli/completions/install') |
26 | 45 | end
|
27 | 46 |
|
28 |
| - context 'when the completions directory is not found' do |
| 47 | + context 'when the installer fails' do |
29 | 48 | 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 |
| 49 | + allow(subject).to receive(:installer).and_return mock_installer |
| 50 | + allow(mock_installer).to receive(:install).and_return(false) |
34 | 51 |
|
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(%[cp "#{completions_path}" "/tmp/bashly"]) |
40 |
| - expect { subject.execute %w[completions --install] }.to output_approval('cli/completions/install-root') |
| 52 | + expect { subject.execute %w[completions --install] } |
| 53 | + .to raise_approval('cli/completions/install-error') |
| 54 | + .diff(leeway) |
41 | 55 | end
|
42 | 56 | end
|
43 | 57 | end
|
|
0 commit comments