Skip to content

Commit e75937b

Browse files
Muskan244koppordependabot[bot]
authored
Split relativizeSymlinks parameterized tests in separate tests (#13782)
* refactor: split OpenExternalFileAction into OpenSingleExternalFileAction and OpenSelectedEntriesFilesAction - Introduced OpenSingleExternalFileAction to handle single entry with one linked file - Introduced OpenSelectedEntriesFilesAction to handle multiple selection cases Fixes #13431 * Cleaned up constant name, dialog text, and removed extra comment * Add null checks to OpenSingleExternalFileAction constructor * Add @nonnull annotations and fix localization key warnings * fix submodules * Use database latest context, update button text, and add comments explaining code * Bump org.junit.platform:junit-platform-launcher in /versions (#13505) Bumps [org.junit.platform:junit-platform-launcher](https://github.com/junit-team/junit-framework) from 1.13.1 to 1.13.3. - [Release notes](https://github.com/junit-team/junit-framework/releases) - [Commits](https://github.com/junit-team/junit-framework/commits) --- updated-dependencies: - dependency-name: org.junit.platform:junit-platform-launcher dependency-version: 1.13.3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Oliver Kopp <[email protected]> * Fix: clarify fallback logic, update dialog labels, and avoid null Optional fallback * Fix: Make FileUtil.relativize symlink-aware for robust relative path resolution - Use real/canonical paths to ensure files are correctly recognized as within a directory, even through symlinks - Add and document unit tests covering symlinked and normal directory scenarios fixes #12995 * remove unnecessary comments and make test more clearer and maintainable * Refactor to avoid using exceptions for normal control flow in tryRealPath and remove unnecessary comments * Refactored symlink tests for independence, used Files.writeString for simplicity, and ensured all edge cases from issue #12995 are now covered. * Fix code style issues with OpenRewrite and update symlink tests * Fix submodules * Fix submodules * Fix typo * Refactor tests * Add comments to explain the logic to not use directory.relativize() * Removed unneccesary comment * Add ignored test for symlink escape case (#12995 comment) * Tests: split relativizeSymlinks() parameterized test into separate test cases for better readability * resolved checkstyle issues * Resolved failed test cases issue and removed unused imports * fix failing tests in jablib module --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Oliver Kopp <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 0eb694b commit e75937b

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

jablib/src/test/java/org/jabref/logic/util/io/FileUtilTest.java

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import java.nio.charset.StandardCharsets;
55
import java.nio.file.Files;
66
import java.nio.file.Path;
7-
import java.util.ArrayList;
87
import java.util.List;
98
import java.util.Optional;
9+
import java.util.UUID;
1010
import java.util.stream.Collectors;
1111
import java.util.stream.Stream;
1212

@@ -449,70 +449,86 @@ public void cTemp() {
449449
}
450450
}
451451

