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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ group :test do
gem "rspec", "~> 3.13.0"
gem "rubocop", "~> 1.90.0"
gem "rubocop-performance", "~> 1.27.0"
gem "ruby-lsp", "~> 0.26.11"
gem "simplecov", "~> 1.0.0"
gem "simplecov-console", "~> 0.9.1"
end
26 changes: 23 additions & 3 deletions allure-ruby-commons/lib/allure_ruby_commons/file_writer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "tempfile"

module Allure
# Allure result file writer
class FileWriter
Expand Down Expand Up @@ -83,12 +85,30 @@ def output_dir
end

def write(name, source)
filename = File.join(output_dir, name)
File.write(filename, source)
atomic_write(name) { |temp_file| temp_file.write(source) }
end

def copy(from, to)
FileUtils.cp(from, File.join(output_dir, to))
atomic_write(to) { |temp_file| IO.copy_stream(from, temp_file) }
end

def atomic_write(name)
filename = File.join(output_dir, name)
target_mode = begin
File.stat(filename).mode
rescue Errno::ENOENT
0o644
end

Tempfile.create("#{name}.tmp", output_dir) do |temp_file|
temp_file.binmode

yield temp_file

temp_file.chmod(target_mode)
temp_file.close
File.rename(temp_file.path, filename)
end
end
end
end