Skip to content

Commit

Permalink
general: add a test for __init__.py fallback for modules which are sw…
Browse files Browse the repository at this point in the history
…itching to namespace packages

for now just a manual ad-hoc test, will try to set it up on CI later

relevant to the discussion here: https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/270465792

also potentially relevant to

- #89 (will try to apply to this to reddit/__init__.py later)
- #102
  • Loading branch information
karlicoss committed Jun 1, 2022
1 parent 049820c commit 179b657
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions misc/check_legacy_init_py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
# NOTE: prerequisites for this test:
# fbmessengerexport installed
# config configured (can set it to '' though)

from pathlib import Path
from subprocess import Popen, run, PIPE
from tempfile import TemporaryDirectory


import logzero # type: ignore[import]
logger = logzero.logger


MSG = 'DEPRECATED! Instead of my.fbmessengerexport'

def expect(*cmd: str, should_warn: bool=True) -> None:
res = run(cmd, stderr=PIPE)
errb = res.stderr; assert errb is not None
err = errb.decode('utf8')
if should_warn:
assert MSG in err, res
else:
assert MSG not in err, res
assert res.returncode == 0, res


def _check(*cmd: str, should_warn: bool, run_as_cmd: bool=True) -> None:
expecter = lambda *cmd: expect(*cmd, should_warn=should_warn)
if cmd[0] == '-c':
[_, code] = cmd
if run_as_cmd:
expecter('python3', '-c', code)
# check as a script
with TemporaryDirectory() as tdir:
script = Path(tdir) / 'script.py'
script.write_text(code)
expecter('python3', str(script))
else:
expecter('python3', *cmd)
what = 'warns' if should_warn else ' ' # meh
logger.info(f"PASSED: {what}: {repr(cmd)}")


def check_warn(*cmd: str, **kwargs) -> None:
_check(*cmd, should_warn=True, **kwargs)

def check_ok(*cmd: str, **kwargs) -> None:
_check(*cmd, should_warn=False, **kwargs)


# NOTE these three are actually sort of OK, they are allowed when it's a proper namespace package with all.py etc.
# but more likely it means legacy behaviour or just misusing the package?
# worst case it's just a warning I guess
check_warn('-c', 'from my import fbmessenger')
check_warn('-c', 'import my.fbmessenger')
check_warn('-c', 'from my.fbmessenger import *')

# note: dump_chat_history should really be deprecated, but it's a quick way to check we actually fell back to fbmessenger/export.py
# NOTE: this is the most common legacy usecase
check_warn('-c', 'from my.fbmessenger import messages, dump_chat_history')
check_warn('-m', 'my.core', 'query' , 'my.fbmessenger.messages')
check_warn('-m', 'my.core', 'doctor', 'my.fbmessenger')

# todo kinda annoying it doesn't work when executed as -c (but does as script!)
# presumably because doesn't have proper line number information?
# either way, it'a a bit of a corner case, the script behaviour is more important
check_ok ('-c', 'from my.fbmessenger import export', run_as_cmd=False)
check_ok ('-c', 'import my.fbmessenger.export')
check_ok ('-c', 'from my.fbmessenger.export import *')
check_ok ('-c', 'from my.fbmessenger.export import messages, dump_chat_history')
check_ok ('-m', 'my.core', 'query' , 'my.fbmessenger.export.messages')
check_ok ('-m', 'my.core', 'doctor', 'my.fbmessenger.export')

# TODO with reddit, currently these don't work properly at all
# only when imported from scripts etc?

0 comments on commit 179b657

Please sign in to comment.