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
3 changes: 3 additions & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# for git-changelog (https://github.com/dannyben/git-changelog)
export CHANGELOG_COMMIT_URL=https://github.com/bashly-framework/completely/commit/%h
export CHANGELOG_COMPARE_URL=https://github.com/bashly-framework/completely/compare/%s

# include the debug gem
export COMPLETELY_DEV=1
1 change: 1 addition & 0 deletions lib/completely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require 'completely/completions'
require 'completely/tester'
require 'completely/installer'
require 'debug' if ENV['COMPLETELY_DEV']
13 changes: 8 additions & 5 deletions lib/completely/commands/install.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'tempfile'
require 'completely/commands/base'

module Completely
Expand Down Expand Up @@ -37,14 +36,18 @@ def run
end

def installer
@installer ||= Installer.new(program: args['PROGRAM'], script_path: script_path)
@installer ||= if stdin?
Installer.from_io program:
else
Installer.new program:, script_path: input_script_path
end
end

private

def script_path
@script_path ||= args['SCRIPT_PATH'] || 'completely.bash'
end
def program = args['PROGRAM']
def stdin? = input_script_path == '-'
def input_script_path = args['SCRIPT_PATH'] || 'completely.bash'
end
end
end
38 changes: 29 additions & 9 deletions lib/completely/installer.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
module Completely
class Installer
attr_reader :program, :script_path
class << self
def from_io(program:, io: nil)
io ||= $stdin

def initialize(program:, script_path: nil)
if script_path == '-'
raise InstallError, 'Nothing is piped on stdin' if $stdin.tty?
raise InstallError, "io must respond to #read" unless io.respond_to?(:read)
raise InstallError, "io is closed" if io.respond_to?(:closed?) && io.closed?

from_string program:, string: io.read
end

def from_string(program:, string:)
tempfile = create_tempfile
script_path = tempfile.path
File.write script_path, $stdin.read
begin
File.write script_path, string
ensure
tempfile.close
end

new program:, script_path:
end

def create_tempfile
tempfile = Tempfile.new ["completely-", '.bash']
tempfiles.push tempfile
tempfile
end

def tempfiles = @tempfiles ||= []
end

attr_reader :program, :script_path

def initialize(program:, script_path: nil)
@program = program
@script_path = script_path
end
Expand Down Expand Up @@ -67,10 +91,6 @@ def uninstall

private

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

def target_exist?
File.exist? target_path
end
Expand Down
16 changes: 3 additions & 13 deletions spec/completely/commands/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
context 'with PROGRAM - (stdin)' do
it 'invokes the Installer using a temp file' do
allow(subject).to receive(:installer).and_return(mock_installer)
allow($stdin).to receive_messages(tty?: false, read: 'dummy data')
allow($stdin).to receive_messages(read: 'dummy data')

expect(mock_installer).to receive(:install)

Expand All @@ -57,23 +57,13 @@

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

expect(mock_installer).not_to receive(:install)

expect { subject.execute %w[install completely-test - --dry] }
.to output_approval('cli/install/stdin-dry')
.except(/[^\s]*stdin-completely-[^\s]*/, '<tmpfile-path>')
end

context 'when stdin is empty' do
it 'raises InstallError' do
allow($stdin).to receive_messages(tty?: true, read: nil)
expect(mock_installer).not_to receive(:install)

expect { subject.execute %w[install completely-test - --dry] }
.to raise_error(InstallError, 'Nothing is piped on stdin')
end
.except(/cp [^\s]*completely-[^\s]*/, 'cp <tmpfile-path>')
end
end

Expand Down
20 changes: 12 additions & 8 deletions spec/completely/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@
%w[sudo rm -f] + targets
end

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

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

subject
describe '::from_string' do
let(:string) { 'dummy data' }
subject { described_class.from_string program:, string: }

expect(File.read subject.script_path).to eq 'dummy data'
end
it 'reads the script from io and writes it to a temp file' do
expect(File.read subject.script_path).to eq 'dummy data'
end
end

Expand Down