Test Setup Failed
Pull Request — develop (#161)
by
unknown
47s
created

describe_keep_location()   B

Complexity

Conditions 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 30
rs 8.5806
c 1
b 0
f 0

1 Method

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