Passed
Pull Request — develop (#162)
by Mario
02:08
created

describe_links()   B

Complexity

Conditions 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

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