Module: Tap::Test::FileTest

Defined in:
lib/tap/test/file_test.rb,
lib/tap/test/file_test/class_methods.rb

Overview

FileTest simplifies testing of code that works with files. FileTest provides a method-specific Tap::Root (method_root) that expedites the creation and access of files, as well as a couple standard cleanup methods.

Cleanup

By default the entire method_root directory is cleaned up at the end of the test. To prevent cleanup, either set the KEEP_OUTPUTS or KEEP_FAILURES ENV variable to ‘true’. The cleanup directories can be specified manually using cleanup_dirs class variable:

class LimitedCleanupTest < Test::Unit::TestCase

  # only cleanup the output directory under root
  acts_as_file_test :cleanup_dirs => [:output]
end

This technique is useful when you want to keep certain static files under version control, for instance.

Requirements

FileTest requires that a method_name method is provided by the including class, in order to properly set the directory for root. Test::Unit::TestCase satisfies this requirement already.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#method_rootObject (readonly)

The method-specific Tap::Root



41
42
43
# File 'lib/tap/test/file_test.rb', line 41

def method_root
  @method_root
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



35
36
37
38
# File 'lib/tap/test/file_test.rb', line 35

def self.included(base) # :nodoc:
  super
  base.extend FileTest::ClassMethods
end

Instance Method Details

#class_rootObject

Convenience method to access the class_root.



83
84
85
# File 'lib/tap/test/file_test.rb', line 83

def class_root
  self.class.class_root or raise "setup failure: no class_root has been set for #{self.class}"
end

#cleanupObject

Cleans up the method_root directory by removing the cleanup_dirs specified for the class. (by default the entire method_root directory is removed). The method_root directory will be removed if it is empty.

Override as necessary in subclasses.



56
57
58
59
60
61
# File 'lib/tap/test/file_test.rb', line 56

def cleanup
  cleanup_dirs = self.class.cleanup_dirs
  cleanup_dirs.each {|dir| clear_dir(method_root.path(dir)) }
  
  try_remove_dir(method_root.path)
end

#clear_dir(dir) ⇒ Object

Attempts to recursively remove the specified method directory and all files within it. Raises an error if the removal does not succeed.



89
90
91
92
# File 'lib/tap/test/file_test.rb', line 89

def clear_dir(dir)
  # clear out the folder if it exists
  FileUtils.rm_r(dir) if File.exists?(dir)
end

#setupObject

Sets up method_root and calls cleanup. Be sure to call super when overriding this method.



45
46
47
48
49
# File 'lib/tap/test/file_test.rb', line 45

def setup
  super
  @method_root = class_root.sub(method_name)
  cleanup
end

#teardownObject

Calls cleanup unless flagged otherwise by an ENV variable (see above). Be sure to call super when overriding this method.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tap/test/file_test.rb', line 65

def teardown
  unless method_root
    raise "teardown failure: method_root is nil (does setup call super?)"
  end
  
  # clear out the output folder if it exists, unless flagged otherwise
  unless ENV["KEEP_OUTPUTS"] == "true" || (!passed? && ENV["KEEP_FAILURES"] == "true")
    begin
      cleanup
    rescue
      raise("cleanup failure: #{$!.message}")
    end
  end
  
  try_remove_dir(class_root.path)
end

#try_remove_dir(dir) ⇒ Object

Attempts to remove the specified directory. The directory will not be removed unless fully empty (including hidden files).



96
97
98
99
100
101
102
# File 'lib/tap/test/file_test.rb', line 96

def try_remove_dir(dir)
  begin
    FileUtils.rmdir(dir) if File.directory?(dir) && Dir.glob(File.join(dir, "*")).empty?
  rescue
    # rescue cases where there is a hidden file, for example .svn
  end
end