module ShellTest::FileMethods::ClassMethods

Attributes

class_dir[RW]
paths_to_cleanup_registry[R]

A registry tracking paths_to_cleanup for the current class.

Protected Class Methods

initialize(base) click to toggle source
   # File lib/shell_test/file_methods.rb
54 def self.initialize(base)
55   # Infers the test directory from the calling file.
56   #   'some_class_test.rb' => 'some_class_test'
57   call_line = caller.find {|value| value !~ /`(includ|inherit|extend)ed'$/ }
58 
59   if call_line
60     calling_file   = call_line.gsub(/:\d+(:in .*)?$/, "")
61     base.class_dir = calling_file.chomp(File.extname(calling_file))
62   else
63     unless Dir.respond_to?(:tmpdir)
64       require 'tmpdir'
65     end
66     base.class_dir = Dir.tmpdir
67   end
68 
69   base.reset_paths_to_cleanup
70   unless base.instance_variable_defined?(:@paths_to_cleanup_registry)
71     base.instance_variable_set(:@paths_to_cleanup_registry, {})
72   end
73 
74   unless base.instance_variable_defined?(:@default_paths_to_cleanup)
75     base.instance_variable_set(:@default_paths_to_cleanup, ['.'])
76   end
77 
78   unless base.instance_variable_defined?(:@cleanup)
79     base.instance_variable_set(:@cleanup, true)
80   end
81 end

Public Instance Methods

paths_to_cleanup() click to toggle source

A hash of (method_name, [relative_path]) pairs identifying which relative paths on each method have been marked on this class or inherited from ancestors. Entries in paths_to_cleanup should not be edited directly. Instead use:

cleanup         : turn on cleanup for methods
do_not_cleanup  : turn off cleanup for methods
default_paths_to_cleanup : set the default paths to cleanup

Or if you need very precise editing, use (with the same semantics as {define/remove/undef}_method):

define_paths_to_cleanup
remove_paths_to_cleanup
undef_paths_to_cleanup
   # File lib/shell_test/file_methods.rb
28 def paths_to_cleanup
29   @paths_to_cleanup ||= begin
30     paths_to_cleanup = {}
31 
32     ancestors.reverse.each do |ancestor|
33       next unless ancestor.kind_of?(ClassMethods)
34       ancestor.paths_to_cleanup_registry.each_pair do |key, value|
35         if value.nil?
36           paths_to_cleanup.delete(key)
37         else
38           paths_to_cleanup[key] = value
39         end
40       end
41     end
42 
43     paths_to_cleanup
44   end
45 end
reset_paths_to_cleanup() click to toggle source

Resets paths_to_cleanup such that it will be recalculated.

   # File lib/shell_test/file_methods.rb
48 def reset_paths_to_cleanup
49   @paths_to_cleanup = nil
50 end

Protected Instance Methods

cleanup(*method_names) click to toggle source

Mark the methods for cleanup using the default_paths_to_cleanup. Call without method names to mark all subsequent methods for cleanup.

    # File lib/shell_test/file_methods.rb
116 def cleanup(*method_names)
117   if method_names.empty?
118     @cleanup = true
119   else
120     method_names.each do |method_name|
121       define_paths_to_cleanup method_name, @default_paths_to_cleanup
122     end
123   end
124 end
default_paths_to_cleanup(*relative_paths) click to toggle source

Sets the default paths_to_cleanup for subsequent methods.

    # File lib/shell_test/file_methods.rb
110 def default_paths_to_cleanup(*relative_paths)
111   @default_paths_to_cleanup = relative_paths
112 end
define_paths_to_cleanup(method_name, relative_paths) click to toggle source

Define the paths_to_cleanup for the specified method. The settings are inherited, but can be overridden in subclasses.

   # File lib/shell_test/file_methods.rb
90 def define_paths_to_cleanup(method_name, relative_paths)
91   reset_paths_to_cleanup
92   paths_to_cleanup_registry[method_name.to_sym] = relative_paths
93 end
do_not_cleanup(*method_names) click to toggle source

Prevent cleanup for the methods. Call without method names to prevent cleanup for subsequent methods.

    # File lib/shell_test/file_methods.rb
128 def do_not_cleanup(*method_names)
129   if method_names.empty?
130     @cleanup = false
131   else
132     method_names.each do |method_name|
133       undef_paths_to_cleanup method_name
134     end
135   end
136 end
mark_for_cleanup?(method_name) click to toggle source

Returns true if the method should be marked for cleanup when added.

    # File lib/shell_test/file_methods.rb
139 def mark_for_cleanup?(method_name)
140   @cleanup && !paths_to_cleanup_registry.has_key?(method_name.to_sym) && method_name.to_s.index("test_") == 0
141 end
method_added(sym) click to toggle source

Overridden to ensure methods marked for cleanup are cleaned up.

Calls superclass method
    # File lib/shell_test/file_methods.rb
144 def method_added(sym)
145   super
146   if mark_for_cleanup?(sym)
147     cleanup sym
148   end
149 end
remove_paths_to_cleanup(method_name) click to toggle source

Remove the paths_to_cleanup for the method as defined on self. The paths_to_cleanup inherited from ancestors will still be in effect.

    # File lib/shell_test/file_methods.rb
 97 def remove_paths_to_cleanup(method_name)
 98   reset_paths_to_cleanup
 99   paths_to_cleanup_registry.delete(method_name.to_sym)
100 end
undef_paths_to_cleanup(method_name) click to toggle source

Undefines the paths_to_cleanup for the method, preventing inheritance from ancestors.

    # File lib/shell_test/file_methods.rb
104 def undef_paths_to_cleanup(method_name)
105   reset_paths_to_cleanup
106   paths_to_cleanup_registry[method_name.to_sym] = nil
107 end