1
|
|
|
# pylint: disable=redefined-outer-name,unused-argument,unused-variable,singleton-comparison,expression-not-assigned |
|
|
|
|
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
import shutil |
5
|
|
|
from contextlib import suppress |
6
|
|
|
import logging |
7
|
|
|
|
8
|
|
|
import pytest |
9
|
|
|
from expecter import expect |
|
|
|
|
10
|
|
|
from freezegun import freeze_time |
|
|
|
|
11
|
|
|
|
12
|
|
|
import gitman |
13
|
|
|
from gitman import shell |
14
|
|
|
from gitman.models import Config |
15
|
|
|
from gitman.exceptions import UncommittedChanges, InvalidRepository |
16
|
|
|
|
17
|
|
|
from .utilities import strip |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__))) |
21
|
|
|
TMP = os.path.join(ROOT, 'tmp') |
22
|
|
|
|
23
|
|
|
CONFIG = """ |
24
|
|
|
location: deps |
25
|
|
|
sources: |
26
|
|
|
- name: gitman_1 |
27
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
28
|
|
|
rev: example-branch |
29
|
|
|
link: '' |
30
|
|
|
scripts: [] |
31
|
|
|
- name: gitman_2 |
32
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
33
|
|
|
rev: example-tag |
34
|
|
|
link: '' |
35
|
|
|
scripts: [] |
36
|
|
|
- name: gitman_3 |
37
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
38
|
|
|
rev: 9bf18e16b956041f0267c21baad555a23237b52e |
39
|
|
|
link: '' |
40
|
|
|
scripts: [] |
41
|
|
|
""".lstrip() |
42
|
|
|
|
43
|
|
|
log = logging.getLogger(__name__) |
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
@pytest.yield_fixture |
47
|
|
|
def config(): |
|
|
|
|
48
|
|
|
log.info("Temporary directory: %s", TMP) |
49
|
|
|
|
50
|
|
|
with suppress(FileNotFoundError, PermissionError): |
51
|
|
|
shutil.rmtree(TMP) |
52
|
|
|
with suppress(FileExistsError): |
53
|
|
|
os.makedirs(TMP) |
54
|
|
|
os.chdir(TMP) |
55
|
|
|
|
56
|
|
|
os.system("touch .git") |
57
|
|
|
config = Config(root=TMP) |
58
|
|
|
config.__mapper__.text = CONFIG |
|
|
|
|
59
|
|
|
|
60
|
|
|
log.debug("File listing: %s", os.listdir(TMP)) |
61
|
|
|
|
62
|
|
|
yield config |
63
|
|
|
|
64
|
|
|
os.chdir(ROOT) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def describe_init(): |
|
|
|
|
68
|
|
|
|
69
|
|
|
def it_creates_a_new_config_file(tmpdir): |
|
|
|
|
70
|
|
|
tmpdir.chdir() |
71
|
|
|
|
72
|
|
|
expect(gitman.init()) == True |
73
|
|
|
|
74
|
|
|
expect(Config().__mapper__.text) == strip(""" |
|
|
|
|
75
|
|
|
location: gitman_sources |
76
|
|
|
sources: |
77
|
|
|
- name: sample_dependency |
78
|
|
|
repo: https://github.com/githubtraining/hellogitworld |
79
|
|
|
rev: master |
80
|
|
|
link: '' |
81
|
|
|
scripts: [] |
82
|
|
|
sources_locked: |
83
|
|
|
- name: sample_dependency |
84
|
|
|
repo: https://github.com/githubtraining/hellogitworld |
85
|
|
|
rev: ebbbf773431ba07510251bb03f9525c7bab2b13a |
86
|
|
|
link: '' |
87
|
|
|
scripts: [] |
88
|
|
|
""") |
89
|
|
|
|
90
|
|
|
def it_does_not_modify_existing_config_file(config): |
|
|
|
|
91
|
|
|
expect(gitman.init()) == False |
92
|
|
|
|
93
|
|
|
expect(config.__mapper__.text) == CONFIG |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
def describe_install(): |
|
|
|
|
97
|
|
|
|
98
|
|
|
def it_creates_missing_directories(config): |
|
|
|
|
99
|
|
|
shell.rm(config.location) |
100
|
|
|
|
101
|
|
|
expect(gitman.install('gitman_1', depth=1)) == True |
102
|
|
|
|
103
|
|
|
expect(os.listdir(config.location)) == ['gitman_1'] |
104
|
|
|
|
105
|
|
|
def it_should_not_modify_config(config): |
|
|
|
|
106
|
|
|
expect(gitman.install('gitman_1', depth=1)) == True |
107
|
|
|
|
108
|
|
|
expect(config.__mapper__.text) == CONFIG |
109
|
|
|
|
110
|
|
|
def it_merges_sources(config): |
|
|
|
|
111
|
|
|
config.__mapper__.text = strip(""" |
112
|
|
|
location: deps |
113
|
|
|
sources: |
114
|
|
|
- name: gitman_1 |
115
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
116
|
|
|
rev: example-branch |
117
|
|
|
link: '' |
118
|
|
|
scripts: [] |
119
|
|
|
sources_locked: |
120
|
|
|
- name: gitman_2 |
121
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
122
|
|
|
rev: example-branch |
123
|
|
|
link: '' |
124
|
|
|
scripts: [] |
125
|
|
|
- name: gitman_3 |
126
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
127
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
128
|
|
|
link: '' |
129
|
|
|
scripts: [] |
130
|
|
|
""") |
131
|
|
|
|
132
|
|
|
expect(gitman.install(depth=1)) == True |
133
|
|
|
|
134
|
|
|
expect(len(os.listdir(config.location))) == 3 |
135
|
|
|
|
136
|
|
|
def it_can_handle_missing_locked_sources(config): |
|
|
|
|
137
|
|
|
config.__mapper__.text = strip(""" |
138
|
|
|
location: deps |
139
|
|
|
sources: |
140
|
|
|
- name: gitman_1 |
141
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
142
|
|
|
rev: example-branch |
143
|
|
|
link: '' |
144
|
|
|
scripts: [] |
145
|
|
|
sources_locked: |
146
|
|
|
- name: gitman_2 |
147
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
148
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
149
|
|
|
link: '' |
150
|
|
|
scripts: [] |
151
|
|
|
""") |
152
|
|
|
|
153
|
|
|
expect(gitman.install('gitman_1', depth=1)) == True |
154
|
|
|
|
155
|
|
|
expect(os.listdir(config.location)) == ['gitman_1'] |
156
|
|
|
|
157
|
|
|
def it_detects_invalid_repositories(config): |
|
|
|
|
158
|
|
|
shell.rm(os.path.join("deps", "gitman_1", ".git")) |
159
|
|
|
shell.mkdir(os.path.join("deps", "gitman_1", ".git")) |
160
|
|
|
|
161
|
|
|
try: |
162
|
|
|
with pytest.raises(InvalidRepository): |
163
|
|
|
expect(gitman.install('gitman_1', depth=1)) == False |
164
|
|
|
|
165
|
|
|
finally: |
166
|
|
|
shell.rm(os.path.join("deps", "gitman_1")) |
167
|
|
|
|
168
|
|
|
def describe_links(): |
|
|
|
|
169
|
|
|
|
170
|
|
|
@pytest.fixture |
171
|
|
|
def config_with_link(config): |
|
|
|
|
172
|
|
|
config.__mapper__.text = strip(""" |
173
|
|
|
location: deps |
174
|
|
|
sources: |
175
|
|
|
- name: gitman_1 |
176
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
177
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
178
|
|
|
link: my_link |
179
|
|
|
scripts: [] |
180
|
|
|
""") |
181
|
|
|
|
182
|
|
|
return config |
183
|
|
|
|
184
|
|
|
@pytest.mark.xfail(os.name == 'nt', reason="No symlinks on Windows") |
185
|
|
|
def it_should_create_links(config_with_link): |
|
|
|
|
186
|
|
|
expect(gitman.install(depth=1)) == True |
187
|
|
|
|
188
|
|
|
expect(os.listdir()).contains('my_link') |
189
|
|
|
|
190
|
|
|
@pytest.mark.xfail(os.name == 'nt', reason="No symlinks on Windows") |
191
|
|
|
def it_should_not_overwrite_files(config_with_link): |
|
|
|
|
192
|
|
|
os.system("touch my_link") |
193
|
|
|
|
194
|
|
|
with pytest.raises(RuntimeError): |
195
|
|
|
gitman.install(depth=1) |
196
|
|
|
|
197
|
|
|
@pytest.mark.xfail(os.name == 'nt', reason="No symlinks on Windows") |
198
|
|
|
def it_overwrites_files_with_force(config_with_link): |
|
|
|
|
199
|
|
|
os.system("touch my_link") |
200
|
|
|
|
201
|
|
|
expect(gitman.install(depth=1, force=True)) == True |
202
|
|
|
|
203
|
|
|
def describe_scripts(): |
|
|
|
|
204
|
|
|
|
205
|
|
|
@pytest.fixture |
206
|
|
|
def config_with_scripts(config): |
|
|
|
|
207
|
|
|
config.__mapper__.text = strip(""" |
208
|
|
|
location: deps |
209
|
|
|
sources: |
210
|
|
|
- name: gitman_1 |
211
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
212
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
213
|
|
|
link: '' |
214
|
|
|
scripts: |
215
|
|
|
- make foobar |
216
|
|
|
""") |
217
|
|
|
|
218
|
|
|
return config |
219
|
|
|
|
220
|
|
|
def it_detects_failures_in_scripts(config_with_scripts): |
|
|
|
|
221
|
|
|
with pytest.raises(RuntimeError): |
222
|
|
|
expect(gitman.install()) |
223
|
|
|
|
224
|
|
|
def script_failures_can_be_ignored(config_with_scripts): |
|
|
|
|
225
|
|
|
expect(gitman.install(force=True)) == True |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
def describe_uninstall(): |
|
|
|
|
229
|
|
|
|
230
|
|
|
def it_deletes_dependencies_when_they_exist(config): |
|
|
|
|
231
|
|
|
gitman.install('gitman_1', depth=1) |
232
|
|
|
expect(os.path.isdir(config.location)) == True |
233
|
|
|
|
234
|
|
|
expect(gitman.uninstall()) == True |
235
|
|
|
|
236
|
|
|
expect(os.path.exists(config.location)) == False |
237
|
|
|
|
238
|
|
|
def it_should_not_fail_when_no_dependnecies_exist(config): |
|
|
|
|
239
|
|
|
expect(os.path.isdir(config.location)) == False |
240
|
|
|
|
241
|
|
|
expect(gitman.uninstall()) == True |
242
|
|
|
|
243
|
|
|
def it_deletes_the_log_file(config): |
|
|
|
|
244
|
|
|
gitman.install('gitman_1', depth=1) |
245
|
|
|
gitman.list() |
246
|
|
|
expect(os.path.exists(config.log_path)) == True |
247
|
|
|
|
248
|
|
|
gitman.uninstall() |
249
|
|
|
expect(os.path.exists(config.log_path)) == False |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
def describe_update(): |
|
|
|
|
253
|
|
|
|
254
|
|
|
def it_should_not_modify_config(config): |
|
|
|
|
255
|
|
|
gitman.update('gitman_1', depth=1) |
256
|
|
|
|
257
|
|
|
expect(config.__mapper__.text) == CONFIG |
258
|
|
|
|
259
|
|
|
def it_locks_previously_locked_dependnecies(config): |
|
|
|
|
260
|
|
|
config.__mapper__.text = strip(""" |
261
|
|
|
location: deps |
262
|
|
|
sources: |
263
|
|
|
- name: gitman_1 |
264
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
265
|
|
|
rev: example-branch |
266
|
|
|
link: '' |
267
|
|
|
scripts: [] |
268
|
|
|
- name: gitman_2 |
269
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
270
|
|
|
rev: example-tag |
271
|
|
|
link: '' |
272
|
|
|
scripts: [] |
273
|
|
|
sources_locked: |
274
|
|
|
- name: gitman_2 |
275
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
276
|
|
|
rev: (old revision) |
277
|
|
|
link: '' |
278
|
|
|
scripts: [] |
279
|
|
|
""") |
280
|
|
|
|
281
|
|
|
gitman.update(depth=1) |
282
|
|
|
|
283
|
|
|
expect(config.__mapper__.text) == strip(""" |
284
|
|
|
location: deps |
285
|
|
|
sources: |
286
|
|
|
- name: gitman_1 |
287
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
288
|
|
|
rev: example-branch |
289
|
|
|
link: '' |
290
|
|
|
scripts: [] |
291
|
|
|
- name: gitman_2 |
292
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
293
|
|
|
rev: example-tag |
294
|
|
|
link: '' |
295
|
|
|
scripts: [] |
296
|
|
|
sources_locked: |
297
|
|
|
- name: gitman_2 |
298
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
299
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
300
|
|
|
link: '' |
301
|
|
|
scripts: [] |
302
|
|
|
""") |
303
|
|
|
|
304
|
|
|
def it_should_not_lock_dependnecies_when_disabled(config): |
|
|
|
|
305
|
|
|
config.__mapper__.text = strip(""" |
306
|
|
|
location: deps |
307
|
|
|
sources: |
308
|
|
|
- name: gitman_1 |
309
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
310
|
|
|
rev: example-branch |
311
|
|
|
link: '' |
312
|
|
|
scripts: [] |
313
|
|
|
- name: gitman_2 |
314
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
315
|
|
|
rev: example-tag |
316
|
|
|
link: '' |
317
|
|
|
scripts: [] |
318
|
|
|
sources_locked: |
319
|
|
|
- name: gitman_2 |
320
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
321
|
|
|
rev: (old revision) |
322
|
|
|
link: '' |
323
|
|
|
scripts: [] |
324
|
|
|
""") |
325
|
|
|
|
326
|
|
|
gitman.update(depth=1, lock=False) |
327
|
|
|
|
328
|
|
|
expect(config.__mapper__.text) == strip(""" |
329
|
|
|
location: deps |
330
|
|
|
sources: |
331
|
|
|
- name: gitman_1 |
332
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
333
|
|
|
rev: example-branch |
334
|
|
|
link: '' |
335
|
|
|
scripts: [] |
336
|
|
|
- name: gitman_2 |
337
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
338
|
|
|
rev: example-tag |
339
|
|
|
link: '' |
340
|
|
|
scripts: [] |
341
|
|
|
sources_locked: |
342
|
|
|
- name: gitman_2 |
343
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
344
|
|
|
rev: (old revision) |
345
|
|
|
link: '' |
346
|
|
|
scripts: [] |
347
|
|
|
""") |
348
|
|
|
|
349
|
|
|
def it_should_lock_all_dependencies_when_enabled(config): |
|
|
|
|
350
|
|
|
gitman.update(depth=1, lock=True) |
351
|
|
|
|
352
|
|
|
expect(config.__mapper__.text) == CONFIG + strip(""" |
353
|
|
|
sources_locked: |
354
|
|
|
- name: gitman_1 |
355
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
356
|
|
|
rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd |
357
|
|
|
link: '' |
358
|
|
|
scripts: [] |
359
|
|
|
- name: gitman_2 |
360
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
361
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
362
|
|
|
link: '' |
363
|
|
|
scripts: [] |
364
|
|
|
- name: gitman_3 |
365
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
366
|
|
|
rev: 9bf18e16b956041f0267c21baad555a23237b52e |
367
|
|
|
link: '' |
368
|
|
|
scripts: [] |
369
|
|
|
""") |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
def describe_list(): |
|
|
|
|
373
|
|
|
|
374
|
|
|
@freeze_time("2012-01-14 12:00:01") |
375
|
|
|
def it_updates_the_log(config): |
|
|
|
|
376
|
|
|
gitman.install() |
377
|
|
|
gitman.list() |
378
|
|
|
|
379
|
|
|
with open(config.log_path) as stream: |
380
|
|
|
contents = stream.read().replace(TMP, "tmp").replace('\\', '/') |
381
|
|
|
expect(contents) == strip(""" |
382
|
|
|
2012-01-14 12:00:01 |
383
|
|
|
tmp/deps/gitman_1: https://github.com/jacebrowning/gitman-demo @ 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd |
384
|
|
|
tmp/deps/gitman_1/gitman_sources/gdm_3: https://github.com/jacebrowning/gdm-demo @ 050290bca3f14e13fd616604202b579853e7bfb0 |
385
|
|
|
tmp/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_3: https://github.com/jacebrowning/gdm-demo @ fb693447579235391a45ca170959b5583c5042d8 |
386
|
|
|
tmp/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_4: https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5 |
387
|
|
|
tmp/deps/gitman_1/gitman_sources/gdm_4: https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5 |
388
|
|
|
tmp/deps/gitman_2: https://github.com/jacebrowning/gitman-demo @ 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
389
|
|
|
tmp/deps/gitman_3: https://github.com/jacebrowning/gitman-demo @ 9bf18e16b956041f0267c21baad555a23237b52e |
390
|
|
|
""", end='\n\n') |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
def describe_lock(): |
|
|
|
|
394
|
|
|
|
395
|
|
|
def it_records_all_versions_when_no_arguments(config): |
|
|
|
|
396
|
|
|
expect(gitman.update(depth=1, lock=False)) == True |
397
|
|
|
expect(gitman.lock()) == True |
398
|
|
|
|
399
|
|
|
expect(config.__mapper__.text) == CONFIG + strip(""" |
400
|
|
|
sources_locked: |
401
|
|
|
- name: gitman_1 |
402
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
403
|
|
|
rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd |
404
|
|
|
link: '' |
405
|
|
|
scripts: [] |
406
|
|
|
- name: gitman_2 |
407
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
408
|
|
|
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04 |
409
|
|
|
link: '' |
410
|
|
|
scripts: [] |
411
|
|
|
- name: gitman_3 |
412
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
413
|
|
|
rev: 9bf18e16b956041f0267c21baad555a23237b52e |
414
|
|
|
link: '' |
415
|
|
|
scripts: [] |
416
|
|
|
""") == config.__mapper__.text |
417
|
|
|
|
418
|
|
|
def it_records_specified_dependencies(config): |
|
|
|
|
419
|
|
|
expect(gitman.update(depth=1, lock=False)) == True |
420
|
|
|
expect(gitman.lock('gitman_1', 'gitman_3')) == True |
421
|
|
|
|
422
|
|
|
expect(config.__mapper__.text) == CONFIG + strip(""" |
423
|
|
|
sources_locked: |
424
|
|
|
- name: gitman_1 |
425
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
426
|
|
|
rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd |
427
|
|
|
link: '' |
428
|
|
|
scripts: [] |
429
|
|
|
- name: gitman_3 |
430
|
|
|
repo: https://github.com/jacebrowning/gitman-demo |
431
|
|
|
rev: 9bf18e16b956041f0267c21baad555a23237b52e |
432
|
|
|
link: '' |
433
|
|
|
scripts: [] |
434
|
|
|
""") == config.__mapper__.text |
435
|
|
|
|
436
|
|
|
def it_should_fail_on_dirty_repositories(config): |
|
|
|
|
437
|
|
|
expect(gitman.update(depth=1, lock=False)) == True |
438
|
|
|
shell.rm(os.path.join("deps", "gitman_1", ".project")) |
439
|
|
|
|
440
|
|
|
try: |
441
|
|
|
with pytest.raises(UncommittedChanges): |
442
|
|
|
gitman.lock() |
443
|
|
|
|
444
|
|
|
expect(config.__mapper__.text).does_not_contain("<dirty>") |
445
|
|
|
|
446
|
|
|
finally: |
447
|
|
|
shell.rm(os.path.join("deps", "gitman_1")) |
448
|
|
|
|
449
|
|
|
def it_should_fail_on_missing_repositories(config): |
|
|
|
|
450
|
|
|
shell.mkdir("deps") |
451
|
|
|
shell.rm(os.path.join("deps", "gitman_1")) |
452
|
|
|
|
453
|
|
|
with pytest.raises(InvalidRepository): |
454
|
|
|
gitman.lock() |
455
|
|
|
|
456
|
|
|
expect(config.__mapper__.text).does_not_contain("<unknown>") |
457
|
|
|
|
458
|
|
|
def it_should_fail_on_invalid_repositories(config): |
|
|
|
|
459
|
|
|
shell.mkdir("deps") |
460
|
|
|
shell.rm(os.path.join("deps", "gitman_1", ".git")) |
461
|
|
|
shell.mkdir(os.path.join("deps", "gitman_1", ".git")) |
462
|
|
|
|
463
|
|
|
try: |
464
|
|
|
with pytest.raises(InvalidRepository): |
465
|
|
|
gitman.lock() |
466
|
|
|
|
467
|
|
|
expect(config.__mapper__.text).does_not_contain("<unknown>") |
468
|
|
|
|
469
|
|
|
finally: |
470
|
|
|
shell.rm(os.path.join("deps", "gitman_1")) |
471
|
|
|
|