open_exception 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -18,7 +18,21 @@ Out if the box, the gem supports three editors (with the following open commands
18
18
  :textmate => '/usr/local/bin/mate -a -d -l {line} {file}',
19
19
  :macvim => '/usr/local/bin/mvim +{line} {file}'
20
20
 
21
- Note: if using emacs, you will need to be running `emacsserver`. To start the server:
21
+ Any of these will open the file and set focus on the editor. `:emacs` is the default.
22
+
23
+ If you are using emacs, you can also open the backtrace along with the file using `:emacs_with_trace` as the `:open_with` argument. This uses:
24
+ /usr/bin/emacsclient -e '(open-trace-and-file "{tracefile}" "{file}" {line})'
25
+ as the open command. To use this, you will need to add the following function to your emacs init:
26
+
27
+ (defun open-trace-and-file (tracefile file linenum)
28
+ "Open visit TRACEFILE in one window (in compilation mode), and visit FILE at LINENUM in another"
29
+ (find-file-other-window tracefile)
30
+ (goto-line 2)
31
+ (compilation-mode)
32
+ (find-file-other-window file)
33
+ (goto-line linenum))
34
+
35
+ Note: for `emacsclient` to work, you will need to be running `emacsserver`. To start the server:
22
36
  M-x server-start
23
37
  or add the following to your init:
24
38
  (server-start)
@@ -29,9 +43,9 @@ Configuration
29
43
  To configure, pass a block to the configure method:
30
44
 
31
45
  OpenException.configure do |oe|
32
- # open_with can be one of the built in editors (:emacs, :macvim, :textmate)
33
- # or a command to execute to open the file, where {file} and {line} will be replaced
34
- # with the file path and line number, respectively. See 'Editors' above for an example.
46
+ # open_with can be one of the built in editors (:emacs, :emacs_with_trace, :macvim, :textmate)
47
+ # or a command to execute to open the file, where {file}, {line}, and {tracefile} will be replaced
48
+ # with the file path, line number, and path to tmp file holding the backtrace, respectively. See 'Editors' above for an example.
35
49
  # The default editor is :emacs.
36
50
 
37
51
  oe.open_with = :emacs
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
@@ -8,24 +8,22 @@ module OpenException
8
8
  end
9
9
 
10
10
  protected
11
- def open_file_with_growl(file, line)
12
- puts 'growl open file'
13
- growl_notify(file, line) if File.readable?(file)
14
- open_file_without_growl(file, line)
11
+ def open_file_with_growl
12
+ growl_notify if File.readable?(file_name)
13
+ open_file_without_growl
15
14
  end
16
15
 
17
- def growl_notify(file, line)
16
+ def growl_notify
18
17
  if Growl.installed?
19
18
  Growl.notify do |n|
20
19
  n.title = 'Open Exception'
21
- n.message = growl_message(file, line)
20
+ n.message = growl_message
22
21
  end
23
22
  end
24
23
  end
25
24
 
26
- def growl_message(file, line)
27
- msg = "Exception: #{exception.message} at #{exception.backtrace.first}\nOpening #{file}:#{line}"
28
- msg << " in #{options[:open_with]}" if options[:open_with].is_a?(Symbol)
25
+ def growl_message
26
+ msg = "Exception: #{exception.message} at #{exception.backtrace.first}\nOpening #{file_name}:#{line_number}"
29
27
  end
30
28
  end
31
29
  end
@@ -1,7 +1,10 @@
1
+ require 'tempfile'
2
+
1
3
  module OpenException
2
4
 
3
5
  EDITOR_COMMANDS = {
4
6
  :emacs => '/usr/bin/emacsclient -n +{line} {file}',
7
+ :emacs_with_trace => '/usr/bin/emacsclient -e \'(open-trace-and-file "{tracefile}" "{file}" {line})\'',
5
8
  :textmate => '/usr/local/bin/mate -a -d -l {line} {file}',
6
9
  :macvim => '/usr/local/bin/mvim +{line} {file}'
7
10
  }
@@ -40,19 +43,17 @@ module OpenException
40
43
  end
41
44
 
42
45
  def open
43
- if !exclude_exception?
44
- file_and_line = extract_file_and_line
45
- open_file(*file_and_line) if file_and_line
46
- end
46
+ extract_file_and_line && open_file unless exclude_exception?
47
47
  end
48
48
 
49
49
  protected
50
- attr_reader :exception
50
+ attr_reader :exception, :file_name, :line_number
51
51
 
52
52
  def extract_file_and_line
53
53
  if exception.backtrace and
54
54
  filter_backtrace(exception.backtrace) =~ /(.*?):(\d*)/
55
- [$1, $2]
55
+ @file_name = $1
56
+ @line_number = $2
56
57
  end
57
58
  end
58
59
 
@@ -92,10 +93,17 @@ module OpenException
92
93
  end
93
94
  end
94
95
 
95
- def open_file(file_name, line_number)
96
+ def open_file
96
97
  if File.readable?(file_name)
97
98
  cmd = open_command.gsub('{file}', file_name).gsub('{line}', line_number)
98
- puts cmd
99
+ if cmd =~ /\{tracefile\}/
100
+ Tempfile.open('open_exception-trace') do |f|
101
+ f << exception.message
102
+ f << "\n"
103
+ f << exception.backtrace.join("\n")
104
+ cmd.gsub!('{tracefile}', f.path)
105
+ end
106
+ end
99
107
  system(cmd)
100
108
  end
101
109
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{open_exception}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Crawley"]
12
- s.date = %q{2010-04-29}
12
+ s.date = %q{2010-05-15}
13
13
  s.description = %q{}
14
14
  s.email = %q{tcrawley@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,37 +1,45 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'tempfile'
2
3
 
3
4
  describe "OpenException" do
4
5
  describe "parsing an exception" do
5
6
  it "should extract the file and line number" do
6
- opener(stub_exception).send(:extract_file_and_line).should == ['/some/file.rb',
7
- '1']
7
+ @opener = opener(stub_exception)
8
+ @opener.send(:extract_file_and_line)
9
+ @opener.send(:file_name).should == '/some/file.rb'
10
+ @opener.send(:line_number).should == '1'
8
11
  end
9
12
 
10
13
  it "should filter with a regex" do
11
14
  @opener = opener(stub_exception,
12
15
  :backtrace_line_filters => [/other_file/])
13
- @opener.send(:extract_file_and_line).should == ["/some/other_file.rb", '22']
16
+ @opener.send(:extract_file_and_line)
17
+ @opener.send(:file_name).should == "/some/other_file.rb"
18
+ @opener.send(:line_number).should == '22'
14
19
  end
15
20
 
16
21
  it "should filter with a lambda" do
17
22
  @opener = opener(stub_exception,
18
23
  :backtrace_line_filters => [lambda { |line| line =~ /other_file/ }])
19
- @opener.send(:extract_file_and_line).should == ["/some/other_file.rb", '22']
24
+ @opener.send(:extract_file_and_line)
25
+ @opener.send(:file_name).should == "/some/other_file.rb"
26
+ @opener.send(:line_number).should == '22'
20
27
  end
21
28
 
22
29
  it "should filter with an array of filters" do
23
30
  @opener = opener(stub_exception,
24
31
  :backtrace_line_filters => [/not gonna match/,
25
- /other_file/])
26
- @opener.send(:extract_file_and_line).should == ["/some/other_file.rb", '22']
32
+ /other_file/])
33
+ @opener.send(:extract_file_and_line)
34
+ @opener.send(:file_name).should == "/some/other_file.rb"
35
+ @opener.send(:line_number).should == '22'
27
36
  end
