Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug in the creation of the result due to missing slashes. #1568

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/src/main/java/de/jplag/reporting/FilePathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ public static String getRelativeSubmissionPath(File file, Submission submission,
return Path.of(submissionToIdFunction.apply(submission), submission.getRoot().toPath().relativize(file.toPath()).toString()).toString();
}

/**
* Joins logical paths using a slash. This method ensures, that no duplicate slashes are created in between.
* @param left The left path segment
* @param right The right path segment
* @return The joined paths
*/
public static String joinPathSegments(String left, String right) {
String rightStripped = right;
while (rightStripped.startsWith("/")) {
rightStripped = rightStripped.substring(1);
}

String leftStripped = left;
while (leftStripped.endsWith("/")) {
leftStripped = leftStripped.substring(0, leftStripped.length() - 1);
}

return leftStripped + "/" + rightStripped;
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void copySubmissionFilesToReport(JPlagResult result) {
if (relativeFilePath.isEmpty()) {
relativeFilePath = file.getName();
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
}
String zipPath = submissionRootPath + relativeFilePath;
String zipPath = FilePathUtil.joinPathSegments(submissionRootPath, relativeFilePath);

File fileToCopy = getFileToCopy(language, file);
this.resultWriter.addFileContentEntry(zipPath, fileToCopy);
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/de/jplag/reporting/FilePathUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.jplag.reporting;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class FilePathUtilTest {
private static final String JOINED = "left/right";
private static final String LEFT = "left";
private static final String RIGHT = "right";

@Test
void testJoinPath() {
assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT, RIGHT));
}

@Test
void testJoinPathWithLeftSlashSuffix() {
assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT + "/", RIGHT));
}

@Test
void testJoinPathWithRightSlashSuffix() {
assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT, "/" + RIGHT));
}
}
Loading