Module: EacConfig::Rspec::Setup

Defined in:
lib/eac_config/rspec/setup.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



12
13
14
15
16
# File 'lib/eac_config/rspec/setup.rb', line 12

def self.extended(obj)
  obj.rspec_config.around do |example|
    obj.on_envvars_load_path_clean(example)
  end
end

Instance Method Details

#envvars_load_path_entryObject



28
29
30
# File 'lib/eac_config/rspec/setup.rb', line 28

def envvars_load_path_entry
  ::EacConfig::EnvvarsNode.new.load_path.entry
end

#on_envvars_load_path_clean(example) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/eac_config/rspec/setup.rb', line 18

def on_envvars_load_path_clean(example)
  old_value = envvars_load_path_entry.value
  begin
    envvars_load_path_entry.value = old_value = []
    example.run
  ensure
    envvars_load_path_entry.value = old_value
  end
end

#stub_eac_config_node(target_example = nil, target_file = nil, &node_builder) ⇒ Object

Wraps a RSpec example in a EacConfig node using a alternative file.

Parameters:

  • target_example (RSpec::Core::ExampleGroup) (defaults to: nil)

    The example to wrap. If not provided, it is applied to all examples.

  • target_file (Pathname) (defaults to: nil)

    The file used by the EacConfig node. If not provided, a temporary file is used.

  • node_builder (Proc)

    Should return the desired EacConfig node. If not provided, a EacConfig::YamlFileNode is created.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eac_config/rspec/setup.rb', line 40

def stub_eac_config_node(target_example = nil, target_file = nil, &node_builder)
  parent_self = self
  (target_example || rspec_config).around do |example|
    parent_self.stub_eac_config_node_on_file(target_file) do |file|
      ::EacConfig::Node
        .context.on(parent_self.stub_eac_config_node_build(file, &node_builder)) do
        example.run
      end
    end
  end
end

#stub_eac_config_node_build(file, &node_builder) ⇒ Object



52
53
54
# File 'lib/eac_config/rspec/setup.rb', line 52

def stub_eac_config_node_build(file, &node_builder)
  node_builder.present? ? node_builder.call(file) : ::EacConfig::YamlFileNode.new(file)
end

#stub_eac_config_node_on_file(target_file) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/eac_config/rspec/setup.rb', line 56

def stub_eac_config_node_on_file(target_file)
  if target_file
    yield(target_file.to_pathname)
  else
    ::EacRubyUtils::Fs::Temp.on_file { |file| yield(file) }
  end
end