forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxargs_test.py
77 lines (55 loc) · 1.84 KB
/
xargs_test.py
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
from __future__ import absolute_import
from __future__ import unicode_literals
import pytest
from pre_commit import xargs
def test_partition_trivial():
assert xargs.partition(('cmd',), ()) == (('cmd',),)
def test_partition_simple():
assert xargs.partition(('cmd',), ('foo',)) == (('cmd', 'foo'),)
def test_partition_limits():
ret = xargs.partition(
('ninechars',), (
# Just match the end (with spaces)
'.' * 5, '.' * 4,
# Just match the end (single arg)
'.' * 10,
# Goes over the end
'.' * 5,
'.' * 6,
),
_max_length=20,
)
assert ret == (
('ninechars', '.' * 5, '.' * 4),
('ninechars', '.' * 10),
('ninechars', '.' * 5),
('ninechars', '.' * 6),
)
def test_argument_too_long():
with pytest.raises(xargs.ArgumentTooLongError):
xargs.partition(('a' * 5,), ('a' * 5,), _max_length=10)
def test_xargs_smoke():
ret, out, err = xargs.xargs(('echo',), ('hello', 'world'))
assert ret == 0
assert out == b'hello world\n'
assert err == b''
exit_cmd = ('bash', '-c', 'exit $1', '--')
# Abuse max_length to control the exit code
max_length = len(' '.join(exit_cmd)) + 2
def test_xargs_negate():
ret, _, _ = xargs.xargs(
exit_cmd, ('1',), negate=True, _max_length=max_length,
)
assert ret == 0
ret, _, _ = xargs.xargs(
exit_cmd, ('1', '0'), negate=True, _max_length=max_length,
)
assert ret == 1
def test_xargs_negate_command_not_found():
ret, _, _ = xargs.xargs(('cmd-not-found',), ('1',), negate=True)
assert ret != 0
def test_xargs_retcode_normal():
ret, _, _ = xargs.xargs(exit_cmd, ('0',), _max_length=max_length)
assert ret == 0
ret, _, _ = xargs.xargs(exit_cmd, ('0', '1'), _max_length=max_length)
assert ret == 1