Skip to content

Commit

Permalink
fix(LineLengthChecker): do not crash if files contain invalid UTF-8 c…
Browse files Browse the repository at this point in the history
…haracters (#23)

Closes #22
  • Loading branch information
chrisknedl authored Dec 4, 2024
1 parent b3c8d2a commit 70a1656
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 57 deletions.
16 changes: 13 additions & 3 deletions src/main/java/de/uni_passau/fim/se2/pipeline_helper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,20 @@ public Integer call() throws Exception {
validateParams();

final Checker checker = buildChecker();
final CheckerResult result = checker.check();
CheckerResultWriter.writeFeedback(parent.outputDirectory, result);
CheckerResult result;
try {
result = checker.check();
System.out.println("Successfully produced a checker result.");
}
catch (Exception e) {
e.printStackTrace();
result = new CheckerResult(
"Error", false,
"The checker crashed. Please contact your instructor to resolve the issue."
);
}

System.out.println("Successfully produced a checker result.");
CheckerResultWriter.writeFeedback(parent.outputDirectory, result);

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package de.uni_passau.fim.se2.pipeline_helper.checkers.line_length;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -36,7 +37,13 @@ public CheckerResult check() throws CheckerException {
final List<FileLineLengthViolations> violations = new ArrayList<>();
for (Iterator<Path> it = files.iterator(); it.hasNext();) {
final Path p = it.next();
final SortedMap<Integer, Integer> linesWithViolations = getAllViolationsWithLength(p);
final SortedMap<Integer, Integer> linesWithViolations;
try {
linesWithViolations = getAllViolationsWithLength(p);
}
catch (CheckerException e) {
return new CheckerResult(CHECKER_NAME, false, e.getMessage());
}
if (!linesWithViolations.isEmpty()) {
violations.add(new FileLineLengthViolations(directory.relativize(p), linesWithViolations));
}
Expand All @@ -57,7 +64,7 @@ private SortedMap<Integer, Integer> getAllViolationsWithLength(final Path path)
try (Stream<String> lines = Files.lines(path)) {
return getAllViolationWithLength(lines);
}
catch (IOException e) {
catch (IOException | UncheckedIOException e) {
throw new CheckerException("Cannot read file " + path, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static com.google.common.truth.Truth.assertThat;
import static de.uni_passau.fim.se2.pipeline_helper.TestUtil.resource;

import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;

Expand All @@ -29,65 +28,57 @@ static void init() throws URISyntaxException {

@Test
void checkFailedSingleViolation() throws Exception {
Path invalidDir = dir.resolve("invalid/");
final LineLengthChecker checker = new LineLengthChecker(
dir, FilteredFilesStream.files(dir, "java"), 80
invalidDir, FilteredFilesStream.files(invalidDir, "java"), 80
);
final CheckerResult result = checker.check();
assertThat(result.getName()).contains("LineLengthChecker");
assertThat(result.isSuccessful()).isFalse();
assertThat(result.getMessage()).contains(
withNormalisedNewline(
"""
invalid%sInvalidFileSingleViolation.java, on 1 line:
-> line 7, length 82""".formatted(File.separator)
InvalidFileSingleViolation.java, on 1 line:
-> line 7, length 82"""
)
);
}

@Test
void checkFailedTwoViolationsDifferentFiles() throws Exception {
void checkFailedDifferentFiles() throws Exception {
Path invalidDir = dir.resolve("invalid/");
final LineLengthChecker checker = new LineLengthChecker(
dir, FilteredFilesStream.files(dir, "java"), 80
invalidDir, FilteredFilesStream.files(invalidDir, "java"), 80
);
final CheckerResult result = checker.check();
assertThat(result.getName()).contains("LineLengthChecker");
assertThat(result.isSuccessful()).isFalse();
assertThat(result.getMessage()).contains(
withNormalisedNewline(
"""
invalid%sInvalidFile.java, on 2 lines:
InvalidFile.java, on 2 lines:
-> line 7, length 82
-> line 10, length 99""".formatted(File.separator)
-> line 10, length 99"""
)
);
assertThat(result.getMessage()).contains(
withNormalisedNewline(
"""
also_invalid%sInvalidFile.java, on 2 lines:
-> line 7, length 82
-> line 10, length 99""".formatted(File.separator)
)
withNormalisedNewline(
"""
invalidFileSingleViolation.java, on 1 line:
-> line 7, length 82"""
);
}

@Test
void checkFailedMoreThanTwoViolations() throws Exception {
void checkFailedIllegalByteSequence() throws Exception {
Path illegalByteSeqDir = dir.resolve("illegal_byte_sequence/");
final LineLengthChecker checker = new LineLengthChecker(
dir, FilteredFilesStream.files(dir, "java"), 80
illegalByteSeqDir, FilteredFilesStream.files(illegalByteSeqDir, "java"), 80
);
final CheckerResult result = checker.check();
assertThat(result.getName()).contains("LineLengthChecker");
assertThat(result.isSuccessful()).isFalse();
assertThat(result.getMessage()).contains(
withNormalisedNewline(
"""
invalid%sInvalidFileFourViolations.java, on 4 lines:
-> line 7, length 82
-> line 8, length 82
-> line 9, length 82
-> line 10, length 82""".formatted(File.separator)
)
);
assertThat(result.getMessage()).startsWith("Cannot read file ");
assertThat(result.getMessage()).endsWith("InvalidByteSequence.java");
}

@Test
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2022 Pipeline Helper Contributors
//
// SPDX-License-Identifier: EUPL-1.2

Ã(

This file was deleted.

0 comments on commit 70a1656

Please sign in to comment.