def test_single_file_insource(make_single_file): """Test importer with in-source generation.""" single_file = make_single_file("sfile_is") gtscript_imports.enable(search_path=[single_file.parent], in_source=True) import sfile_is assert sfile_is.SENTINEL == 5 assert (single_file.parent / "sfile_is.py").exists()
def test_single_file_nosearchpath(make_single_file): """Test import statement without giving a search path.""" single_file = make_single_file("sfile_nsp") gtscript_imports.enable() sys.path.append(single_file.parent) import sfile_nsp assert sfile_nsp.SENTINEL == 5
def test_single_file_import(make_single_file, reset_importsys): """Test using basic import statement for a gtscript module.""" single_file = make_single_file("sfile_imp") gtscript_imports.enable(search_path=[single_file.parent]) assert str(single_file.parent) in sys.path import sfile_imp assert sfile_imp.SENTINEL == 5
def test_single_file_import_module(make_single_file): """Test importlib.import_module in combination with importer.""" from importlib import import_module single_file = make_single_file("sfile_im") gtscript_imports.enable(search_path=[single_file.parent]) print(single_file.stem) print(single_file.suffixes) sfile_im = import_module(single_file.stem.split(".")[0]) assert sfile_im.SENTINEL == 5
def test_single_file_builddir(tmp_path, make_single_file): """Test importer with build folder.""" single_file = make_single_file("sfile_bf") gen_dir = tmp_path / "generate" gtscript_imports.enable(search_path=[single_file.parent], generate_path=gen_dir) import sfile_bf assert sfile_bf.SENTINEL == 5 assert (gen_dir / "sfile_bf.py").exists()
def test_pkg_import(make_package): """Test importing from package in same folder.""" main_file = make_package("pkg_simple") gtscript_imports.enable(search_path=[main_file.parent]) import pkg_simple assert pkg_simple.SENTINEL == 1 assert pkg_simple.CONST == 3.14 assert pkg_simple.mf assert pkg_simple.ms assert pkg_simple.sf1 assert pkg_simple.sss
def test_twofile_import(make_two_files): """Test importing from the same folder.""" file_one, file_two = make_two_files("simple") gtscript_imports.enable(search_path=[file_one.parent]) import simple_one assert simple_one.SENTINEL == 43 assert simple_one.simple_two.SENTINEL == 47 from simple_one import simple_two assert simple_two.get_sentinel_one() == simple_one.SENTINEL
def test_single_file_load(make_single_file): """Test the finder's load_module method.""" single_file = make_single_file("sfile_load") finder = gtscript_imports.enable(search_path=[single_file.parent]) sf_module = finder.load_module("sfile_load") assert sf_module.SENTINEL == 5