Ruby's standard library includes a class for creating temporary directories. Similar to Tempfile Show archive.org snapshot it creates a unique directory name.
Note:
- You need to use a block or take care of the cleanup manually
- You can create a prefix and suffix e.g.
Dir.mktmpdir(['foo', 'bar']) => /tmp/foo20220912-14561-3g93n1bar
- You can choose a different base directory than
Dir.tmpdir
e.g.Dir.mktmpdir('foo', Rails.root.join('tmp')) => /home/user/rails_example/tmp/foo20220912-14561-pyr8qd
. This might be necessary when your tests are running on CI. For this you might also want to committmp/.gitkeep
to git so thetmp
dir inside your project is present.
Example
Dir.mktmpdir('exports')
=> "/tmp/exports20220912-14561-pobh0a"
Improving your parallel tests with Dir.mktmpdir
When creating directories in parallel tests manually, you normally need to handle these issues:
- When multiple processes read and write to a directory with the same name e.g.
tmp/some-test-folder
you might end up in flaky specs. - There is no guarantee that a folder is always removed e.g. in case your test run crashes. Following tests might see outdated files from a previous test run.
#mktmpdir
solves these. However, you should still remove them after you're done. Here are three options:
Option 1: Using a block
it 'does something with a temporary directory' do
Dir.mktmpdir('exports') do |export_folder_path|
# ...
end
end
Option 2: Ensuring the removal of the folder
it 'does something with a temporary directory' do
export_path = Dir.mktmpdir('exports')
# ...
ensure
FileUtils.remove_entry(export_path)
end
Option 3: Using a before/after block
before { @export_path = Dir.mktmpdir('exports') }
after do
if @export_path.present?
FileUtils.remove_entry(@export_path)
end
end
it 'does something with a temporary directory' do
# ...
end
Option 4: Without Dir.mktmpdir
Use something like "#{Rails.env}#{ENV['TEST_ENV_NUMBER']}"
in you directory path when using Dir.mkdir
.
Further reading:
- If a single temporary file is enough for you, use Tempfile.new
Posted by Emanuel to makandra dev (2022-09-12 08:45)