From 89578d6d2a1c8e71d3799d292d10524174f46aab Mon Sep 17 00:00:00 2001 From: Kevin Olbrich Date: Fri, 15 May 2020 12:07:10 -0400 Subject: [PATCH] Adds command line argument for adjusting timeout --- lib/flog.rb | 15 +++++++++++---- lib/flog_cli.rb | 4 ++++ test/test_flog_cli.rb | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/flog.rb b/lib/flog.rb index 4e416ab..d113b2a 100644 --- a/lib/flog.rb +++ b/lib/flog.rb @@ -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. @@ -101,6 +106,7 @@ class Flog < MethodBasedSexpProcessor attr_reader :method_scores, :scores attr_reader :total_score, :totals attr_writer :threshold + attr_reader :timeout # :startdoc: @@ -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 @@ -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 @@ -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 diff --git a/lib/flog_cli.rb b/lib/flog_cli.rb index 3853635..8a52443 100644 --- a/lib/flog_cli.rb +++ b/lib/flog_cli.rb @@ -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:" diff --git a/test/test_flog_cli.rb b/test/test_flog_cli.rb index b8ec0b7..8599019 100644 --- a/test/test_flog_cli.rb +++ b/test/test_flog_cli.rb @@ -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)