1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
# frozen_string_literal: true
require "pathname"
require "securerandom"
module RSpec
module Support
module Files
private
def generate_file(destination, contents)
mkdir_p(destination)
File.open(destination, File::WRONLY | File::TRUNC | File::CREAT, 0o664) { |f|
f.write(contents)
}
end
def random_file_name(tmp: RELATIVE_TMP)
Pathname.new(tmp).join(random_string, "#{random_string}.log")
end
def file_with_directory(tmp: RELATIVE_TMP)
random_file_name(tmp: tmp).tap do |result|
mkdir_p(result)
end
end
def mkdir_p(destination)
Pathname.new(destination).dirname.mkpath
end
def random_string(length: 16)
SecureRandom.hex(length)
end
end
end
end
RSpec.configure do |config|
config.include RSpec::Support::Files
end
|