File: test_bisect.rb

package info (click to toggle)
ruby-minitest 6.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 588 kB
  • sloc: ruby: 6,908; makefile: 7
file content (235 lines) | stat: -rw-r--r-- 5,566 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
require "minitest/autorun"
require "minitest/bisect"

module TestMinitest; end

class TestMinitest::TestBisect < Minitest::Test
  attr_accessor :bisect

  def setup
    self.bisect = Minitest::Bisect.new
    bisect.reset
  end

  def test_class_run
    skip "Need to write test_class_run"
  end

  def test_bisect_files
    skip "Need to write test_bisect_files"
  end

  def test_bisect_methods
    skip "Need to write test_bisect_methods"
  end

  def test_build_files_cmd
    files = %w[a.rb b.rb c.rb]
    rb    = %w[-Ilib:test]
    mt    = %w[--seed 42]

    ruby = Minitest::Bisect::RUBY
    body = "require \"./a.rb\" ; require \"./b.rb\" ; require \"./c.rb\""

    exp = "#{ruby} -Ilib:test -e '#{body}' -- --seed 42"
    act = bisect.build_files_cmd(files, rb, mt)

    assert_equal exp, act
  end

  def test_build_methods_cmd
    cmd = "cmd"
    assert_equal "cmd", bisect.build_methods_cmd(cmd)
  end

  def test_build_methods_cmd_verify
    cmd = "cmd"
    cul = []
    bad = %w[A#test_1 B#test_2]

    exp = "cmd -n \"/^(?:A#(?:test_1)|B#(?:test_2))$/\""

    assert_equal exp, bisect.build_methods_cmd(cmd, cul, bad)
  end

  def test_build_methods_cmd_verify_same
    cmd = "cmd"
    cul = []
    bad = %w[C#test_5 C#test_6]

    exp = "cmd -n \"/^(?:C#(?:test_5|test_6))$/\""

    assert_equal exp, bisect.build_methods_cmd(cmd, cul, bad)
  end

  def test_build_methods_cmd_full
    cmd = "cmd"
    cul = %w[A#test_1 A#test_2 B#test_3 B#test_4]
    bad = %w[C#test_5 C#test_6]

    a = "A#(?:test_1|test_2)"
    b = "B#(?:test_3|test_4)"
    c = "C#(?:test_5|test_6)"
    exp = "cmd -n \"/^(?:#{a}|#{b}|#{c})$/\""

    assert_equal exp, bisect.build_methods_cmd(cmd, cul, bad)
  end

  def test_build_re
    bad = %w[A#test_1 B#test_2]

    exp = "/^(?:A#(?:test_1)|B#(?:test_2))$/"

    assert_equal exp, bisect.build_re(bad)
  end

  def test_build_re_same
    bad = %w[C#test_5 C#test_6]

    exp = "/^(?:C#(?:test_5|test_6))$/"

    assert_equal exp, bisect.build_re(bad)
  end

  def test_build_re_class_escaping
    bad = ["{}#[]"]

    exp = "/^(?:\\{\\}#(?:\\[\\]))$/"

    assert_equal exp, bisect.build_re(bad)
  end

  def test_build_re_method_escaping
    bad = ["Some Class#It shouldn't care what the name is"]

    exp = "/^(?:Some Class#(?:It shouldn\\'t care what the name is))$/"

    assert_equal exp, bisect.build_re(bad)
  end

  def test_map_failures
    bisect.failures =
      {
       "file.rb" => { "Class" => %w[test_method1 test_method2] },
       "blah.rb" => { "Apple" => %w[test_method3 test_method4] },
      }

    exp = %w[
           Apple#test_method3
           Apple#test_method4
           Class#test_method1
           Class#test_method2
          ]

    assert_equal exp, bisect.map_failures
  end

  def test_minitest_result
    bisect.minitest_result "file.rb", "TestClass", "test_method", [], 1, 1

    assert_equal false, bisect.tainted
    assert_empty bisect.failures
    assert_equal ["TestClass#test_method"], bisect.culprits
  end

  def test_minitest_result_skip
    fail = Minitest::Skip.new("woot")

    bisect.minitest_result "file.rb", "TestClass", "test_method", [fail], 1, 1

    assert_equal false, bisect.tainted
    assert_empty bisect.failures
    assert_equal ["TestClass#test_method"], bisect.culprits
  end

  def test_minitest_result_fail
    fail = Minitest::Assertion.new "msg"

    bisect.minitest_result "file.rb", "TestClass", "test_method", [fail], 1, 1

    exp = {"file.rb" => {"TestClass" => ["test_method"] }}

    assert_equal true, bisect.tainted
    assert_equal exp, bisect.failures
    assert_empty bisect.culprits
  end

  def test_minitest_result_error
    fail = Minitest::UnexpectedError.new RuntimeError.new("woot")

    bisect.minitest_result "file.rb", "TestClass", "test_method", [fail], 1, 1

    exp = {"file.rb" => {"TestClass" => ["test_method"] }}

    assert_equal true, bisect.tainted
    assert_equal exp, bisect.failures
    assert_empty bisect.culprits
  end

  def test_minitest_start
    bisect.failures["file.rb"]["Class"] << "test_bad1"

    bisect.minitest_start

    assert_empty bisect.failures
  end

  def test_reset
    bisect.seen_bad = true
    bisect.tainted  = true
    bisect.failures["file.rb"]["Class"] << "test_bad1"
    bisect.culprits << "A#test_1" << "B#test_2"

    bisect.reset

    assert_equal false, bisect.seen_bad
    assert_equal false, bisect.tainted
    assert_empty bisect.failures
    assert_equal %w[A#test_1 B#test_2], bisect.culprits
  end

  def test_run
    skip "Need to write test_run"
  end

  def test_time_it
    exp = /\Ado stuff: in 0.\d\d sec\n\z/

    assert_output exp, "" do
      bisect.time_it "do stuff:", "echo you should not see me"
    end
  end
end

class TestMinitest::TestBisect::TestPathExpander < Minitest::Test
  def test_sanity
    args = %w[1 -Iblah 2 -d 3 -w 4 5 6]

    mtbpe = Minitest::Bisect::PathExpander
    expander = mtbpe.new args

    assert_equal %w[-Itest:lib], expander.rb_flags
    assert_same mtbpe::TEST_GLOB, expander.glob
  end

  def test_process_flags
    args = %w[1 -Iblah 2 -d 3 -w 4 5 6]

    expander = Minitest::Bisect::PathExpander.new args

    exp_files = %w[1 2 3 4 5 6]
    exp_flags = %w[-Itest:lib -Iblah -d -w]

    files = expander.process_flags(args)

    assert_equal files, exp_files

    # process_flags only filters and does not mutate args
    assert_same args, expander.args
    refute_equal args, exp_files
    refute_equal files, args

    # separates rb_flags out for separate handling
    assert_equal exp_flags, expander.rb_flags
  end
end