Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions lib/completely/commands/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ class Install < Base
USAGE

def run
if script_path == '-'
raise InstallError, "Nothing is piped on stdin" if $stdin.tty?

@script_path = tempfile.path
File.write script_path, $stdin.read
end

if args['--dry']
puts installer.install_command_string
return
Expand All @@ -43,16 +36,12 @@ def run
say 'You may need to restart your session to test it'
end

def tempfile
@tempfile ||= Tempfile.new('stdin-completely-')
end

private

def installer
@installer ||= Installer.new(program: args['PROGRAM'], script_path: script_path)
end

private

def script_path
@script_path ||= args['SCRIPT_PATH'] || 'completely.bash'
end
Expand Down
19 changes: 17 additions & 2 deletions lib/completely/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ class Installer
attr_reader :program, :script_path

def initialize(program:, script_path: nil)
if script_path == '-'
raise InstallError, 'Nothing is piped on stdin' if $stdin.tty?

script_path = tempfile.path
File.write script_path, $stdin.read
end

@program = program
@script_path = script_path
end
Expand Down Expand Up @@ -60,6 +67,10 @@ def uninstall

private

def tempfile
@tempfile ||= Tempfile.new('stdin-completely-')
end

def target_exist?
File.exist? target_path
end
Expand All @@ -74,11 +85,15 @@ def root_user?

def completions_path
@completions_path ||= begin
result = nil
target_directories.each do |target|
return target if Dir.exist? target
if Dir.exist? target
result = target
break
end
end

nil
result
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions spec/completely/commands/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

expect { subject.execute %w[install completely-test -] }
.to output_approval('cli/install/stdin-install')

expect(File.read subject.tempfile.path).to eq 'dummy data'
end
end

Expand Down Expand Up @@ -79,7 +77,6 @@
end
end


context 'when the installer fails' do
it 'raises an error' do
allow(subject).to receive(:installer).and_return(mock_installer)
Expand Down
2 changes: 1 addition & 1 deletion spec/completely/completions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

describe '::read' do
it 'reads from io' do
io = double :io, read: "cli: [--help, --version]"
io = double :io, read: 'cli: [--help, --version]'
expect(described_class.read(io).config.config).to eq({ 'cli' => %w[--help --version] })
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/completely/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

let(:path) { "spec/fixtures/#{file}.yaml" }
let(:file) { 'nested' }
let(:config_string) { "cli: [--help, --version]" }
let(:config_string) { 'cli: [--help, --version]' }
let(:config_hash) { { 'cli' => %w[--help --version] } }

describe '::parse' do
Expand All @@ -13,7 +13,7 @@

context 'when the string is not a valid YAML' do
it 'raises ParseError' do
expect { described_class.parse("not: a: yaml") }.to raise_error(Completely::ParseError)
expect { described_class.parse('not: a: yaml') }.to raise_error(Completely::ParseError)
end
end
end
Expand Down
24 changes: 14 additions & 10 deletions spec/completely/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
%w[sudo rm -f] + targets
end

describe '#initialize' do
context 'when script_path == "-"' do
let(:script_path) { '-' }

it 'reads the script from stdin and writes it to a temp file' do
allow($stdin).to receive_messages(tty?: false, read: 'dummy data')

subject

expect(File.read subject.script_path).to eq 'dummy data'
end
end
end

describe '#target_directories' do
it 'returns an array of potential completion directories' do
expect(subject.target_directories).to be_an Array
Expand All @@ -25,16 +39,6 @@
expect(subject.target_path)
.to eq '/usr/share/bash-completion/completions/completely-test'
end

# This method will not be called if there is no completions path
# The test is here to cover the nil fallback
context 'when no paths found' do
it 'returns nil as the base path' do
allow(subject).to receive(:target_directories).and_return([])

expect(subject.target_path).to eq '/completely-test'
end
end
end

describe '#install_command' do
Expand Down