28
37
  end
29
38
 
30
39
  describe "opening an exception" do
31
- it "should pass the exception args to open_file" do
40
+ it "should try to open the file" do
32
41
  @opener = opener(stub_exception)
33
- @opener.should_receive(:open_file).with('/some/file.rb',
34
- '1')
42
+ @opener.should_receive(:open_file)
35
43
  @opener.open
36
44
  end
37
45
 
@@ -72,12 +80,29 @@ describe "OpenException" do
72
80
  it "should not try to open a file that does not exist" do
73
81
  @opener = opener(nil)
74
82
  @opener.should_not_receive(:system)
75
- @opener.send(:open_file, '/a/nonexistent/file', '0')
83
+ @opener.file_name = '/a/nonexistent/file'
84
+ @opener.send(:open_file)
85
+ end
86
+
87
+ it "should write out the trace to a file if the open_command contains {tracefile}" do
88
+ File.stub!(:readable?).and_return(true)
89
+ @opener = opener(stub_exception)
90
+ @opener.stub!(:open_command).and_return('{tracefile}')
91
+ Tempfile.should_receive(:open)
92
+ @opener.file_name = '/a/file'
93
+ @opener.line_number = '2'
94
+ @opener.stub!(:system)
95
+ @opener.send(:open_file)
96
+
76
97
  end
77
98
  end
78
99
  end
79
100
  end
80
101
 
102
+ class OpenException::ExceptionOpener
103
+ attr_writer :file_name, :line_number
104
+ end
105
+
81
106
  def opener(ex, options = { })
82
107
  OpenException::ExceptionOpener.new(ex, options)
83
108
  end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,7 @@ end
12
12
 
13
13
  def stub_exception
14
14
  ex = mock('exception')
15
+ ex.stub!(:message).and_return("The message")
15
16
  ex.stub!(:backtrace).and_return([
16
17
  "/some/file.rb:1:in 'level_one'",
17
18
  "/some/other_file.rb:22:in 'level_two'",
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
7
  - 3
9
- version: 0.2.3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tobias Crawley
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-29 00:00:00 -04:00
17
+ date: 2010-05-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency