File: test_framework_detection_test.rb

package info (click to toggle)
ruby-shoulda-context 2.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: ruby: 1,712; sh: 200; makefile: 4
file content (147 lines) | stat: -rw-r--r-- 4,037 bytes parent folder | download | duplicates (2)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require "test_helper"
require "tempfile"

class TestFrameworkDetectionTest < PARENT_TEST_CASE
  if Tests::CurrentBundle.instance.current_appraisal == "rails_5_2"
    should "detect Minitest 5.x w/ Rails 5.2" do
      assert_integration_with_rails_and "Minitest::Test"
    end
  end

  if Tests::CurrentBundle.instance.current_appraisal == "rails_5_1"
    should "detect Minitest 5.x w/ Rails 5.1" do
      assert_integration_with_rails_and "Minitest::Test"
    end
  end

  if Tests::CurrentBundle.instance.current_appraisal == "rails_5_0"
    should "detect Minitest 5.x w/ Rails 5.0" do
      assert_integration_with_rails_and "Minitest::Test"
    end
  end

  if Tests::CurrentBundle.instance.current_appraisal == "rails_4_2"
    should "detect ActiveSupport::TestCase and Minitest 4.x w/ Rails 4.2" do
      assert_integration_with_rails_and "Minitest::Test"
    end
  end

  if TEST_FRAMEWORK == "minitest"
    should "detect Minitest 5.x when it is loaded standalone" do
      assert_integration_with "Minitest::Test", setup: <<-RUBY
        require "minitest/autorun"
      RUBY
    end
  end

  if TEST_FRAMEWORK == "test_unit"
    should "detect the test-unit gem when it is loaded standalone" do
      assert_integration_with "Test::Unit::TestCase",
        setup: <<-RUBY
          require "test/unit"
        RUBY
    end
  end

  def assert_integration_with_rails_and(*test_cases)
    test_cases = ["ActiveSupport::TestCase"] | test_cases
    options = test_cases.last.is_a?(Hash) ? test_cases.pop : {}
    options[:setup] = <<-RUBY
      require "rails/all"
      require "rails/test_help"
      ActiveRecord::Base.establish_connection(
        adapter: "sqlite3",
        database: ":memory:"
      )
    RUBY
    args = test_cases + [options]

    assert_integration_with(*args)
  end

  def assert_integration_with(*test_cases)
    assert_test_cases_are_detected(*test_cases)
    assert_our_api_is_available_in_test_cases(*test_cases)
  end

  def assert_test_cases_are_detected(*expected_test_cases)
    options = expected_test_cases.last.is_a?(Hash) ? expected_test_cases.pop : {}
    setup = options[:setup] || ""
    output = execute(file_that_detects_test_framework_test_cases([setup]))
    actual_test_cases = output.split("\n").first.split(", ")
    assert_equal expected_test_cases, actual_test_cases
  end

  def file_that_detects_test_framework_test_cases(mixins)
    <<-RUBY
      #{require_gems(mixins)}
      require "yaml"
      test_cases =
      Shoulda::Context.test_framework_test_cases.map do |test_case|
        test_case.to_s
      end
      puts test_cases.join(', ')
    RUBY
  end

  def require_gems(mixins)
    <<-RUBY
      ENV["BUNDLE_GEMFILE"] =
        "#{PROJECT_DIR}/gemfiles/" +
        "#{Tests::CurrentBundle.instance.current_appraisal}.gemfile"
      require "bundler"
      Bundler.setup
      #{mixins.join("\n")}
      require "shoulda-context"
    RUBY
  end

  def assert_our_api_is_available_in_test_cases(*test_cases)
    options = test_cases.last.is_a?(Hash) ? test_cases.pop : {}
    setup = options[:setup] || ""

    test_cases.each do |test_case|
      output = execute(
        file_that_runs_a_test_within_test_case(test_case, [setup])
      )
      assert_match(/1 (tests|runs)/, output)
      assert_match(/1 assertions/, output)
      assert_match(/0 failures/, output)
      assert_match(/0 errors/, output)
    end
  end

  def file_that_runs_a_test_within_test_case(test_case, mixins)
    <<-RUBY
      #{require_gems(mixins)}

      class FrameworkIntegrationTest < #{test_case}
        context "a context" do
          should "have a test" do
            assert_equal true, true
          end
        end
      end
    RUBY
  end

  def execute(code)
    tempfile = Tempfile.new("shoulda-context-test")
    tempfile.write(code)
    tempfile.close

    if ENV["DEBUG"]
      puts "Code:"
      puts code
    end

    output = `RUBYOPT="" ruby #{tempfile.path} 2>/dev/null`

    if ENV["DEBUG"]
      puts "Output:"
      puts output
    end

    output
  end
end