Module: GollyUtils::Testing::DynamicFixtures::ClassMethods

Included in:
GollyUtils::Testing::DynamicFixtures
Defined in:
lib/golly-utils/testing/dynamic_fixtures.rb

Instance Method Summary collapse

Instance Method Details

#def_fixture(name, options = {}, &block) { ... } ⇒ void

This method returns an undefined value.

Defines a dynamic fixture.

Parameters:

  • name (Symbol|String)

    The dynamic fixture name.

  • options (Hash) (defaults to: {})
  • block (Proc)

    Code that when run in an empty, temporary directory, will create the fixture contents.

Options Hash (options):

  • :cd_into (nil|String) — default: nil

    A fixture subdirectory to change directory into by default when using the fixture.

  • :dir_name (nil|String) — default: nil

    A specific name to call the temporary directory used when first creating the fixture. If nil then the name will non-deterministic.

Yields:

  • Yields control in an empty directory at a later point in time.



56
57
58
59
60
61
62
63
64
65
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 56

def def_fixture(name, options={}, &block)
  raise "Block not provided." unless block
  options.validate_option_keys :cd_into, :dir_name

  name= DynamicFixtures.normalise_dynfix_name(name)
  STDERR.warn "Dyanmic fixture being redefined: #{name}." if $gu_dynamic_fixtures[name]
  $gu_dynamic_fixtures[name]= options.merge(block: block)

  nil
end

#run_all_in_dynamic_fixture(name, options = {}) { ... } ⇒ void

This method returns an undefined value.

RSpec helper that directs that all examples be run in the same (initially-clean) copy of a given dynamic fixture.

Parameters:

  • name (Symbol|String)

    The dynamic fixture name.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :dir_name (nil|String) — default: nil

    A specific name to call the empty directory basename.

  • :cd_into (nil|String) — default: nil

    A fixture subdirectory to change directory into when running examples.

Yields:

  • Invokes the given block (if one given) once before any examples run, inside the empty dir, to perform any additional initialisation required.

See Also:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 99

def run_all_in_dynamic_fixture(name, options={}, &block)
  options.validate_option_keys :cd_into, :dir_name

  require 'golly-utils/testing/rspec/files'
  name= DynamicFixtures.normalise_dynfix_name(name) # actually just wanted dup
  run_all_in_empty_dir(options[:dir_name]) {
    copy_dynamic_fixture name
    block.() if block
  }

  if cd_into= options[:cd_into]
    class_eval <<-EOB
      around(:each){|ex|
        Dir.chdir(#{cd_into.inspect}){ ex.run }
      }
    EOB
  end
end

#run_each_in_dynamic_fixture(name, options = {}) ⇒ void

This method returns an undefined value.

RSpec helper that directs that each example be run in it's own clean copy of a given dynamic fixture.

Parameters:

See Also:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 74

def run_each_in_dynamic_fixture(name, options={})
  raise "Block not supported." if block_given?
  options.validate_option_keys DynamicFixtures::INSIDE_DYNAMIC_FIXTURE_OPTIONS

  class_eval <<-EOB
    around :each do |ex|
      inside_dynamic_fixture(#{name.inspect}, #{options.inspect}){ ex.run }
    end
  EOB

  nil
end