Module: GollyUtils::Testing::DynamicFixtures

Extended by:
ClassMethods
Defined in:
lib/golly-utils/testing/dynamic_fixtures.rb

Overview

Provides globally-shared, cached, lazily-loaded fixtures that are generated once on-demand, and copied when access is required.

Examples:

describe 'My Git Utility' do
  include GollyUtils::Testing::DynamicFixtures

  def_fixture :git do                             # Dynamic fixture definition.
    system 'git init'                             # Expensive to create.
    File.write 'test.txt', 'hello'                # Only runs once.
    system 'git add -A && git commit -m x'
  end

  run_each_in_dynamic_fixture :git                # RSpec helper for fixture usage.

  it("detects deleted files") {                   # Runs in a copy of the fixture.
    File.delete 'test.txt'                        # Free to modify its fixture copy.
    subject.deleted_files.should == %w[test.txt]  # Other tests isolated from these these changes.
  }

  it("detects new files") {                       # Runs in a clean copy of the fixture.
    File.create 'new.txt'                         # Generated quickly by copying cache.
    subject.new_files.should == %w[new.txt]       # Unaffected by other tests' fixture modification.
  }

end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

def_fixture, run_all_in_dynamic_fixture, run_each_in_dynamic_fixture

Instance Method Details

#after_dynamic_fixture_creation(name, creation_time_in_sec) ⇒ void

This method returns an undefined value.

Callback invoked just after creating a dynamic fixture for the first time.

Override to customise.

Parameters:

  • name (Symbol)

    The name of the dynamic fixture being created.

  • creation_time_in_sec (Float)

    The number of seconds taken to create the fixture.



138
139
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 138

def after_dynamic_fixture_creation(name, creation_time_in_sec)
end

#before_dynamic_fixture_creation(name) ⇒ void

This method returns an undefined value.

Callback invoked just before creating a dynamic fixture for the first time.

Override to customise.

Parameters:

  • name (Symbol)

    The name of the dynamic fixture being created.



128
129
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 128

def before_dynamic_fixture_creation(name)
end

#copy_dynamic_fixture(name, target_dir = '.') ⇒ void

This method returns an undefined value.

Copies the contents of a dynamic fixture to a given directory.

Parameters:

  • name (Symbol|String)

    The name of the dynamic fixture to copy.

  • target_dir (String) (defaults to: '.')

    The (existing) directory to copy the fixture to.



146
147
148
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 146

def copy_dynamic_fixture(name, target_dir = '.')
  FileUtils.cp_r "#{dynamic_fixture_dir name}/.", target_dir
end

#inside_dynamic_fixture(name, options = {}) { ... } ⇒ Object

Creates a clean copy of a predefined dynamic fixture, changes directory into it and yields. The fixture copy is removed from the file system after the yield block returns.

Parameters:

  • name (Symbol|String)

    The name of the dynamic fixture.

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

Options Hash (options):

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

    A fixture subdirectory to change directory into before yielding.

Yields:

  • Yields control in the directory of a fixture copy.

Returns:

  • The value of the given block.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/golly-utils/testing/dynamic_fixtures.rb', line 158

def inside_dynamic_fixture(name, options={}, &block)
  options.validate_option_keys INSIDE_DYNAMIC_FIXTURE_OPTIONS

  Dir.mktmpdir {|dir|
    copy_dynamic_fixture name, dir
    df= get_dynamic_fixture_data(name)

    if cd_into= options[:cd_into] || df[:cd_into]
      dir= File.join dir, cd_into
    end

    $gu_dynamic_fixture_chdir_lock.synchronize {
      return Dir.chdir dir, &block
    }
  }
end