Skip to content
Open
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
23 changes: 12 additions & 11 deletions mrblib/rf/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion spec/global_options/in_place_option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading