-
Notifications
You must be signed in to change notification settings - Fork 727
/
update_processor_test_resources.rb
71 lines (60 loc) · 3.26 KB
/
update_processor_test_resources.rb
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
# A script to update processor test files in the "src/test/resources" package with the latest test output.
# This is useful when changes are made to the annotation processor and all the tests need to have their expected values updated.
#
# You may have to run this multiple times for the tests that output several generated source files.
# In those cases the test report only details the first failure, so you will have to run `./gradlew test`
# again to generate a new test report before running this script again.
require 'rubygems'
require 'nokogiri'
def updateTestClass(test_class_result)
page = Nokogiri::HTML(open(test_class_result))
# Failing processor tests have their output in a <pre></pre> block
page.css('pre').each do |preBlock|
if preBlock.to_s.include? "[Robolectric]"
# This block is a robolectric info output, and is not a test failure
next
end
# Just a sanity check to make sure the pre block we're looking at is a processor source output
if !preBlock.to_s.include? "Source declared the same top-level types of an expected source"
puts "Pre block did not contain source. (#{test_class_result})"
next
end
# We expect to see a line like:
# Expected file: </ModelWithViewClickListener_.java>;
# Which tells us where the original processor test file lives
expected_file_match = /Expected file: <([^>]*)>/m.match(preBlock)
if expected_file_match.nil? || expected_file_match.captures.empty?
puts "Could not find expected file name in pre block (#{test_class_result})"
puts preBlock.class
puts preBlock
next
end
# The test copies the source file to the build folder. We need to modify the original file to update its expected source
expected_source_file_path_in_build_folder = expected_file_match.captures[0]
expected_source_file_path = expected_source_file_path_in_build_folder.sub!(
"build/intermediates/sourceFolderJavaResources/debug",
"src/test/resources"
)
# The error message includes the source code that was generated. We use a regex to extract the source from the following expected pattern
#
# Actual Source:
# =================
# ... Source code here
# at com.google.testing.compile.JavaSourcesSubject$CompilationClause.failWithCandidate(JavaSourcesSubject.java:224)
# at com.google.testing.compile.JavaSourcesSubject$CompilationClause.parsesAs(JavaSourcesSubject.java:186)
# at com.google.testing.compile.JavaSourcesSubject.parsesAs(JavaSourcesSubject.java:95)
actual_source_match = /Actual Source:[\s]*=*[\s]*(package.*?})[\s]*javaSources was/m.match(preBlock)
if actual_source_match.nil? || actual_source_match.captures.empty?
puts "Could not find actual source in pre block (#{test_class_result})"
next
end
puts "Updating class: #{expected_source_file_path.split('/')[-1]}"
# Finally we simply overwrite the original expected test source with the actual test output in order to update it
actual_source = actual_source_match.captures[0]
File.write(expected_source_file_path, actual_source)
end
end
# Looks through each module's build folder for debug test results
Dir.glob("*/build/reports/tests/testDebugUnitTest/classes/*.html") do |test_class_result|
updateTestClass(test_class_result)
end