1
+ import itertools
1
2
import os
3
+ import re
2
4
import shlex
3
5
import subprocess
4
6
import tempfile
7
+ try :
8
+ # For Python 3.0 and later
9
+ from urllib .request import urlopen
10
+ except ImportError :
11
+ # Fall back to Python 2's urllib2
12
+ from urllib2 import urlopen
5
13
6
14
import pytest
7
15
11
19
INSTALL_COMMAND_BASE = 'pip install {0} ' .format (PROJECT_DIR )
12
20
13
21
14
- # The --version option is available since version 2.10
15
- VERSIONS = {
16
- '2.10' : (
17
- '4fecc99b066cb1a346035bf022607104' ,
18
- '058cd8b7b4b9688507701b5e648fd821' ,
19
- 'fd0dafc3ada3619edda2961f2beadc5c' ,
20
- '082e91e5c8994a7879710caeed62e334'
21
- ),
22
- '2.11' : (
23
- 'bf0d731cd34fd07e22f4641c9aec8483' ,
24
- '7a7336caea140f6ac1cb8fae8df50d36' ,
25
- '447ebc91ac355fc11e960c95f2c15622' ,
26
- '44443738344b887ff1fe94710a8d45dc'
27
- ),
28
- '2.12' : (
29
- '6f4041e7f8300380cc2a13babbac354e' ,
30
- 'f306b93ff1b34af74371cee87d6560e4' ,
31
- '259bb87f4ebf3b0bc4792ed203bd69f5' ,
32
- '51eb47ad5ea91422aa1aaa400a724e7b'
33
- ),
34
- '2.13' : (
35
- '187dbe7973c82e59446d5bca7ed40acd' ,
36
- 'fcfd993330704f8cfb307a2fca5a9687' ,
37
- 'e37a65a1be68523385761d29decf15d4' ,
38
- 'ae85407694d3849450a25431f9669a81'
39
- ),
40
- '2.14' : (
41
- 'f130cb3b94a8fbd28212f18c929f79ee' ,
42
- '8368d266bd832ff2ea292baedbc770be' ,
43
- '2b8a3e7249c80dec95264cbb55c897ed' ,
44
- '00c70587c3e215f030307d546b315323'
45
- ),
46
- '2.15' : (
47
- '844c200a4d8e79ad68d76ed68f67aadd' ,
48
- '21c22803a1fd903ba15ea21ee81de317' ,
49
- '3842a1ed3edc23997ca78bc980310024' ,
50
- 'c75b03bf76ab53d185fc18fc89ca1af9' ,
51
- ),
52
- '2.16' : (
53
- 'fffdb4c098adc2ab61b0e0f5b694f27e' ,
54
- 'fa8e1bc6f9ce474582876653604d675e' ,
55
- '3ae88facdc6ad1b716820cc5f678a6c4' ,
56
- '3e1b4a91e12a9872a4ed83c9e61c122e' ,
57
- ),
58
- '2.17' : (
59
- '569364de37c2743597cd6b8bb333d21a' ,
60
- 'aa2b200f118f8eaa7ed1d6bd7ca49005' ,
61
- 'bfa2d4c80e701980b5fd656e85329806' ,
62
- ),
63
- '2.18' : (
64
- '0ac230f5f19c72cebf3042970217ad01' ,
65
- '709e2dd132ac6c2a09de084fdc19db45' ,
66
- '6839b10023a48d87c4835481b9fad7a7' ,
67
- '4bdfb306e2bce6bf42e663617578ca24'
68
- ),
69
- '2.19' : (
70
- 'b1e881182574cd2354c00c384d0949cc' ,
71
- '9e476aa088baab9bed9c1a5e7007c9c3' ,
72
- '425ed409f989f4ca9eb1f2a745039b59' ,
73
- '4bc98ef466ef49e45fbee95a003e01b8'
74
- ),
75
- '2.20' : (
76
- '1e8cbdb84c5b70f86030297c4be3a5f9' ,
77
- '245858cc984bd946df6a1e6719c8e6f5' ,
78
- '749d4be0a317e92fd3aecaa022385439' ,
79
- '028ee452b8e23890ec5ec6b0717fd295'
80
- ),
81
- '2.21' : (
82
- 'd0a589f70e53774db95bf6f46972837c' ,
83
- '06e57f4c411e1135c6277d17ea8390fd' ,
84
- '452d8c9cba353ba366d15fbeba013943' ,
85
- '8a93dc3ff02ef9bc3161dd4b20f87215'
86
- ),
87
- '2.22' : (
88
- 'c498db13c92abf0d504c784d2191e9b1' ,
89
- '2a5e6ccbceb9f498788dc257334dfaa3' ,
90
- 'eed98b5a2895b9cd2432fa52f7091a71' ,
91
- 'c5962f884bd58987b1ef0fa04c6a3ce5'
92
- ),
93
- '2.23' : (
94
- '45df7fb52f4cba39a5bdcdaf2c16e171' ,
95
- 'cc89692d75fb0bdfe700ec0ca19808a5' ,
96
- '92fa9afe62c75557bc08af3fe3450b2b' ,
97
- '6856adb07cc8683d2a1587c05f37f1be'
98
- ),
99
- '2.24' : (
100
- '8e6b6d358f1b919a0d1369f90d61e1a4' ,
101
- 'c56e41bdc769ad2c31225b8495fc1a93' ,
102
- 'd117b66fac514344eaf80691ae9a4687' ,
103
- '1a46c83926f891d502427df10b4646b9'
104
- ),
105
- '2.25' : (
106
- '175ac6d5a9d7579b612809434020fd3c' ,
107
- '16673c4a4262d0f4c01836b5b3b2b110' ,
108
- '384031f9bb782edce149c0bea89921b6' ,
109
- '2727729883ac960c2edd63558f08f601'
110
- ),
111
- '2.26' : (
112
- 'b89d58dbd6182b3d30653a28eb304850' ,
113
- '3cdae483af1e54c6732abc9af875b9c1' ,
114
- 'd5ef788e1a5b4bfdb22836a45078f0c1' ,
115
- '05b5f443cb1f363b81a9ac7618e862bd'
116
- ),
117
- '2.27' : (
118
- '980387b5885be8f69343ba9f11cc7a9f' ,
119
- 'c6d21c8fecf8bd0b880b2c36692153ef' ,
120
- '56d908397af997f04fab32c05a26b994' ,
121
- '2125188a206e2258364c3e46f07724e5'
122
- ),
123
- '2.28' : (
124
- '82e4c112862be0e7e06e5ddcbe8b077c' ,
125
- 'a72088c0a6b018ded2c0fff616da8f65' ,
126
- '7261a8a4d45f63e6c6f5b712e089cbc3' ,
127
- 'daef8743d113cecc3bfe0c65c3423565'
128
- ),
129
- }
130
-
131
-
132
- @pytest .fixture (params = VERSIONS )
133
- def version_info (request ):
22
+ def generate_version_fixture_params ():
23
+ """
24
+ Loads all known versions of chromedriver from
25
+ `https://chromedriver.storage.googleapis.com`__
26
+ and returns a dictionary with keys ``params`` and ``ids`` which should be
27
+ unpacked as arguments to :func:`pytest.fixture` decorator.
28
+
29
+ This way we can generate and ``params`` and ``ids`` arguments with a single
30
+ function call. We need the ``ids`` parameter for nice display of tested
31
+ versions in the verbose ``pytest`` output.
32
+
33
+ :returns:
34
+ A dictionary with keys ``params`` and ``ids``.
35
+ """
36
+ body = urlopen ('https://chromedriver.storage.googleapis.com' ).read ()
37
+ versions = re .findall (
38
+ r'<Key>(\d+\.\d{2}).*?<ETag>"(.*?)"</ETag>' ,
39
+ body .decode ('utf-8' ),
40
+ )
41
+
42
+ params = [
43
+ (version , [checksum for _ , checksum in checksums ])
44
+ for version , checksums in itertools .groupby (versions , lambda x : x [0 ])
45
+ ]
46
+
47
+ return dict (
48
+ params = params ,
49
+ ids = [version for version , _ in params ]
50
+ )
51
+
52
+
53
+ @pytest .fixture (** generate_version_fixture_params ())
54
+ def version (request ):
55
+ request .param_index = request .param [0 ]
134
56
return request .param
135
57
136
58
137
59
class Base (object ):
138
60
def _uninstall (self ):
139
61
try :
140
- subprocess .check_call (shlex .split ('pip uninstall chromedriver_installer -y' ))
62
+ subprocess .check_call (
63
+ shlex .split ('pip uninstall chromedriver_installer -y' )
64
+ )
141
65
except subprocess .CalledProcessError :
142
66
pass
143
67
144
68
chromedriver_executable = os .path .join (VIRTUALENV_DIR ,
145
69
'bin' , 'chromedriver' )
146
70
147
71
if os .path .exists (chromedriver_executable ):
148
- print ('REMOVING chromedriver executable: ' + chromedriver_executable )
72
+ print ('REMOVING chromedriver executable: ' +
73
+ chromedriver_executable )
149
74
os .remove (chromedriver_executable )
150
75
151
76
def teardown (self ):
@@ -156,28 +81,6 @@ def _not_available(self):
156
81
subprocess .check_call (shlex .split ('chromedriver --version' ))
157
82
158
83
159
- class TestNewest (Base ):
160
- def test_newest (self ):
161
- subprocess .check_call (shlex .split (INSTALL_COMMAND_BASE ))
162
- # subprocess.check_call(shlex.split('chromedriver --version'))
163
-
164
- version_info = subprocess .Popen (
165
- shlex .split ('chromedriver --version' ),
166
- stdout = subprocess .PIPE ,
167
- stderr = subprocess .PIPE ,
168
- ).communicate ()[0 ]
169
-
170
- latest_version = max (VERSIONS .keys ())
171
-
172
- assert latest_version in version_info , (
173
- 'There seems to be a new version of chromedriver installer '
174
- 'than {0}.\n Detected: "{1}"\n '
175
- 'You need to add this version to the tests.VERSIONS dictionary to '
176
- 'make this test pass.'
177
- .format (latest_version , version_info .strip ())
178
- )
179
-
180
-
181
84
class TestFailure (Base ):
182
85
def test_bad_checksum (self ):
183
86
self ._not_available ()
@@ -198,7 +101,6 @@ def test_bad_checksum(self):
198
101
199
102
200
103
class VersionBase (Base ):
201
-
202
104
def _assert_cached_files_exist (self , exists , remove = False ):
203
105
path = os .path .join (tempfile .gettempdir (),
204
106
'chromedriver_{0}.zip' .format (self .version ))
@@ -208,9 +110,8 @@ def _assert_cached_files_exist(self, exists, remove=False):
208
110
209
111
assert os .path .exists (path ) is exists
210
112
211
- def _test_version (self , version_info , cached ):
212
- self .version = version_info
213
- self .checksums = VERSIONS [version_info ]
113
+ def _test_version (self , version , cached ):
114
+ self .version , self .checksums = version
214
115
215
116
# Chromedriver executable should not be available.
216
117
self ._not_available ()
@@ -222,16 +123,16 @@ def _test_version(self, version_info, cached):
222
123
subprocess .check_call (shlex .split (self ._get_install_command ()))
223
124
224
125
# ...the chromedriver executable should be available...
225
- expected_version = subprocess .Popen (
126
+ expected_version , error = subprocess .Popen (
226
127
shlex .split ('chromedriver -v' ),
227
128
stdout = subprocess .PIPE
228
- ).communicate ()[ 0 ]
129
+ ).communicate ()
229
130
230
131
# ...and should be of the right version.
231
132
assert self .version in str (expected_version )
232
133
233
- def test_version_uncached (self , version_info ):
234
- self ._test_version (version_info , cached = False )
134
+ def test_version_uncached (self , version ):
135
+ self ._test_version (version , cached = False )
235
136
236
137
237
138
class TestVersionOnly (VersionBase ):
@@ -249,5 +150,5 @@ def _get_install_command(self):
249
150
'--install-option="--chromedriver-checksums={1}"'
250
151
).format (self .version , ',' .join (self .checksums ))
251
152
252
- def test_version_cached (self , version_info ):
253
- self ._test_version (version_info , cached = True )
153
+ def test_version_cached (self , version ):
154
+ self ._test_version (version , cached = True )
0 commit comments