Skip to content

Commit

Permalink
Fix #444: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gurhar1133 committed Mar 29, 2024
2 parents f05ba71 + b27059e commit fa35bd2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 48 deletions.
82 changes: 52 additions & 30 deletions pykern/pkcli/test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
"""run test files in separate processes
:copyright: Copyright (c) 2019 RadiaSoft LLC. All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""

from pykern import pkconfig
from pykern import pkio
from pykern import pksubprocess
Expand All @@ -26,6 +26,7 @@


_cfg = pkconfig.init(
ignore_warnings=(False, bool, "override pytest's output of all warnings"),
max_failures=(5, int, "maximum number of test failures before exit"),
restartable=(
False,
Expand Down Expand Up @@ -173,9 +174,6 @@ def _except(exc, output, restartable):
return "restart"
return _fail(output)

def _skipped(ouput):
return _TEST_SKIPPED.findall(ouput)

def _fail(output):
self.failures.append(output)
return f"FAIL {output}"
Expand All @@ -185,36 +183,60 @@ def _remove_passing_messages(content):
content = re.sub(pattern, "", content)
return content

def _ignore_warnings():
if not _cfg.ignore_warnings:
return []
rv = []
for w in (
# https://docs.python.org/3/library/warnings.html#default-warning-filter
"DeprecationWarning",
"PendingDeprecationWarning",
"ImportWarning",
"ResourceWarning",
):
rv.extend(("-W", f"ignore::{w}"))
return rv

def _try(output, restartable):
sys.stdout.write(test_f)
sys.stdout.flush()
c = (
["pytest"]
+ _ignore_warnings()
+ [
"--tb=native",
"-v",
"-s",
"-rs",
test_f,
]
+ self.flags
)
v = _env(restartable)
try:
sys.stdout.write(test_f)
sys.stdout.flush()
pksubprocess.check_call_with_signals(
["py.test", "--tb=native", "-v", "-s", "-rs", test_f] + self.flags,
output=output,
env=_env(restartable),
)
o = pkio.read_text(output)
if m := re.findall(_COROUTINE_NEVER_AWAITED, o):
pkio.write_text(
output,
"\n".join(
[
_remove_passing_messages(o),
'Failed due to these "coroutine was never awaited" warnings:',
"\n\n".join(
[f"{i + 1}) " + e for i, e in enumerate(m)]
),
]
)
+ "\n",
)
return _fail(output)
if _TEST_SKIPPED.search(o):
return "\n".join(["pass"] + _skipped(o))
return "pass"
pksubprocess.check_call_with_signals(c, output=output, env=v)
except Exception as e:
return _except(e, output, restartable)
o = pkio.read_text(output)
if m := re.findall(_COROUTINE_NEVER_AWAITED, o):
pkio.write_text(
output,
"\n".join(
[
_remove_passing_messages(o),
'Failed due to these "coroutine was never awaited" warnings:',
"\n\n".join([f"{i + 1}) " + e for i, e in enumerate(m)]),
]
)
+ "\n",
)
return _fail(output)
if _TEST_SKIPPED.search(o):
return "\n".join(["pass"] + _skipped(o))
return "pass"

def _skipped(ouput):
return _TEST_SKIPPED.findall(ouput)

o = test_f.replace(".py", ".log")
for t in range(4 if _cfg.restartable else 0, -1, -1):
Expand Down
3 changes: 3 additions & 0 deletions pykern/pkconst.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
#: Copied from numconv, which copied from RFC1924
BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

#: The subdirectory in the top-level Python where to put resources
PACKAGE_DATA = "package_data"

#: Use this (sparingly, pkdlog is prefered) when you want to use print directly.
builtin_print = print
23 changes: 7 additions & 16 deletions pykern/pkresource.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# -*- coding: utf-8 -*-
"""Where external resources are stored
:copyright: Copyright (c) 2015 Bivio Software, Inc. All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function

# Root module: Import only builtin packages so avoid dependency issues
# Root module: avoid importing modules which import pkconfig
from pykern import pkconst
from pykern import pkinspect
from pykern import pkio
import errno
import glob
import importlib
import os.path
import pkg_resources

# TODO(e-carlin): discuss with rn if ok to import pkio
from pykern import pkinspect
from pykern import pkio
from pykern import pksetup
import os.path


def file_path(relative_filename, caller_context=None, packages=None):
Expand Down Expand Up @@ -89,16 +85,11 @@ def _files(path, caller_context, packages):
],
)
):
# TODO(e-carlin): using pkg_resources is discouraged
# https://setuptools.readthedocs.io/en/latest/pkg_resources.html
# But, as of py3.7 importlib.resources doesn't offer a good API
# for accessing directories
# https://docs.python.org/3/library/importlib.html#module-importlib.resources
# https://gitlab.com/python-devs/importlib_resources/-/issues/58
yield (
# Will be fixed in https://github.com/radiasoft/pykern/issues/462
pkg_resources.resource_filename(
p,
os.path.join(pksetup.PACKAGE_DATA, path),
os.path.join(pkconst.PACKAGE_DATA, path),
),
p,
)
Expand Down
4 changes: 2 additions & 2 deletions pykern/pksetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class is provided by this module.
import locale
import os
import os.path
import pkg_resources
import packaging.version
import re
import setuptools
import setuptools.command.sdist
Expand Down Expand Up @@ -719,7 +719,7 @@ def _version_float(value):
def _version_from_datetime(value=None):
# Avoid 'UserWarning: Normalizing' by setuptools
return str(
pkg_resources.parse_version(
packaging.version.Version(
(value or datetime.datetime.utcnow()).strftime("%Y%m%d.%H%M%S"),
),
)
Expand Down

0 comments on commit fa35bd2

Please sign in to comment.