| 1 | #!/usr/bin/env python3
|
|---|
| 2 | # Copyright (c) 2012 Google Inc. All rights reserved.
|
|---|
| 3 | # Use of this source code is governed by a BSD-style license that can be
|
|---|
| 4 | # found in the LICENSE file.
|
|---|
| 5 |
|
|---|
| 6 | """gyptest.py -- test runner for GYP tests."""
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | import argparse
|
|---|
| 10 | import os
|
|---|
| 11 | import platform
|
|---|
| 12 | import subprocess
|
|---|
| 13 | import sys
|
|---|
| 14 | import time
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | def is_test_name(f):
|
|---|
| 18 | return f.startswith("gyptest") and f.endswith(".py")
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | def find_all_gyptest_files(directory):
|
|---|
| 22 | result = []
|
|---|
| 23 | for root, dirs, files in os.walk(directory):
|
|---|
| 24 | result.extend([os.path.join(root, f) for f in files if is_test_name(f)])
|
|---|
| 25 | result.sort()
|
|---|
| 26 | return result
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | def main(argv=None):
|
|---|
| 30 | if argv is None:
|
|---|
| 31 | argv = sys.argv
|
|---|
| 32 |
|
|---|
| 33 | parser = argparse.ArgumentParser()
|
|---|
| 34 | parser.add_argument("-a", "--all", action="store_true", help="run all tests")
|
|---|
| 35 | parser.add_argument("-C", "--chdir", action="store", help="change to directory")
|
|---|
| 36 | parser.add_argument(
|
|---|
| 37 | "-f",
|
|---|
| 38 | "--format",
|
|---|
| 39 | action="store",
|
|---|
| 40 | default="",
|
|---|
| 41 | help="run tests with the specified formats",
|
|---|
| 42 | )
|
|---|
| 43 | parser.add_argument(
|
|---|
| 44 | "-G",
|
|---|
| 45 | "--gyp_option",
|
|---|
| 46 | action="append",
|
|---|
| 47 | default=[],
|
|---|
| 48 | help="Add -G options to the gyp command line",
|
|---|
| 49 | )
|
|---|
| 50 | parser.add_argument(
|
|---|
| 51 | "-l", "--list", action="store_true", help="list available tests and exit"
|
|---|
| 52 | )
|
|---|
| 53 | parser.add_argument(
|
|---|
| 54 | "-n",
|
|---|
| 55 | "--no-exec",
|
|---|
| 56 | action="store_true",
|
|---|
| 57 | help="no execute, just print the command line",
|
|---|
| 58 | )
|
|---|
| 59 | parser.add_argument(
|
|---|
| 60 | "--path", action="append", default=[], help="additional $PATH directory"
|
|---|
| 61 | )
|
|---|
| 62 | parser.add_argument(
|
|---|
| 63 | "-q",
|
|---|
| 64 | "--quiet",
|
|---|
| 65 | action="store_true",
|
|---|
| 66 | help="quiet, don't print anything unless there are failures",
|
|---|
| 67 | )
|
|---|
| 68 | parser.add_argument(
|
|---|
| 69 | "-v",
|
|---|
| 70 | "--verbose",
|
|---|
| 71 | action="store_true",
|
|---|
| 72 | help="print configuration info and test results.",
|
|---|
| 73 | )
|
|---|
| 74 | parser.add_argument("tests", nargs="*")
|
|---|
| 75 | args = parser.parse_args(argv[1:])
|
|---|
| 76 |
|
|---|
| 77 | if args.chdir:
|
|---|
| 78 | os.chdir(args.chdir)
|
|---|
| 79 |
|
|---|
| 80 | if args.path:
|
|---|
| 81 | extra_path = [os.path.abspath(p) for p in args.path]
|
|---|
| 82 | extra_path = os.pathsep.join(extra_path)
|
|---|
| 83 | os.environ["PATH"] = extra_path + os.pathsep + os.environ["PATH"]
|
|---|
| 84 |
|
|---|
| 85 | if not args.tests:
|
|---|
| 86 | if not args.all:
|
|---|
| 87 | sys.stderr.write("Specify -a to get all tests.\n")
|
|---|
| 88 | return 1
|
|---|
| 89 | args.tests = ["test"]
|
|---|
| 90 |
|
|---|
| 91 | tests = []
|
|---|
| 92 | for arg in args.tests:
|
|---|
| 93 | if os.path.isdir(arg):
|
|---|
| 94 | tests.extend(find_all_gyptest_files(os.path.normpath(arg)))
|
|---|
| 95 | else:
|
|---|
| 96 | if not is_test_name(os.path.basename(arg)):
|
|---|
| 97 | print(arg, "is not a valid gyp test name.", file=sys.stderr)
|
|---|
| 98 | sys.exit(1)
|
|---|
| 99 | tests.append(arg)
|
|---|
| 100 |
|
|---|
| 101 | if args.list:
|
|---|
| 102 | for test in tests:
|
|---|
| 103 | print(test)
|
|---|
| 104 | sys.exit(0)
|
|---|
| 105 |
|
|---|
| 106 | os.environ["PYTHONPATH"] = os.path.abspath("test/lib")
|
|---|
| 107 |
|
|---|
| 108 | if args.verbose:
|
|---|
| 109 | print_configuration_info()
|
|---|
| 110 |
|
|---|
| 111 | if args.gyp_option and not args.quiet:
|
|---|
| 112 | print("Extra Gyp options: %s\n" % args.gyp_option)
|
|---|
| 113 |
|
|---|
| 114 | if args.format:
|
|---|
| 115 | format_list = args.format.split(",")
|
|---|
| 116 | else:
|
|---|
| 117 | format_list = {
|
|---|
| 118 | "aix5": ["make"],
|
|---|
| 119 | "freebsd7": ["make"],
|
|---|
| 120 | "freebsd8": ["make"],
|
|---|
| 121 | "openbsd5": ["make"],
|
|---|
| 122 | "cygwin": ["msvs"],
|
|---|
| 123 | "win32": ["msvs", "ninja"],
|
|---|
| 124 | "linux": ["make", "ninja"],
|
|---|
| 125 | "linux2": ["make", "ninja"],
|
|---|
| 126 | "linux3": ["make", "ninja"],
|
|---|
| 127 | # TODO: Re-enable xcode-ninja.
|
|---|
| 128 | # https://bugs.chromium.org/p/gyp/issues/detail?id=530
|
|---|
| 129 | # 'darwin': ['make', 'ninja', 'xcode', 'xcode-ninja'],
|
|---|
| 130 | "darwin": ["make", "ninja", "xcode"],
|
|---|
| 131 | }[sys.platform]
|
|---|
| 132 |
|
|---|
| 133 | gyp_options = []
|
|---|
| 134 | for option in args.gyp_option:
|
|---|
| 135 | gyp_options += ["-G", option]
|
|---|
| 136 |
|
|---|
| 137 | runner = Runner(format_list, tests, gyp_options, args.verbose)
|
|---|
| 138 | runner.run()
|
|---|
| 139 |
|
|---|
| 140 | if not args.quiet:
|
|---|
| 141 | runner.print_results()
|
|---|
| 142 |
|
|---|
| 143 | return 1 if runner.failures else 0
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | def print_configuration_info():
|
|---|
| 147 | print("Test configuration:")
|
|---|
| 148 | if sys.platform == "darwin":
|
|---|
| 149 | sys.path.append(os.path.abspath("test/lib"))
|
|---|
| 150 | import TestMac
|
|---|
| 151 |
|
|---|
| 152 | print(f" Mac {platform.mac_ver()[0]} {platform.mac_ver()[2]}")
|
|---|
| 153 | print(f" Xcode {TestMac.Xcode.Version()}")
|
|---|
| 154 | elif sys.platform == "win32":
|
|---|
| 155 | sys.path.append(os.path.abspath("pylib"))
|
|---|
| 156 | import gyp.MSVSVersion
|
|---|
| 157 |
|
|---|
| 158 | print(" Win %s %s\n" % platform.win32_ver()[0:2])
|
|---|
| 159 | print(" MSVS %s" % gyp.MSVSVersion.SelectVisualStudioVersion().Description())
|
|---|
| 160 | elif sys.platform in ("linux", "linux2"):
|
|---|
| 161 | print(" Linux %s" % " ".join(platform.linux_distribution()))
|
|---|
| 162 | print(f" Python {platform.python_version()}")
|
|---|
| 163 | print(f" PYTHONPATH={os.environ['PYTHONPATH']}")
|
|---|
| 164 | print()
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | class Runner:
|
|---|
| 168 | def __init__(self, formats, tests, gyp_options, verbose):
|
|---|
| 169 | self.formats = formats
|
|---|
| 170 | self.tests = tests
|
|---|
| 171 | self.verbose = verbose
|
|---|
| 172 | self.gyp_options = gyp_options
|
|---|
| 173 | self.failures = []
|
|---|
| 174 | self.num_tests = len(formats) * len(tests)
|
|---|
| 175 | num_digits = len(str(self.num_tests))
|
|---|
| 176 | self.fmt_str = "[%%%dd/%%%dd] (%%s) %%s" % (num_digits, num_digits)
|
|---|
| 177 | self.isatty = sys.stdout.isatty() and not self.verbose
|
|---|
| 178 | self.env = os.environ.copy()
|
|---|
| 179 | self.hpos = 0
|
|---|
| 180 |
|
|---|
| 181 | def run(self):
|
|---|
| 182 | run_start = time.time()
|
|---|
| 183 |
|
|---|
| 184 | i = 1
|
|---|
| 185 | for fmt in self.formats:
|
|---|
| 186 | for test in self.tests:
|
|---|
| 187 | self.run_test(test, fmt, i)
|
|---|
| 188 | i += 1
|
|---|
| 189 |
|
|---|
| 190 | if self.isatty:
|
|---|
| 191 | self.erase_current_line()
|
|---|
| 192 |
|
|---|
| 193 | self.took = time.time() - run_start
|
|---|
| 194 |
|
|---|
| 195 | def run_test(self, test, fmt, i):
|
|---|
| 196 | if self.isatty:
|
|---|
| 197 | self.erase_current_line()
|
|---|
| 198 |
|
|---|
| 199 | msg = self.fmt_str % (i, self.num_tests, fmt, test)
|
|---|
| 200 | self.print_(msg)
|
|---|
| 201 |
|
|---|
| 202 | start = time.time()
|
|---|
| 203 | cmd = [sys.executable, test] + self.gyp_options
|
|---|
| 204 | self.env["TESTGYP_FORMAT"] = fmt
|
|---|
| 205 | proc = subprocess.Popen(
|
|---|
| 206 | cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=self.env
|
|---|
| 207 | )
|
|---|
| 208 | proc.wait()
|
|---|
| 209 | took = time.time() - start
|
|---|
| 210 |
|
|---|
| 211 | stdout = proc.stdout.read().decode("utf8")
|
|---|
| 212 | if proc.returncode == 2:
|
|---|
| 213 | res = "skipped"
|
|---|
| 214 | elif proc.returncode:
|
|---|
| 215 | res = "failed"
|
|---|
| 216 | self.failures.append(f"({test}) {fmt}")
|
|---|
| 217 | else:
|
|---|
| 218 | res = "passed"
|
|---|
| 219 | res_msg = f" {res} {took:.3f}s"
|
|---|
| 220 | self.print_(res_msg)
|
|---|
| 221 |
|
|---|
| 222 | if stdout and not stdout.endswith(("PASSED\n", "NO RESULT\n")):
|
|---|
| 223 | print()
|
|---|
| 224 | print("\n".join(f" {line}" for line in stdout.splitlines()))
|
|---|
| 225 | elif not self.isatty:
|
|---|
| 226 | print()
|
|---|
| 227 |
|
|---|
| 228 | def print_(self, msg):
|
|---|
| 229 | print(msg, end="")
|
|---|
| 230 | index = msg.rfind("\n")
|
|---|
| 231 | if index == -1:
|
|---|
| 232 | self.hpos += len(msg)
|
|---|
| 233 | else:
|
|---|
| 234 | self.hpos = len(msg) - index
|
|---|
| 235 | sys.stdout.flush()
|
|---|
| 236 |
|
|---|
| 237 | def erase_current_line(self):
|
|---|
| 238 | print("\b" * self.hpos + " " * self.hpos + "\b" * self.hpos, end="")
|
|---|
| 239 | sys.stdout.flush()
|
|---|
| 240 | self.hpos = 0
|
|---|
| 241 |
|
|---|
| 242 | def print_results(self):
|
|---|
| 243 | num_failures = len(self.failures)
|
|---|
| 244 | if num_failures:
|
|---|
| 245 | print()
|
|---|
| 246 | if num_failures == 1:
|
|---|
| 247 | print("Failed the following test:")
|
|---|
| 248 | else:
|
|---|
| 249 | print("Failed the following %d tests:" % num_failures)
|
|---|
| 250 | print("\t" + "\n\t".join(sorted(self.failures)))
|
|---|
| 251 | print()
|
|---|
| 252 | print(
|
|---|
| 253 | "Ran %d tests in %.3fs, %d failed."
|
|---|
| 254 | % (self.num_tests, self.took, num_failures)
|
|---|
| 255 | )
|
|---|
| 256 | print()
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 | if __name__ == "__main__":
|
|---|
| 260 | sys.exit(main())
|
|---|