Skip to content

Commit

Permalink
@wip Fix issue in jt.rb with detecting test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Oct 25, 2024
1 parent 434c1ca commit ac457f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tool/jt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1392,13 +1392,24 @@ def retag(*args)

files_to_retag.each do |test_file|
puts '', test_file
test_classes = File.read(test_file).scrub.scan(/class\s+([\w:]+)\s*<.+Test/).map(&:first) # see test/mri/tests/mkmf/test_config.rb, test/mri/tests/rdoc/test_rdoc_alias.rb...

# Detect test classes in runtime
# It's difficult to find out test classes declared in a test file properly by static analysis (probably only parsing and checking AST may work)
# There are the following issues with static analysis:
# - there may be a base class for several test classes - so we cannot just look up with a regexp for classes that directly inherit Test::Unit::TestCase
# - test classes can inherit one another
# - test classes may share common test cases by including a module with test cases
# - there may be several test classes in a single test file
output = run_ruby("tool/retag_helper.rb", Dir.pwd + "/" + test_file, capture: :both)
test_classes = output.lines.map(&:chomp)

if test_classes.empty?
puts "\nWARNING: Could not find class inheriting from TestCase in #{test_file}"
next
end

puts "Preprocess exclude files for classes: #{test_classes}"

test_classes.each do |test_class|
prefix = "test/mri/excludes/#{test_class.gsub('::', '/')}"
["#{prefix}.rb", prefix].each do |file|
Expand Down
11 changes: 11 additions & 0 deletions tool/retag_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
filename = ARGV[0]

def deep_subclasses(klass)
klass.subclasses + klass.subclasses.map { |c| deep_subclasses(c) }.flatten
end

require filename.chomp('.rb')

Test::Unit::AutoRunner.need_auto_run = false;
puts deep_subclasses(Test::Unit::TestCase)

0 comments on commit ac457f1

Please sign in to comment.