1
1
import os
2
2
import re
3
+ import shutil
3
4
import unittest
4
5
from collections .abc import Collection
5
6
from unittest import mock
@@ -76,6 +77,13 @@ def _test_state(self, expected_message, expected_versions, redirect=True,
76
77
class TestDeploy (TestBase ):
77
78
stage_dir = 'deploy'
78
79
80
+ def setUp (self ):
81
+ super ().setUp ()
82
+ self .cfg .site_dir = os .path .join (self .cfg .site_dir , 'site' )
83
+
84
+ def _mock_build (self ):
85
+ copytree (self .stage , self .cfg .site_dir )
86
+
79
87
def _test_deploy (self , expected_message = None ,
80
88
expected_versions = [versions .VersionInfo ('1.0' )],
81
89
** kwargs ):
@@ -86,6 +94,8 @@ def _test_deploy(self, expected_message=None,
86
94
.format (rev , expected_versions [0 ].version )
87
95
)
88
96
97
+ if os .path .exists (self .cfg .site_dir ):
98
+ shutil .rmtree (self .cfg .site_dir )
89
99
self ._test_state (expected_message , expected_versions , ** kwargs )
90
100
91
101
def _mock_commit (self ):
@@ -101,19 +111,22 @@ def _mock_commit(self):
101
111
commit .add_file (git_utils .FileInfo ('latest/dir/index.html' , '' ))
102
112
103
113
def test_default (self ):
104
- commands .deploy (self .cfg , '1.0' )
114
+ with commands .deploy (self .cfg , '1.0' ):
115
+ self ._mock_build ()
105
116
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
106
117
self ._test_deploy ()
107
118
108
119
def test_title (self ):
109
- commands .deploy (self .cfg , '1.0' , '1.0.0' )
120
+ with commands .deploy (self .cfg , '1.0' , '1.0.0' ):
121
+ self ._mock_build ()
110
122
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
111
123
self ._test_deploy (expected_versions = [
112
124
versions .VersionInfo ('1.0' , '1.0.0' )
113
125
])
114
126
115
127
def test_aliases (self ):
116
- commands .deploy (self .cfg , '1.0' , aliases = ['latest' ])
128
+ with commands .deploy (self .cfg , '1.0' , aliases = ['latest' ]):
129
+ self ._mock_build ()
117
130
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
118
131
self ._test_deploy (expected_versions = [
119
132
versions .VersionInfo ('1.0' , aliases = ['latest' ])
@@ -126,7 +139,8 @@ def test_aliases(self):
126
139
127
140
def test_aliases_no_directory_urls (self ):
128
141
self .cfg .use_directory_urls = False
129
- commands .deploy (self .cfg , '1.0' , aliases = ['latest' ])
142
+ with commands .deploy (self .cfg , '1.0' , aliases = ['latest' ]):
143
+ self ._mock_build ()
130
144
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
131
145
self ._test_deploy (expected_versions = [
132
146
versions .VersionInfo ('1.0' , aliases = ['latest' ])
@@ -138,17 +152,24 @@ def test_aliases_no_directory_urls(self):
138
152
self .assertRegex (f .read (), match_redir ('../../1.0/dir/index.html' ))
139
153
140
154
def test_aliases_copy (self ):
141
- commands .deploy (self .cfg , '1.0' , aliases = ['latest' ], redirect = False )
155
+ with commands .deploy (self .cfg , '1.0' , aliases = ['latest' ],
156
+ redirect = False ):
157
+ self ._mock_build ()
142
158
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
143
159
self ._test_deploy (expected_versions = [
144
160
versions .VersionInfo ('1.0' , aliases = ['latest' ])
145
161
], redirect = False )
146
162
147
163
def test_aliases_custom_redirect (self ):
164
+ real_open = open
148
165
with mock .patch ('builtins.open' ,
149
166
mock .mock_open (read_data = b'{{href}}' )):
150
- commands .deploy (self .cfg , '1.0' , aliases = ['latest' ],
151
- template = 'template.html' )
167
+ with commands .deploy (self .cfg , '1.0' , aliases = ['latest' ],
168
+ template = 'template.html' ):
169
+ # Un-mock `open` so we can copy files for real.
170
+ with mock .patch ('builtins.open' , real_open ):
171
+ self ._mock_build ()
172
+
152
173
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
153
174
self ._test_deploy (expected_versions = [
154
175
versions .VersionInfo ('1.0' , aliases = ['latest' ])
@@ -160,17 +181,21 @@ def test_aliases_custom_redirect(self):
160
181
self .assertEqual (f .read (), '../../1.0/dir/' )
161
182
162
183
def test_branch (self ):
163
- commands .deploy (self .cfg , '1.0' , branch = 'branch' )
184
+ with commands .deploy (self .cfg , '1.0' , branch = 'branch' ):
185
+ self ._mock_build ()
164
186
check_call_silent (['git' , 'checkout' , 'branch' ])
165
187
self ._test_deploy ()
166
188
167
189
def test_commit_message (self ):
168
- commands .deploy (self .cfg , '1.0' , message = 'commit message' )
190
+ with commands .deploy (self .cfg , '1.0' , message = 'commit message' ):
191
+ self ._mock_build ()
169
192
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
170
193
self ._test_deploy ('^commit message$' )
171
194
172
195
def test_prefix (self ):
173
- commands .deploy (self .cfg , '1.0' , aliases = ['latest' ], prefix = 'prefix' )
196
+ with commands .deploy (self .cfg , '1.0' , aliases = ['latest' ],
197
+ prefix = 'prefix' ):
198
+ self ._mock_build ()
174
199
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
175
200
self ._test_deploy (expected_versions = [
176
201
versions .VersionInfo ('1.0' , aliases = ['latest' ])
@@ -185,23 +210,26 @@ def test_overwrite_version(self):
185
210
commit .add_file (git_utils .FileInfo ('1.0/old-page.html' , '' ))
186
211
commit .add_file (git_utils .FileInfo ('latest/old-page.html' , '' ))
187
212
188
- commands .deploy (self .cfg , '1.0' , '1.0.1' , ['greatest' ])
213
+ with commands .deploy (self .cfg , '1.0' , '1.0.1' , ['greatest' ]):
214
+ self ._mock_build ()
189
215
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
190
216
self ._test_deploy (expected_versions = [
191
217
versions .VersionInfo ('1.0' , '1.0.1' , ['latest' , 'greatest' ])
192
218
])
193
219
194
220
def test_overwrite_same_alias (self ):
195
221
self ._mock_commit ()
196
- commands .deploy (self .cfg , '1.0' , '1.0.1' , ['latest' ])
222
+ with commands .deploy (self .cfg , '1.0' , '1.0.1' , ['latest' ]):
223
+ self ._mock_build ()
197
224
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
198
225
self ._test_deploy (expected_versions = [
199
226
versions .VersionInfo ('1.0' , '1.0.1' , ['latest' ])
200
227
])
201
228
202
229
def test_overwrite_include_same_alias (self ):
203
230
self ._mock_commit ()
204
- commands .deploy (self .cfg , '1.0' , '1.0.1' , ['latest' , 'greatest' ])
231
+ with commands .deploy (self .cfg , '1.0' , '1.0.1' , ['latest' , 'greatest' ]):
232
+ self ._mock_build ()
205
233
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
206
234
self ._test_deploy (expected_versions = [
207
235
versions .VersionInfo ('1.0' , '1.0.1' , ['latest' , 'greatest' ])
@@ -210,15 +238,18 @@ def test_overwrite_include_same_alias(self):
210
238
def test_overwrite_alias_error (self ):
211
239
self ._mock_commit ()
212
240
with self .assertRaises (ValueError ):
213
- commands .deploy (self .cfg , '2.0' , '2.0.0' , ['latest' ])
241
+ with commands .deploy (self .cfg , '2.0' , '2.0.0' , ['latest' ]):
242
+ raise AssertionError ('should not get here' )
214
243
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
215
244
self ._test_deploy ('add versions.json' , [
216
245
versions .VersionInfo ('1.0' , '1.0' , ['latest' ])
217
246
])
218
247
219
248
def test_update_aliases (self ):
220
249
self ._mock_commit ()
221
- commands .deploy (self .cfg , '2.0' , '2.0.0' , ['latest' ], True )
250
+ with commands .deploy (self .cfg , '2.0' , '2.0.0' , ['latest' ],
251
+ update_aliases = True ):
252
+ self ._mock_build ()
222
253
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
223
254
self ._test_deploy ('.*' , [
224
255
versions .VersionInfo ('2.0' , '2.0.0' , ['latest' ]),
@@ -230,9 +261,11 @@ class TestDelete(TestBase):
230
261
stage_dir = 'delete'
231
262
232
263
def _deploy (self , branch = 'gh-pages' , prefix = '' ):
233
- commands .deploy (self .cfg , '1.0' , aliases = ['stable' ], branch = branch ,
234
- prefix = prefix )
235
- commands .deploy (self .cfg , '2.0' , branch = branch , prefix = prefix )
264
+ with commands .deploy (self .cfg , '1.0' , aliases = ['stable' ],
265
+ branch = branch , prefix = prefix ):
266
+ pass
267
+ with commands .deploy (self .cfg , '2.0' , branch = branch , prefix = prefix ):
268
+ pass
236
269
237
270
def _test_delete (self , expected_message = None ,
238
271
expected_versions = [versions .VersionInfo ('2.0' )],
@@ -306,8 +339,9 @@ class TestAlias(TestBase):
306
339
stage_dir = 'alias'
307
340
308
341
def _deploy (self , branch = 'gh-pages' , prefix = '' ):
309
- commands .deploy (self .cfg , '1.0' , aliases = ['latest' ], branch = branch ,
310
- prefix = prefix )
342
+ with commands .deploy (self .cfg , '1.0' , aliases = ['latest' ],
343
+ branch = branch , prefix = prefix ):
344
+ pass
311
345
312
346
def _test_alias (self , expected_message = None , expected_src = '1.0' ,
313
347
expected_aliases = ['greatest' ], ** kwargs ):
@@ -385,7 +419,8 @@ def test_alias_overwrite_include_same(self):
385
419
386
420
def test_alias_overwrite_error (self ):
387
421
self ._deploy ()
388
- commands .deploy (self .cfg , '2.0' )
422
+ with commands .deploy (self .cfg , '2.0' ):
423
+ pass
389
424
with self .assertRaises (ValueError ):
390
425
commands .alias (self .cfg , '2.0' , ['latest' ])
391
426
check_call_silent (['git' , 'checkout' , 'gh-pages' ])
@@ -395,7 +430,15 @@ def test_alias_overwrite_error(self):
395
430
])
396
431
397
432
def test_alias_update (self ):
398
- pass
433
+ self ._deploy ()
434
+ with commands .deploy (self .cfg , '2.0' ):
435
+ pass
436
+ commands .alias (self .cfg , '2.0' , ['latest' ], update_aliases = True )
437
+ check_call_silent (['git' , 'checkout' , 'gh-pages' ])
438
+ self ._test_state (r'^Copied 2\.0 to latest' , [
439
+ versions .VersionInfo ('2.0' , '2.0' , ['latest' ]),
440
+ versions .VersionInfo ('1.0' , '1.0' ),
441
+ ])
399
442
400
443
def test_branch (self ):
401
444
self ._deploy ('branch' )
@@ -431,7 +474,8 @@ def setUp(self):
431
474
commit_files (['file.txt' ])
432
475
433
476
def _deploy (self , branch = 'gh-pages' , prefix = '' ):
434
- commands .deploy (self .cfg , '1.0' , branch = branch , prefix = prefix )
477
+ with commands .deploy (self .cfg , '1.0' , branch = branch , prefix = prefix ):
478
+ pass
435
479
436
480
def _test_retitle (self , expected_message = None , directory = '.' ):
437
481
message = check_output (['git' , 'log' , '-1' , '--pretty=%B' ]).rstrip ()
@@ -492,7 +536,8 @@ def setUp(self):
492
536
commit_files (['file.txt' ])
493
537
494
538
def _deploy (self , branch = 'gh-pages' , prefix = '' ):
495
- commands .deploy (self .cfg , '1.0' , branch = branch , prefix = prefix )
539
+ with commands .deploy (self .cfg , '1.0' , branch = branch , prefix = prefix ):
540
+ pass
496
541
497
542
def _test_default (self , expr = r'window\.location\.replace\("1\.0/"\)' ,
498
543
expected_message = None , directory = '.' ):
0 commit comments