Mk `test_implicit_mod_name_applied_for_child()` check init-mods

Test pkg-level init module and sub-pkg module logger naming
to better validate auto-naming logic.

Deats,
- create `pkg_init_mod` and write `mod_code` to it for
  testing pkg-level `__init__.py` logger instance creation.
  * assert `snakelib.__init__` logger name is `proj_name`.
- write `mod_code` to `subpkg/__init__.py`` as well and check the same.

Also,
- rename some vars,
  * `pkg_mod` -> `pkg_submod`,
  * `pkgmod` -> `subpkgmod`
- add `ModuleType` import for type hints
- improve comments explaining pkg init vs first-level
  sub-module naming expectations.
- drop trailing whitespace and unused TODO comment
- remove masked `breakpoint()` call

(this commit msg was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
log_sys_testing
Gud Boi 2026-02-11 21:36:41 -05:00
parent ff02939213
commit 5850844297
1 changed files with 31 additions and 12 deletions

View File

@ -4,6 +4,7 @@
''' '''
from pathlib import Path from pathlib import Path
import shutil import shutil
from types import ModuleType
import pytest import pytest
import tractor import tractor
@ -57,28 +58,32 @@ def test_implicit_mod_name_applied_for_child(
mod_code: str = ( mod_code: str = (
f'import tractor\n' f'import tractor\n'
f'\n' f'\n'
# f'breakpoint()\n' # if you want to trace it all # if you need to trace `testdir` stuff @ import-time..
# f'breakpoint()\n'
f'log = tractor.log.get_logger(pkg_name="{proj_name}")\n' f'log = tractor.log.get_logger(pkg_name="{proj_name}")\n'
) )
# create a sub-module for each pkg layer # create a sub-module for each pkg layer
_lib = testdir.mkpydir(proj_name) _lib = testdir.mkpydir(proj_name)
pkg: Path = Path(_lib) pkg: Path = Path(_lib)
pkg_init_mod: Path = pkg / "__init__.py"
pkg_init_mod.write_text(mod_code)
subpkg: Path = pkg / 'subpkg' subpkg: Path = pkg / 'subpkg'
subpkg.mkdir() subpkg.mkdir()
subpkgmod: Path = subpkg / "__init__.py"
pkgmod: Path = subpkg / "__init__.py" subpkgmod.touch()
pkgmod.touch() subpkgmod.write_text(mod_code)
_submod: Path = testdir.makepyfile( _submod: Path = testdir.makepyfile(
_mod=mod_code, _mod=mod_code,
) )
pkg_mod = pkg / 'mod.py' pkg_submod = pkg / 'mod.py'
pkg_subpkg_submod = subpkg / 'submod.py' pkg_subpkg_submod = subpkg / 'submod.py'
shutil.copyfile( shutil.copyfile(
_submod, _submod,
pkg_mod, pkg_submod,
) )
shutil.copyfile( shutil.copyfile(
_submod, _submod,
@ -91,10 +96,11 @@ def test_implicit_mod_name_applied_for_child(
# XXX NOTE, once the "top level" pkg mod has been # XXX NOTE, once the "top level" pkg mod has been
# imported, we can then use `import` syntax to # imported, we can then use `import` syntax to
# import it's sub-pkgs and modules. # import it's sub-pkgs and modules.
pkgmod = _code_load.load_module_from_path( subpkgmod: ModuleType = _code_load.load_module_from_path(
Path(pkg / '__init__.py'), Path(pkg / '__init__.py'),
module_name=proj_name, module_name=proj_name,
) )
pkg_root_log = log.get_logger( pkg_root_log = log.get_logger(
pkg_name=proj_name, pkg_name=proj_name,
mk_sublog=False, mk_sublog=False,
@ -107,16 +113,31 @@ def test_implicit_mod_name_applied_for_child(
# ^TODO! test this same output but created via a `get_logger()` # ^TODO! test this same output but created via a `get_logger()`
# call in the `snakelib.__init__py`!! # call in the `snakelib.__init__py`!!
# a first-pkg-level module should only # NOTE, the pkg-level "init mod" should of course
# use # have the same name as the package ns-path.
import snakelib as init_mod
assert init_mod.log.name == proj_name
# NOTE, a first-pkg-level sub-module should only
# use the package-name since the leaf-node-module
# will be included in log headers by default.
from snakelib import mod from snakelib import mod
assert mod.log.name == proj_name assert mod.log.name == proj_name
from snakelib import subpkg
assert (
subpkg.log.name
==
subpkg.__package__
==
f'{proj_name}.subpkg'
)
from snakelib.subpkg import submod from snakelib.subpkg import submod
assert ( assert (
submod.log.name submod.log.name
== ==
submod.__package__ # ?TODO, use this in `.get_logger()` instead? submod.__package__
== ==
f'{proj_name}.subpkg' f'{proj_name}.subpkg'
) )
@ -125,8 +146,6 @@ def test_implicit_mod_name_applied_for_child(
assert len(sub_logs) == 1 # only one nested sub-pkg module assert len(sub_logs) == 1 # only one nested sub-pkg module
assert submod.log.logger in sub_logs assert submod.log.logger in sub_logs
# breakpoint()
# TODO, moar tests against existing feats: # TODO, moar tests against existing feats:
# ------ - ------ # ------ - ------