test_temp_file_helper Gem
The TestTempFileHelper::TempFileHelper
class manages the creation and
cleanup of temporary files in automated tests.
Downloads and API docs are available on the test_temp_file_helper RubyGems page. API documentation is written using YARD markup.
Contributed by the 18F team, part of the United States General Services Administration: https://18f.gsa.gov/
Motivation
Rather than reimplement Pyfakefs in Ruby,
in the short-term, I wrote this class to emulate Google's TEST_DATADIR
and
TEST_TMPDIR
convention instead, so that I could test code that works with
the file system.
Installation
Add this line to your application's Gemfile:
gem 'test_temp_file_helper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install test_temp_file_helper
Usage
Create a TempFileHelper
in your test's setup
method, and call
TempFileHelper.teardown
in your test's teardown
method.
require 'minitest/autorun'
require 'test_temp_file_helper'
class MyTest < ::Minitest::Test
def setup
@temp_file_helper = TestTempFileHelper::TempFileHelper.new
end
def teardown
@temp_file_helper.teardown
end
def test_something_that_handles_files
dir_path = @temp_file_helper.mkdir(File.join('path', 'to', 'dir'))
file_path = @temp_file_helper.mkfile(File.join('path', 'to', 'file'))
...
end
end
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
The path to the temporary directory itself can be accessed via
TempFileHelper.tmpdir
.
Contributing
- Fork the repo ( https://github.com/18F/test_temp_file_helper/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Feel free to ping @mbland with any questions you may have, especially if the current documentation should've addressed your needs, but didn't.
Public domain
This project is in the worldwide public domain. As stated in CONTRIBUTING:
This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication.
All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.