Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 9, 2013
1 parent 1413411 commit 3f9ba3e
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions lib/testrbl.rb
Expand Up @@ -7,6 +7,8 @@ module Testrbl
/^(\s+)def\s+(test_)([a-z_\d]+)\s*(?:#.*)?$/
]

OPTION_WITH_ARGUMENT = ["-I", "-r", "-n"]

# copied from minitest
MINITEST_NAME_RE = if RUBY_VERSION >= "1.9"
Regexp.new("[^[[:word:]]]+")
Expand All @@ -17,19 +19,18 @@ module Testrbl
INTERPOLATION = /\\\#\\\{.*?\\\}/

def self.run_from_cli(argv)
options, file, line = detect_usable(argv)

if file and line
file = localize(file)
run(ruby + options + [file, "-n", "/#{pattern_from_file(File.readlines(file), line)}/"])
file, line, options = detect_usable(argv)

elsif file
run(ruby + [file] + options)

# TODO use parse
elsif argv.all? { |f| File.file?(f) } # multiple files without arguments
if file
load_options, options = extract_load_options(options)
if line
file = localize(file)
run(ruby + load_options + [file, "-n", "/#{pattern_from_file(File.readlines(file), line)}/"] + options)
else
run(ruby + load_options + [file] + options)
end
elsif argv.all? { |f| File.file?(f) } # multiple files without arguments # TODO use parse
run(ruby + argv.map { |f| "-r#{localize(f)}" } + ["-e", ""])

else # pass though
# no bundle exec: projects with mini and unit-test do not run well via bundle exec testrb
run ["testrb"] + argv
Expand Down Expand Up @@ -62,6 +63,23 @@ def self.pattern_from_file(lines, line)

private

def self.extract_load_options(options)
next_is_before = false
options.partition do |option|
if next_is_before
next_is_before = false
true
else
if option =~ /^-(r|I)/
next_is_before = (option.size == 2)
true
else
false
end
end
end
end

# fix 1.9 not being able to load local files
def self.localize(file)
file =~ /^[-a-z\d_]/ ? "./#{file}" : file
Expand All @@ -73,9 +91,9 @@ def self.detect_usable(argv)
return unless files.size == 1

if files.first =~ /^(\S+):(\d+)$/
[options, $1, $2]
[$1, $2, options]
elsif File.file?(files.first)
[options, files.first, false]
[files.first, false, options]
end
end

Expand All @@ -86,7 +104,7 @@ def self.partition_argv(argv)
next_is_option = false
else
if arg =~ /^-.$/ or arg =~ /^--/ # single letter option followed by argument like -I test or long options like --verbose
next_is_option = true if ["-I", "-r", "-n"].include?(arg)
next_is_option = true if OPTION_WITH_ARGUMENT.include?(arg)
false
elsif arg =~ /^-/ # multi letter option like -Itest
false
Expand Down

0 comments on commit 3f9ba3e

Please sign in to comment.