summaryrefslogtreecommitdiff
path: root/test/rake/test_rake_backtrace.rb
blob: 98f1ee6646aab2ac622891dbab6d46713f106bc2 (plain)
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
require File.expand_path('../helper', __FILE__)
require 'open3'

class TestRakeBacktrace < Rake::TestCase
  # TODO: factor out similar code in test_rake_functional.rb
  def rake(*args)
    Open3.popen3(RUBY, "-I", @rake_lib, @rake_exec, *args) { |_, _, err, _|
      err.read
    }
  end

  def invoke(task_name)
    rake task_name.to_s
  end

  def test_single_collapse
    rakefile %q{
      task :foo do
        raise "foooo!"
      end
    }

    lines = invoke(:foo).split("\n")

    assert_equal "rake aborted!", lines[0]
    assert_equal "foooo!", lines[1]
    assert_something_matches %r!\A#{Regexp.quote Dir.pwd}/Rakefile:3!i, lines
    assert_something_matches %r!\ATasks:!, lines
  end

  def test_multi_collapse
    rakefile %q{
      task :foo do
        Rake.application.invoke_task(:bar)
      end
      task :bar do
        raise "barrr!"
      end
    }

    lines = invoke(:foo).split("\n")

    assert_equal "rake aborted!", lines[0]
    assert_equal "barrr!", lines[1]
    assert_something_matches %r!\A#{Regexp.quote Dir.pwd}/Rakefile:6!i, lines
    assert_something_matches %r!\A#{Regexp.quote Dir.pwd}/Rakefile:3!i, lines
    assert_something_matches %r!\ATasks:!, lines
  end

  def test_suppress_option
    rakefile %q{
      task :baz do
        raise "bazzz!"
      end
    }

    lines = rake("baz").split("\n")
    assert_equal "rake aborted!", lines[0]
    assert_equal "bazzz!", lines[1]
    assert_something_matches %r!Rakefile!i, lines

    lines = rake("--suppress-backtrace", ".ak.file", "baz").split("\n")
    assert_equal "rake aborted!", lines[0]
    assert_equal "bazzz!", lines[1]
    refute_match %r!Rakefile!i, lines[2]
  end

  private

  # Assert that the pattern matches at least one line in +lines+.
  def assert_something_matches(pattern, lines)
    lines.each do |ln|
      if pattern =~ ln
        assert_match pattern, ln
        return
      end
    end
    flunk "expected #{pattern.inspect} to match something in:\n    #{lines.join("\n    ")}"
  end

end