Skip to content

Commit 2f285fd

Browse files
author
Andrew Lu
committed
8323994: gtest runner repeats test name for every single gtest assertion
8158048: Fix failure message from jtreg gtest wrapper 8263659: Reflow GTestResultParser for better readability Reviewed-by: lucy Backport-of: 1aae980c549741cf5fc5ca51f3c299285bafa49d
1 parent 9ac4063 commit 2f285fd

File tree

2 files changed

+118
-19
lines changed

2 files changed

+118
-19
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import javax.xml.XMLConstants;
25+
import javax.xml.stream.XMLInputFactory;
26+
import javax.xml.stream.XMLStreamConstants;
27+
import javax.xml.stream.XMLStreamException;
28+
import javax.xml.stream.XMLStreamReader;
29+
import java.io.IOException;
30+
import java.io.Reader;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
import java.util.List;
36+
37+
public class GTestResultParser {
38+
private final List<String> _failedTests;
39+
40+
public GTestResultParser(Path file) {
41+
List<String> failedTests = new ArrayList<>();
42+
try (Reader r = Files.newBufferedReader(file)) {
43+
XMLInputFactory factory = XMLInputFactory.newInstance();
44+
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
45+
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
46+
XMLStreamReader xmlReader = factory.createXMLStreamReader(r);
47+
String testSuite = null;
48+
String testCase = null;
49+
while (xmlReader.hasNext()) {
50+
int code = xmlReader.next();
51+
if (code == XMLStreamConstants.START_ELEMENT) {
52+
switch (xmlReader.getLocalName()) {
53+
case "testsuite":
54+
testSuite = xmlReader.getAttributeValue("", "name");
55+
break;
56+
case "testcase":
57+
testCase = xmlReader.getAttributeValue("", "name");
58+
break;
59+
case "failure":
60+
String failedStr = testSuite + "::" + testCase;
61+
if (!failedTests.contains(failedStr)) {
62+
failedTests.add(failedStr);
63+
}
64+
break;
65+
default:
66+
// ignore
67+
}
68+
}
69+
}
70+
} catch (XMLStreamException e) {
71+
throw new IllegalArgumentException("can't open parse xml " + file, e);
72+
} catch (IOException e) {
73+
throw new IllegalArgumentException("can't open result file " + file, e);
74+
}
75+
_failedTests = Collections.unmodifiableList(failedTests);
76+
}
77+
78+
public List<String> failedTests() {
79+
return _failedTests;
80+
}
81+
}

test/hotspot/jtreg/gtest/GTestWrapper.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,24 +25,22 @@
2525
* @summary a jtreg wrapper for gtest tests
2626
* @library /test/lib
2727
* @modules java.base/jdk.internal.misc
28+
* java.xml
2829
* @requires vm.flagless
2930
* @run main/native GTestWrapper
3031
*/
3132

32-
import java.util.Arrays;
33-
import java.util.List;
34-
import java.util.Map;
35-
import java.util.stream.Stream;
36-
import java.util.stream.Collectors;
37-
38-
import java.io.File;
39-
import java.nio.file.Paths;
40-
import java.nio.file.Path;
41-
4233
import jdk.test.lib.Platform;
4334
import jdk.test.lib.Utils;
4435
import jdk.test.lib.process.ProcessTools;
45-
import jdk.test.lib.process.OutputAnalyzer;
36+
37+
import java.io.File;
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.nio.file.Paths;
41+
import java.util.Collections;
42+
import java.util.List;
43+
import java.util.Map;
4644

4745
public class GTestWrapper {
4846
public static void main(String[] args) throws Throwable {
@@ -77,13 +75,33 @@ public static void main(String[] args) throws Throwable {
7775
env.put(pathVar, path + File.pathSeparator + ldLibraryPath);
7876
}
7977

80-
pb.command(new String[] {
81-
execPath.toString(),
82-
"-jdk",
83-
System.getProperty("test.jdk"),
84-
"--gtest_catch_exceptions=0"
85-
});
86-
ProcessTools.executeCommand(pb).shouldHaveExitValue(0);
78+
Path resultFile = Paths.get("test_result.xml");
79+
pb.command(execPath.toAbsolutePath().toString(),
80+
"-jdk", Utils.TEST_JDK,
81+
"--gtest_output=xml:" + resultFile);
82+
int exitCode = ProcessTools.executeCommand(pb).getExitValue();
83+
if (exitCode != 0) {
84+
List<String> failedTests = failedTests(resultFile);
85+
String message = "gtest execution failed; exit code = " + exitCode + ".";
86+
if (!failedTests.isEmpty()) {
87+
message += " the failed tests: " + failedTests;
88+
}
89+
throw new AssertionError(message);
90+
}
91+
}
92+
93+
private static List<String> failedTests(Path xml) {
94+
if (!Files.exists(xml)) {
95+
System.err.println("WARNING: test result file (" + xml + ") hasn't been found");
96+
}
97+
98+
try {
99+
return new GTestResultParser(xml).failedTests();
100+
} catch (Throwable t) {
101+
System.err.println("WARNING: failed to parse result file (" + xml + ") " + t);
102+
t.printStackTrace();
103+
}
104+
return Collections.emptyList();
87105
}
88106

89107
private static String getJVMVariantSubDir() {

0 commit comments

Comments
 (0)