Skip to content

Commit

Permalink
use centralized color print function
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 30, 2020
1 parent 1b954c7 commit 1be6080
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions psutil/tests/runner.py
Expand Up @@ -53,11 +53,21 @@
VERBOSITY = 1 if TOX else 2
FAILED_TESTS_FNAME = '.failed-tests.txt'
NWORKERS = psutil.cpu_count() or 1
USE_COLORS = term_supports_colors() and not APPVEYOR

HERE = os.path.abspath(os.path.dirname(__file__))
loadTestsFromTestCase = unittest.defaultTestLoader.loadTestsFromTestCase


def cprint(msg, color, bold=False, file=None):
if file is None:
file = sys.stderr if color == 'red' else sys.stdout
if USE_COLORS:
print_color(msg, color, bold=bold, file=file)
else:
print(msg, file=file)


class TestLoader:

testdir = HERE
Expand Down Expand Up @@ -110,24 +120,21 @@ def from_name(self, name):

class ColouredResult(unittest.TextTestResult):

def _print_color(self, s, color, bold=False):
print_color(s, color, bold=bold)

def addSuccess(self, test):
unittest.TestResult.addSuccess(self, test)
self._print_color("OK", "green")
cprint("OK", "green")

def addError(self, test, err):
unittest.TestResult.addError(self, test, err)
self._print_color("ERROR", "red", bold=True)
cprint("ERROR", "red", bold=True)

def addFailure(self, test, err):
unittest.TestResult.addFailure(self, test, err)
self._print_color("FAIL", "red")
cprint("FAIL", "red")

def addSkip(self, test, reason):
unittest.TestResult.addSkip(self, test, reason)
self._print_color("skipped: %s" % reason.strip(), "brown")
cprint("skipped: %s" % reason.strip(), "brown")

def printErrorList(self, flavour, errors):
flavour = hilite(flavour, "red", bold=flavour == 'ERROR')
Expand All @@ -139,11 +146,7 @@ class ColouredTextRunner(unittest.TextTestRunner):
A coloured text runner which also prints failed tests on KeyboardInterrupt
and save failed tests in a file so that they can be re-run.
"""

if term_supports_colors() and not APPVEYOR:
resultclass = ColouredResult
else:
resultclass = unittest.TextTestResult
resultclass = ColouredResult if USE_COLORS else unittest.TextTestResult

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -180,11 +183,11 @@ def _run(self, suite):

def _exit(self, success):
if success:
print_color("SUCCESS", "green", bold=True)
cprint("SUCCESS", "green", bold=True)
safe_rmpath(FAILED_TESTS_FNAME)
sys.exit(0)
else:
print_color("FAILED", "red", bold=True)
cprint("FAILED", "red", bold=True)
self._write_last_failed()
sys.exit(1)

Expand Down Expand Up @@ -228,8 +231,8 @@ def run(self, suite):
par_suite = self._parallelize(par_suite)

# run parallel
print_color("starting parallel tests using %s workers" % NWORKERS,
"green", bold=True)
cprint("starting parallel tests using %s workers" % NWORKERS,
"green", bold=True)
t = time.time()
par = self._run(par_suite)
par_elapsed = time.time() - t
Expand All @@ -239,7 +242,7 @@ def run(self, suite):
orphans = psutil.Process().children()
gone, alive = psutil.wait_procs(orphans, timeout=1)
if alive:
print_color("alive processes %s" % alive, "red")
cprint("alive processes %s" % alive, "red")
reap_children()

# run serial
Expand Down Expand Up @@ -274,8 +277,7 @@ def run(self, suite):

def get_runner(parallel=False):
def warn(msg):
print_color(msg + " Running serial tests instead.",
"red", file=sys.stderr)
cprint(msg + " Running serial tests instead.", "red")
if parallel:
if psutil.WINDOWS:
warn("Can't run parallel tests on Windows.")
Expand Down

0 comments on commit 1be6080

Please sign in to comment.