Conversation
Summary of ChangesHello @vorburger, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a targeted test case to validate the file watching mechanism's response to file overwrite operations. By simulating an overwrite using "java.nio.file.Files.move" with the "REPLACE_EXISTING" option, the new test ensures that the "DirectoryWatcher" accurately detects and reports the "ChangeKind.CREATED" event, thereby improving the robustness and reliability of file system change detection. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a new test case to demonstrate an issue with file overwrite detection, and also applies some stylistic changes to existing test methods. My review focuses on improving the new test case by removing debugging artifacts and using modern, non-deprecated APIs. Additionally, I've provided suggestions to revert the stylistic changes to maintain consistency with the existing codebase and adhere to common Java style conventions.
| dir.mkdirs(); | ||
| final File file = File.createTempFile("test", "txt"); | ||
| final File to = dir.toPath().resolve("test.txt").toFile(); | ||
| Files.copy(file, to); |
There was a problem hiding this comment.
The com.google.common.io.Files.copy(File, File) method is deprecated and scheduled for removal. It's better to use the standard java.nio.file.Files.copy() instead. This also improves consistency with other parts of the new test that use java.nio.file.Files.
| Files.copy(file, to); | |
| java.nio.file.Files.copy(file.toPath(), to.toPath()); |
| AtomicReference<ChangeKind> change = new AtomicReference<>(); | ||
|
|
||
| try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).listener((p, c) -> { | ||
| System.out.println("c = " + c); |
| assertableExceptionHandler.assertNoErrorInTheBackgroundThread(); | ||
| java.nio.file.Files.move(file.toPath(), to.toPath(), REPLACE_EXISTING); | ||
|
|
||
| // Files.move(file, to); |
There was a problem hiding this comment.
Pull request overview
This PR adds a new test case testOverwriteExistingFile() to demonstrate issue #95, which appears to be related to file overwriting behavior in the directory watcher. The test attempts to verify that moving a file to overwrite an existing file is correctly detected as a CREATED event.
Key changes:
- New test case added to verify file overwrite detection behavior
- Import added for
StandardCopyOption.REPLACE_EXISTINGto support file move operations - Import added for
AtomicReferenceto track change events in the new test - Minor formatting changes to consolidate annotations with method declarations
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| AtomicReference<ChangeKind> change = new AtomicReference<>(); | ||
|
|
||
| try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).listener((p, c) -> { | ||
| System.out.println("c = " + c); |
There was a problem hiding this comment.
Debug output should be removed or replaced with proper logging. The test already has proper assertion mechanisms, and printing to System.out is not appropriate for production test code.
| System.out.println("c = " + c); |
| System.out.println("c = " + c); | ||
| change.set(c); | ||
| }).exceptionHandler(assertableExceptionHandler).build()) { | ||
| // We want it to call the listener once for setup, even without any change |
There was a problem hiding this comment.
This comment is misleading. The DirectoryWatcherBuilder does not call the listener during setup by default. The existingFiles flag is not set to true in this test, so the listener will only be called when file system events occur. This comment should be corrected or removed.
| // We want it to call the listener once for setup, even without any change | |
| // Verify that overwriting an existing file triggers a CREATED event via the listener |
|
|
||
| // Files.move(file, to); |
There was a problem hiding this comment.
Commented-out code should be removed. If this was an alternative implementation being considered, it should either be implemented properly or removed entirely.
| // Files.move(file, to); |
| try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).listener((p, c) -> { | ||
| System.out.println("c = " + c); | ||
| change.set(c); | ||
| }).exceptionHandler(assertableExceptionHandler).build()) { | ||
| // We want it to call the listener once for setup, even without any change | ||
| assertableExceptionHandler.assertNoErrorInTheBackgroundThread(); | ||
| java.nio.file.Files.move(file.toPath(), to.toPath(), REPLACE_EXISTING); | ||
|
|
||
| // Files.move(file, to); | ||
| await().atMost(5, SECONDS).until(change::get, is(ChangeKind.CREATED)); |
There was a problem hiding this comment.
The test expects ChangeKind.CREATED, but the DirectoryWatcherBuilder's default eventKinds only includes DELETED and MODIFIED (not CREATED). You need to explicitly configure the watcher to listen for CREATED events by adding .eventKinds(ChangeKind.CREATED, ChangeKind.MODIFIED, ChangeKind.DELETED) to the builder chain, or at minimum .eventKinds(ChangeKind.CREATED). Without this, the test will timeout waiting for a CREATED event that will never be captured.
| dir.mkdirs(); | ||
| final File file = File.createTempFile("test", "txt"); | ||
| final File to = dir.toPath().resolve("test.txt").toFile(); | ||
| Files.copy(file, to); |
There was a problem hiding this comment.
The Guava Files.copy method signature for copying between two File objects requires a target file that doesn't exist, or it will throw an exception. Since this test is intended to demonstrate overwriting an existing file, this line may cause the test to fail. Consider using java.nio.file.Files.copy with appropriate options, or using Files.asCharSink to create the initial file content.
| Files.copy(file, to); | |
| java.nio.file.Files.copy(file.toPath(), to.toPath(), REPLACE_EXISTING); |
#96 for #95