module ShellTest::FileMethods
Attributes
Returns the absolute path to the current working directory.
Public Instance Methods
Same as prepare but does not outdent content.
# File lib/shell_test/file_methods.rb 256 def _prepare(relative_path, content=nil, options={}, &block) 257 target = path(relative_path) 258 259 if File.exists?(target) 260 FileUtils.rm(target) 261 else 262 target_dir = File.dirname(target) 263 FileUtils.mkdir_p(target_dir) unless File.exists?(target_dir) 264 end 265 266 FileUtils.touch(target) 267 File.open(target, 'w') {|io| io << content } if content 268 File.open(target, 'a', &block) if block 269 270 if mode = options[:mode] 271 FileUtils.chmod(mode, target) 272 end 273 274 atime = options[:atime] 275 mtime = options[:mtime] 276 277 if atime || mtime 278 atime ||= File.atime(target) 279 mtime ||= File.mtime(target) 280 File.utime(atime, mtime, target) 281 end 282 283 target 284 end
Returns the atime for the file under method_dir
, if it exists.
# File lib/shell_test/file_methods.rb 319 def atime(relative_path) 320 full_path = path(relative_path) 321 File.exists?(full_path) ? File.atime(full_path) : nil 322 end
Returns the absolute path to a directory specific to the current test class, specifically the class.class_dir expanded relative to the user_dir.
# File lib/shell_test/file_methods.rb 207 def class_dir 208 @class_dir ||= File.expand_path(self.class.class_dir, user_dir) 209 end
Recursively removes paths specified for cleanup by paths_to_cleanup.
# File lib/shell_test/file_methods.rb 343 def cleanup 344 if paths = self.class.paths_to_cleanup[method_name.to_sym] 345 paths.each {|path| remove(path) } 346 end 347 end
Returns the content of the file under method_dir
, if it exists.
# File lib/shell_test/file_methods.rb 306 def content(relative_path, length=nil, offset=nil) 307 full_path = path(relative_path) 308 File.exists?(full_path) ? File.read(full_path, length, offset) : nil 309 end
Returns the ctime for the file under method_dir
, if it exists.
# File lib/shell_test/file_methods.rb 325 def ctime(relative_path) 326 full_path = path(relative_path) 327 File.exists?(full_path) ? File.ctime(full_path) : nil 328 end
Globs the pattern under method_dir.
# File lib/shell_test/file_methods.rb 242 def glob(pattern) 243 Dir.glob path(pattern) 244 end
Returns true if KEEP_OUTPUTS is set to ‘true’ in ENV.
# File lib/shell_test/file_methods.rb 200 def keep_outputs? 201 ENV["KEEP_OUTPUTS"] == "true" 202 end
Returns the absolute path to a directory specific to the current test method, specifically method_name
expanded relative to class_dir.
# File lib/shell_test/file_methods.rb 213 def method_dir 214 @method_dir ||= File.expand_path(method_name.to_s, class_dir) 215 end
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.
# File lib/shell_test/file_methods.rb 224 def method_name 225 __name__ 226 end
Returns the formatted string mode (ex ‘100640’) of the file under method_dir
, if it exists.
# File lib/shell_test/file_methods.rb 313 def mode(relative_path) 314 full_path = path(relative_path) 315 File.exists?(full_path) ? sprintf("%o", File.stat(full_path).mode) : nil 316 end
Returns the mtime for the file under method_dir
, if it exists.
# File lib/shell_test/file_methods.rb 331 def mtime(relative_path) 332 full_path = path(relative_path) 333 File.exists?(full_path) ? File.mtime(full_path) : nil 334 end
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.
# File lib/shell_test/file_methods.rb 231 def path(relative_path) 232 full_path = File.expand_path(relative_path, method_dir) 233 234 unless full_path.index(method_dir) == 0 235 raise "does not make a path relative to method_dir: #{relative_path.inspect}" 236 end 237 238 full_path 239 end
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.
# File lib/shell_test/file_methods.rb 300 def prepare(relative_path, content=nil, options={}, &block) 301 content = outdent(content) if content 302 _prepare(relative_path, content, options, &block) 303 end
Creates a directory under method_dir.
# File lib/shell_test/file_methods.rb 247 def prepare_dir(relative_path) 248 target_dir = path(relative_path) 249 unless File.directory?(target_dir) 250 FileUtils.mkdir_p(target_dir) 251 end 252 target_dir 253 end
Removes a file or directory under method_dir
, if it exists.
# File lib/shell_test/file_methods.rb 337 def remove(relative_path) 338 full_path = path(relative_path) 339 FileUtils.rm_r(full_path) if File.exists?(full_path) 340 end
Calls cleanup to remove any files left over from previous test runs (for instance by running with a flag to keep outputs).
# File lib/shell_test/file_methods.rb 172 def setup 173 super 174 @user_dir = Dir.pwd 175 cleanup 176 end
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.
# File lib/shell_test/file_methods.rb 183 def teardown 184 Dir.chdir(user_dir) 185 186 unless keep_outputs? 187 cleanup 188 189 dir = method_dir 190 while dir != class_dir 191 dir = File.dirname(dir) 192 Dir.rmdir(dir) 193 end rescue(SystemCallError) 194 end 195 196 super 197 end