-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_utils.bzl
106 lines (80 loc) · 2.82 KB
/
test_utils.bzl
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
load("@rules_python//python:defs.bzl", "py_test")
def vendordep_check_test(vendor_file, allowable_warnings = 0, allowable_errors = 0, verbosity_level = "-v", cache_directory = None):
file_no_extension = vendor_file[:-5]
gen_name = file_no_extension + ".gen"
test_file_base = file_no_extension + "_test"
test_file_name = test_file_base + ".py"
cache_replacement = ""
if cache_directory:
cache_replacement = 'args.append("--cache_directory=' + cache_directory + '")'
verbosity_replacement = ""
if verbosity_level:
verbosity_replacement = 'args.append("' + verbosity_level + '")'
test_contents = """
import unittest
from check import check_file, parse_args, file_config
class VendordepCheck(unittest.TestCase):
def test_check(self):
vendor_file = "{vendor_file}"
warnings_allowed = {allowable_warnings}
errors_allowed = {allowable_errors}
args = [vendor_file]
{verbosity_replacement}
{cache_replacement}
parse_args(args)
file_config.load(vendor_file)
check_file(vendor_file)
from check import got_error, got_warn
print(f"Errors: {{got_error}}")
print(f"Warnings: {{got_warn}}")
if errors_allowed is not None:
self.assertLessEqual(got_error, errors_allowed)
if warnings_allowed is not None:
self.assertLessEqual(got_warn, warnings_allowed)
if __name__ == "__main__":
unittest.main() # run all tests
""".format(
vendor_file = vendor_file,
allowable_warnings = allowable_warnings,
allowable_errors = allowable_errors,
cache_replacement = cache_replacement,
verbosity_replacement = verbosity_replacement,
)
native.genrule(
name = gen_name,
outs = [test_file_name],
cmd = "echo '{}' >> $@".format(test_contents),
)
py_test(
name = test_file_base,
srcs = [test_file_name],
deps = ["//:check"],
data = [vendor_file],
)
def year_bundle_test(year, year_files):
gen_name = year + ".gen"
test_file_base = year + "_test"
test_file_name = test_file_base + ".py"
test_contents = """
import unittest
import pathlib
from check_year_bundle import check_year_bundle
class VendordepCheck(unittest.TestCase):
def test_check(self):
results = check_year_bundle(pathlib.Path("{year}"))
print(results)
self.assertTrue(results.is_valid())
if __name__ == "__main__":
unittest.main() # run all tests
""".format(year=year)
native.genrule(
name = gen_name,
outs = [test_file_name],
cmd = "echo '{}' >> $@".format(test_contents),
)
py_test(
name = test_file_base,
srcs = [test_file_name],
deps = ["//:check_year_bundle"],
data = [str(year) + ".json"] + year_files,
)