rspec-cleverbacktrace 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ == This is a Really Cool Ruby Thing
2
+ === ( That deserves better documentation than this. )
@@ -0,0 +1,10 @@
1
+
2
+ Spec::Runner::CleverBacktraceTweaker
3
+ - should trim empty backtraces
4
+ - should trim single line backtraces
5
+ - should trim single file backtraces
6
+ - should trim multi file backtraces
7
+
8
+ Finished in 0.01455 seconds
9
+
10
+ 4 examples, 0 failures
@@ -0,0 +1,30 @@
1
+ require 'rspec/backtrace/clever-tweaker'
2
+
3
+ # RSpec is great, but sometimes you'd like to know a little bit more than the
4
+ # default about exceptions that get raised during a run. That's what this
5
+ # little beggar is supposed to be about: it presents a limited backtrace, that
6
+ # still includes lines within your code (which is presumed to be in ./lib)
7
+ #
8
+ # To use it, add these options to spec, however you run it:
9
+ #
10
+ # spec -r cbt-tweaker -b spec
11
+ #
12
+ # What these do:
13
+ #
14
+ # -r cbt-tweaker # ("hey, that's me!") so that spec will require this file
15
+ # -b # turns on "Noisy" backtraces, which Clever impersonates
16
+ #
17
+ # == To Do
18
+ #
19
+ # So many things - this thing is certainly not _done._ Specifically
20
+ #
21
+ # * Ironically, the tests suck
22
+ # * Don't do nasty interposition tricksies
23
+ # * Lobby for changes to RSpec to allow the previous
24
+ #
25
+
26
+
27
+ class Spec::Runner::OptionParser
28
+ #This is the least bad of many bad choices
29
+ NoisyBacktraceTweaker = ::Spec::Runner::CleverBacktraceTweaker
30
+ end
@@ -0,0 +1,82 @@
1
+ module Spec
2
+ module Runner
3
+ class CleverBacktraceTweaker < BacktraceTweaker
4
+ DefaultIgnorePatterns = [
5
+ %r{/rspec-[^/]*/lib/spec/},
6
+ %r{/spork-[^/]*/lib/spork/},
7
+ %r{/lib/ruby/},
8
+ %r{bin/spec:},
9
+ %r{bin/spork:},
10
+ %r{bin/rcov:},
11
+ %r{lib/rspec-rails},
12
+ %r{vendor/rails},
13
+ # TextMate's Ruby and RSpec plugins
14
+ %r{Ruby\.tmbundle/Support/tmruby.rb:},
15
+ %r{RSpec\.tmbundle/Support/lib},
16
+ %r{temp_textmate\.},
17
+ %r{mock_frameworks/rspec},
18
+ %r{spec_server%}
19
+ ]
20
+
21
+ def initialize(*patterns)
22
+ @ignore_patterns = []
23
+ end
24
+
25
+ def clean_up_double_slashes(line)
26
+ line.gsub!('//','/')
27
+ end
28
+
29
+ def ignore_patterns(*patterns)
30
+ @ignore_patterns += patterns
31
+ end
32
+
33
+ def ignored_patterns
34
+ DefaultIgnorePatterns + @ignore_patterns
35
+ end
36
+
37
+ def tweak_backtrace(error)
38
+ return if error.backtrace.nil?
39
+ tweaked = error.backtrace.collect do |message|
40
+ clean_up_double_slashes(message)
41
+ kept_lines = message.split("\n").select do |line|
42
+ ignored_patterns.each do |ignore|
43
+ break if line =~ ignore
44
+ end
45
+ end
46
+ kept_lines.empty?? nil : kept_lines.join("\n")
47
+ end.compact
48
+
49
+ tweaked.map! do |line|
50
+ line = line.split(/:/)
51
+ [File::expand_path(line[0]).split(File::Separator)] + line[1..-1]
52
+ end
53
+
54
+ bedrock = Dir::pwd.split(File::Separator)
55
+ bedrock.shift
56
+ prefix = ((tweaked.first||[]).first||[]).first
57
+ until tweaked.find {|line| line.first.first != prefix}
58
+ tweaked.map! do |line|
59
+ [(line[0])[1..-1]] + line[1..-1]
60
+ end
61
+ break if bedrock.shift.nil?
62
+ prefix = ((tweaked.first||[]).first||[]).first
63
+ end
64
+
65
+ paths_seen = {}
66
+ result = []
67
+ jump_header = ' \> '
68
+ tweaked.each do |line|
69
+ if paths_seen.has_key?(line.first)
70
+ result.last << paths_seen[line.first].to_s
71
+ else
72
+ index = paths_seen.keys.length + 1
73
+ result << "#{index} " + ([File::join(*line[0])] + line[1..-1]).join(":")
74
+ result << jump_header.dup
75
+ paths_seen[line.first] = index
76
+ end
77
+ end
78
+ error.set_backtrace(result.find_all{|line| line != jump_header})
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,48 @@
1
+ require 'rspec/backtrace/clever-tweaker'
2
+
3
+ describe Spec::Runner::CleverBacktraceTweaker do
4
+ before do
5
+ @exception = Exception.new
6
+ @tweaker = Spec::Runner::CleverBacktraceTweaker.new
7
+ end
8
+
9
+ it "should trim empty backtraces" do
10
+ @exception.set_backtrace([])
11
+ @tweaker.tweak_backtrace(@exception)
12
+ @exception.backtrace.should == []
13
+ end
14
+
15
+ it "should trim single line backtraces" do
16
+ @exception.set_backtrace([Dir::pwd + "/some/crazy/file.rb:5:in a bad place"])
17
+ @tweaker.tweak_backtrace(@exception)
18
+ @exception.backtrace.should == ["1 some/crazy/file.rb:5:in a bad place"]
19
+ end
20
+
21
+ it "should trim single file backtraces" do
22
+ file = Dir::pwd + "/some/file.rb"
23
+ @exception.set_backtrace([
24
+ file + ":1:in x",
25
+ file + ":2:in y",
26
+ file + ":3:in z"
27
+ ])
28
+ @tweaker.tweak_backtrace(@exception)
29
+ @exception.backtrace.should == [
30
+ "1 some/file.rb:1:in x",
31
+ ' \> 11'
32
+ ]
33
+ end
34
+
35
+ it "should trim multi file backtraces" do
36
+ @exception.set_backtrace([
37
+ "../../a/crazy/file:1:in x",
38
+ "../../a/crazy/other:2:in y",
39
+ "../../a/different/file:3:in z"
40
+ ])
41
+ @tweaker.tweak_backtrace(@exception)
42
+ @exception.backtrace.should == [
43
+ "1 crazy/file:1:in x",
44
+ "2 crazy/other:2:in y",
45
+ "3 different/file:3:in z"
46
+ ]
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ puts Dir::pwd
2
+ require 'test/unit'
3
+ begin
4
+ require 'spec'
5
+ rescue LoadError
6
+ false
7
+ end
8
+
9
+ class RSpecTest < Test::Unit::TestCase
10
+ def test_that_rspec_is_available
11
+ assert_nothing_raised("\n\n * RSpec isn't available - please run: gem install rspec *\n\n"){ ::Spec }
12
+ end
13
+
14
+ def test_that_specs_pass
15
+ assert(system(*%w{spec -f e -p **/*.rb spec}),"\n\n * Specs failed *\n\n")
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-cleverbacktrace
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Judson Lester
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-08 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: " The default backtrace handling for rspec is ... okay. It would be nice if\n you could find the source of exceptions during test runs without having\n to see *all* the backtraces, right?\n\n That's what this thing is for: it compacts backtraces, skims out things that\n should be ignored (and respects ignore_patterns), then trims filenames and \n contracts repetition into a legible format.\n"
26
+ email: nyarly@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - doc/README
33
+ - doc/Specifications
34
+ files:
35
+ - lib/rspec/backtrace/clever-tweaker.rb
36
+ - lib/cbt-tweaker.rb
37
+ - doc/README
38
+ - doc/Specifications
39
+ - spec/clever-tweaker.rb
40
+ has_rdoc: true
41
+ homepage: http://cleverbacktrace.rubyforge.org/
42
+ licenses: []
43
+
44
+ post_install_message: Another tidy package brought to you by Judson
45
+ rdoc_options:
46
+ - --inline-source
47
+ - --main
48
+ - doc/README
49
+ - --title
50
+ - rspec-cleverbacktrace-0.1 RDoc
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: cleverbacktrace
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: A backtrace tweaker too clever for its own good
72
+ test_files:
73
+ - spec_help/gem_test_suite.rb