forked from arp242/sanitize_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·60 lines (45 loc) · 1.69 KB
/
tests.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
#!/usr/bin/env python
# encoding: utf-8
import sys
import tempfile
from sanitize_files import run
had_errors = False
def contents_is(fp, expect, desc, opt={}):
global had_errors
fp.flush()
run([fp.name], **opt)
fp.seek(0)
content = fp.read()
try:
assert content == expect, "%s: %s" % (desc, content)
except AssertionError:
print('Error: %s' % desc)
print(' Expected: %s' % repr(expect))
print(' Actual: %s' % repr(content))
had_errors = True
else:
print('Okay: %s' % desc)
with tempfile.NamedTemporaryFile() as fp:
fp.write(b'Hello\nWorld')
contents_is(fp, b'Hello\nWorld\n', 'Adds a newline to the end of the file')
with tempfile.NamedTemporaryFile() as fp:
fp.write(b'Hello\r\nWorld\r\n')
contents_is(fp, b'Hello\nWorld\n', 'Converts DOS to UNIX line endings')
with tempfile.NamedTemporaryFile() as fp:
fp.write(b'\tHello\n\t\tW\to rld\n')
contents_is(fp, b' Hello\n W\to rld\n',
'Converts tabs to spaces',
{'indent_type': 'spaces'})
with tempfile.NamedTemporaryFile() as fp:
fp.write(b' Hello\n W o\trld\n')
contents_is(fp, b'\tHello\n\t\tW o\trld\n', 'Converts spaces to tabs',
{'indent_type': 'tabs'})
with tempfile.NamedTemporaryFile() as fp:
fp.write(b'Hello\n\n\n\nWorld\nHello\nWorld\n \n \n\t\nxxx\n')
contents_is(fp, b'Hello\n\n\nWorld\nHello\nWorld\n\n\nxxx\n',
'Removes consecutive newlines')
with tempfile.NamedTemporaryFile() as fp:
fp.write(b'Hello \nWorld\t\t\n')
contents_is(fp, b'Hello\nWorld\n', 'Removes trailing whitespace')
if had_errors:
sys.exit(1)