summaryrefslogtreecommitdiff
path: root/spec/mspec/tool/tag_from_output.rb
blob: b6b46038556ae1661f48142b4d969508bbd0591f (plain)
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
#!/usr/bin/env ruby

# Adds tags based on error and failures output (e.g., from a CI log),
# without running any spec code.

tag = ENV["TAG"] || "fails"

tags_dir = %w[
  spec/tags
  spec/tags/ruby
].find { |dir| Dir.exist?("#{dir}/language") }
abort 'Could not find tags directory' unless tags_dir

output = ARGF.readlines

# Automatically strip datetime of GitHub Actions
if output.first =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z /
  output = output.map { |line| line.split(' ', 2).last }
end

NUMBER = /^\d+\)$/
ERROR_OR_FAILED = / (ERROR|FAILED)$/
SPEC_FILE = /^(\/.+_spec\.rb)\:\d+/

output.slice_before(NUMBER).select { |number, *rest|
  number =~ NUMBER and rest.any? { |line| line =~ ERROR_OR_FAILED }
}.each { |number, *rest|
  error_line = rest.find { |line| line =~ ERROR_OR_FAILED }
  description = error_line.match(ERROR_OR_FAILED).pre_match

  spec_file = rest.find { |line| line =~ SPEC_FILE }
  if spec_file
    spec_file = spec_file[SPEC_FILE, 1] or raise
  else
    if error_line =~ /^([\w:]+)[#\.](\w+) /
      mod, method = $1, $2
      file = "#{mod.downcase.gsub('::', '/')}/#{method}_spec.rb"
      spec_file = ['spec/ruby/core', 'spec/ruby/library', *Dir.glob('spec/ruby/library/*')].find { |dir|
        path = "#{dir}/#{file}"
        break path if File.exist?(path)
      }
    end

    unless spec_file
      warn "Could not find file for:\n#{error_line}"
      next
    end
  end

  prefix = spec_file.index('spec/ruby/') || spec_file.index('spec/truffle/')
  spec_file = spec_file[prefix..-1]

  tags_file = spec_file.sub('spec/ruby/', "#{tags_dir}/").sub('spec/truffle/', "#{tags_dir}/truffle/")
  tags_file = tags_file.sub(/_spec\.rb$/, '_tags.txt')

  dir = File.dirname(tags_file)
  Dir.mkdir(dir) unless Dir.exist?(dir)

  tag_line = "#{tag}:#{description}"
  lines = File.exist?(tags_file) ? File.readlines(tags_file, chomp: true) : []
  unless lines.include?(tag_line)
    puts tags_file
    File.write(tags_file, (lines + [tag_line]).join("\n") + "\n")
  end
}