Skip to content

Commit

Permalink
[cdd/{compound/exmod{_utils}.py,tests/{mocks/exmod.py,test_compound/t…
Browse files Browse the repository at this point in the history
…est_exmod.py}}] Improve exmod testing by testing with subpackages
  • Loading branch information
SamuelMarks committed Jun 27, 2023
1 parent 7ac627a commit 3d122b5
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 113 deletions.
9 changes: 7 additions & 2 deletions cdd/compound/exmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from cdd.shared.pure_utils import (
INIT_FILENAME,
find_module_filepath,
pp,
read_file_to_str,
rpartial,
)
Expand Down Expand Up @@ -102,7 +103,9 @@ def exmod(
emit_name, (str, type(None))
), "Expected `str` got `{emit_name_type!r}`".format(emit_name_type=type(emit_name))

module_name, new_module_name = map(path.basename, (module, output_directory))
pp({"exmod::module": module, "exmod::output_directory": output_directory})
module_name, new_module_name = module, ".".join((module.rpartition(".")[0], "gold"))
pp({"exmod::module_name": module_name, "exmod::new_module_name": new_module_name})

module_root_dir = (
path.dirname(find_module_filepath(*module.rsplit(".", 1))) + path.sep
Expand Down Expand Up @@ -137,7 +140,9 @@ def exmod(
)
if not imports:
# Case: no obvious folder hierarchy, so parse the `__init__` file in root
with open(module_root_dir + "__init__{}py".format(path.extsep), "rt") as f:
with open(
module_root_dir + "__init__{extsep}py".format(extsep=path.extsep), "rt"
) as f:
mod = parse(f.read())

# TODO: Optimise these imports
Expand Down
4 changes: 2 additions & 2 deletions cdd/compound/exmod_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from cdd.shared.pkg_utils import relative_filename
from cdd.shared.pure_utils import (
INIT_FILENAME,
no_magic_dir2attr,
no_magic_or_builtin_dir2attr,
rpartial,
sanitise_emit_name,
)
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_module_contents(obj, module_root_dir, current_module=None, _result={}):
)
)

for name, symbol in no_magic_dir2attr(obj).items():
for name, symbol in no_magic_or_builtin_dir2attr(obj).items():
fq = "{current_module}.{name}".format(current_module=current_module, name=name)
try:
symbol_location = getfile(symbol)
Expand Down
7 changes: 5 additions & 2 deletions cdd/shared/pure_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sys import version_info
from textwrap import fill as _fill
from textwrap import indent
from types import BuiltinFunctionType
from typing import Callable, Dict, FrozenSet, Optional, Tuple, Union

pp: Callable[[object], None] = PrettyPrinter(indent=4, width=100).pprint
Expand Down Expand Up @@ -1113,9 +1114,10 @@ def default(self, obj):
)


def no_magic_dir2attr(p_object):
def no_magic_or_builtin_dir2attr(p_object):
"""
Dictionary of `dir` without the __ prefix magics (also without _ prefix)
and without builtins
return the names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
Expand All @@ -1129,6 +1131,7 @@ def no_magic_dir2attr(p_object):
attr: getattr(p_object, attr)
for attr in dir(p_object)
if not attr.startswith("_")
and not isinstance(getattr(p_object, attr), BuiltinFunctionType)
}


Expand Down Expand Up @@ -1252,7 +1255,7 @@ def namespaced_upper_camelcase_to_pascal(s, sep="__"):
"multiline",
"namespaced_pascal_to_upper_camelcase",
"namespaced_upper_camelcase_to_pascal",
"no_magic_dir2attr",
"no_magic_or_builtin_dir2attr",
"none_types",
"num_of_nls",
"omit_whitespace",
Expand Down
1 change: 1 addition & 0 deletions cdd/tests/mocks/exmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from setuptools import find_packages, setup
package_name = {package_name!r}
module_name = {module_name!r}
def main():
Expand Down
Loading

0 comments on commit 3d122b5

Please sign in to comment.