Module: FileTestHelper

Includes:
FileUtils
Defined in:
lib/file_test_helper.rb

Instance Method Summary collapse

Instance Method Details

#with_files(files_with_contents = {}) ⇒ Object

Specify the files you need to create in a hash. The keys of the hash represent the file path and the value represents it’s content. Examples:

  • dir/a file’ => ‘content’ creates the ‘a dir’ directory containing an ‘a file’ file which content is ‘content’.

  • dir/’ => ” creates an empty ‘another dir’ directory. The value must be an empty string.

Optionally you can specify a different base directory (other than the default tmp) to use as a base directory. Example:

  • ‘stylesheets’, ‘file.txt’ => “” will create the file.txt file inside the existent stylesheets directory. The stylesheets directory won’t be deleted



14
15
16
17
18
# File 'lib/file_test_helper.rb', line 14

def with_files(files_with_contents = {})
  with_files_in_directory(nil, files_with_contents) do
    yield
  end
end

#with_files_in_directory(base_dir, files_with_contents) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/file_test_helper.rb', line 20

def with_files_in_directory(base_dir, files_with_contents)
  initial_directory = current_directory
  raise ArgumentError, "The base directory '#{base_dir}' does not exist." if base_dir && !File.exist?(base_dir)
  working_directory = base_dir || create_working_directory

  begin
    create_files_in_working_directory(working_directory, files_with_contents)
    yield
  ensure
    cd initial_directory
    if base_dir.nil?
      remove_dir(working_directory) if File.exist?(working_directory)
    else
      remove_files(base_dir, files_with_contents)
    end
  end
end