Module: Maxitest::Testrbl
- Defined in:
- lib/maxitest/vendor/testrbl.rb
Constant Summary collapse
- PATTERNS =
[ /^(\s+)(should|test|it)\s+['"](.*)['"]\s+do\s*(?:#.*)?$/, /^(\s+)(context|describe)\s+['"]?(.*?)['"]?\s+do\s*(?:#.*)?$/, /^(\s+)def\s+(test_)([a-z_\d]+)\s*(?:#.*)?$/ ]
- OPTION_WITH_ARGUMENT =
["-I", "-r", "-n", "-e"]
- INTERPOLATION =
/\\\#\\\{.*?\\\}/
Class Method Summary collapse
- .all_test_files_in(folder) ⇒ Object
- .block_start_from_line(line) ⇒ Object
- .changed_files ⇒ Object
-
.filter_duplicate_final(patterns) ⇒ Object
only keep 1 pattern that stops matching via $.
-
.line_pattern_option(file, line) ⇒ Object
overwritten by maxitest to just return line.
-
.localize(file) ⇒ Object
fix 1.9 not being able to load local files.
- .partition_argv(argv) ⇒ Object
- .partition_options(options) ⇒ Object
-
.pattern_from_file(lines, line) ⇒ Object
usable via external tools like zeus.
- .ruby ⇒ Object
- .run(command) ⇒ Object
- .run_from_cli(argv) ⇒ Object
- .test_pattern_from_line(line) ⇒ Object
- .test_pattern_from_match(method, test_name) ⇒ Object
Class Method Details
.all_test_files_in(folder) ⇒ Object
69 70 71 |
# File 'lib/maxitest/vendor/testrbl.rb', line 69 def self.all_test_files_in(folder) Dir[File.join(folder, "{**/,}*_{test,spec}.rb")].uniq end |
.block_start_from_line(line) ⇒ Object
133 134 135 136 137 |
# File 'lib/maxitest/vendor/testrbl.rb', line 133 def self.block_start_from_line(line) if line =~ /^(\s*).* do( \|.*\|)?$/ [$1, nil] end end |
.changed_files ⇒ Object
113 114 115 116 117 |
# File 'lib/maxitest/vendor/testrbl.rb', line 113 def self.changed_files changed_files = `git status -s`.split("\n").map { |l| l.strip.split(/\s+/, 2)[1] } raise "Failed: #{changed_files}" unless $?.success? changed_files.select { |f| f =~ /_(test|spec)\.rb$/ && File.exist?(f) } end |
.filter_duplicate_final(patterns) ⇒ Object
only keep 1 pattern that stops matching via $
62 63 64 65 |
# File 'lib/maxitest/vendor/testrbl.rb', line 62 def self.filter_duplicate_final(patterns) found_final = 0 patterns.reject { |p| p.end_with?("$") and (found_final += 1) > 1 } end |
.line_pattern_option(file, line) ⇒ Object
overwritten by maxitest to just return line
40 41 42 |
# File 'lib/maxitest/vendor/testrbl.rb', line 40 def self.line_pattern_option(file, line) [file, "-n", "/#{pattern_from_file(File.readlines(file), line)}/"] end |
.localize(file) ⇒ Object
fix 1.9 not being able to load local files
91 92 93 |
# File 'lib/maxitest/vendor/testrbl.rb', line 91 def self.localize(file) file =~ /^[-a-z\d_]/ ? "./#{file}" : file end |
.partition_argv(argv) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/maxitest/vendor/testrbl.rb', line 95 def self.partition_argv(argv) next_is_option = false argv.partition do |arg| if next_is_option 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 OPTION_WITH_ARGUMENT.include?(arg) false elsif arg =~ /^-/ # multi letter option like -Itest false else true end end end end |
.partition_options(options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/maxitest/vendor/testrbl.rb', line 73 def self.() next_is_before = false .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 |
.pattern_from_file(lines, line) ⇒ Object
usable via external tools like zeus
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/maxitest/vendor/testrbl.rb', line 45 def self.pattern_from_file(lines, line) possible_lines = lines[0..(line.to_i-1)].reverse found = possible_lines.map { |line| test_pattern_from_line(line) || block_start_from_line(line) }.compact # pattern and the groups it is nested under (like describe - describe - it) last_spaces = " " * 100 patterns = found.select do |spaces, name| last_spaces = spaces if spaces.size < last_spaces.size end.map(&:last).compact return filter_duplicate_final(patterns).reverse.join(".*") if found.size > 0 raise "no test found before line #{line}" end |
.ruby ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/maxitest/vendor/testrbl.rb', line 119 def self.ruby if File.file?("Gemfile") ["ruby", "-rbundler/setup"] # faster then bundle exec ruby else ["ruby"] end end |
.run(command) ⇒ Object
127 128 129 130 131 |
# File 'lib/maxitest/vendor/testrbl.rb', line 127 def self.run(command) puts command.join(" ") STDOUT.flush # if exec fails horribly we at least see some output Kernel.exec *command end |
.run_from_cli(argv) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/maxitest/vendor/testrbl.rb', line 16 def self.run_from_cli(argv) files, = partition_argv(argv) files.concat(changed_files) if .delete("--changed") files = files.map { |f| localize(f) } , = () if files.size == 1 and files.first =~ /^(\S+):(\d+)$/ file = $1 line = $2 run(ruby + + line_pattern_option(file, line) + ) else if files.size == 1 and File.file?(files.first) run(ruby + + files + ) elsif .none? { |arg| arg =~ /^-n/ } files = files.map { |f| File.directory?(f) ? all_test_files_in(f) : f }.flatten run(ruby + + files.map { |f| "-r#{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 end end end |
.test_pattern_from_line(line) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/maxitest/vendor/testrbl.rb', line 139 def self.test_pattern_from_line(line) PATTERNS.each do |r| next unless line =~ r whitespace, method, test_name = $1, $2, $3 return [whitespace, test_pattern_from_match(method, test_name)] end nil end |
.test_pattern_from_match(method, test_name) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/maxitest/vendor/testrbl.rb', line 148 def self.test_pattern_from_match(method, test_name) regex = Regexp.escape(test_name).gsub("\\ "," ").gsub(INTERPOLATION, ".*") regex = case method when "should" optional_test_name = "(?:\(.*\))?" "#{method} #{regex}\. #{optional_test_name}$" when "describe" "#{test_name}(::)?" when "test" # test "xxx -_ yyy" # test-unit: "test: xxx -_ yyy" # activesupport: "test_xxx_-__yyy" "^test(: |_)#{regex.gsub(" ", ".")}$" when "it" "#test_\\d+_#{test_name}$" else regex end regex.gsub("'", ".") end |