Completed
Pull Request — develop (#117)
by Jace
10:05
created

filenames_in()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# pylint: disable=redefined-outer-name,unused-argument,unused-variable,singleton-comparison,expression-not-assigned
0 ignored issues
show
introduced by
Bad option value 'singleton-comparison'
Loading history...
2
3
import os
4
import shutil
5
from contextlib import suppress
6
from pathlib import Path
7
import logging
8
9
import pytest
10
from expecter import expect
0 ignored issues
show
Configuration introduced by
The import expecter could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
11
from freezegun import freeze_time
0 ignored issues
show
Configuration introduced by
The import freezegun could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
12
13
import gitman
14
from gitman.models import Config
15
from gitman.exceptions import 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="/tmp/gitman-shared"):
42
    with suppress(FileNotFoundError):
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(config.location_path.is_dir()) == False
62
63
        expect(gitman.install('gitman_1', depth=1)) == True
64
65
        expect(filenames_in(config.location_path)) == ['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(filenames_in(config.location_path))) == 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(filenames_in(config.location_path)) == ['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
        def it_should_create_links(config_with_link):
130
            expect(gitman.install(depth=1)) == True
131
132
            expect(filenames_in(Path.cwd())).contains('my_link')
133
134
        def it_should_not_overwrite_files(config_with_link):
135
            os.system("touch my_link")
136
137
            with pytest.raises(RuntimeError):
138
                gitman.install(depth=1)
139
140
        def it_overwrites_files_with_force(config_with_link):
141
            os.system("touch my_link")
142
143
            expect(gitman.install(depth=1, force=True)) == True
144
145
146
def describe_uninstall():
147
148
    def it_deletes_dependencies_when_they_exist(config):
149
        gitman.install('gitman_1', depth=1)
150
        expect(config.location_path.is_dir()) == True
151
152
        expect(gitman.uninstall()) == True
153
154
        expect(config.location_path.exists()) == False
155
156
    def it_should_not_fail_when_no_dependnecies_exist(config):
157
        expect(config.location_path.is_dir()) == False
158
159
        expect(gitman.uninstall()) == True
160
161
    def it_deletes_the_log_file(config):
162
        gitman.install('gitman_1', depth=1)
163
        gitman.list()
164
        expect(config.log_path.exists()) == True
165
166
        gitman.uninstall()
167
        expect(config.log_path.exists()) == False
168
169
170
def describe_update():
171
172
    def it_should_not_modify_config(config):
173
        gitman.update('gitman_1', depth=1)
174
175
        expect(config.__mapper__.text) == CONFIG
176
177
    def it_locks_previously_locked_dependnecies(config):
178
        config.__mapper__.text = strip("""
179
        location: deps
180
        sources:
181
        - name: gitman_1
182
          link: ''
183
          repo: https://github.com/jacebrowning/gitman-demo
184
          rev: example-branch
185
        - name: gitman_2
186
          link: ''
187
          repo: https://github.com/jacebrowning/gitman-demo
188
          rev: example-tag
189
        sources_locked:
190
        - name: gitman_2
191
          link: ''
192
          repo: https://github.com/jacebrowning/gitman-demo
193
          rev: (old revision)
194
        """)
195
196
        gitman.update(depth=1)
197
198
        expect(config.__mapper__.text) == strip("""
199
        location: deps
200
        sources:
201
        - name: gitman_1
202
          link: ''
203
          repo: https://github.com/jacebrowning/gitman-demo
204
          rev: example-branch
205
        - name: gitman_2
206
          link: ''
207
          repo: https://github.com/jacebrowning/gitman-demo
208
          rev: example-tag
209
        sources_locked:
210
        - name: gitman_2
211
          link: ''
212
          repo: https://github.com/jacebrowning/gitman-demo
213
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
214
        """)
215
216
    def it_should_not_lock_dependnecies_when_disabled(config):
217
        config.__mapper__.text = strip("""
218
        location: deps
219
        sources:
220
        - name: gitman_1
221
          link: ''
222
          repo: https://github.com/jacebrowning/gitman-demo
223
          rev: example-branch
224
        - name: gitman_2
225
          link: ''
226
          repo: https://github.com/jacebrowning/gitman-demo
227
          rev: example-tag
228
        sources_locked:
229
        - name: gitman_2
230
          link: ''
231
          repo: https://github.com/jacebrowning/gitman-demo
232
          rev: (old revision)
233
        """)
234
235
        gitman.update(depth=1, lock=False)
236
237
        expect(config.__mapper__.text) == strip("""
238
        location: deps
239
        sources:
240
        - name: gitman_1
241
          link: ''
242
          repo: https://github.com/jacebrowning/gitman-demo
243
          rev: example-branch
244
        - name: gitman_2
245
          link: ''
246
          repo: https://github.com/jacebrowning/gitman-demo
247
          rev: example-tag
248
        sources_locked:
249
        - name: gitman_2
250
          link: ''
251
          repo: https://github.com/jacebrowning/gitman-demo
252
          rev: (old revision)
253
        """)
254
255
    def it_should_lock_all_dependencies_when_enabled(config):
256
        gitman.update(depth=1, lock=True)
257
258
        expect(config.__mapper__.text) == CONFIG + strip("""
259
        sources_locked:
260
        - name: gitman_1
261
          link: ''
262
          repo: https://github.com/jacebrowning/gitman-demo
263
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
264
        - name: gitman_2
265
          link: ''
266
          repo: https://github.com/jacebrowning/gitman-demo
267
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
268
        - name: gitman_3
269
          link: ''
270
          repo: https://github.com/jacebrowning/gitman-demo
271
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
272
        """)
273
274
275
def describe_list():
276
277
    @freeze_time("2012-01-14 12:00:01")
278
    def it_updates_the_log(config):
279
        gitman.install()
280
        gitman.list()
281
        with open(str(config.log_path)) as stream:
282
            contents = stream.read().replace("/private", "")
283
        expect(contents) == strip("""
284
        2012-01-14 12:00:01
285
        /tmp/gitman-shared/deps/gitman_1: https://github.com/jacebrowning/gitman-demo @ 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
286
        /tmp/gitman-shared/deps/gitman_1/gitman_sources/gdm_3: https://github.com/jacebrowning/gdm-demo @ 050290bca3f14e13fd616604202b579853e7bfb0
287
        /tmp/gitman-shared/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_3: https://github.com/jacebrowning/gdm-demo @ fb693447579235391a45ca170959b5583c5042d8
288
        /tmp/gitman-shared/deps/gitman_1/gitman_sources/gdm_3/gitman_sources/gdm_4: https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5
289
        /tmp/gitman-shared/deps/gitman_1/gitman_sources/gdm_4: https://github.com/jacebrowning/gdm-demo @ 63ddfd82d308ddae72d31b61cb8942c898fa05b5
290
        /tmp/gitman-shared/deps/gitman_2: https://github.com/jacebrowning/gitman-demo @ 7bd138fe7359561a8c2ff9d195dff238794ccc04
291
        /tmp/gitman-shared/deps/gitman_3: https://github.com/jacebrowning/gitman-demo @ 9bf18e16b956041f0267c21baad555a23237b52e
292
        """, end='\n\n')
293
294
295
def describe_lock():
296
297
    def it_records_all_versions_when_no_arguments(config):
298
        expect(gitman.update(depth=1, lock=False)) == True
299
        expect(gitman.lock()) == True
300
301
        expect(config.__mapper__.text) == CONFIG + strip("""
302
        sources_locked:
303
        - name: gitman_1
304
          link: ''
305
          repo: https://github.com/jacebrowning/gitman-demo
306
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
307
        - name: gitman_2
308
          link: ''
309
          repo: https://github.com/jacebrowning/gitman-demo
310
          rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
311
        - name: gitman_3
312
          link: ''
313
          repo: https://github.com/jacebrowning/gitman-demo
314
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
315
        """) == config.__mapper__.text
316
317
    def it_records_specified_dependencies(config):
318
        expect(gitman.update(depth=1, lock=False)) == True
319
        expect(gitman.lock('gitman_1', 'gitman_3')) == True
320
321
        expect(config.__mapper__.text) == CONFIG + strip("""
322
        sources_locked:
323
        - name: gitman_1
324
          link: ''
325
          repo: https://github.com/jacebrowning/gitman-demo
326
          rev: 1de84ca1d315f81b035cd7b0ecf87ca2025cdacd
327
        - name: gitman_3
328
          link: ''
329
          repo: https://github.com/jacebrowning/gitman-demo
330
          rev: 9bf18e16b956041f0267c21baad555a23237b52e
331
        """) == config.__mapper__.text
332
333
    def it_should_fail_on_invalid_repositories(config):
334
        os.system("mkdir deps && touch deps/gitman_1")
335
336
        with pytest.raises(InvalidRepository):
337
            gitman.lock()
338
339
        expect(config.__mapper__.text).does_not_contain("<unknown>")
340
341
342
def filenames_in(path):
343
    """Return a list of all filenames in a given directory `Path`."""
344
    return [p.name for p in path.iterdir()]
345