1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
# frozen_string_literal: true
require "test_prof/logging"
require "test_prof/rspec_stamp/parser"
module TestProf
# Mark RSpec examples with provided tags
module RSpecStamp
EXAMPLE_RXP = /(\s*)(\w+\s*(?:.*)\s*)(do|{)/.freeze
# RSpecStamp configuration
class Configuration
attr_reader :tags
attr_accessor :ignore_files, :dry_run
def initialize
@ignore_files = [%r{spec/support}]
@dry_run = ENV["RSTAMP_DRY_RUN"] == "1"
self.tags = ENV["RSTAMP"]
end
def dry_run?
@dry_run == true
end
def tags=(val)
@tags = if val.is_a?(String)
parse_tags(val)
else
val
end
end
private
def parse_tags(str)
str.split(/\s*,\s*/).each_with_object([]) do |tag, acc|
k, v = tag.split(":")
acc << if v.nil?
k.to_sym
else
Hash[k.to_sym, v.to_sym]
end
end
end
end
# Stamper collects statistics about applying tags
# to examples.
class Stamper
include TestProf::Logging
attr_reader :total, :failed, :ignored
def initialize
@total = 0
@failed = 0
@ignored = 0
end
def stamp_file(file, lines)
@total += lines.size
return if ignored?(file)
log :info, "(dry-run) Patching #{file}" if dry_run?
code = File.readlines(file)
@failed += RSpecStamp.apply_tags(code, lines, RSpecStamp.config.tags)
File.write(file, code.join) unless dry_run?
end
private
def ignored?(file)
ignored = RSpecStamp.config.ignore_files.find do |pattern|
file =~ pattern
end
return unless ignored
log :warn, "Ignore stamping file: #{file}"
@ignored += 1
end
def dry_run?
RSpecStamp.config.dry_run?
end
end
class << self
include TestProf::Logging
def config
@config ||= Configuration.new
end
def configure
yield config
end
# Accepts source code (as array of lines),
# line numbers (of example to apply tags)
# and an array of tags.
def apply_tags(code, lines, tags)
failed = 0
lines.each do |line|
unless stamp_example(code[line - 1], tags)
failed += 1
log :warn, "Failed to stamp: #{code[line - 1]}"
end
end
failed
end
private
# rubocop: disable Metrics/CyclomaticComplexity
# rubocop: disable Metrics/PerceivedComplexity
def stamp_example(example, tags)
matches = example.match(EXAMPLE_RXP)
return false unless matches
code = matches[2]
block = matches[3]
parsed = Parser.parse(code)
return false unless parsed
desc = parsed.desc_const || quote(parsed.desc || "works")
tags.each do |t|
if t.is_a?(Hash)
t.each_key do |k|
parsed.remove_tag(k)
parsed.add_htag(k, t[k])
end
else
parsed.remove_tag(t)
parsed.add_tag(t)
end
end
need_parens = block == "{"
tags_str = parsed.tags.map { |t| t.is_a?(Symbol) ? ":#{t}" : t }.join(", ") unless
parsed.tags.nil? || parsed.tags.empty?
unless parsed.htags.nil? || parsed.htags.empty?
htags_str = parsed.htags.map do |(k, v)|
vstr = v.is_a?(Symbol) ? ":#{v}" : quote(v)
"#{k}: #{vstr}"
end
end
replacement = "\\1#{parsed.fname}#{need_parens ? "(" : " "}"\
"#{[desc, tags_str, htags_str].compact.join(", ")}"\
"#{need_parens ? ") " : " "}\\3"
if config.dry_run?
log :info, "Patched: #{example.sub(EXAMPLE_RXP, replacement)}"
else
example.sub!(EXAMPLE_RXP, replacement)
end
true
end
# rubocop: enable Metrics/CyclomaticComplexity
# rubocop: enable Metrics/PerceivedComplexity
def quote(str)
return str unless str.is_a?(String)
if str.include?("'")
"\"#{str}\""
else
"'#{str}'"
end
end
end
end
end
require "test_prof/rspec_stamp/rspec" if TestProf.rspec?
|