def test_modname_to_relpath(self): modname = 'ballet.util._util' expected_relpath = 'ballet/util/_util.py' actual_relpath = modname_to_relpath(modname) self.assertEqual(actual_relpath, expected_relpath) modname = 'ballet.util' # mypackage.__file__ resolves to the __init__.py project_root = pathlib.Path(ballet.__file__).parent.parent expected_relpath = 'ballet/util/__init__.py' actual_relpath = modname_to_relpath(modname, project_root=project_root) self.assertEqual(actual_relpath, expected_relpath) # TODO patch this # # without providing project root, behavior is undefined, as we don't # # know whether the relative path will resolve to a directory # # within a temporary directory, the relpath *should not* be a dir # with tempfile.TemporaryDirectory() as tmpdir: # cwd = os.getcwd() # try: # os.chdir(tmpdir) # actual_relpath = modname_to_relpath(modname) # expected_relpath = 'ballet/util.py' # self.assertEqual(actual_relpath, expected_relpath) # finally: # os.chdir(cwd) # # from the actual project root, the relpath *should* be a dir # cwd = os.getcwd() # try: # os.chdir(str(project_root)) # actual_relpath = modname_to_relpath(modname) # expected_relpath = 'ballet/util/__init__.py' # self.assertEqual(actual_relpath, expected_relpath) # finally: # os.chdir(cwd) # without add_init modname = 'ballet.util' add_init = False expected_relpath = 'ballet/util' actual_relpath = modname_to_relpath(modname, project_root=project_root, add_init=add_init) self.assertEqual(actual_relpath, expected_relpath)
def test_modname_to_relpath_package(): modname = 'ballet.util' # mypackage.__file__ resolves to the __init__.py project_root = pathlib.Path(ballet.__file__).parent.parent expected_relpath = 'ballet/util/__init__.py' actual_relpath = modname_to_relpath(modname, project_root=project_root) assert actual_relpath == expected_relpath
def test_modname_to_relpath_package_no_add_init(): project_root = pathlib.Path(ballet.__file__).parent.parent # without add_init modname = 'ballet.util' add_init = False expected_relpath = 'ballet/util' actual_relpath = modname_to_relpath(modname, project_root=project_root, add_init=add_init) assert actual_relpath == expected_relpath
def _import(modname): relpath = modname_to_relpath(modname, project_root=base, add_init=False) abspath = base.joinpath(relpath) return import_module_at_path(modname, abspath)
def test_modname_to_relpath_module(): modname = 'ballet.util._util' expected_relpath = 'ballet/util/_util.py' actual_relpath = modname_to_relpath(modname) assert actual_relpath == expected_relpath