Module: LintTrappings::Spec::DirectoryHelpers

Defined in:
lib/lint_trappings/spec/directory_helpers.rb

Overview

Helpers for creating temporary directories for testing.

Class Method Summary collapse

Class Method Details

.directory(name = 'some-dir', &block) ⇒ String

Creates a directory in a temporary directory which will automatically be destroyed at the end of the spec run. Any block passed to this will be executed with the created directory as the working directory.

Returns:

  • (String)

    The full path of the directory.



12
13
14
15
16
17
18
19
20
21
# File 'lib/lint_trappings/spec/directory_helpers.rb', line 12

def directory(name = 'some-dir', &block)
  tmpdir = Dir.mktmpdir.tap do |path|
    Dir.chdir(path) do
      Dir.mkdir(name)
      Dir.chdir(name, &block) if block_given?
    end
  end

  File.join(tmpdir, name)
end