|
1 | 1 | #!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
2 | 3 |
|
3 | | -require 'find' |
4 | | -require 'set' |
| 4 | +require "find" |
5 | 5 |
|
6 | | -ALLOWED_LABELS = [ |
7 | | - 'unit', |
8 | | - 'integration', |
9 | | - 'benchmark', |
10 | | - 'transactions', |
| 6 | +ALLOWED_LABELS = %w[ |
| 7 | + unit |
| 8 | + integration |
| 9 | + benchmark |
| 10 | + transactions |
11 | 11 | ].freeze |
12 | 12 |
|
13 | | -test_dir = File.expand_path('../test', __dir__) |
| 13 | +test_dir = File.expand_path("../test", __dir__) |
14 | 14 | tests_missing_label = [] |
15 | | -invalid_labels = Set[] |
| 15 | +tests_missing_allowed_label = [] |
16 | 16 |
|
17 | 17 | Find.find(test_dir) do |path| |
18 | | - next unless File.file?(path) |
19 | | - next unless File.basename(path).start_with?('test_') |
20 | | - content = File.read(path) |
21 | | - content.delete("\n").scan(/TEST_CASE\(("(.*?)")\)/) do |match| |
22 | | - labels = match[0].scan(/\[(.*?)\]/) |
23 | | - if labels.empty? |
24 | | - tests_missing_label << "#{match[0]} (#{path})" |
25 | | - else |
26 | | - labels.flatten.each do |label| |
27 | | - unless ALLOWED_LABELS.include?(label) |
28 | | - invalid_labels << label |
29 | | - end |
30 | | - end |
31 | | - end |
| 18 | + next unless File.file?(path) |
| 19 | + next unless File.basename(path).start_with?("test_") |
| 20 | + |
| 21 | + content = File.read(path) |
| 22 | + content.delete("\n").scan(/TEST_CASE\(("(.*?)")\)/) do |match| |
| 23 | + test_name = "#{match[0]} (#{path})" |
| 24 | + labels = match[0].scan(/\[(.*?)\]/) |
| 25 | + if labels.empty? |
| 26 | + tests_missing_label << test_name |
| 27 | + elsif !labels.flatten.intersect?(ALLOWED_LABELS) |
| 28 | + tests_missing_allowed_label << test_name |
32 | 29 | end |
| 30 | + end |
33 | 31 | end |
34 | 32 |
|
35 | 33 | unless tests_missing_label.empty? |
36 | | - puts "Tests missing label:" |
37 | | - tests_missing_label.each do |test| |
38 | | - puts " #{test}" |
39 | | - end |
| 34 | + puts "Tests missing label:" |
| 35 | + tests_missing_label.each do |test| |
| 36 | + puts " #{test}" |
| 37 | + end |
40 | 38 | end |
41 | 39 |
|
42 | | -unless invalid_labels.empty? |
43 | | - puts "Invalid labels found:" |
44 | | - invalid_labels.each do |label| |
45 | | - puts " #{label}" |
46 | | - end |
| 40 | +unless tests_missing_allowed_label.empty? |
| 41 | + puts "Tests have labels, but missing allowed ones:" |
| 42 | + tests_missing_allowed_label.each do |label| |
| 43 | + puts " #{label}" |
| 44 | + end |
47 | 45 | end |
48 | 46 |
|
49 | | -unless tests_missing_label.empty? && invalid_labels.empty? |
50 | | - puts "\nERROR: Missing or unexpected test labels found. This can result in tests not being run in CI." |
51 | | - exit 1 |
| 47 | +unless tests_missing_label.empty? && tests_missing_allowed_label.empty? |
| 48 | + puts "\nERROR: Missing or unexpected test labels found. This can result in tests not being run in CI." |
| 49 | + exit 1 |
52 | 50 | end |
0 commit comments