forked from authomatic/chromedriver_installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
154 lines (116 loc) · 4.61 KB
/
tests.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import itertools
import os
import re
import shlex
import subprocess
import tempfile
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import pytest
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
VIRTUALENV_DIR = os.environ['VIRTUAL_ENV']
INSTALL_COMMAND_BASE = 'pip install {0} '.format(PROJECT_DIR)
def generate_version_fixture_params():
"""
Loads all known versions of chromedriver from
`https://chromedriver.storage.googleapis.com`__
and returns a dictionary with keys ``params`` and ``ids`` which should be
unpacked as arguments to :func:`pytest.fixture` decorator.
This way we can generate and ``params`` and ``ids`` arguments with a single
function call. We need the ``ids`` parameter for nice display of tested
versions in the verbose ``pytest`` output.
:returns:
A dictionary with keys ``params`` and ``ids``.
"""
body = urlopen('https://chromedriver.storage.googleapis.com').read()
versions = re.findall(
r'<Key>(\d+\.\d{2}).*?<ETag>"(.*?)"</ETag>',
body.decode('utf-8'),
)
params = [
(version, [checksum for _, checksum in checksums])
for version, checksums in itertools.groupby(versions, lambda x: x[0])
]
return dict(
params=params,
ids=[version for version, _ in params]
)
@pytest.fixture(**generate_version_fixture_params())
def version(request):
request.param_index = request.param[0]
return request.param
class Base(object):
def _uninstall(self):
try:
subprocess.check_call(
shlex.split('pip uninstall chromedriver_installer -y')
)
except subprocess.CalledProcessError:
pass
chromedriver_executable = os.path.join(VIRTUALENV_DIR,
'bin', 'chromedriver')
if os.path.exists(chromedriver_executable):
print('REMOVING chromedriver executable: ' +
chromedriver_executable)
os.remove(chromedriver_executable)
def teardown(self):
self._uninstall()
def _not_available(self):
with pytest.raises(OSError):
subprocess.check_call(shlex.split('chromedriver --version'))
class TestFailure(Base):
def test_bad_checksum(self):
self._not_available()
command = INSTALL_COMMAND_BASE + (
'--install-option="--chromedriver-version=2.10" '
'--install-option="--chromedriver-checksums=foo,bar,baz"'
)
error_message = subprocess.Popen(
shlex.split(command),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate()[0]
assert ('matches none of the checksums '
'foo, bar, baz!') in str(error_message)
class VersionBase(Base):
def _assert_cached_files_exist(self, exists, remove=False):
path = os.path.join(tempfile.gettempdir(),
'chromedriver_{0}.zip'.format(self.version))
if remove and os.path.exists(path):
os.remove(path)
assert os.path.exists(path) is exists
def _test_version(self, version, cached):
self.version, self.checksums = version
# Chromedriver executable should not be available.
self._not_available()
# Assert that zip archives are cached or not, depending on test type.
self._assert_cached_files_exist(cached, remove=not cached)
# After installation...
subprocess.check_call(shlex.split(self._get_install_command()))
# ...the chromedriver executable should be available...
expected_version, error = subprocess.Popen(
shlex.split('chromedriver -v'),
stdout=subprocess.PIPE
).communicate()
# ...and should be of the right version.
assert self.version in str(expected_version)
def test_version_uncached(self, version):
self._test_version(version, cached=False)
class TestVersionOnly(VersionBase):
def _get_install_command(self):
return (
INSTALL_COMMAND_BASE +
'--install-option="--chromedriver-version={0}"'.format(self.version)
)
class TestVersionAndChecksums(VersionBase):
def _get_install_command(self):
return INSTALL_COMMAND_BASE + (
'--install-option="--chromedriver-version={0}" '
'--install-option="--chromedriver-checksums={1}"'
).format(self.version, ','.join(self.checksums))
def test_version_cached(self, version):
self._test_version(version, cached=True)