Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds command line argument for adjusting timeout #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/flog.rb
Expand Up @@ -20,6 +20,11 @@ class Flog < MethodBasedSexpProcessor

THRESHOLD = DEFAULT_THRESHOLD # :nodoc:

##
# Timeout in seconds, can be set with --timeout.

DEFAULT_TIMEOUT = 10

##
# The scoring system hash. Maps node type to score.

Expand Down Expand Up @@ -101,6 +106,7 @@ class Flog < MethodBasedSexpProcessor
attr_reader :method_scores, :scores
attr_reader :total_score, :totals
attr_writer :threshold
attr_reader :timeout

# :startdoc:

Expand Down Expand Up @@ -190,8 +196,8 @@ def flog(*files)
# 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
def flog_ruby ruby, file="-", timeout_override = nil
flog_ruby! ruby, file, timeout_override || timeout
rescue Timeout::Error
warn "TIMEOUT parsing #{file}. Skipping."
rescue RubyParser::SyntaxError, Racc::ParseError => e
Expand All @@ -216,12 +222,12 @@ def flog_ruby ruby, file="-", timeout = 10
# 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
def flog_ruby! ruby, file="-", timeout_override = nil
@parser = (option[:parser] || RubyParser).new

warn "** flogging #{file}" if option[:verbose]

ast = @parser.process ruby, file, timeout
ast = @parser.process ruby, file, timeout_override ||timeout

return unless ast

Expand All @@ -238,6 +244,7 @@ def initialize option = {}
@mass = {}
@parser = nil
@threshold = option[:threshold] || DEFAULT_THRESHOLD
@timeout = option[:timeout] || DEFAULT_TIMEOUT
self.auto_shift_type = true
self.reset
end
Expand Down
4 changes: 4 additions & 0 deletions lib/flog_cli.rb
Expand Up @@ -131,6 +131,10 @@ def self.parse_options args = ARGV
option[:verbose] = true
end

opts.on("--timeout=N", Integer, "Set the timeout (in sec) for parsing (default: 10s)") do |n|
option[:timeout] = n
end

next if self.plugins.empty?
opts.separator "Plugin options:"

Expand Down
1 change: 1 addition & 0 deletions test/test_flog_cli.rb
Expand Up @@ -39,6 +39,7 @@ def test_cls_parse_options
"--score" => :score,
"-t" => [:threshold, "75", 0.75],
"--threshold" => [:threshold, "75", 0.75],
"--timeout" => [:timeout, "60", 60],
"-v" => :verbose,
"--verbose" => :verbose,
# TODO: (maybe)
Expand Down