Completed
Push — develop ( a3b333...e4fb95 )
by Jace
10s
created

describe_sparse_paths()   B

Complexity

Conditions 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 28
rs 8.5806
c 1
b 0
f 0
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
278
def describe_uninstall():
279
280
    def it_deletes_dependencies_when_they_exist(config):
281
        gitman.install('gitman_1', depth=1)
282
        expect(os.path.isdir(config.location)) == True
283
284
        expect(gitman.uninstall()) == True
285
286
        expect(os.path.exists(config.location)) == False
287
288
    def it_should_not_fail_when_no_dependnecies_exist(config):
289
        expect(os.path.isdir(config.location)) == False
290
291
        expect(gitman.uninstall()) == True
292
293
    def it_deletes_the_log_file(config):
294
        gitman.install('gitman_1', depth=1)
295
        gitman.list()
296
        expect(os.path.exists(config.log_path)) == True
297
298
        gitman.uninstall()
299
        expect(os.path.exists(config.log_path)) == False
300
301
302
def describe_keep_location():
303
304
    def it_deletes_dependencies_when_they_exist(config):
305
        gitman.install('gitman_1', depth=1)
306
        expect(os.path.isdir(config.location)) == True
307
308
        expect(gitman.uninstall(keep_location=True)) == True
309
310
        expect(os.path.exists(os.path.join(config.location, 'gitman_1'))) == False
311
312
        expect(os.path.exists(config.location)) == True
313
314
        gitman.uninstall()
315
316
    def it_should_not_fail_when_no_dependencies_exist(config):
317
        expect(os.path.isdir(config.location)) == False
318
319
        expect(gitman.uninstall(keep_location=True)) == True
320
321
        gitman.uninstall()
322
323
    def it_deletes_the_log_file(config):
324
        gitman.install('gitman_1', depth=1)
325
        gitman.list()
326
        expect(os.path.exists(config.log_path)) == True
327
328
        gitman.uninstall(keep_location=True)
329
        expect(os.path.exists(config.log_path)) == False
330
331
        gitman.uninstall()
332
333
334
def describe_update():
335
336
    def it_should_not_modify_config(config):
337
        gitman.update('gitman_1', depth=1)
338
339
        expect(config.__mapper__.text) == CONFIG
340
341
    def it_locks_previously_locked_dependnecies(config):
342
        config.__mapper__.text = strip("""
343
        location: deps
344
        sources:
345
        - name: gitman_1
346
          repo: https://github.com/jacebrowning/gitman-demo
347
          sparse_paths:
348
          -
349
          rev: example-branch
350
          link:
351
          scripts:
352
          -
353
        - name: gitman_2
354
          repo: https://github.com/jacebrowning/gitman-demo
355
          sparse_paths:
356
          -
357
          rev: example-tag
358
          link:
359
          scripts:
360
          -
361
        sources_locked:
362
        - name: gitman_2
363
          repo: https://github.com/jacebrowning/gitman-demo
364
          sparse_paths:
365
          -
366
          rev: (old revision)
367
          link:
368
          scripts:
369
          -
370
        """)
371
372
        gitman.update(depth=1)
373
374
        expect(config.__mapper__.text) == strip("""
375
        location: deps
376
        sources:
377
        - name: gitman_1
378
          repo: https://github.com/jacebrowning/gitman-demo
379
          sparse_paths:
380
          -
381
          rev: example-branch
382
          link:
383
          scripts:
384
          -
385
        - name: gitman_2
386
          repo: https://github.com/jacebrowning/gitman-demo
387
          sparse_paths:
388
          -
389
          rev: example-tag
390
          link:
391
          scripts:
392
          -
393
        sources_locked:
394
        - name: gitman_2
395
          repo: https://github.com/jacebrowning/gitman-demo
396
          sparse_paths:
397
          -
398
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
399
          link:
400
          scripts:
401
          -
402
        """)
403
404
    def it_should_not_lock_dependnecies_when_disabled(config):
405
        config.__mapper__.text = strip("""
406
        location: deps
407
        sources:
408
        - name: gitman_1
409
          repo: https://github.com/jacebrowning/gitman-demo
410
          sparse_paths:
411
          -
412
          rev: example-branch
413
          link:
414
          scripts:
415
          -
416
        - name: gitman_2
417
          repo: https://github.com/jacebrowning/gitman-demo
418
          sparse_paths:
419
          -
420
          rev: example-tag
421
          link:
422
          scripts:
423
          -
424
        sources_locked:
425
        - name: gitman_2
426
          repo: https://github.com/jacebrowning/gitman-demo
427
          sparse_paths:
428
          -
429
          rev: (old revision)
430
          link:
431
          scripts:
432
          -
433
        """)
434
435
        gitman.update(depth=1, lock=False)
436
437
        expect(config.__mapper__.text) == strip("""
438
        location: deps
439
        sources:
440
        - name: gitman_1
441
          repo: https://github.com/jacebrowning/gitman-demo
442
          sparse_paths:
443
          -
444
          rev: example-branch
445
          link:
446
          scripts:
447
          -
448
        - name: gitman_2
449
          repo: https://github.com/jacebrowning/gitman-demo
450
          sparse_paths:
451
          -
452
          rev: example-tag
453
          link:
454
          scripts:
455
          -
456
        sources_locked:
457
        - name: gitman_2
458
          repo: https://github.com/jacebrowning/gitman-demo
459
          sparse_paths:
460
          -
461
          rev: (old revision)
462
          link:
463
          scripts:
464
          -
465
        """)
