|
| 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