Module: Linecook::Test::FileTest

Extended by:
ModuleMethods
Included in:
Linecook::Test
Defined in:
lib/linecook/test/file_test.rb

Defined Under Namespace

Modules: ClassMethods, ModuleMethods

Instance Method Summary collapse

Methods included from ModuleMethods

included

Instance Method Details

#class_dirObject



146
147
148
# File 'lib/linecook/test/file_test.rb', line 146

def class_dir
  @class_dir  ||= File.expand_path(self.class.class_dir, user_dir)
end

#cleanupObject



158
159
160
161
162
# File 'lib/linecook/test/file_test.rb', line 158

def cleanup
  if cleanup_paths = cleanup_methods[method_name.to_sym]
    cleanup_paths.each {|relative_path| remove(relative_path) }
  end
end

#cleanup_methodsObject



154
155
156
# File 'lib/linecook/test/file_test.rb', line 154

def cleanup_methods
  self.class.cleanup_methods
end

#method_dirObject



150
151
152
# File 'lib/linecook/test/file_test.rb', line 150

def method_dir
  @method_dir ||= File.expand_path(method_name.to_s, class_dir)
end

#path(relative_path) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/linecook/test/file_test.rb', line 164

def path(relative_path)
  path = File.expand_path(relative_path, method_dir)
  
  unless path.index(method_dir) == 0
    raise "does not make a path relative to method_dir: #{relative_path.inspect}"
  end
  
  path
end

#prepare(relative_path, content = nil, &block) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/linecook/test/file_test.rb', line 174

def prepare(relative_path, content=nil, &block)
  target = path(relative_path)
  
  if File.exists?(target)
    FileUtils.rm(target)
  else
    target_dir = File.dirname(target)
    FileUtils.mkdir_p(target_dir) unless File.exists?(target_dir)
  end
  
  FileUtils.touch(target)
  File.open(target, 'w') {|io| io << content } if content
  File.open(target, 'a', &block) if block
  
  target
end

#remove(relative_path) ⇒ Object



191
192
193
194
# File 'lib/linecook/test/file_test.rb', line 191

def remove(relative_path)
  full_path = path(relative_path)
  FileUtils.rm_r(full_path) if File.exists?(full_path)
end

#setupObject



121
122
123
124
# File 'lib/linecook/test/file_test.rb', line 121

def setup
  super
  cleanup
end

#teardownObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/linecook/test/file_test.rb', line 126

def teardown
  Dir.chdir(user_dir)
  
  unless ENV["KEEP_OUTPUTS"] == "true"
    cleanup
    
    dir = method_dir
    while dir != class_dir
      dir = File.dirname(dir)
      Dir.rmdir(dir)
    end rescue(SystemCallError)
  end
  
  super
end

#user_dirObject



142
143
144
# File 'lib/linecook/test/file_test.rb', line 142

def user_dir
  @user_dir   ||= File.expand_path('.')
end