466
467
    def it_should_lock_all_dependencies_when_enabled(config):
468
        gitman.update(depth=1, lock=True)
469
470
        expect(config.__mapper__.text) == CONFIG + strip("""
471
        sources_locked:
472
        - name: gitman_1
473
          repo: https://github.com/jacebrowning/gitman-demo
474
          sparse_paths:
475
          -
476
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
477
          link:
478
          scripts:
479
          -
480
        - name: gitman_2
481
          repo: https://github.com/jacebrowning/gitman-demo
482
          sparse_paths:
483
          -
484
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
485
          link:
486
          scripts:
487
          -
488
        - name: gitman_3
489
          repo: https://github.com/jacebrowning/gitman-demo
490
          sparse_paths:
491
          -
492
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
493
          link:
494
          scripts:
495
          -
496
        """)
497
498
499
def describe_list():
500
501
    @freeze_time("2012-01-14 12:00:01")
502
    def it_updates_the_log(config):
503
        gitman.install()
504
        gitman.list()
505
506
        with open(config.log_path) as stream:
507
            contents = stream.read().replace(TMP, "tmp").replace('\\', '/')
508
        expect(contents) == strip("""
509
        2012-01-14 12:00:01
510
        tmp/deps/gitman_1: https://github.com/jacebrowning/gitman-demo @ 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
511
        tmp/deps/gitman_1/gitman_sources/gdm_3: https://github.com/jacebrowning/gdm-demo @ 050290bca3f14e13fd616604202b579853e7bfb0
512
        tmp/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_3: https://github.com/jacebrowning/gdm-demo @ fb693447579235391a45ca170959b5583c5042d8
513
        tmp/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_4: https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5
514
        tmp/deps/gitman_1/gitman_sources/gdm_4: https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5
515
        tmp/deps/gitman_2: https://github.com/jacebrowning/gitman-demo @ 7bd138fe7359561a8c2ff9d195dff238794ccc04
516
        tmp/deps/gitman_3: https://github.com/jacebrowning/gitman-demo @ 9bf18e16b956041f0267c21baad555a23237b52e
517
        """, end='\n\n')
518
519
520
def describe_lock():
521
522
    def it_records_all_versions_when_no_arguments(config):
523
        expect(gitman.update(depth=1, lock=False)) == True
524
        expect(gitman.lock()) == True
525
526
        expect(config.__mapper__.text) == CONFIG + strip("""
527
        sources_locked:
528
        - name: gitman_1
529
          repo: https://github.com/jacebrowning/gitman-demo
530
          sparse_paths:
531
          -
532
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
533
          link:
534
          scripts:
535
          -
536
        - name: gitman_2
537
          repo: https://github.com/jacebrowning/gitman-demo
538
          sparse_paths:
539
          -
540
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
541
          link:
542
          scripts:
543
          -
544
        - name: gitman_3
545
          repo: https://github.com/jacebrowning/gitman-demo
546
          sparse_paths:
547
          -
548
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
549
          link:
550
          scripts:
551
          -
552
        """) == config.__mapper__.text
553
554
    def it_records_specified_dependencies(config):
555
        expect(gitman.update(depth=1, lock=False)) == True
556
        expect(gitman.lock('gitman_1', 'gitman_3')) == True
557
558
        expect(config.__mapper__.text) == CONFIG + strip("""
559
        sources_locked:
560
        - name: gitman_1
561
          repo: https://github.com/jacebrowning/gitman-demo
562
          sparse_paths:
563
          -
564
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
565
          link:
566
          scripts:
567
          -
568
        - name: gitman_3
569
          repo: https://github.com/jacebrowning/gitman-demo
570
          sparse_paths:
571
          -
572
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
573
          link:
574
          scripts:
575
          -
576
        """) == config.__mapper__.text
577
578
    def it_should_fail_on_dirty_repositories(config):
579
        expect(gitman.update(depth=1, lock=False)) == True
580
        shell.rm(os.path.join("deps", "gitman_1", ".project"))
581
582
        try:
583
            with pytest.raises(UncommittedChanges):
584
                gitman.lock()
585
586
            expect(config.__mapper__.text).does_not_contain("<dirty>")
587
588
        finally:
589
            shell.rm(os.path.join("deps", "gitman_1"))
590
591
    def it_should_fail_on_missing_repositories(config):
592
        shell.mkdir("deps")
593
        shell.rm(os.path.join("deps", "gitman_1"))
594
595
        with pytest.raises(InvalidRepository):
596
            gitman.lock()
597
598
        expect(config.__mapper__.text).does_not_contain("<unknown>")
599
600
    def it_should_fail_on_invalid_repositories(config):
601
        shell.mkdir("deps")
602
        shell.rm(os.path.join("deps", "gitman_1", ".git"))
603
        shell.mkdir(os.path.join("deps", "gitman_1", ".git"))
604
605
        try:
606
            with pytest.raises(InvalidRepository):
607
                gitman.lock()
608
609
            expect(config.__mapper__.text).does_not_contain("<unknown>")
610
611
        finally:
612
            shell.rm(os.path.join("deps", "gitman_1"))
613