Skip to content

Commit d860854

Browse files
authored
Merge branch 'master' into fix/double-completion
2 parents 0e6668e + 85615b7 commit d860854

File tree

6 files changed

+56
-35
lines changed

6 files changed

+56
-35
lines changed

.envrc.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# for git-changelog (https://github.com/dannyben/git-changelog)
22
export CHANGELOG_COMMIT_URL=https://github.com/bashly-framework/completely/commit/%h
33
export CHANGELOG_COMPARE_URL=https://github.com/bashly-framework/completely/compare/%s
4+
5+
# include the debug gem
6+
export COMPLETELY_DEV=1

lib/completely.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
require 'completely/completions'
55
require 'completely/tester'
66
require 'completely/installer'
7+
require 'debug' if ENV['COMPLETELY_DEV']

lib/completely/commands/install.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'tempfile'
21
require 'completely/commands/base'
32

43
module Completely
@@ -37,14 +36,18 @@ def run
3736
end
3837

3938
def installer
40-
@installer ||= Installer.new(program: args['PROGRAM'], script_path: script_path)
39+
@installer ||= if stdin?
40+
Installer.from_io program:
41+
else
42+
Installer.new program:, script_path: input_script_path
43+
end
4144
end
4245

4346
private
4447

45-
def script_path
46-
@script_path ||= args['SCRIPT_PATH'] || 'completely.bash'
47-
end
48+
def program = args['PROGRAM']
49+
def stdin? = input_script_path == '-'
50+
def input_script_path = args['SCRIPT_PATH'] || 'completely.bash'
4851
end
4952
end
5053
end

lib/completely/installer.rb

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
module Completely
22
class Installer
3-
attr_reader :program, :script_path
3+
class << self
4+
def from_io(program:, io: nil)
5+
io ||= $stdin
46

5-
def initialize(program:, script_path: nil)
6-
if script_path == '-'
7-
raise InstallError, 'Nothing is piped on stdin' if $stdin.tty?
7+
raise InstallError, "io must respond to #read" unless io.respond_to?(:read)
8+
raise InstallError, "io is closed" if io.respond_to?(:closed?) && io.closed?
9+
10+
from_string program:, string: io.read
11+
end
812

13+
def from_string(program:, string:)
14+
tempfile = create_tempfile
915
script_path = tempfile.path
10-
File.write script_path, $stdin.read
16+
begin
17+
File.write script_path, string
18+
ensure
19+
tempfile.close
20+
end
21+
22+
new program:, script_path:
23+
end
24+
25+
def create_tempfile
26+
tempfile = Tempfile.new ["completely-", '.bash']
27+
tempfiles.push tempfile
28+
tempfile
1129
end
1230

31+
def tempfiles = @tempfiles ||= []
32+
end
33+
34+
attr_reader :program, :script_path
35+
36+
def initialize(program:, script_path: nil)
1337
@program = program
1438
@script_path = script_path
1539
end
@@ -67,10 +91,6 @@ def uninstall
6791

6892
private
6993

70-
def tempfile
71-
@tempfile ||= Tempfile.new('stdin-completely-')
72-
end
73-
7494
def target_exist?
7595
File.exist? target_path
7696
end

spec/completely/commands/install_spec.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
context 'with PROGRAM - (stdin)' do
3838
it 'invokes the Installer using a temp file' do
3939
allow(subject).to receive(:installer).and_return(mock_installer)
40-
allow($stdin).to receive_messages(tty?: false, read: 'dummy data')
40+
allow($stdin).to receive_messages(read: 'dummy data')
4141

4242
expect(mock_installer).to receive(:install)
4343

@@ -57,23 +57,13 @@
5757

5858
context 'with PROGRAM - --dry (stdin)' do
5959
it 'shows the command and does not install anything' do
60-
allow($stdin).to receive_messages(tty?: false, read: 'dummy data')
60+
allow($stdin).to receive_messages(read: 'dummy data')
6161

6262
expect(mock_installer).not_to receive(:install)
6363

6464
expect { subject.execute %w[install completely-test - --dry] }
6565
.to output_approval('cli/install/stdin-dry')
66-
.except(/[^\s]*stdin-completely-[^\s]*/, '<tmpfile-path>')
67-
end
68-
69-
context 'when stdin is empty' do
70-
it 'raises InstallError' do
71-
allow($stdin).to receive_messages(tty?: true, read: nil)
72-
expect(mock_installer).not_to receive(:install)
73-
74-
expect { subject.execute %w[install completely-test - --dry] }
75-
.to raise_error(InstallError, 'Nothing is piped on stdin')
76-
end
66+
.except(/cp [^\s]*completely-[^\s]*/, 'cp <tmpfile-path>')
7767
end
7868
end
7969

spec/completely/installer_spec.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313
%w[sudo rm -f] + targets
1414
end
1515

16-
describe '#initialize' do
17-
context 'when script_path == "-"' do
18-
let(:script_path) { '-' }
16+
describe '::from_io' do
17+
let(:io) { StringIO.new 'dummy data' }
18+
subject { described_class.from_io program:, io: }
1919

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')
20+
it 'reads the script from io and writes it to a temp file' do
21+
expect(File.read subject.script_path).to eq 'dummy data'
22+
end
23+
end
2224

23-
subject
25+
describe '::from_string' do
26+
let(:string) { 'dummy data' }
27+
subject { described_class.from_string program:, string: }
2428

25-
expect(File.read subject.script_path).to eq 'dummy data'
26-
end
29+
it 'reads the script from io and writes it to a temp file' do
30+
expect(File.read subject.script_path).to eq 'dummy data'
2731
end
2832
end
2933

0 commit comments

Comments
 (0)