Skip to content

test case that shows the issue - #215

Open
vorburger wants to merge 3 commits into
mainfrom
issue-95
Open

test case that shows the issue#215
vorburger wants to merge 3 commits into
mainfrom
issue-95

Conversation

@vorburger

Copy link
Copy Markdown
Owner

#96 for #95

Copilot AI review requested due to automatic review settings January 3, 2026 00:33
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @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

  • New Test Case for File Overwrite: A new test method, "testOverwriteExistingFile", has been added to "DirectoryAndFileWatcherTest.java" to specifically test the behavior of the file watcher when an existing file is overwritten.
  • REPLACE_EXISTING Option Usage: The new test case utilizes "java.nio.file.Files.move" with the "REPLACE_EXISTING" option to accurately simulate the file overwrite scenario.
  • ChangeKind.CREATED Verification: The test asserts that the "DirectoryWatcher" correctly reports a "ChangeKind.CREATED" event when an existing file is overwritten, ensuring proper event handling for such operations.
  • Code Formatting: Minor formatting adjustments were made to "@BeforeClass" and "@test" annotations, moving the "public" keyword to the same line for consistency.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java Outdated
Comment thread src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java Outdated
Comment thread src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java Outdated
Comment thread src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java Outdated
Comment thread src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java Outdated
Comment thread src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java Outdated
dir.mkdirs();
final File file = File.createTempFile("test", "txt");
final File to = dir.toPath().resolve("test.txt").toFile();
Files.copy(file, to);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This System.out.println appears to be a debugging statement. It should be removed to keep the test output clean.

assertableExceptionHandler.assertNoErrorInTheBackgroundThread();
java.nio.file.Files.move(file.toPath(), to.toPath(), REPLACE_EXISTING);

// Files.move(file, to);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This commented-out code should be removed to improve code clarity.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_EXISTING to support file move operations
  • Import added for AtomicReference to 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);

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
System.out.println("c = " + c);

Copilot uses AI. Check for mistakes.
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

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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

Copilot uses AI. Check for mistakes.
Comment on lines +241 to +242

// Files.move(file, to);

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented-out code should be removed. If this was an alternative implementation being considered, it should either be implemented properly or removed entirely.

Suggested change
// Files.move(file, to);

Copilot uses AI. Check for mistakes.
Comment on lines +234 to +243
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));

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
dir.mkdirs();
final File file = File.createTempFile("test", "txt");
final File to = dir.toPath().resolve("test.txt").toFile();
Files.copy(file, to);

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
Files.copy(file, to);
java.nio.file.Files.copy(file.toPath(), to.toPath(), REPLACE_EXISTING);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants