Ich habe alle oben genannten Vorschläge ausprobiert und kann keine Lösung für einen wirklich einfachen Fall finden, in dem ich ein CLI-Skript ohne .py
Erweiterung testen möchte .
Warum wird spec.loader.exec_module()
ein IsADirectoryError auf " .
" ausgelöst ?
Ich habe zwei Dateien in einem Verzeichnis:
%. ls
cli_with_no_dot_py test_cli_with_no_dot_py.py
%. cat cli_with_no_dot_py
cool_thing = True
print("cool_thing is", cool_thing)
%. ./cli_with_no_dot_py
cool_thing is True
%. cat test_cli_with_no_dot_py.py
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
spec = spec_from_loader("cli_with_no_dot_py",
SourceFileLoader("cli_with_no_dot_py", "."))
print("spec:", spec)
cli_with_no_dot_py = module_from_spec(spec)
print("cli_with_no_dot_py):", cli_with_no_dot_py)
print("dir(cli_with_no_dot_py:", dir(cli_with_no_dot_py))
print("spec.loader:", spec.loader)
spec.loader.exec_module(cli_with_no_dot_py)
def test_cool_thing():
print("cli_with_no_dot_py.cool_thing is", cli_with_no_dot_py.cool_thing)
assert cli_with_no_dot_py.cool_thing
%. pytest
======================================================================== test session starts ========================================================================
platform darwin -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/cclauss/Python/pytest_a_cli_tool
collected 0 items / 1 error
============================================================================== ERRORS ===============================================================================
____________________________________________________________ ERROR collecting test_cli_with_no_dot_py.py ____________________________________________________________
test_cli_with_no_dot_py.py:11: in <module>
spec.loader.exec_module(cli_with_no_dot_py)
<frozen importlib._bootstrap_external>:779: in exec_module
???
<frozen importlib._bootstrap_external>:915: in get_code
???
<frozen importlib._bootstrap_external>:972: in get_data
???
E IsADirectoryError: [Errno 21] Is a directory: '.'
-------------------------------------------------------------------------- Captured stdout --------------------------------------------------------------------------
spec: ModuleSpec(name='cli_with_no_dot_py', loader=<_frozen_importlib_external.SourceFileLoader object at 0x10e106f70>, origin='.')
cli_with_no_dot_py: <module 'cli_with_no_dot_py' from '.'>
dir(cli_with_no_dot_py): ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
spec.loader: <_frozen_importlib_external.SourceFileLoader object at 0x10e106f70>
====================================================================== short test summary info ======================================================================
ERROR test_cli_with_no_dot_py.py - IsADirectoryError: [Errno 21] Is a directory: '.'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================= 1 error in 0.09s ==========================================================================
py
Erweiterung?