forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_util.py
140 lines (105 loc) · 3.91 KB
/
test_util.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
#!/usr/bin/env python
# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
# Runs the full test suite.
# Usage: ./tools/test.py out/Debug
import argparse
import contextlib
import os
import sys
import unittest
from util import (enable_ansi_colors, build_path, RESET, FG_RED, FG_GREEN,
executable_suffix, rmtree, tests_path)
class DenoTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
args = parse_test_args()
cls.build_dir = args.build_dir
cls.deno_exe = args.executable
# overload the test result class
class ColorTextTestResult(unittest.TextTestResult):
@contextlib.contextmanager
def color(self, code):
self.stream.write(code)
try:
yield
finally:
self.stream.write(RESET)
def getDescription(self, test):
name = str(test)
if name.startswith("test_"):
name = name[5:]
return name
def addSuccess(self, test):
with self.color(FG_GREEN):
super(ColorTextTestResult, self).addSuccess(test)
def addError(self, test, err):
with self.color(FG_RED):
super(ColorTextTestResult, self).addError(test, err)
def addFailure(self, test, err):
with self.color(FG_RED):
super(ColorTextTestResult, self).addFailure(test, err)
class ColorTextTestRunner(unittest.TextTestRunner):
resultclass = ColorTextTestResult
def create_test_arg_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'--failfast', '-f', action='store_true', help='Stop on first failure')
parser.add_argument(
'--verbose', '-v', action='store_true', help='Verbose output')
parser.add_argument("--executable", help="Use external executable of Deno")
parser.add_argument(
'--release',
action='store_true',
help='Test against release executable')
parser.add_argument(
'--pattern', '-p', help='Run tests that match provided pattern')
parser.add_argument(
'--build-dir', dest="build_dir", help='Deno build directory')
return parser
TestArgParser = create_test_arg_parser()
def parse_test_args(argv=None):
if argv is None:
argv = sys.argv[1:]
args = TestArgParser.parse_args(argv)
if args.executable and args.release:
raise argparse.ArgumentError(
None, "Path to executable is inferred from "
"--release, cannot provide both.")
if not args.build_dir:
args.build_dir = build_path()
if not args.executable:
args.executable = os.path.join(args.build_dir,
"deno" + executable_suffix)
if not os.path.isfile(args.executable):
raise argparse.ArgumentError(
None, "deno executable not found at {}".format(args.executable))
return args
def filter_test_suite(suite, pattern):
filtered_tests = []
for test_case in suite:
if isinstance(test_case, unittest.TestSuite):
filtered_tests += filter_test_suite(test_case, pattern)
else:
if pattern in str(test_case):
filtered_tests.append(test_case)
return filtered_tests
def run_tests(test_cases=None):
args = parse_test_args()
loader = unittest.TestLoader()
# if suite was not explicitly passed load test
# cases from calling module
if test_cases is None:
import __main__
suite = loader.loadTestsFromModule(__main__)
else:
suite = unittest.TestSuite()
for test_case in test_cases:
suite.addTests(loader.loadTestsFromTestCase(test_case))
if args.pattern:
filtered_tests = filter_test_suite(suite, args.pattern)
suite = unittest.TestSuite(filtered_tests)
runner = ColorTextTestRunner(
verbosity=args.verbose + 2, failfast=args.failfast)
result = runner.run(suite)
if not result.wasSuccessful():
sys.exit(1)