Skip to content

Bug fixes, new features and test suite#102

Open
eviweb wants to merge 11 commits intojoh:masterfrom
eviweb:pr/fixes-and-features
Open

Bug fixes, new features and test suite#102
eviweb wants to merge 11 commits intojoh:masterfrom
eviweb:pr/fixes-and-features

Conversation

@eviweb
Copy link
Contributor

@eviweb eviweb commented Mar 16, 2026

This PR addresses several open issues and adds new features.

Bug fixes

New features

Tests

  • Add tests #80 Add a test suite of 69 tests covering:
    • is_interested() and exclusion patterns
    • all event handlers (on_created, on_modified, on_moved, on_deleted)
    • debounce behavior
    • run_command() including -1, -k, quiet mode
    • CLI argument parsing for all flags
    • -p pattern filtering

PowerKiKi and others added 11 commits March 16, 2026 11:25
At least on Ubuntu 24.04, `pip` would error out with `error: externally-managed-environment`. According to the following SO, the proper way for an application that should be globally available, is to use `pipx` instead.

See https://stackoverflow.com/a/78652149/37706
Replace [px]* with [a-z] to match all vim swap extensions
(.swo, .swn, .swm, etc.) not just .swp and .swx.

Signed-off-by: Eric Villard <dev@eviweb.fr>
Signed-off-by: Eric Villard <dev@eviweb.fr>
inotify fires both created and modified events when a file is created.
Previously on_created was entirely skipped for InotifyObserver, which
caused two bugs:
- files moved into a watched directory did not trigger the command (joh#68)
- WHEN_CHANGED_EVENT was never set to file_created on Linux (joh#76)

Fix: track recently created paths in a set and skip the redundant
modified event, instead of ignoring created events entirely.
last_run was recorded after subprocess.call(), so any file change
occurring during command execution had mtime > last_run and triggered
a re-run. Also the comparison used < instead of <=.

Fix: record last_run before subprocess.call() so that changes during
execution are correctly ignored on the next event.
Add modern pyproject.toml with build-system configuration.
Remove obsolete subprocess32 from requirements (Python 2 only).
A wheel can now be built with: python3 -m build
-k: kill the currently running command before restarting it,
    useful for long-running processes (servers, watchers).
    Uses SIGTERM with 5s timeout fallback to SIGKILL.

-d DELAY: wait DELAY seconds before running the command,
    coalescing rapid successive changes into a single run.
    Uses threading.Timer, each new event resets the timer.
Covers: is_interested(), event handlers, debounce, run_command,
CLI parsing. Also documents regex bug in backup file exclusion (.~$)."

Signed-off-by: Eric Villard <dev@eviweb.fr>
Replace set with dict (path -> timestamp). On each on_modified,
purge entries older than 1s before checking. Handles the case where
on_modified never fires after on_created (e.g. empty file creation).

Signed-off-by: Eric Villard <dev@eviweb.fr>
r'.~$' matched any char before ~ due to unescaped dot.
Corrected to r'\.~$' to match only literal dot (e.g. file.py~).

Signed-off-by: Eric Villard <dev@eviweb.fr>
New option -p PATTERN (glob) restricts reactions to files whose
basename matches the pattern. Can be repeated for multiple patterns.
No -p means watch all files (existing behavior preserved).

Uses fnmatch for glob matching (*, ?, [...] supported).
Pattern check is applied in is_interested() after exclude rules,
so excluded files (vim swap, .git, etc.) are never triggered
regardless of patterns.

Examples:
  when-changed -r -p '*.py' src/ make test
  when-changed -r -p '*.py' -p '*.yml' src/ make lint

Signed-off-by: Eric Villard <dev@eviweb.fr>
@joh
Copy link
Owner

joh commented Mar 18, 2026

Fantastic, thank you for all this work! Given the amount of changes, I'm going to need some time to review, so please bear with me :)

@eviweb
Copy link
Contributor Author

eviweb commented Mar 18, 2026

You're welcome.
To be honnest Claude AI helped me a lot.
I'm just back from a 3 years sick leave, and I'm a bit rusty.
I use this great tool daily and I'm in a hurry.
So I wanted to make a new contribution quickly to get a more robust version.
I prioritized the issues that I considered to have the most added value, without devaluing the other proposals.
This PR is normally rebased on your master branch so should be mergeable as is.
Take your time for the review.
I hope this will help.
All ly best

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