File: test_force_polling.py

package info (click to toggle)
python-watchfiles 0.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 468 kB
  • sloc: python: 1,973; makefile: 91
file content (93 lines) | stat: -rw-r--r-- 2,551 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations as _annotations

from typing import TYPE_CHECKING

import pytest

from watchfiles import watch
from watchfiles.main import _default_force_polling

if TYPE_CHECKING:
    from .conftest import SetEnv


class MockRustNotify:
    @staticmethod
    def watch(*args):
        return 'stop'

    def __enter__(self):
        return self

    def __exit__(self, *args):
        pass


def test_watch_polling_not_env(mocker):
    m = mocker.patch('watchfiles.main.RustNotify', return_value=MockRustNotify())

    for _ in watch('.'):
        pass

    m.assert_called_once_with(['.'], False, False, 300, True, False)


def test_watch_polling_env(mocker, env: SetEnv):
    env('WATCHFILES_FORCE_POLLING', '1')
    m = mocker.patch('watchfiles.main.RustNotify', return_value=MockRustNotify())

    for _ in watch('.'):
        pass

    m.assert_called_once_with(['.'], False, True, 300, True, False)


@pytest.mark.parametrize(
    'env_var,arg,expected',
    [
        (None, True, True),
        (None, False, False),
        (None, None, False),
        ('', True, True),
        ('', False, False),
        ('', None, False),
        ('1', True, True),
        ('1', False, False),
        ('1', None, True),
        ('disable', True, True),
        ('disable', False, False),
        ('disable', None, False),
    ],
)
def test_default_force_polling(mocker, env: SetEnv, env_var, arg, expected):
    uname = type('Uname', (), {'system': 'Linux', 'release': '1'})
    mocker.patch('platform.uname', return_value=uname())
    if env_var is not None:
        env('WATCHFILES_FORCE_POLLING', env_var)
    assert _default_force_polling(arg) == expected


@pytest.mark.parametrize(
    'env_var,arg,expected,call_count',
    [
        (None, True, True, 0),
        (None, False, False, 0),
        (None, None, True, 1),
        ('', True, True, 0),
        ('', False, False, 0),
        ('', None, True, 1),
        ('1', True, True, 0),
        ('1', False, False, 0),
        ('1', None, True, 0),
        ('disable', True, True, 0),
        ('disable', False, False, 0),
        ('disable', None, False, 0),
    ],
)
def test_default_force_polling_wsl(mocker, env: SetEnv, env_var, arg, expected, call_count):
    uname = type('Uname', (), {'system': 'Linux', 'release': 'Microsoft-Standard'})
    m = mocker.patch('platform.uname', return_value=uname())
    if env_var is not None:
        env('WATCHFILES_FORCE_POLLING', env_var)
    assert _default_force_polling(arg) == expected
    assert m.call_count == call_count