452-
@ParameterizedTest
453-
@DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows")
454-
@MethodSource
455-
void relativizeSymlinks(Path file, List<Path> directories, Path expected, String message) {
456-
if (message.startsWith("IGNORED")) {
457-
org.junit.jupiter.api.Assumptions.assumeTrue(false, message);
458-
}
459-
Path result = FileUtil.relativize(file, directories);
460-
assertEquals(expected, result, message);
461-
}
462-
463452
/// Tests for issue <https://github.com/JabRef/jabref/issues/12995>
464-
static Stream<Arguments> relativizeSymlinks() throws IOException {
465-
List<Arguments> result = new ArrayList<>();
466-
467-
Path realDir = bibTempDir.resolve("realDir");
453+
@Test
454+
@DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows")
455+
void simpleRelativizeSymlinks() throws IOException {
456+
Path realDir = bibTempDir.resolve("realDir_" + UUID.randomUUID());
468457
Files.createDirectories(realDir);
469458

470-
// symlinkDir -> realDir
471-
// realDir/simple.pdf
472459
Path simpleFile = Files.createFile(realDir.resolve("simple.pdf"));
473-
Path symlinkDir = bibTempDir.resolve("symlinkDir");
460+
Path symlinkDir = bibTempDir.resolve("symlinkDir_" + UUID.randomUUID());
474461
Files.createSymbolicLink(symlinkDir, realDir);
475-
result.add(Arguments.of(simpleFile, List.of(symlinkDir), Path.of("simple.pdf"), "Simple symlink resolves to relative"));
476462

477-
// chainLink1 -> chainLink2 -> chainReal
478-
// chainReal/chained.pdf
479-
Path chainReal = bibTempDir.resolve("chainReal");
463+
Path result = FileUtil.relativize(simpleFile, List.of(symlinkDir));
464+
assertEquals(Path.of("simple.pdf"), result, "Simple symlink resolves to relative");
465+
}
466+
467+
@Test
468+
@DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows")
469+
void chainedRelativizeSymlinks() throws IOException {
470+
Path chainReal = bibTempDir.resolve("chainReal_" + UUID.randomUUID());
480471
Files.createDirectories(chainReal);
472+
481473
Path chainedFile = Files.createFile(chainReal.resolve("chained.pdf"));
482-
Path chainLink2 = bibTempDir.resolve("chainLink2");
474+
Path chainLink2 = bibTempDir.resolve("chainLink2_" + UUID.randomUUID());
483475
Files.createSymbolicLink(chainLink2, chainReal);
484-
Path chainLink1 = bibTempDir.resolve("chainLink1");
476+
Path chainLink1 = bibTempDir.resolve("chainLink1_" + UUID.randomUUID());
485477
Files.createSymbolicLink(chainLink1, chainLink2);
486-
result.add(Arguments.of(chainedFile, List.of(chainLink1), Path.of("chained.pdf"), "Chained symlink resolves to relative"));
487478

488-
// realDir/nestedLink -> realDir/nested
489-
// realDir/nested/nested.pdf
490-
Path nestedDir = realDir.resolve("nested");
479+
Path result = FileUtil.relativize(chainedFile, List.of(chainLink1));
480+
assertEquals(Path.of("chained.pdf"), result, "Chained symlink resolves to relative");
481+
}
482+
483+
@Test
484+
@DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows")
485+
void nestedRelativizeSymlinks() throws IOException {
486+
Path realDir = bibTempDir.resolve("realDir_" + UUID.randomUUID());
487+
Files.createDirectories(realDir);
488+
489+
Path nestedDir = realDir.resolve("nested_" + UUID.randomUUID());
491490
Files.createDirectories(nestedDir);
492491
Path nestedFile = Files.createFile(nestedDir.resolve("nested.pdf"));
493-
Path nestedSymlink = realDir.resolve("nestedLink");
492+
Path nestedSymlink = realDir.resolve("nestedLink_" + UUID.randomUUID());
494493
Files.createSymbolicLink(nestedSymlink, nestedDir);
495-
result.add(Arguments.of(nestedFile, List.of(nestedSymlink), Path.of("nested.pdf"), "Nested symlink resolves to relative"));
496494

497-
// symlinkDir -> realDir
498-
// outside.pdf
495+
Path result = FileUtil.relativize(nestedFile, List.of(nestedSymlink));
496+
assertEquals(Path.of("nested.pdf"), result, "Nested symlink resolves to relative");
497+
}
498+
499+
@Test
500+
@DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows")
501+
void unrelatedFileRemainsAbsolute() throws IOException {
502+
Path realDir = bibTempDir.resolve("realDir_" + UUID.randomUUID());
503+
Files.createDirectories(realDir);
504+
Path symlinkDir = bibTempDir.resolve("symlinkDir_" + UUID.randomUUID());
505+
Files.createSymbolicLink(symlinkDir, realDir);
506+
499507
Path outsideFile = Files.createFile(bibTempDir.resolve("outside.pdf"));
500-
result.add(Arguments.of(outsideFile, List.of(symlinkDir), outsideFile, "Unrelated file remains absolute"));
501508

502-
// symlink chain escaping base dir (ignored test case, see <https://github.com/JabRef/jabref/issues/12995#issuecomment-3065149862>)
509+
Path result = FileUtil.relativize(outsideFile, List.of(symlinkDir));
510+
assertEquals(outsideFile, result, "Unrelated file remains absolute");
511+
}
512+
513+
@Test
514+
@DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows")
515+
void symlinkEscapeCaseIgnored() throws IOException {
503516
Path veryPrivate = bibTempDir.resolve("veryprivate");
504517
Files.createDirectories(veryPrivate);
505518
Path secretFile = Files.createFile(veryPrivate.resolve("a.pdf"));
519+
506520
Path expensive = bibTempDir.resolve("expensive");
507521
Files.createSymbolicLink(expensive, veryPrivate);
508522
Path things = bibTempDir.resolve("things");
509523
Files.createSymbolicLink(things, expensive);
524+
510525
Path libDir = bibTempDir.resolve("lib");
511526
Files.createDirectories(libDir);
512-
Path bibFile = Files.createFile(libDir.resolve("bib.bib"));
513-
result.add(Arguments.of(secretFile, List.of(things), secretFile, "IGNORED: Symlink chain escaping base dir (#12995 comment)"));
527+
Files.createFile(libDir.resolve("bib.bib"));
514528

515-
return result.stream();
529+
org.junit.jupiter.api.Assumptions.assumeTrue(false, "IGNORED: Symlink chain escaping base dir, see <https://github.com/JabRef/jabref/issues/12995#issuecomment-3065149862>");
530+
Path result = FileUtil.relativize(secretFile, List.of(things));
531+
assertEquals(secretFile, result);
516532
}
517533

518534
/**

0 commit comments

Comments
 (0)