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:

Attributes

tmpdir[RW]

Public Class Methods

new(tmp_dir: nil) click to toggle source

@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

mkdir(relative_path) click to toggle source

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
mkfile(relative_path, content: '') click to toggle source

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
teardown() click to toggle source

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