Skip to content
Snippets Groups Projects
Commit dd4ebe88 authored by Florian Angerer's avatar Florian Angerer
Browse files

Using Closable.

parent 3057b83b
No related branches found
No related tags found
No related merge requests found
Showing
with 267 additions and 169 deletions
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.test.packages.analyzer;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NoSuchElementException;
public class FileLineIterator extends LineIterator {
/** Maximum number of lines that will be analyzed. */
public static final int MAX_LINES = 500000;
private int cnt = 0;
private BufferedReader reader;
private String lookahead = null;
public FileLineIterator(Path p) {
try {
this.reader = Files.newBufferedReader(p);
nextLine();
} catch (IOException e) {
// ignore
}
}
@Override
public void close() throws IOException {
if (reader != null) {
reader.close();
}
}
private String nextLine() {
if (cnt++ >= MAX_LINES) {
return null;
}
String line = lookahead;
try {
lookahead = reader.readLine();
} catch (IOException e) {
}
return line;
}
@Override
public boolean hasNext() {
return cnt < MAX_LINES && lookahead != null;
}
@Override
public String next() {
if (hasNext()) {
return nextLine();
}
throw new NoSuchElementException();
}
}
......@@ -28,31 +28,38 @@ import java.util.List;
public class FileLineListReader extends FileLineReader {
private final Iterator<String> it;
private final boolean empty;
private final List<String> l;
public FileLineListReader(List<String> lines) {
Iterator<String> iterator = lines.iterator();
this.it = iterator;
this.empty = !iterator.hasNext();
}
@Override
public String readLine() throws IOException {
if (!it.hasNext()) {
return null;
}
return it.next();
this.l = lines;
}
@Override
public boolean isEmpty() {
return empty;
return l.isEmpty();
}
@Override
public Iterator<String> iterator() {
return it;
public LineIterator iterator() {
return new LineIterator() {
private final Iterator<String> it = l.iterator();
@Override
public void close() throws IOException {
// nothing to do
}
@Override
public String next() {
return it.next();
}
@Override
public boolean hasNext() {
return it.hasNext();
}
};
}
}
......@@ -22,12 +22,11 @@
*/
package com.oracle.truffle.r.test.packages.analyzer;
import java.io.IOException;
public abstract class FileLineReader implements Iterable<String> {
public abstract String readLine() throws IOException;
public abstract boolean isEmpty();
@Override
public abstract LineIterator iterator();
}
......@@ -22,71 +22,31 @@
*/
package com.oracle.truffle.r.test.packages.analyzer;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileLineStreamReader extends FileLineReader {
/** Maximum number of lines that will be analyzed. */
public static final int MAX_LINES = 50000;
private final BufferedReader reader;
private int cnt = 0;
private String lookahead = null;
private boolean empty;
private final Path p;
public FileLineStreamReader(BufferedReader in) {
this.reader = in;
empty = true;
try {
nextLine();
empty = lookahead == null;
} catch (IOException e) {
// ignore
}
}
private String nextLine() throws IOException {
if (cnt++ >= MAX_LINES) {
throw new IOException("File is too large for analysis.");
}
String line = lookahead;
lookahead = reader.readLine();
return line;
}
public FileLineStreamReader(Path p) {
this.p = p;
@Override
public String readLine() throws IOException {
return nextLine();
}
@Override
public boolean isEmpty() {
return empty;
try {
return Files.size(p) == 0;
} catch (IOException e) {
}
return true;
}
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
@Override
public boolean hasNext() {
return lookahead != null;
}
@Override
public String next() {
if (hasNext()) {
try {
return nextLine();
} catch (IOException e) {
}
}
throw new NoSuchElementException();
}
};
public LineIterator iterator() {
return new FileLineIterator(p);
}
}
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.test.packages.analyzer;
import java.io.Closeable;
import java.util.Iterator;
public abstract class LineIterator implements Iterator<String>, Closeable {
}
......@@ -22,9 +22,11 @@
*/
package com.oracle.truffle.r.test.packages.analyzer.detectors;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -49,13 +51,18 @@ public class ConfigureErrorDetector extends LineDetector {
Collection<Problem> problems = new LinkedList<>();
assert body.isEmpty() || startLocation != null;
int lineNr = startLocation != null ? startLocation.lineNr : 0;
for (String line : body) {
if (line.startsWith(PREFIX)) {
String message = line.substring(PREFIX.length());
problems.add(new ConfigureErrorProblem(pkgTestRun, this, new Location(startLocation.file, lineNr), message));
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
if (line.startsWith(PREFIX)) {
String message = line.substring(PREFIX.length());
problems.add(new ConfigureErrorProblem(pkgTestRun, this, new Location(startLocation.file, lineNr), message));
}
++lineNr;
}
++lineNr;
} catch (IOException e) {
// ignore
}
return problems;
}
......
......@@ -61,6 +61,6 @@ public abstract class Detector<T> {
* @param body The content to analyze (e.g. a list of lines in a file).
* @return A list of detected problems (must not be {@code null}).
*/
public abstract Collection<Problem> detect(RPackageTestRun pkgTestRun, Location startLineLocation, T body) throws IOException;
public abstract Collection<Problem> detect(RPackageTestRun pkgTestRun, Location startLineLocation, T body);
}
......@@ -22,11 +22,13 @@
*/
package com.oracle.truffle.r.test.packages.analyzer.detectors;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -49,12 +51,17 @@ public class InstallationProblemDetector extends LineDetector {
@Override
public Collection<Problem> detect(RPackageTestRun pkg, Location startLocation, FileLineReader body) {
int lineNr = startLocation.lineNr;
for (String line : body) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
return Collections.singletonList(new PackageInstallationProblem(pkg, this, new Location(startLocation.file, lineNr), line));
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
return Collections.singletonList(new PackageInstallationProblem(pkg, this, new Location(startLocation.file, lineNr), line));
}
++lineNr;
}
++lineNr;
} catch (IOException e) {
// ignore
}
return Collections.emptyList();
}
......
......@@ -22,12 +22,13 @@
*/
package com.oracle.truffle.r.test.packages.analyzer.detectors;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -55,21 +56,24 @@ public class RErrorDetector extends LineDetector {
int lineOffset = startLocation != null ? startLocation.lineNr : 0;
int i = -1;
Iterator<String> it = body.iterator();
while (it.hasNext()) {
String line = it.next();
++i;
Matcher matcher = PATTERN.matcher(line);
if (matcher.matches()) {
String callString = matcher.group("CALLSTR");
String message = matcher.group("MSG");
if (message.trim().isEmpty() && it.hasNext()) {
// message could be in the next line
message = it.next();
++i;
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
++i;
Matcher matcher = PATTERN.matcher(line);
if (matcher.matches()) {
String callString = matcher.group("CALLSTR");
String message = matcher.group("MSG");
if (message.trim().isEmpty() && it.hasNext()) {
// message could be in the next line
message = it.next();
++i;
}
problems.add(new RErrorProblem(pkg, this, new Location(startLocation.file, i + lineOffset), callString, message));
}
problems.add(new RErrorProblem(pkg, this, new Location(startLocation.file, i + lineOffset), callString, message));
}
} catch (IOException e) {
// ignore
}
return problems;
......
......@@ -26,6 +26,7 @@ import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -42,22 +43,25 @@ public class RInternalErrorDetector extends LineDetector {
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<Problem> detect(RPackageTestRun pkg, Location startLocation, FileLineReader body) throws IOException {
public Collection<Problem> detect(RPackageTestRun pkg, Location startLocation, FileLineReader body) {
Collection<Problem> problems = new LinkedList<>();
int lineNr = startLocation != null ? startLocation.lineNr : 0;
String line = null;
while ((line = body.readLine()) != null) {
int indexOf = line.indexOf(P);
if (indexOf != -1) {
String message = line.substring(indexOf + P.length());
problems.add(new RInternalErrorProblem(pkg, this, new Location(startLocation.file, lineNr), message));
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
int indexOf = line.indexOf(P);
if (indexOf != -1) {
String message = line.substring(indexOf + P.length());
problems.add(new RInternalErrorProblem(pkg, this, new Location(startLocation.file, lineNr), message));
}
++lineNr;
}
++lineNr;
} catch (IOException e) {
// ignore
}
return problems;
}
......
......@@ -22,9 +22,11 @@
*/
package com.oracle.truffle.r.test.packages.analyzer.detectors;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -50,22 +52,27 @@ public class SegfaultDetector extends LineDetector {
assert body.isEmpty() || startLocation != null;
int lineNr = startLocation != null ? startLocation.lineNr : 0;
boolean takeNextLine = false;
for (String line : body) {
if (line.contains(SIGSEGV_START)) {
collect = true;
}
if (collect) {
if (takeNextLine) {
segfaultMessage.append(line);
takeNextLine = false;
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
if (line.contains(SIGSEGV_START)) {
collect = true;
}
if (line.contains("Problematic frame")) {
takeNextLine = true;
} else if (!line.contains("#")) {
break;
if (collect) {
if (takeNextLine) {
segfaultMessage.append(line);
takeNextLine = false;
}
if (line.contains("Problematic frame")) {
takeNextLine = true;
} else if (!line.contains("#")) {
break;
}
}
++lineNr;
}
++lineNr;
} catch (IOException e) {
// ignore
}
if (collect) {
return Collections.singleton(new SegfaultProblem(pkg, this, new Location(startLocation.file, lineNr), segfaultMessage.toString()));
......
......@@ -22,11 +22,13 @@
*/
package com.oracle.truffle.r.test.packages.analyzer.detectors;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -50,14 +52,19 @@ public final class SymbolLookupErrorDetector extends LineDetector {
public Collection<Problem> detect(RPackageTestRun pkgTestRun, Location startLocation, FileLineReader body) {
Collection<Problem> problems = new LinkedList<>();
int lineNr = startLocation != null ? startLocation.lineNr : 0;
for (String line : body) {
Matcher matcher = PATTERN.matcher(line);
if (matcher.matches()) {
String message = matcher.group("MSG");
problems.add(new SymbolLookupErrorProblem(pkgTestRun, this, new Location(startLocation.file, lineNr), message));
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
Matcher matcher = PATTERN.matcher(line);
if (matcher.matches()) {
String message = matcher.group("MSG");
problems.add(new SymbolLookupErrorProblem(pkgTestRun, this, new Location(startLocation.file, lineNr), message));
}
++lineNr;
}
++lineNr;
} catch (IOException e) {
// ignore
}
return problems;
}
......
......@@ -22,11 +22,13 @@
*/
package com.oracle.truffle.r.test.packages.analyzer.detectors;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.oracle.truffle.r.test.packages.analyzer.LineIterator;
import com.oracle.truffle.r.test.packages.analyzer.FileLineReader;
import com.oracle.truffle.r.test.packages.analyzer.Location;
import com.oracle.truffle.r.test.packages.analyzer.Problem;
......@@ -55,14 +57,19 @@ public class UnsupportedSpecializationDetector extends LineDetector {
assert body.isEmpty() || startLocation != null;
int lineNr = startLocation != null ? startLocation.lineNr : 0;
int problemStartLine = lineNr;
for (String line : body) {
Matcher matcher = PATTERN.matcher(line);
if (matcher.find()) {
message = matcher.group("MSG");
problemStartLine = lineNr;
problems.add(new UnsupportedSpecializationProblem(pkg, this, new Location(startLocation.file, problemStartLine), message));
try (LineIterator it = body.iterator()) {
while (it.hasNext()) {
String line = it.next();
Matcher matcher = PATTERN.matcher(line);
if (matcher.find()) {
message = matcher.group("MSG");
problemStartLine = lineNr;
problems.add(new UnsupportedSpecializationProblem(pkg, this, new Location(startLocation.file, problemStartLine), message));
}
++lineNr;
}
++lineNr;
} catch (IOException e) {
// ignore
}
return problems;
}
......
......@@ -32,7 +32,6 @@ import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
......@@ -179,23 +178,15 @@ public class LogFileParser {
if (Files.isReadable(outputFile)) {
ignoreFiles.add(outputFile);
try (BufferedReader in = Files.newBufferedReader(outputFile)) {
checkResults.problems.addAll(applyDetectors(Token.OUTPUT_MISMATCH_FASTR, outputFile, 0, new FileLineStreamReader(in)));
} catch (IOException e) {
// silently ignore
}
checkResults.problems.addAll(applyDetectors(Token.OUTPUT_MISMATCH_FASTR, outputFile, 0, new FileLineStreamReader(outputFile)));
} else {
// try to find the file anywhere in the test run directory (there were some
// cases)
Optional<Path> findFirst = null;
findFirst = Files.find(logFile.path.getParent(), 3, (path, attr) -> path.getFileName().equals(outputFile.getFileName())).findFirst();
if (findFirst.isPresent()) {
try (BufferedReader in = Files.newBufferedReader(findFirst.get())) {
ignoreFiles.add(findFirst.get());
checkResults.problems.addAll(applyDetectors(Token.OUTPUT_MISMATCH_FASTR, findFirst.get(), 0, new FileLineStreamReader(in)));
} catch (IOException e) {
// silently ignore
}
ignoreFiles.add(findFirst.get());
checkResults.problems.addAll(applyDetectors(Token.OUTPUT_MISMATCH_FASTR, findFirst.get(), 0, new FileLineStreamReader(findFirst.get())));
}
if (findFirst == null || !findFirst.isPresent()) {
LOGGER.warning("Cannot read output file " + outputFile);
......@@ -314,15 +305,11 @@ public class LogFileParser {
// apply detectors to diff chunks
testing.problems.addAll(applyTestResultDetectors(diffResult));
diffResult.stream().forEach(chunk -> {
try {
if (!chunk.getLeft().isEmpty()) {
testing.problems.addAll(applyDetectors(Token.RUNNING_SPECIFIC_TESTS, chunk.getLeftFile(), chunk.getLeftStartLine(), new FileLineListReader(chunk.getLeft())));
}
if (!chunk.getRight().isEmpty()) {
testing.problems.addAll(applyDetectors(Token.RUNNING_SPECIFIC_TESTS, chunk.getRightFile(), chunk.getRightStartLine(), new FileLineListReader(chunk.getRight())));
}
} catch (IOException e) {
// ignore, since this won't happen here
if (!chunk.getLeft().isEmpty()) {
testing.problems.addAll(applyDetectors(Token.RUNNING_SPECIFIC_TESTS, chunk.getLeftFile(), chunk.getLeftStartLine(), new FileLineListReader(chunk.getLeft())));
}
if (!chunk.getRight().isEmpty()) {
testing.problems.addAll(applyDetectors(Token.RUNNING_SPECIFIC_TESTS, chunk.getRightFile(), chunk.getRightStartLine(), new FileLineListReader(chunk.getRight())));
}
});
......@@ -344,7 +331,6 @@ public class LogFileParser {
if (laMatches("Running ")) {
consumeLine();
}
// TODO anything more to parse ?
testing.problems.addAll(applyDetectors(Token.RUNNING_VIGNETTES, logFile.path, collectBody(Token.END_TESTING)));
}
......@@ -437,11 +423,7 @@ public class LogFileParser {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (endsWith(file, ".Rout", ".fail") && includeCheck.test(file)) {
try (BufferedReader in = Files.newBufferedReader(file)) {
outputCheck.problems.addAll(applyDetectors(Token.BEGIN_CHECKING, file, 0, new FileLineStreamReader(in)));
} catch (IOException e) {
LOGGER.severe(String.format("Error analyzing output file %s: %s", file, e.getMessage()));
}
outputCheck.problems.addAll(applyDetectors(Token.BEGIN_CHECKING, file, 0, new FileLineStreamReader(file)));
}
return FileVisitResult.CONTINUE;
}
......@@ -468,7 +450,7 @@ public class LogFileParser {
});
}
private Collection<Problem> applyDetectors(Token start, Path file, List<Line> body) throws IOException {
private Collection<Problem> applyDetectors(Token start, Path file, List<Line> body) {
if (!body.isEmpty()) {
Line firstLine = body.get(0);
List<String> strBody = body.stream().map(l -> l.text).collect(Collectors.toList());
......@@ -477,7 +459,7 @@ public class LogFileParser {
return new LinkedList<>();
}
private Collection<Problem> applyDetectors(Token start, Path file, int startLineNr, FileLineReader body) throws IOException {
private Collection<Problem> applyDetectors(Token start, Path file, int startLineNr, FileLineReader body) {
assert Files.isRegularFile(file);
Location startLocation = null;
if (!body.isEmpty()) {
......@@ -506,12 +488,7 @@ public class LogFileParser {
private Collection<Problem> applyTestResultDetectors(List<DiffChunk> diffChunk) {
return testResultDetectors.stream().map(detector -> {
try {
return detector.detect(pkg, null, diffChunk);
} catch (IOException e) {
// just abort, won't happen here
}
return Collections.<Problem> emptyList();
return detector.detect(pkg, null, diffChunk);
}).flatMap(l -> l.stream()).collect(Collectors.toList());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment