-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_helpers.py
70 lines (58 loc) · 1.98 KB
/
test_helpers.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
import socket
from unittest import mock
import pytest
import jupyter_forward.helpers
from .misc import sample_log_file_contents, sample_log_file_contents_with_contamination
@pytest.mark.parametrize(
'stdout, expected',
[
(
sample_log_file_contents,
{
'hostname': 'eniac01',
'port': 59628,
'token': 'Loremipsumdolorsitamet',
'url': 'http://eniac01:59628/?token=Loremipsumdolorsitamet',
},
),
(
sample_log_file_contents_with_contamination,
{
'hostname': 'eniac01',
'port': 59628,
'token': 'Loremipsumdolorsitamet',
'url': 'http://eniac01:59628/?token=Loremipsumdolorsitamet',
},
),
('', {'hostname': None, 'port': None, 'token': None, 'url': None}),
],
)
def test_parse_stdout(stdout, expected):
stdout = '\n'.join(stdout)
parsed_results = jupyter_forward.helpers.parse_stdout(stdout)
assert parsed_results == expected
@pytest.mark.parametrize('port', [8888, 9999])
def test_is_port_available(
port,
):
@mock.create_autospec
def connect_ex(self, address):
# if address[1] == 8888:
return 0
with mock.patch.object(socket.socket, 'connect_ex', connect_ex) as m:
jupyter_forward.helpers.is_port_available(port)
m.assert_called_once()
def test_open_browser_exception():
with pytest.raises(ValueError):
jupyter_forward.helpers.open_browser(token='ssh')
@pytest.mark.parametrize(
'port, token, url, expected',
[
(9999, 'ssh', None, 'http://localhost:9999/?token=ssh'),
(None, None, 'http://localhost:9999', 'http://localhost:9999'),
],
)
def test_open_browser(port, token, url, expected):
with mock.patch('webbrowser.open') as mockwebopen:
jupyter_forward.helpers.open_browser(port, token, url)
mockwebopen.assert_called_once_with(expected, new=2)