Skip to content

Commit bba72cb

Browse files
committed
Get rid of normalizedLineEndings and readAsString
By reusing `assertEqualToFile` it is possible to handle line endings nicer and more efficient.
1 parent 8db9f57 commit bba72cb

4 files changed

Lines changed: 6 additions & 58 deletions

File tree

src/main/java/org/variantsync/diffdetective/util/FileUtils.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
import org.tinylog.Logger;
55

66
import java.io.IOException;
7-
import java.nio.charset.StandardCharsets;
87
import java.nio.file.Files;
98
import java.nio.file.Path;
109
import java.util.List;
1110

1211
public class FileUtils {
13-
public static String normalizedLineEndings(final String text) {
14-
return text.replaceAll(StringUtils.LINEBREAK_REGEX, StringUtils.LINEBREAK);
15-
}
16-
1712
public static boolean isEmptyDirectory(final Path p) throws IOException {
1813
return !Files.exists(p) || (Files.isDirectory(p) && Files.list(p).findAny().isEmpty());
1914
}
@@ -46,22 +41,6 @@ public static boolean isLineGraph(final Path p) {
4641
return hasExtension(p, ".lg");
4742
}
4843

49-
/**
50-
* Read a text file.
51-
*
52-
* @param path Path to the file to read.
53-
* @return The content of the file as a utf8 encoded string.
54-
*/
55-
public static String readUTF8(final Path path) {
56-
try {
57-
byte[] encoded = Files.readAllBytes(path);
58-
return new String(encoded, StandardCharsets.UTF_8);
59-
} catch (IOException e) {
60-
e.printStackTrace();
61-
return "";
62-
}
63-
}
64-
6544
public static Path addExtension(final Path p, final String extension) {
6645
return p.resolveSibling(p.getFileName() + extension);
6746
}

src/main/java/org/variantsync/diffdetective/util/IO.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.nio.file.StandardOpenOption;
1212
import java.util.Optional;
1313
import java.util.StringJoiner;
14-
import java.util.stream.Collectors;
1514

1615
/**
1716
* Util class for exporting data.
@@ -21,18 +20,6 @@
2120
public class IO {
2221
private static final String CSV_DELIMITER = ",";
2322

24-
public static String readAsString(final Path p) throws IOException {
25-
try (
26-
final FileReader f = new FileReader(p.toFile());
27-
final BufferedReader reader = new BufferedReader(f)
28-
) {
29-
return reader.lines().collect(Collectors.joining("\r\n"));
30-
} catch (final IOException e) {
31-
Logger.error(e, "Failed to read lines from file");
32-
throw e;
33-
}
34-
}
35-
3623
/**
3724
* Exports data to a csv-file
3825
* @param fileName Name of the file to export to

src/test/java/PrintWorkingTreeDiff.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,9 @@ public void testWorkingTreeDiff() throws IOException, NoHeadException, GitAPIExc
4545
diffOutput += patchDiff.getDiff();
4646
}
4747

48-
// Load diff to verfiy computed output
49-
String fileForVerification = "src/test/resources/" + repoName + ".txt";
50-
String result = read(Paths.get(fileForVerification));
51-
52-
// Remove all white spaces to simplify comparison
53-
diffOutput = diffOutput.replaceAll("\\s", "");
54-
result = result.replaceAll("\\s", "");
55-
5648
// Check whether diffs match
57-
Assert.assertTrue(diffOutput.equals(result));
49+
Path fileForVerification = Path.of("src", "test", "resources", repoName + ".txt");
50+
TestUtils.assertEqualToFile(fileForVerification, diffOutput);
5851

5952
}
60-
61-
/**
62-
* Read in diff from external file.
63-
*
64-
* @param filePath Path to the file
65-
* @return The diff
66-
* @throws IOException
67-
*/
68-
private static String read(Path filePath) throws IOException {
69-
return IO.readAsString(filePath);
70-
}
71-
7253
}

src/test/java/StarfoldTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.variantsync.diffdetective.util.FileUtils;
66

77
import java.io.IOException;
8+
import java.nio.file.Files;
89
import java.nio.file.Path;
910
import java.util.List;
1011
import java.util.stream.Stream;
@@ -31,9 +32,9 @@ public void testAll() throws IOException {
3132
final DiffTree t = DiffTree.fromFile(testCase.inputDiff, true, true).unwrap().getSuccess();
3233
Starfold.RespectNodeOrder().transform(t);
3334
// System.out.println(t.toTextDiff());
34-
Assert.assertEquals(
35-
FileUtils.normalizedLineEndings(t.toTextDiff().trim()),
36-
FileUtils.normalizedLineEndings(FileUtils.readUTF8(testCase.expectedDiff).trim())
35+
TestUtils.assertEqualToFile(
36+
testCase.expectedDiff,
37+
t.toTextDiff().trim()
3738
);
3839
}
3940
}

0 commit comments

Comments
 (0)