Skip to content

Commit 7823790

Browse files
authored
Merge pull request #86 from bashly-framework/refactor/install
Refactor Installer
2 parents 301de12 + d4ce51d commit 7823790

File tree

6 files changed

+36
-31
lines changed

6 files changed

+36
-31
lines changed

lib/completely/commands/install.rb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ class Install < Base
2424
USAGE
2525

2626
def run
27-
if script_path == '-'
28-
raise InstallError, "Nothing is piped on stdin" if $stdin.tty?
29-
30-
@script_path = tempfile.path
31-
File.write script_path, $stdin.read
32-
end
33-
3427
if args['--dry']
3528
puts installer.install_command_string
3629
return
@@ -43,16 +36,12 @@ def run
4336
say 'You may need to restart your session to test it'
4437
end
4538

46-
def tempfile
47-
@tempfile ||= Tempfile.new('stdin-completely-')
48-
end
49-
50-
private
51-
5239
def installer
5340
@installer ||= Installer.new(program: args['PROGRAM'], script_path: script_path)
5441
end
5542

43+
private
44+
5645
def script_path
5746
@script_path ||= args['SCRIPT_PATH'] || 'completely.bash'
5847
end

lib/completely/installer.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ class Installer
33
attr_reader :program, :script_path
44

55
def initialize(program:, script_path: nil)
6+
if script_path == '-'
7+
raise InstallError, 'Nothing is piped on stdin' if $stdin.tty?
8+
9+
script_path = tempfile.path
10+
File.write script_path, $stdin.read
11+
end
12+
613
@program = program
714
@script_path = script_path
815
end
@@ -60,6 +67,10 @@ def uninstall
6067

6168
private
6269

70+
def tempfile
71+
@tempfile ||= Tempfile.new('stdin-completely-')
72+
end
73+
6374
def target_exist?
6475
File.exist? target_path
6576
end
@@ -74,11 +85,15 @@ def root_user?
7485

7586
def completions_path
7687
@completions_path ||= begin
88+
result = nil
7789
target_directories.each do |target|
78-
return target if Dir.exist? target
90+
if Dir.exist? target
91+
result = target
92+
break
93+
end
7994
end
8095

81-
nil
96+
result
8297
end
8398
end
8499
end

spec/completely/commands/install_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343

4444
expect { subject.execute %w[install completely-test -] }
4545
.to output_approval('cli/install/stdin-install')
46-
47-
expect(File.read subject.tempfile.path).to eq 'dummy data'
4846
end
4947
end
5048

@@ -79,7 +77,6 @@
7977
end
8078
end
8179

82-
8380
context 'when the installer fails' do
8481
it 'raises an error' do
8582
allow(subject).to receive(:installer).and_return(mock_installer)

spec/completely/completions_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
describe '::read' do
88
it 'reads from io' do
9-
io = double :io, read: "cli: [--help, --version]"
9+
io = double :io, read: 'cli: [--help, --version]'
1010
expect(described_class.read(io).config.config).to eq({ 'cli' => %w[--help --version] })
1111
end
1212
end

spec/completely/config_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

99
describe '::parse' do
@@ -13,7 +13,7 @@
1313

1414
context 'when the string is not a valid YAML' do
1515
it 'raises ParseError' do
16-
expect { described_class.parse("not: a: yaml") }.to raise_error(Completely::ParseError)
16+
expect { described_class.parse('not: a: yaml') }.to raise_error(Completely::ParseError)
1717
end
1818
end
1919
end

spec/completely/installer_spec.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
%w[sudo rm -f] + targets
1414
end
1515

16+
describe '#initialize' do
17+
context 'when script_path == "-"' do
18+
let(:script_path) { '-' }
19+
20+
it 'reads the script from stdin and writes it to a temp file' do
21+
allow($stdin).to receive_messages(tty?: false, read: 'dummy data')
22+
23+
subject
24+
25+
expect(File.read subject.script_path).to eq 'dummy data'
26+
end
27+
end
28+
end
29+
1630
describe '#target_directories' do
1731
it 'returns an array of potential completion directories' do
1832
expect(subject.target_directories).to be_an Array
@@ -25,16 +39,6 @@
2539
expect(subject.target_path)
2640
.to eq '/usr/share/bash-completion/completions/completely-test'
2741
end
28-
29-
# This method will not be called if there is no completions path
30-
# The test is here to cover the nil fallback
31-
context 'when no paths found' do
32-
it 'returns nil as the base path' do
33-
allow(subject).to receive(:target_directories).and_return([])
34-
35-
expect(subject.target_path).to eq '/completely-test'
36-
end
37-
end
3842
end
3943

4044
describe '#install_command' do

0 commit comments

Comments
 (0)