forked from samuelcolvin/watchfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cli.py
95 lines (74 loc) · 3.07 KB
/
test_cli.py
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
94
95
from pathlib import Path
from watchgod.cli import callback, cli, run_function
def foobar():
# used by tests below
Path('sentinel').write_text('ok')
def test_simple(mocker, tmpdir):
mocker.patch('watchgod.cli.set_start_method')
mocker.patch('watchgod.cli.sys.stdin.fileno')
mocker.patch('os.ttyname', return_value='/path/to/tty')
mock_run_process = mocker.patch('watchgod.cli.run_process')
cli('tests.test_cli.foobar', str(tmpdir))
mock_run_process.assert_called_once_with(
Path(str(tmpdir)),
run_function,
args=('tests.test_cli.foobar', '/path/to/tty'),
callback=callback
)
def test_invalid_import1(mocker, tmpdir, capsys):
sys_exit = mocker.patch('watchgod.cli.sys.exit')
cli('foobar')
sys_exit.assert_called_once_with(1)
out, err = capsys.readouterr()
assert out == ''
assert err == 'ImportError: "foobar" doesn\'t look like a module path\n'
def test_invalid_import2(mocker, tmpdir, capsys):
sys_exit = mocker.patch('watchgod.cli.sys.exit')
cli('pprint.foobar')
sys_exit.assert_called_once_with(1)
out, err = capsys.readouterr()
assert out == ''
assert err == 'ImportError: Module "pprint" does not define a "foobar" attribute\n'
def test_invalid_path(mocker, capsys):
sys_exit = mocker.patch('watchgod.cli.sys.exit')
cli('tests.test_cli.foobar', '/does/not/exist')
sys_exit.assert_called_once_with(1)
out, err = capsys.readouterr()
assert out == ''
assert err == 'path "/does/not/exist" is not a directory\n'
def test_tty_os_error(mocker, tmpworkdir):
mocker.patch('watchgod.cli.set_start_method')
mocker.patch('watchgod.cli.sys.stdin.fileno', side_effect=OSError)
mock_run_process = mocker.patch('watchgod.cli.run_process')
cli('tests.test_cli.foobar')
mock_run_process.assert_called_once_with(
Path(str(tmpworkdir)),
run_function,
args=('tests.test_cli.foobar', '/dev/tty'),
callback=callback
)
def test_tty_attribute_error(mocker, tmpdir):
mocker.patch('watchgod.cli.set_start_method')
mocker.patch('watchgod.cli.sys.stdin.fileno', side_effect=AttributeError)
mock_run_process = mocker.patch('watchgod.cli.run_process')
cli('tests.test_cli.foobar', str(tmpdir))
mock_run_process.assert_called_once_with(
Path(str(tmpdir)),
run_function,
args=('tests.test_cli.foobar', None),
callback=callback
)
def test_run_function(tmpworkdir):
assert not tmpworkdir.join('sentinel').exists()
run_function('tests.test_cli.foobar', None)
assert tmpworkdir.join('sentinel').exists()
def test_run_function_tty(tmpworkdir):
# could this cause problems by changing sys.stdin?
assert not tmpworkdir.join('sentinel').exists()
run_function('tests.test_cli.foobar', '/dev/tty')
assert tmpworkdir.join('sentinel').exists()
def test_callback(mocker):
# boring we have to test this directly, but we do
mock_logger = mocker.patch('watchgod.cli.logger.info')
callback({1, 2, 3})
mock_logger.assert_called_once_with('%d files changed, reloading', 3)