Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

+ Refactored ruby processing code to flog_ruby!

+ Refactored error handling code to flog_ruby. Calls flog_ruby! for actual work
[git-p4: depot-paths = "//src/flog/dev/": change = 8069]
  • Loading branch information...
commit 10ab41b6dbeaeb8cc6f065956a0a429e7786682b 1 parent af45b88
@zenspider zenspider authored
Showing with 54 additions and 30 deletions.
  1. +41 −30 lib/flog.rb
  2. +13 −0 test/test_flog.rb
View
71 lib/flog.rb
@@ -251,43 +251,54 @@ def flog(*files_or_dirs)
files = Flog.expand_dirs_to_files(*files_or_dirs)
files.each do |file|
- begin
- # TODO: replace File.open to deal with "-"
- ruby = file == '-' ? $stdin.read : File.binread(file)
- warn "** flogging #{file}" if option[:verbose]
-
- @parser = (option[:parser] || RubyParser).new
+ ruby = file == '-' ? $stdin.read : File.binread(file)
- begin
- ast = @parser.process(ruby, file)
- rescue Timeout::Error
- warn "TIMEOUT parsing #{file}. Skipping."
- end
+ flog_ruby ruby, file
+ end
+ end
- next unless ast
- mass[file] = ast.mass
- process ast
- rescue RubyParser::SyntaxError, Racc::ParseError => e
- q = option[:quiet]
- if e.inspect =~ /<\%|%\>/ or ruby =~ /<\%|%\>/ then
- return if q
- warn "#{e.inspect} at #{e.backtrace.first(5).join(', ')}"
- warn "\n...stupid lemmings and their bad erb templates... skipping"
- else
- warn "ERROR: parsing ruby file #{file}" unless q
- unless option[:continue] then
- warn "ERROR! Aborting. You may want to run with --continue."
- raise e
- end
- return if q
- warn "%s: %s at:\n %s" % [e.class, e.message.strip,
- e.backtrace.first(5).join("\n ")]
- end
+ ##
+ # Flog the given ruby source, optionally using file to provide paths
+ # for methods. Smart. Handles syntax errors and timeouts so you
+ # don't have to.
+
+ def flog_ruby ruby, file="-", timeout = 10
+ flog_ruby! ruby, file, timeout
+ rescue Timeout::Error
+ warn "TIMEOUT parsing #{file}. Skipping."
+ rescue RubyParser::SyntaxError, Racc::ParseError => e
+ q = option[:quiet]
+ if e.inspect =~ /<\%|%\>/ or ruby =~ /<\%|%\>/ then
+ return if q
+ warn "#{e.inspect} at #{e.backtrace.first(5).join(', ')}"
+ warn "\n...stupid lemmings and their bad erb templates... skipping"
+ else
+ warn "ERROR: parsing ruby file #{file}" unless q
+ unless option[:continue] then
+ warn "ERROR! Aborting. You may want to run with --continue."
+ raise e
end
+ return if q
+ warn "%s: %s at:\n %s" % [e.class, e.message.strip,
+ e.backtrace.first(5).join("\n ")]
end
end
##
+ # Flog the given ruby source, optionally using file to provide paths for
+ # methods. Does not handle timeouts or syntax errors. See #flog_ruby.
+
+ def flog_ruby! ruby, file="-", timeout = 10
+ @parser = (option[:parser] || RubyParser).new
+
+ warn "** flogging #{file}" if option[:verbose]
+
+ ast = @parser.process ruby, file, timeout
+ mass[file] = ast.mass
+ process ast
+ end
+
+ ##
# Adds name to the class stack, for the duration of the block
def in_klass name
View
13 test/test_flog.rb
@@ -114,6 +114,19 @@ def test_flog
ensure
$stdin = old_stdin
end
+
+ def test_flog_ruby
+ ruby = "2 + 3"
+ file = "sample.rb"
+
+ @flog.flog_ruby ruby, file
+
+ exp = { "main#none" => { :+ => 1.0, :lit_fixnum => 0.6 } }
+ assert_equal exp, @flog.calls
+
+ assert_equal 1.6, @flog.total unless @flog.option[:methods]
+ assert_equal 3, @flog.mass[file]
+ end
def test_flog_erb
old_stdin = $stdin
Please sign in to comment.
Something went wrong with that request. Please try again.