-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_csv_test.py
179 lines (160 loc) · 4.28 KB
/
_csv_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
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
import os
import random
import string
import shell
from hypothesis import given, settings
from hypothesis.strategies import text, lists, composite, integers, sampled_from
from test_util import compile_buffer_sizes, run, rm_whitespace, clone_source
if os.environ.get('TEST_FACTOR'):
buffers = list(sorted(set([5, 8, 11, 17, 64, 128, 256, 1024, 1024 * 1024 * 5] + [random.randint(8, 1024) for _ in range(10)])))
else:
buffers = [8, 128]
def setup_module(m):
m.tempdir = clone_source()
m.orig = os.getcwd()
m.path = os.environ['PATH']
os.chdir(m.tempdir)
os.environ['PATH'] = f'{os.getcwd()}/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/bin'
shell.run('make clean', stream=True)
compile_buffer_sizes('_csv', buffers)
shell.run('make _csv')
def teardown_module():
with shell.climb_git_root():
shell.run('make clean', stream=True)
@composite
def inputs(draw):
buffer = draw(sampled_from(buffers))
num_columns = draw(integers(min_value=1, max_value=64))
column = text(string.ascii_lowercase + ' ', min_size=1, max_size=64)
line = lists(column, min_size=1, max_size=num_columns)
line = line.filter(lambda x: len(' '.join(x)) + len(x) * 2 + 1 < buffer)
lines = lists(line)
csv = '\n'.join([','.join(x) for x in draw(lines)]) + '\n'
cmd = '_csv.%s' % buffer
return cmd, csv
def expected(csv):
res = []
for line in csv.splitlines():
for col in line.split(','):
res.append(col)
return '\n'.join(res) + '\n'
@given(inputs())
@settings(max_examples=100 * int(os.environ.get('TEST_FACTOR', 1)), deadline=os.environ.get("TEST_DEADLINE", 1000 * 60))
def test_props(arg):
cmd, csv = arg
result = expected(csv)
assert result == run(csv, cmd)
def test_cycling1():
stdin = """
a
b
c
d
e
f
"""
assert rm_whitespace(stdin) == run(rm_whitespace(stdin), '_csv.8').strip()
def test_cycling2():
stdin = """
a
b
c
d
eee
"""
assert rm_whitespace(stdin) == run(rm_whitespace(stdin), '_csv.8').strip()
def test_cycling3():
stdin = """
a
b
c
d
e
fff
"""
assert rm_whitespace(stdin) == run(rm_whitespace(stdin), '_csv.8').strip()
def test_cycling4():
stdin = """
a
b
c
de
"""
assert rm_whitespace(stdin) == run(rm_whitespace(stdin), '_csv.8').strip()
def test_holes():
stdin = 'a,,c\n'
stdout = 'a\n\nc\n'
assert stdout == run(stdin, '_csv.8')
def test_basic():
stdin = """
a,b
cd,e
"""
stdout = """
a
b
cd
e
"""
assert rm_whitespace(stdout) + '\n' == run(rm_whitespace(stdin), '_csv.8')
def test_empties():
stdin = 'a,,,b,c'
stdout = 'a\n\n\nb\nc\n'
assert stdout == run(stdin, '_csv.8')
def test_whitespace1():
stdin = ('a\n'
'\n'
'b\n'
'\n'
'c\n')
stdout = ('a\n'
'\n'
'b\n'
'\n'
'c\n')
assert stdout == run(stdin, '_csv.8')
def test_whitespace2():
stdin = ('\n'
'a\n'
'\n'
'b\n'
'\n'
'c\n'
'\n')
stdout = ('\n'
'a\n'
'\n'
'b\n'
'\n'
'c\n'
'\n')
assert stdout == run(stdin, '_csv.8')
def test_whitespace3():
stdin = ('\n'
'\n'
'\n'
'\n')
stdout = ('\n'
'\n'
'\n'
'\n')
assert stdout == run(stdin, '_csv.8')
def test_fails_when_too_many_columns():
with shell.climb_git_root():
stdin = 'a,' * (2**16 - 1)
with shell.tempdir(cleanup=False):
with open('input', 'w') as f:
f.write(stdin)
path = os.path.abspath('input')
try:
res = shell.run('cat', path, '| _csv >/dev/null', warn=True)
finally:
shell.run('rm', path)
assert res['exitcode'] == 1
assert 'fatal: line with more than 65535 columns' == res['stderr']
def test_oversized_line():
with shell.climb_git_root():
stdin = 'a' * 8
res = shell.run('_csv.8', stdin=stdin, warn=True)
assert res['exitcode'] == 1
assert 'fatal: line longer than BUFFER_SIZE' == res['stderr']