File: rspec_integration.rb

package info (click to toggle)
ruby-test-construct 2.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: ruby: 762; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 1,933 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require "test_construct"
require "rspec"
module TestConstruct
  module RSpecIntegration
    module_function

    # the :test_construct metadata key can be either:
    # - true (for all defaults)
    # - a Hash of options
    # - false/missing (disable the construct for this test)
    def test_construct_options(example)
      options = test_construct_default_options
      options[:name] = example.full_description
      metadata_options = example.metadata[:test_construct]
      if metadata_options.is_a?(Hash)
        options.merge!(metadata_options)
      end
      options
    end

    def test_construct_enabled?(example)
      !!example.metadata[:test_construct]
    end

    def test_construct_default_options
      {
        base_dir:       TestConstruct.tmpdir,
        chdir:          true,
        keep_on_error:  true,
      }
    end
  end
end

RSpec.configure do |config|
  config.include TestConstruct::Helpers
  config.include TestConstruct::RSpecIntegration

  version = RSpec::Version::STRING
  major, minor, patch, rest = version.split(".")
  major, minor, patch = [major, minor, patch].map(&:to_i)

  def before_each(example)
    return unless test_construct_enabled?(example)
    options = test_construct_options(example)
    example.metadata[:construct] = setup_construct(options)
  end

  def after_each(example)
    return unless test_construct_enabled?(example)
    options = test_construct_options(example)
    teardown_construct(
      example.metadata[:construct],
      example.exception,
      options)
  end

  if major == 2 && minor >= 7
    config.before :each do
      before_each(example)
    end

    config.after :each do
      after_each(example)
    end
  elsif major == 3
    config.before :each do |example|
      before_each(example)
    end

    config.after :each do |example|
      after_each(example)
    end
  else
    raise "TestConstruct does not support RSpec #{version}"
  end
end