Project

fstest

0.0
No commit activity in last 3 years
No release in over 3 years
Include the FSTest module for testing file existence, and file contents.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

FSTest

Blatantly stolen file and directory assertion methods from Rails::Generators::TestCase. Include the FSTest module for testing file existence, and file contents.

Installation

gem install fstest

Example

class MyClassTest < MiniTest::Unit::TestCase
  include FSTest

  def test_writes_file
    assert_no_file '/tmp/foo'
    File.open('/tmp/foo', 'w') {|fh| fh.puts "nom nom"}
    assert_file '/tmp/foo', /^nom nom$/
  end
end

Overview

I couldn't find any good examples of tests for Rails generators, so I looked at the Rails tests. Rails uses a custom test class called Rails::Generators::TestCase. This class has methods for testing the output of generators. I thought the methods would be useful outside of a Rails context, so I wrapped it up in the FSTest gem. Enjoy!

Extras

If you want to work with relative paths instead of absolute paths, you can set the 'base_directory' in a setup block.

class MyClassTest < MiniTest::Unit::TestCase
  include FSTest

  def setup
    # all assert_file assertions will be relative to your homedir
    self.base_directory = File.expand_path("~")
  end

  def test_dot_emacs
    # this will look for ~/.emacs
    assert_file '.emacs'
  end
end

I've also found it useful to use FakeFS alongside this.