Completed
Pull Request — develop (#140)
by
unknown
09:57
created

it_should_not_lock_dependnecies_when_disabled()   A

Complexity

Conditions 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
dl 0
loc 22
rs 9.2
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
import tempfile
8
9
import pytest
10
from expecter import expect
11
from freezegun import freeze_time
12
13
import gitman
14
from gitman.models import Config
15
from gitman.exceptions import UncommittedChanges, InvalidRepository
16
17
from .utilities import strip
18
19
20
CONFIG = """
21
location: deps
22
sources:
23
- name: gitman_1
24
  link: ''
25
  repo: https://github.com/jacebrowning/gitman-demo
26
  rev: example-branch
27
- name: gitman_2
28
  link: ''
29
  repo: https://github.com/jacebrowning/gitman-demo
30
  rev: example-tag
31
- name: gitman_3
32
  link: ''
33
  repo: https://github.com/jacebrowning/gitman-demo
34
  rev: 9bf18e16b956041f0267c21baad555a23237b52e
35
""".lstrip()
36
37
log = logging.getLogger(__name__)
38
39
40
@pytest.fixture
41
def config(root=os.path.normpath(tempfile.gettempdir() + "/gitman-shared")):
42
    with suppress(FileNotFoundError, PermissionError):
43
        shutil.rmtree(root)
44
    with suppress(FileExistsError):
45
        os.makedirs(root)
46
    os.chdir(root)
47
    log.info("Temporary directory: %s", root)
48
49
    os.system("touch .git")
50
    config = Config(root=root)
51
    config.__mapper__.text = CONFIG  # pylint: disable=no-member
52
53
    log.debug("File listing: %s", os.listdir(root))
54
55
    return config
56
57
58
def describe_install():
59
60
    def it_creates_missing_directories(config):
61
        expect(os.path.isdir(config.location)) == False
62
63
        expect(gitman.install('gitman_1', depth=1)) == True
64
65
        expect(os.listdir(config.location)) == ['gitman_1']
66
67
    def it_should_not_modify_config(config):
68
        expect(gitman.install('gitman_1', depth=1)) == True
69
70
        expect(config.__mapper__.text) == CONFIG
71
72
    def it_merges_sources(config):
73
        config.__mapper__.text = strip("""
74
        location: deps
75
        sources:
76
        - name: gitman_1
77
          link: ''
78
          repo: https://github.com/jacebrowning/gitman-demo
79
          rev: example-branch
80
        sources_locked:
81
        - name: gitman_2
82
          link: ''
83
          repo: https://github.com/jacebrowning/gitman-demo
84
          rev: example-branch
85
        - name: gitman_3
86
          link: ''
87
          repo: https://github.com/jacebrowning/gitman-demo
88
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
89
        """)
90
91
        expect(gitman.install(depth=1)) == True
92
93
        expect(len(os.listdir(config.location))) == 3
94
95
    def it_can_handle_missing_locked_sources(config):
96
        config.__mapper__.text = strip("""
97
        location: deps
98
        sources:
99
        - name: gitman_1
100
          link: ''
101
          repo: https://github.com/jacebrowning/gitman-demo
102
          rev: example-branch
103
        sources_locked:
104
        - name: gitman_2
105
          link: ''
106
          repo: https://github.com/jacebrowning/gitman-demo
107
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
108
        """)
109
110
        expect(gitman.install('gitman_1', depth=1)) == True
111
112
        expect(os.listdir(config.location)) == ['gitman_1']
113
114
    def describe_links():
115
116
        @pytest.fixture
117
        def config_with_link(config):
118
            config.__mapper__.text = strip("""
119
            location: deps
120
            sources:
121
            - name: gitman_1
122
              link: my_link
123
              repo: https://github.com/jacebrowning/gitman-demo
124
              rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
125
            """)
126
127
            return config
128
129
        @pytest.mark.skipif(os.name == 'nt', reason="no symlink on Windows")
130
        def it_should_create_links(config_with_link):
131
            expect(gitman.install(depth=1)) == True
132
133
            expect(os.listdir()).contains('my_link')
134
135
        @pytest.mark.skipif(os.name == 'nt', reason="no symlink on Windows")
136
        def it_should_not_overwrite_files(config_with_link):
137
            os.system("touch my_link")
138
139
            with pytest.raises(RuntimeError):
140
                gitman.install(depth=1)
141
142
        @pytest.mark.skipif(os.name == 'nt', reason="no symlink on Windows")
143
        def it_overwrites_files_with_force(config_with_link):
144
            os.system("touch my_link")
145
146
            expect(gitman.install(depth=1, force=True)) == True
147
148
149
def describe_uninstall():
150
151
    def it_deletes_dependencies_when_they_exist(config):
152
        gitman.install('gitman_1', depth=1)
153
        expect(os.path.isdir(config.location)) == True
154
155
        expect(gitman.uninstall()) == True
156
157
        expect(os.path.exists(config.location)) == False
158
159
    def it_should_not_fail_when_no_dependnecies_exist(config):
160
        expect(os.path.isdir(config.location)) == False
161
162
        expect(gitman.uninstall()) == True
163
164
    def it_deletes_the_log_file(config):
165
        gitman.install('gitman_1', depth=1)
166
        gitman.list()
167
        expect(os.path.exists(config.log_path)) == True
168
169
        gitman.uninstall()
170
        expect(os.path.exists(config.log_path)) == False
171
172
173
def describe_update():
174
175
    def it_should_not_modify_config(config):
176
        gitman.update('gitman_1', depth=1)
177
178
        expect(config.__mapper__.text) == CONFIG
179
180
    def it_locks_previously_locked_dependnecies(config):
181
        config.__mapper__.text = strip("""
182
        location: deps
183
        sources:
184
        - name: gitman_1
185
          link: ''
186
          repo: https://github.com/jacebrowning/gitman-demo
187
          rev: example-branch
188
        - name: gitman_2
189
          link: ''
190
          repo: https://github.com/jacebrowning/gitman-demo
191
          rev: example-tag
192
        sources_locked:
193
        - name: gitman_2
194
          link: ''
195
          repo: https://github.com/jacebrowning/gitman-demo
196
          rev: (old revision)
197
        """)
198
199
        gitman.update(depth=1)
200
201
        expect(config.__mapper__.text) == strip("""
202
        location: deps
203
        sources:
204
        - name: gitman_1
205
          link: ''
206
          repo: https://github.com/jacebrowning/gitman-demo
207
          rev: example-branch
208
        - name: gitman_2
209
          link: ''
210
          repo: https://github.com/jacebrowning/gitman-demo
211
          rev: example-tag
212
        sources_locked:
213
        - name: gitman_2
214
          link: ''
215
          repo: https://github.com/jacebrowning/gitman-demo
216
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
217
        """)
218
219
    def it_should_not_lock_dependnecies_when_disabled(config):
220
        config.__mapper__.text = strip("""
221
        location: deps
222
        sources:
223
        - name: gitman_1
224
          link: ''
225
          repo: https://github.com/jacebrowning/gitman-demo
226
          rev: example-branch
227
        - name: gitman_2
228
          link: ''
229
          repo: https://github.com/jacebrowning/gitman-demo
230
          rev: example-tag
231
        sources_locked:
232
        - name: gitman_2
233
          link: ''
234
          repo: https://github.com/jacebrowning/gitman-demo
235
          rev: (old revision)
236
        """)
237
238
        gitman.update(depth=1, lock=False)
239
240
        expect(config.__mapper__.text) == strip("""
241
        location: deps
242
        sources:
243
        - name: gitman_1
244
          link: ''
245
          repo: https://github.com/jacebrowning/gitman-demo
246
          rev: example-branch
247
        - name: gitman_2
248
          link: ''
249
          repo: https://github.com/jacebrowning/gitman-demo
250
          rev: example-tag
251
        sources_locked:
252
        - name: gitman_2
253
          link: ''
254
          repo: https://github.com/jacebrowning/gitman-demo
255
          rev: (old revision)
256
        """)
257
258
    def it_should_lock_all_dependencies_when_enabled(config):
259
        gitman.update(depth=1, lock=True)
260
261
        expect(config.__mapper__.text) == CONFIG + strip("""
262
        sources_locked:
263
        - name: gitman_1
264
          link: ''
265
          repo: https://github.com/jacebrowning/gitman-demo
266
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
267
        - name: gitman_2
268
          link: ''
269
          repo: https://github.com/jacebrowning/gitman-demo
270
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
271
        - name: gitman_3
272
          link: ''
273
          repo: https://github.com/jacebrowning/gitman-demo
274
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
275
        """)
276
277
278
def describe_list():
279
280
    @freeze_time("2012-01-14 12:00:01")
281
    def it_updates_the_log(config):
282
        gitman.install()
283
        gitman.list()
284
        with open(config.log_path) as stream:
285
            contents = stream.read().replace("/private", "")
286
287
        temp_path=os.path.normpath(tempfile.gettempdir())
288
        test_string = "2012-01-14 12:00:01\n" +
0 ignored issues
show
introduced by
invalid syntax
Loading history...
289
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_1") + ": https://github.com/jacebrowning/gitman-demo @ 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd\n" +
290
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_1/gitman_sources/gdm_3") + ": https://github.com/jacebrowning/gdm-demo @ 050290bca3f14e13fd616604202b579853e7bfb0\n" +
291
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_3") + ": https://github.com/jacebrowning/gdm-demo @ fb693447579235391a45ca170959b5583c5042d8\n" +
292
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_4") + ": https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5\n" +
293
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_1/gitman_sources/gdm_4") + ": https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5\n" +
294
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_2") + ": https://github.com/jacebrowning/gitman-demo @ 7bd138fe7359561a8c2ff9d195dff238794ccc04\n" +
295
        temp_path + os.path.normpath("/gitman-shared/deps/gitman_3") + ": https://github.com/jacebrowning/gitman-demo @ 9bf18e16b956041f0267c21baad555a23237b52e\n"
296
        expect(contents) == strip(test_string, end='\n\n')
297
298
299
def describe_lock():
300
301
    def it_records_all_versions_when_no_arguments(config):
302
        expect(gitman.update(depth=1, lock=False)) == True
303
        expect(gitman.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
        """) == config.__mapper__.text
320
321
    def it_records_specified_dependencies(config):
322
        expect(gitman.update(depth=1, lock=False)) == True
323
        expect(gitman.lock('gitman_1', 'gitman_3')) == True
324
325
        expect(config.__mapper__.text) == CONFIG + strip("""
326
        sources_locked:
327
        - name: gitman_1
328
          link: ''
329
          repo: https://github.com/jacebrowning/gitman-demo
330
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
331
        - name: gitman_3
332
          link: ''
333
          repo: https://github.com/jacebrowning/gitman-demo
334
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
335
        """) == config.__mapper__.text
336
337
    def it_should_fail_on_dirty_repositories(config):
338
        expect(gitman.update(depth=1, lock=False)) == True
339
        os.remove("deps/gitman_1/.project")
340
341
        with pytest.raises(UncommittedChanges):
342
            gitman.lock()
343
344
    def it_should_fail_on_invalid_repositories(config):
345
        os.system("mkdir deps && touch deps/gitman_1")
346
347
        with pytest.raises(InvalidRepository):
348
            gitman.lock()
349
350
        expect(config.__mapper__.text).does_not_contain("<unknown>")
351