File: testcases-check

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (84 lines) | stat: -rwxr-xr-x 2,743 bytes parent folder | download
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
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'

TESTCASE_FORMAT = %r{
  (https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/\d+)|
  (https://jihulab.com/gitlab-cn/quality/testcases/-/quality/test_cases/\d+)
}x

testcases = []
missing_testcases = []
formatted_duplicates = []
testcase_format_errors = []
missing_message = %(\n*** The following tests are missing testcase links:\n\n%s\n)
duplicate_message = %(\n*** The following tests have duplicate testcase links:\n\n%s)
format_message = %(\n*** The following testcase links are incorrectly formatted:\n\n%s\n)

test_metadata_file = ARGV.shift

unless test_metadata_file
  puts "usage: #{__FILE__} <test_metadata_file>"
  exit 1
end

file = File.read(test_metadata_file)

unless %r{.*\"examples\":\[\{\"id\"\:.*}.match?(file)
  puts "\nRspec output did not match regex. Check test-metadata.json file.\n"
  exit 1
end

puts "\nAnalyzing testcase data...\n"

data_hash = JSON.parse(file)
tests = data_hash['examples']

tests.each do |test|
  next if %r{.\/qa\/specs\/features\/sanity\/*}.match?(test['id'])

  if test['testcase']
    testcases.push([(test['testcase']).to_s, "#{test['id']} - #{test['full_description']}"])

    unless TESTCASE_FORMAT.match?(test['testcase'])
      testcase_format_errors.push(
        <<~FORMAT_ERRORS
           ==> #{test['testcase']} in file: #{test['id']} with title:
               #{test['full_description']}
        FORMAT_ERRORS
      )
    end
  else
    missing_testcases.push(" ==> #{test['id']} - #{test['full_description']}\n")
  end
end

testcase_list = testcases.group_by { |testcase| testcase.shift }.transform_values(&:flatten)

duplicates = testcase_list.select { |k, v| v.count > 1 }

unless duplicates.empty?
  duplicates.each do |duplicate|
    formatted_duplicates.append(
      <<~DUPLICATES
        Testcase link #{duplicate[0]} is used in too many tests:
         ==> #{duplicate[1].join("\n ==> ")}\n
      DUPLICATES
    )
  end
end

if formatted_duplicates.empty? && missing_testcases.empty? && testcase_format_errors.empty?
  puts "\nNo errors found."
else
  puts "\n*** Testcase link violations detected! ***\n"
  puts duplicate_message % formatted_duplicates.join("\n") unless formatted_duplicates.empty?
  puts missing_message % missing_testcases.join("\n") unless missing_testcases.empty?
  puts format_message % testcase_format_errors.join("\n") unless testcase_format_errors.empty?
  puts "\n*** Please link a unique test case from the GitLab project for the errors listed above.\n"
  puts "    See: https://docs.gitlab.com/ee/development/testing_guide/end_to_end/" \
       "best_practices.html#link-a-test-to-its-test-case " \
       "for further details on how to create test cases"
  exit 1
end