diff --git a/mrblib/rf/runner.rb b/mrblib/rf/runner.rb index 62ec7d0..8943f1b 100644 --- a/mrblib/rf/runner.rb +++ b/mrblib/rf/runner.rb @@ -51,7 +51,6 @@ def run if in_place write_file = write_open(filename, in_place) $output = write_file - tempfile = write_file if in_place.empty? end records = filter.new(input) @@ -60,11 +59,11 @@ def run binary_match = apply_expressions(records) warn_binary_match(filename) if binary_match - next unless tempfile + next if $output == $stdout - tempfile.close + $output.close input.close - File.rename(tempfile.path, filename) + File.rename($output.path, filename) end end @@ -83,15 +82,17 @@ def read_open(file) end def write_open(file, in_place) - if in_place.empty? - dir = File.dirname(file) - Tempfile.new('.rf', dir) - else - raise NotFound, file unless File.exist?(file) - raise NotRegularFile, file unless File.file?(file) + raise NotFound, file unless File.exist?(file) + raise NotRegularFile, file unless File.file?(file) - File.open("#{file}#{in_place}", 'w') + if in_place + File.open("#{file}#{in_place}", 'w') do |f| # rubocop: disable Style/FileWrite + f.write(File.read(file)) + end end + + dir = File.dirname(file) + Tempfile.new('.rf', dir) end def split(val) diff --git a/spec/global_options/in_place_option_spec.rb b/spec/global_options/in_place_option_spec.rb index c18dffd..8c85bb1 100644 --- a/spec/global_options/in_place_option_spec.rb +++ b/spec/global_options/in_place_option_spec.rb @@ -19,9 +19,13 @@ end it_behaves_like 'a successful exec' do - let(:file_content) { read_file("foo#{suffix}") } + let(:file_content) { read_file('foo') } it { expect(file_content).to eq "bar\n" } + + it 'creates backup file when suffix is provided' do + expect(read_file("foo#{suffix}")).to eq 'foo' unless suffix.empty? + end end end