This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_files.py
executable file
·145 lines (119 loc) · 4.23 KB
/
test_files.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
#!/usr/bin/env python
"""
Unittests for included adept files
Dependencies:
- python-2.7
- python-unittest2
- PyYAML
- pylint
"""
import sys
import os
import os.path
from pdb import Pdb
import mmap
import re
from glob import glob
# ref: https://docs.python.org/dev/library/unittest.html
import unittest2 as unittest
import test_adept
# Prefer to keep number of extra files/modules low
# pylint: disable=C0302
class TestCaseBase(unittest.TestCase):
"""Base class for all other test classes to use"""
def setUp(self):
# Keep Debugger close at hand
self.pdb = Pdb()
def trace(self):
"""Enter the pdb debugger, 'n' will step back to self.trace() caller"""
return self.pdb.set_trace()
def subtests(self, items):
"""
Return each item's value inside an active context manager
:param tuple items: Tuple or list of items to create contexts for
:returns: Each item's value inside an active context manager
:rtype: type(item)
"""
for item in items:
ctxmgr = self.subTest(item=item)
with ctxmgr:
yield item
class TestPylint(test_adept.TestPylint):
"""Run pylint over this unittest"""
def setUp(self):
"""Override test_adept.TestCaseBase method"""
pass
def tearDown(self):
"""Override test_adept.TestCaseBase method"""
pass
def test_unittest_pylint(self):
"Run pylint on the unittest module itself"
self._pylintrun(os.path.basename(__file__))
@unittest.skip("Not relevant")
def test_uut_pylint(self):
pass
class TestContentRegexs(TestCaseBase):
"""
Iterate over files from a glob, matching forbidden regular expressions
"""
globs = ('*.sh', '*.xn')
regexes = (re.compile(r'/usr/bin/bash'),
re.compile(r'/usr/bin/sh'),
re.compile(r'/usr/bin/cp'),
re.compile(r'/usr/bin/mkdir'),
re.compile(r'/usr/bin/python'),
re.compile(r' /bin/test'),
re.compile(r' /bin/env'),
)
# If non-None, contain iterable of relative paths to files
check_files = None
def setUp(self):
super(TestContentRegexs, self).setUp()
here = os.path.dirname(sys.modules[__name__].__file__)
here = os.path.abspath(here)
for _glob in self.globs:
for dirpath, _, _ in os.walk(here):
if '.git' in dirpath:
continue
if self.check_files is None:
self.check_files = []
self.check_files += glob(os.path.join(dirpath, _glob))
if not self.check_files:
self.check_files = None
@staticmethod
def regexs_not_in(regexes, openfile):
"""
Return non-empty details-string if any regex found anywhere in openfile
"""
# No context manager for this :(
mmfile = None
found_one = ''
try:
mmfile = mmap.mmap(openfile.fileno(), 0,
mmap.MAP_PRIVATE, mmap.PROT_READ)
for regex in regexes:
# Only care about first one found
for found in regex.finditer(mmfile):
if bool(found):
found_one = ('Matched forbidden r"%s" in %s'
% (regex.pattern, openfile.name))
break
if found_one:
break
finally:
if mmfile:
mmfile.close()
return found_one
def test_regexes(self):
"""Verify no globbed file matches any regex"""
self.assertTrue(self.check_files, "glob did not match any files")
for filename in self.subtests(self.check_files):
# Can't mmap empty files
if os.stat(filename).st_size < 1: # ignore unexp. st_size too
continue
with open(filename, 'r+') as openfile:
# The value is it's own test-failure message
found_one = self.regexs_not_in(self.regexes, openfile)
self.assertEqual('', found_one, found_one)
if __name__ == '__main__':
unittest.main(failfast=True, catchbreak=True, verbosity=2)