class TestTempFileHelper::TempFileHelper
Automatically generates and cleans up temporary files in automated tests.
The temporary directory containing all generated files and directories is set by the first of these items which is not nil
:
-
the
tmp_dir
argument toTempFileHelper.new
-
the
TEST_TMPDIR
environment variable -
Dir.mktmpdir
Attributes
Public Class Methods
@param tmpdir [String] (optional) if set, determines the temp dir
# File lib/test_temp_file_helper.rb, line 34 def initialize(tmp_dir: nil) @tmpdir = tmp_dir || ENV['TEST_TMPDIR'] || Dir.mktmpdir end
Public Instance Methods
Creates a temporary test directory relative to TEST_TMPDIR. @param relative_path [String] directory to create @return [String] File.join(@tmpdir, relative_path)
# File lib/test_temp_file_helper.rb, line 41 def mkdir(relative_path) new_dir = File.join self.tmpdir, relative_path FileUtils.mkdir_p new_dir new_dir end
Creates a temporary file relative to TEST_TMPDIR. @param relative_path [String] file to create @param content [String] (optional) content to include in the file @return [String] File.join(@tmpdir, relative_path)
# File lib/test_temp_file_helper.rb, line 51 def mkfile(relative_path, content: '') mkdir File.dirname(relative_path) filename = File.join self.tmpdir, relative_path File.open(filename, 'w') {|f| f << content} filename end
Removes all files and directories created by the instance. Should be called from the test's teardown
method.
# File lib/test_temp_file_helper.rb, line 60 def teardown FileUtils.remove_entry @tmpdir end