Skip to content

Commit

Permalink
Reduce default delay to 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
olzhasar committed Jun 2, 2023
1 parent beb4b29 commit 8fbc4b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ptw . --ignore-patterns 'settings.py,db.py'

## Delay

`pytest-watcher` uses a short delay (0.5 seconds) by default before triggering the actual test run. The main motivation for this is post-processors that can run after you save the file (e.g., black plugin in your IDE). This ensures that tests will be run with the latest version of your code.
`pytest-watcher` uses a short delay (0.2 seconds by default) before triggering the actual test run. The main motivation for this is post-processors that can run after you save the file (e.g., black plugin in your IDE). This ensures that tests will be run with the latest version of your code.

You can control the actual delay value with the `--delay` flag:

Expand Down
6 changes: 4 additions & 2 deletions pytest_watcher/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from watchdog.observers import Observer
from watchdog.utils.patterns import match_any_paths

DEFAULT_DELAY = 0.2

trigger_lock = threading.Lock()
trigger = None

Expand Down Expand Up @@ -103,8 +105,8 @@ def _parse_arguments(args: Sequence[str]) -> ParsedArguments:
parser.add_argument(
"--delay",
type=float,
default=0.5,
help="Watcher delay in seconds (default 0.5)",
default=DEFAULT_DELAY,
help=f"Watcher delay in seconds (default DEFAULT_DELAY)",
)
parser.add_argument(
"--runner",
Expand Down
29 changes: 15 additions & 14 deletions tests/test_pytest_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pytest_mock.plugin import MockerFixture

from pytest_watcher import __version__, watcher
from pytest_watcher.watcher import DEFAULT_DELAY


def test_version():
Expand Down Expand Up @@ -35,18 +36,18 @@ def test_emit_trigger():
@pytest.mark.parametrize(
("sys_args", "path_to_watch", "now", "delay", "runner_args", "runner", "patterns", "ignore_patterns"),
[
(["/home/"], "/home", False, 0.5, [], "pytest", ['*.py'], []),
(["/home/", "--lf", "--nf", "-x"], "/home", False, 0.5, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
(["/home/", "--lf", "--now", "--nf", "-x"], "/home", True, 0.5, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
(["/home/", "--now", "--lf", "--nf", "-x"], "/home", True, 0.5, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
([".", "--lf", "--nf", "-x"], ".", False, 0.5, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
([".", "--delay=0.2", "--lf", "--nf", "-x"], ".", False, 0.2, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
(["/home/"], "/home", False, DEFAULT_DELAY, [], "pytest", ['*.py'], []),
(["/home/", "--lf", "--nf", "-x"], "/home", False, DEFAULT_DELAY, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
(["/home/", "--lf", "--now", "--nf", "-x"], "/home", True, DEFAULT_DELAY, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
(["/home/", "--now", "--lf", "--nf", "-x"], "/home", True, DEFAULT_DELAY, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
([".", "--lf", "--nf", "-x"], ".", False, DEFAULT_DELAY, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
([".", "--delay=0.8", "--lf", "--nf", "-x"], ".", False, 0.8, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
([".", "--lf", "--nf", "--delay=0.3", "-x"], ".", False, 0.3, ["--lf", "--nf", "-x"], "pytest", ['*.py'], []),
(["/home/", "--runner", "tox"], "/home", False, 0.5, [], "tox", ['*.py'], []),
(["/home/", "--runner", "'make test'"], "/home", False, 0.5, [], "'make test'", ['*.py'], []),
(["/home/", "--runner", "make", "test"], "/home", False, 0.5, ["test"], "make", ['*.py'], []),
(["/home/", "--patterns", "*.py,*.env"], "/home", False, 0.5, [], "pytest", ['*.py', '*.env'], []),
(["/home/", "--patterns=*.py,*.env", "--ignore-patterns", "long-long-long-path,templates/*.py"], "/home", False, 0.5, [], "pytest", ['*.py', '*.env'], ["long-long-long-path", "templates/*.py"]),
(["/home/", "--runner", "tox"], "/home", False, DEFAULT_DELAY, [], "tox", ['*.py'], []),
(["/home/", "--runner", "'make test'"], "/home", False, DEFAULT_DELAY, [], "'make test'", ['*.py'], []),
(["/home/", "--runner", "make", "test"], "/home", False, DEFAULT_DELAY, ["test"], "make", ['*.py'], []),
(["/home/", "--patterns", "*.py,*.env"], "/home", False, DEFAULT_DELAY, [], "pytest", ['*.py', '*.env'], []),
(["/home/", "--patterns=*.py,*.env", "--ignore-patterns", "long-long-long-path,templates/*.py"], "/home", False, DEFAULT_DELAY, [], "pytest", ['*.py', '*.env'], ["long-long-long-path", "templates/*.py"]),
],
)
def test_parse_arguments(sys_args, path_to_watch, now, delay, runner_args, runner, patterns, ignore_patterns):
Expand Down Expand Up @@ -127,7 +128,7 @@ def test_run(
mock_emit_trigger.assert_not_called()

mock_run_main_loop.assert_called_once_with(
runner="pytest", runner_args=["--lf", "--nf"], delay=0.5
runner="pytest", runner_args=["--lf", "--nf"], delay=DEFAULT_DELAY
)


Expand All @@ -152,7 +153,7 @@ def test_run_now(
mock_emit_trigger.assert_called_once_with()

mock_run_main_loop.assert_called_once_with(
runner="pytest", runner_args=["--lf", "--nf"], delay=0.5
runner="pytest", runner_args=["--lf", "--nf"], delay=DEFAULT_DELAY
)


Expand All @@ -179,5 +180,5 @@ def test_invoke_runner(
mock_emit_trigger.assert_called_once_with()

mock_run_main_loop.assert_called_once_with(
runner=runner, runner_args=["--lf", "--nf"], delay=0.5
runner=runner, runner_args=["--lf", "--nf"], delay=DEFAULT_DELAY
)

0 comments on commit 8fbc4b7

Please sign in to comment.