Test FS is a gem that’s used in your testing environment to create a temporary directory and file structure so that you don’t have to muck around with mocking out filesystem calls or have a specific directory structure as part of your project. It’s easy to set up and easy to destroy.
sudo gem install reagent-test-fs --source=http://gems.github.com
TestFS is designed to be used from within your Unit Tests to set up a directory and then tear it down immediately after your test runs:
require 'rubygems' require 'test/unit' require 'test_fs' class LameExample def initialize(root) @root = root end def magic? File.exist?("#{@root}/path/file.txt") end end class LameExampleTest < Test::Unit::TestCase def setup @fs = setup_filesystem do |root| root.dir 'path' do |p| p.file 'file.txt' end end end def teardown @fs.destroy! end def test_magic lame_example = LameExample.new(@fs.path) assert_equal true, lame_example.magic? end end
That’s just a simple example, you can have parallel directories and nest as deep as you need:
setup_filesystem do |root| root.dir 'app' do |app| app.dir 'controllers' app.dir 'helpers app.dir 'models' end root.dir 'config' do |config| config.file 'database.yml' config.file 'environment.rb' config.dir 'environments' do |environments| environments.file 'test.rb' environments.file 'development.rb' end end
-
Teardown of filesystem is manual (e.g. @fs.destroy!) - this should be hooked into standard Test::Unit teardown if there is a filesystem that was created as part of the test
-
This doesn’t handle the creation of multiple filesytems in a single test run - new filesystems will clobber existing ones
-
The
file
method just uses FileUtils.touch to create a file, it needs a way to easily get content into the file
Copyright © 2008 Patrick Reagan of Viget Labs (patrick.reagan@viget.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.