1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
from __future__ import annotations
import builtins
import errno
import io
import os
import socket
import stat
from tempfile import TemporaryDirectory
from unittest import mock
import pytest
from identify import identify
def test_all_tags_includes_basic_ones():
assert 'file' in identify.ALL_TAGS
assert 'directory' in identify.ALL_TAGS
assert 'executable' in identify.ALL_TAGS
assert 'text' in identify.ALL_TAGS
assert 'socket' in identify.ALL_TAGS
@pytest.mark.parametrize(
'tag_group',
(
identify.TYPE_TAGS,
identify.MODE_TAGS,
identify.ENCODING_TAGS,
),
)
def test_all_tags_contains_all_groups(tag_group):
assert tag_group < identify.ALL_TAGS
def test_all_tags_contains_each_type():
assert 'xml' in identify.ALL_TAGS # extension
assert 'plist' in identify.ALL_TAGS # extension, needs binary check
assert 'dockerfile' in identify.ALL_TAGS # by file convention
assert 'python3' in identify.ALL_TAGS # by shebang
assert 'php8' in identify.ALL_TAGS # by shebang
def test_tags_from_path_does_not_exist(tmpdir):
x = tmpdir.join('foo')
with pytest.raises(ValueError):
identify.tags_from_path(x.strpath)
def test_tags_from_path_directory(tmpdir):
x = tmpdir.join('foo')
x.mkdir()
assert identify.tags_from_path(x.strpath) == {'directory'}
def test_tags_from_path_symlink(tmpdir):
x = tmpdir.join('foo')
x.mksymlinkto(tmpdir.join('lol').ensure())
assert identify.tags_from_path(x.strpath) == {'symlink'}
def test_tags_from_path_socket():
tmproot = '/tmp' # short path avoids `OSError: AF_UNIX path too long`
with TemporaryDirectory(dir=tmproot) as tmpdir:
socket_path = os.path.join(tmpdir, 'socket')
with socket.socket(socket.AF_UNIX) as sock:
sock.bind(socket_path)
tags = identify.tags_from_path(socket_path)
assert tags == {'socket'}
def test_tags_from_path_broken_symlink(tmpdir):
x = tmpdir.join('foo')
x.mksymlinkto(tmpdir.join('lol'))
assert identify.tags_from_path(x.strpath) == {'symlink'}
def test_tags_from_path_simple_file(tmpdir):
x = tmpdir.join('test.py').ensure()
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable', 'python',
}
def test_tags_from_path_file_with_incomplete_shebang(tmpdir):
x = tmpdir.join('test')
x.write_text('#! \n', encoding='UTF-8')
make_executable(x.strpath)
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'executable',
}
def test_tags_from_path_file_with_shebang_non_executable(tmpdir):
x = tmpdir.join('test')
x.write_text('#!/usr/bin/env python\nimport sys\n', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable',
}
def test_tags_from_path_file_with_shebang_executable(tmpdir):
x = tmpdir.join('test')
x.write_text('#!/usr/bin/env python\nimport sys\n', encoding='UTF-8')
make_executable(x.strpath)
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'executable', 'python',
}
def test_tags_from_path_binary(tmpdir):
x = tmpdir.join('test')
x.write(b'\x7f\x45\x4c\x46\x02\x01\x01')
make_executable(x.strpath)
assert identify.tags_from_path(x.strpath) == {
'file', 'binary', 'executable',
}
def test_tags_from_path_plist_binary(tmpdir):
x = tmpdir.join('t.plist')
x.write_binary(
b'bplist00\xd1\x01\x02_\x10\x0fLast Login NameWDefault\x08\x0b\x1d\x00'
b'\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00%',
)
assert identify.tags_from_path(x.strpath) == {
'file', 'plist', 'binary', 'non-executable',
}
def test_tags_from_path_plist_text(tmpdir):
x = tmpdir.join('t.plist')
x.write(
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n' # noqa: E501
'<plist version="1.0">\n'
'<dict>\n'
'\t<key>Last Login Name</key>\n'
'\t<string>Default</string>\n'
'</dict>\n'
'</plist>\n',
)
assert identify.tags_from_path(x.strpath) == {
'file', 'plist', 'text', 'non-executable',
}
@pytest.mark.parametrize(
('filename', 'expected'),
(
('.salt-lint', {'text', 'salt-lint', 'yaml'}),
('test.py', {'text', 'python'}),
('test.mk', {'text', 'makefile'}),
('Makefile', {'text', 'makefile'}),
('Containerfile', {'text', 'dockerfile'}),
('Dockerfile', {'text', 'dockerfile'}),
('Dockerfile.xenial', {'text', 'dockerfile'}),
('xenial.Dockerfile', {'text', 'dockerfile'}),
('Pipfile', {'text', 'toml'}),
('Pipfile.lock', {'text', 'json'}),
('mod/test.py', {'text', 'python'}),
('mod/Dockerfile', {'text', 'dockerfile'}),
('config.ru', {'text', 'ruby'}),
('Gemfile', {'text', 'ruby'}),
('Gemfile.lock', {'text'}),
('Jenkinsfile', {'text', 'groovy', 'jenkins'}),
('build.jenkins', {'text', 'groovy', 'jenkins'}),
('build.jenkinsfile', {'text', 'groovy', 'jenkins'}),
('meson.build', {'text', 'meson'}),
('meson_options.txt', {'text', 'plain-text', 'meson'}),
('Vagrantfile', {'text', 'ruby'}),
# does not set binary / text
('f.plist', {'plist'}),
# case of extension should be ignored
('f.JPG', {'binary', 'image', 'jpeg'}),
# but case of name checks should still be honored
('dockerfile.py', {'text', 'python'}),
# full filename tests should take precedence over extension tests
('test.cfg', {'text'}),
('setup.cfg', {'text', 'ini'}),
# Filename matches should still include extensions if applicable
('README.md', {'text', 'markdown', 'plain-text'}),
('test.weird-unrecognized-extension', set()),
('test', set()),
('', set()),
),
)
def test_tags_from_filename(filename, expected):
assert identify.tags_from_filename(filename) == expected
@pytest.mark.parametrize(
('interpreter', 'expected'),
(
('python', {'python'}),
('python3', {'python3', 'python'}),
('python3.5.2', {'python3', 'python'}),
('/usr/bin/python3.5.2', {'python3', 'python'}),
('/usr/bin/herpderpderpderpderp', set()),
('something-random', set()),
('', set()),
),
)
def test_tags_from_interpreter(interpreter, expected):
assert identify.tags_from_interpreter(interpreter) == expected
@pytest.mark.parametrize(
('data', 'expected'),
(
(b'hello world', True),
(b'', True),
('éóñəå ⊂(◉‿◉)つ(ノ≥∇≤)ノ'.encode(), True),
(r'¯\_(ツ)_/¯'.encode(), True),
('♪┏(・o・)┛♪┗ ( ・o・) ┓♪┏ ( ) ┛♪┗ (・o・ ) ┓♪'.encode(), True),
('éóñå'.encode('latin1'), True),
(b'hello world\x00', False),
# first few bytes of /bin/bash
(b'\x7f\x45\x4c\x46\x02\x01\x01', False),
# some /dev/urandom output
(b'\x43\x92\xd9\x0f\xaf\x32\x2c', False),
),
)
def test_is_text(data, expected):
assert identify.is_text(io.BytesIO(data)) is expected
def test_file_is_text_simple(tmpdir):
x = tmpdir.join('f')
x.write_text('hello there\n', encoding='UTF-8')
assert identify.file_is_text(x.strpath) is True
def test_file_is_text_does_not_exist(tmpdir):
x = tmpdir.join('f')
with pytest.raises(ValueError):
identify.file_is_text(x.strpath)
@pytest.mark.parametrize(
('s', 'expected'),
(
(b'', ()),
(b'#!/usr/bin/python', ('/usr/bin/python',)),
(b'#!/usr/bin/env python', ('python',)),
(b'#! /usr/bin/python', ('/usr/bin/python',)),
(b'#!/usr/bin/foo python', ('/usr/bin/foo', 'python')),
# despite this being invalid, setuptools will write shebangs like this
(b'#!"/path/with spaces/x" y', ('/path/with spaces/x', 'y')),
# this is apparently completely ok to embed quotes
(b"#!/path'with/quotes y", ("/path'with/quotes", 'y')),
# Don't regress on leading/trailing ws
(b"#! /path'with/quotes y ", ("/path'with/quotes", 'y')),
# Test nix-shell specialites with shebang on second line
(
b'#! /usr/bin/env nix-shell\n'
b'#! nix-shell -i bash -p python',
('bash',),
),
(
b'#! /usr/bin/env nix-shell\n'
b'#! nix-shell -i python -p coreutils',
('python',),
),
(
b'#! /usr/bin/env nix-shell\n'
b'#! nix-shell -p coreutils -i python',
('python',),
),
# multi-line and no whitespace variation
(
b'#! /usr/bin/env nix-shell\n'
b'#! nix-shell -p coreutils\n'
b'#! nix-shell -i python',
('python',),
),
(
b'#! /usr/bin/env nix-shell\n'
b'#!nix-shell -p coreutils\n'
b'#!nix-shell -i python',
('python',),
),
(
b'#! /usr/bin/env nix-shell\n'
b'#!\xf9\x93\x01\x42\xcd',
('nix-shell',),
),
(
b'#! /usr/bin/env nix-shell\n'
b'#!\x00\x00\x00\x00',
('nix-shell',),
),
# non-proper nix-shell
(b'#! /usr/bin/nix-shell', ('/usr/bin/nix-shell',)),
(b'#! /usr/bin/env nix-shell', ('nix-shell',)),
(
b'#! /usr/bin/env nix-shell non-portable-argument',
('nix-shell', 'non-portable-argument'),
),
(
b'#! /usr/bin/env nix-shell\n'
b'#! nix-shell -i',
('nix-shell',), # guard against index error
),
# interpret quotes correctly
(
b'#!/usr/bin/env nix-shell\n'
b'#!nix-shell --argstr x "a -i python3 p"\n'
b'#!nix-shell -p hello\n'
b'#!nix-shell -i bash\n'
b'#!nix-shell --argstr y "b -i runhaskell q"',
('bash',),
),
(b'\xf9\x93\x01\x42\xcd', ()),
(b'#!\xf9\x93\x01\x42\xcd', ()),
(b'#!\x00\x00\x00\x00', ()),
# shebang lines with multiple arguments
(b'#!/usr/bin/env -S python -u', ('python', '-u')),
),
)
def test_parse_shebang(s, expected):
assert identify.parse_shebang(io.BytesIO(s)) == expected
def test_parse_shebang_from_file_does_not_exist():
with pytest.raises(ValueError):
identify.parse_shebang_from_file('herp derp derp')
def test_parse_shebang_from_file_nonexecutable(tmpdir):
x = tmpdir.join('f')
x.write_text('#!/usr/bin/env python', encoding='UTF-8')
assert identify.parse_shebang_from_file(x.strpath) == ()
def test_parse_shebang_from_file_simple(tmpdir):
x = tmpdir.join('f')
x.write_text('#!/usr/bin/env python', encoding='UTF-8')
make_executable(x.strpath)
assert identify.parse_shebang_from_file(x.strpath) == ('python',)
def test_parse_shebang_open_raises_einval(tmpdir):
x = tmpdir.join('f')
x.write('#!/usr/bin/env not-expected\n')
make_executable(x)
error = OSError(errno.EINVAL, f'Invalid argument {x}')
with mock.patch.object(builtins, 'open', side_effect=error):
assert identify.parse_shebang_from_file(x.strpath) == ()
def make_executable(filename):
original_mode = os.stat(filename).st_mode
os.chmod(
filename,
original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
)
def test_license_identification():
assert identify.license_id('LICENSE') == 'MIT'
def test_license_exact_identification(tmpdir):
wtfpl = '''\
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
'''
f = tmpdir.join('LICENSE')
f.write(wtfpl)
assert identify.license_id(f.strpath) == 'WTFPL'
def test_license_not_identified():
assert identify.license_id(os.devnull) is None
|