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