Module: ShellTest::FileMethods

Extended by:
ModuleMethods
Includes:
StringMethods
Included in:
ShellTest
Defined in:
lib/shell_test/file_methods.rb

Defined Under Namespace

Modules: ClassMethods, ModuleMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ModuleMethods

included

Methods included from StringMethods

#_assert_str_equal, #_assert_str_match, #assert_str_equal, #assert_str_match, #expand_ctrl_chars, #indent, #outdent, #whitespace_escape

Instance Attribute Details

#user_dirObject (readonly)

Returns the absolute path to the current working directory.



168
169
170
# File 'lib/shell_test/file_methods.rb', line 168

def user_dir
  @user_dir
end

Instance Method Details

#_prepare(relative_path, content = nil, options = {}, &block) ⇒ Object

Same as prepare but does not outdent content.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/shell_test/file_methods.rb', line 256

def _prepare(relative_path, content=nil, options={}, &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

  if mode = options[:mode]
    FileUtils.chmod(mode, target)
  end

  atime = options[:atime]
  mtime = options[:mtime]

  if atime || mtime
    atime  ||= File.atime(target)
    mtime  ||= File.mtime(target)
    File.utime(atime, mtime, target)
  end

  target
end

#atime(relative_path) ⇒ Object

Returns the atime for the file under method_dir, if it exists.



319
320
321
322
# File 'lib/shell_test/file_methods.rb', line 319

def atime(relative_path)
  full_path = path(relative_path)
  File.exists?(full_path) ? File.atime(full_path) : nil
end

#class_dirObject

Returns the absolute path to a directory specific to the current test class, specifically the class.class_dir expanded relative to the user_dir.



207
208
209
# File 'lib/shell_test/file_methods.rb', line 207

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

#cleanupObject

Recursively removes paths specified for cleanup by paths_to_cleanup.



343
344
345
346
347
# File 'lib/shell_test/file_methods.rb', line 343

def cleanup
  if paths = self.class.paths_to_cleanup[method_name.to_sym]
    paths.each {|path| remove(path) }
  end
end

#content(relative_path, length = nil, offset = nil) ⇒ Object

Returns the content of the file under method_dir, if it exists.



306
307
308
309
# File 'lib/shell_test/file_methods.rb', line 306

def content(relative_path, length=nil, offset=nil)
  full_path = path(relative_path)
  File.exists?(full_path) ? File.read(full_path, length, offset) : nil
end

#ctime(relative_path) ⇒ Object

Returns the ctime for the file under method_dir, if it exists.



325
326
327
328
# File 'lib/shell_test/file_methods.rb', line 325

def ctime(relative_path)
  full_path = path(relative_path)
  File.exists?(full_path) ? File.ctime(full_path) : nil
end

#glob(pattern) ⇒ Object

Globs the pattern under method_dir.



242
243
244
# File 'lib/shell_test/file_methods.rb', line 242

def glob(pattern)
  Dir.glob path(pattern)
end

#keep_outputs?Boolean

Returns true if KEEP_OUTPUTS is set to ‘true’ in ENV.

Returns:

  • (Boolean)


200
201
202
# File 'lib/shell_test/file_methods.rb', line 200

def keep_outputs?
  ENV["KEEP_OUTPUTS"] == "true"
end

#method_dirObject

Returns the absolute path to a directory specific to the current test method, specifically method_name expanded relative to class_dir.



213
214
215
# File 'lib/shell_test/file_methods.rb', line 213

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

#method_nameObject

Returns the method name of the current test.

Really this method is an alias for __name__ which is present in MiniTest::Unit and reproduces the method_name in Test::Unit. ShellTest::Unit ensures this method is set up correctly in those frameworks. If this module is used in other frameworks, then method_name must be implemented separately.



224
225
226
# File 'lib/shell_test/file_methods.rb', line 224

def method_name
  __name__
end

#mode(relative_path) ⇒ Object

Returns the formatted string mode (ex ‘100640’) of the file under method_dir, if it exists.



313
314
315
316
# File 'lib/shell_test/file_methods.rb', line 313

def mode(relative_path)
  full_path = path(relative_path)
  File.exists?(full_path) ? sprintf("%o", File.stat(full_path).mode) : nil
end

#mtime(relative_path) ⇒ Object

Returns the mtime for the file under method_dir, if it exists.



331
332
333
334
# File 'lib/shell_test/file_methods.rb', line 331

def mtime(relative_path)
  full_path = path(relative_path)
  File.exists?(full_path) ? File.mtime(full_path) : nil
end

#path(relative_path) ⇒ Object

Expands relative_path relative to method_dir and returns the resulting absolute path. Raises an error if the resulting path is not relative to method_dir.



231
232
233
234
235
236
237
238
239
# File 'lib/shell_test/file_methods.rb', line 231

def path(relative_path)
  full_path = File.expand_path(relative_path, method_dir)

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

  full_path
end

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

Creates a file under method_dir with the specified content, which may be provided as a string or with a block (the block recieves an open File). If no content is given, then an empty file is created.

Content provided as a string is outdented (see StringMethods#outdent), so this syntax is possible:

path = prepare 'file', %{
  line one
  line two
}
File.read(path)  # => "line one\nline two\n"

Returns the absolute path to the new file.



300
301
302
303
# File 'lib/shell_test/file_methods.rb', line 300

def prepare(relative_path, content=nil, options={}, &block)
  content = outdent(content) if content
  _prepare(relative_path, content, options, &block)
end

#prepare_dir(relative_path) ⇒ Object

Creates a directory under method_dir.



247
248
249
250
251
252
253
# File 'lib/shell_test/file_methods.rb', line 247

def prepare_dir(relative_path)
  target_dir = path(relative_path)
  unless File.directory?(target_dir)
    FileUtils.mkdir_p(target_dir)
  end
  target_dir
end

#remove(relative_path) ⇒ Object

Removes a file or directory under method_dir, if it exists.



337
338
339
340
# File 'lib/shell_test/file_methods.rb', line 337

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

#setupObject

Calls cleanup to remove any files left over from previous test runs (for instance by running with a flag to keep outputs).



172
173
174
175
176
# File 'lib/shell_test/file_methods.rb', line 172

def setup
  super
  @user_dir = Dir.pwd
  cleanup
end

#teardownObject

Generic cleanup method. Returns users to the user_dir then calls cleanup unless keep_outputs? returns true. If cleanup is called, any empty directories under method_dir are also removed.

Be sure to call super if teardown is overridden in a test case.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/shell_test/file_methods.rb', line 183

def teardown
  Dir.chdir(user_dir)

  unless keep_outputs?
    cleanup

    dir = method_dir
    while dir != class_dir
      dir = File.dirname(dir)
      Dir.rmdir(dir)
    end rescue(SystemCallError)
  end

  super
end