Skip to content

Commit

Permalink
ADDS raise error if there is no default editor or if editing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kimpers committed Aug 28, 2017
1 parent 61c7dc1 commit a38f3bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/terjira/option_support/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
module Terjira
class Editor
def self.editor_text
editor = ENV['EDITOR']
if editor.nil? || editor.empty?
raise 'EDITOR environment variable not found. Please set a default editor.'
end

tmp_file = Tempfile.new('content')
success = system "$EDITOR #{tmp_file.path}"
content = File.read(tmp_file.path) if success
success = system "#{editor} #{tmp_file.path}"
raise 'Editor returned a non-zero exit code. Something must have gone wrong' unless success

content = File.read(tmp_file.path)

tmp_file.unlink
content
Expand Down
22 changes: 21 additions & 1 deletion spec/option/option_supportable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class TestCLI < Thor
end

it 'opens default editor for comment' do
stub_const('ENV', 'EDITOR' => 'vim')
expect_any_instance_of(Object).to receive(:system).and_return(true)
expect(File).to receive(:read).and_return("editor\ncomment")

Expand All @@ -148,7 +149,8 @@ class TestCLI < Thor
expect(resource_store.get(:description)).to be == "multiline\ndescription"
end

it 'opend default editor for description' do
it 'opens default editor for description' do
stub_const('ENV', 'EDITOR' => 'vim')
expect_any_instance_of(Object).to receive(:system).and_return(true)
expect(File).to receive(:read).and_return("editor\ndescription")

Expand All @@ -158,4 +160,22 @@ class TestCLI < Thor

expect(resource_store.get(:description)).to be == "editor\ndescription"
end

it 'raises an error if there is no default edtor' do
stub_const('ENV', 'EDITOR' => nil)
subject.options = { 'description' => 'description' }

expect { subject.suggest_options(resources: { editor: true }) }
.to raise_error('EDITOR environment variable not found. Please set a default editor.')
end

it 'raises an error if editor returns a non-zero exit code' do
stub_const('ENV', 'EDITOR' => 'vim')
expect_any_instance_of(Object).to receive(:system).and_return(false)

subject.options = { 'description' => 'description' }

expect { subject.suggest_options(resources: { editor: true }) }
.to raise_error('Editor returned a non-zero exit code. Something must have gone wrong')
end
end

0 comments on commit a38f3bc

Please sign in to comment.