Skip to content

Commit 3b81fff

Browse files
committed
Add test for py_import:add_path/1
1 parent e30edfc commit 3b81fff

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

test/py_import_SUITE.erl

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
%% sys.modules verification tests
4141
import_in_sys_modules_test/1,
4242
registry_import_in_sys_modules_test/1,
43-
context_import_in_sys_modules_test/1
43+
context_import_in_sys_modules_test/1,
44+
%% Path registry tests
45+
add_path_test/1
4446
]).
4547

4648
all() ->
@@ -72,7 +74,9 @@ groups() ->
7274
%% sys.modules verification tests
7375
import_in_sys_modules_test,
7476
registry_import_in_sys_modules_test,
75-
context_import_in_sys_modules_test
77+
context_import_in_sys_modules_test,
78+
%% Path registry tests
79+
add_path_test
7680
]}].
7781

7882
init_per_suite(Config) ->
@@ -705,3 +709,55 @@ _sys_keys = list(sys.modules.keys())
705709
py_context:destroy(Ctx),
706710

707711
ct:pal("Context imports correctly populate sys.modules").
712+
713+
%% ============================================================================
714+
%% Path Registry Tests
715+
%% ============================================================================
716+
717+
%% @doc Test that add_path registers a path and makes modules importable
718+
%%
719+
%% This test creates a custom module in priv_dir, adds its path via add_path,
720+
%% then verifies the module can be imported and called.
721+
add_path_test(Config) ->
722+
%% Clear any existing paths
723+
ok = py_import:clear_paths(),
724+
725+
%% Create test module in priv_dir (guaranteed writable during tests)
726+
PrivDir = ?config(priv_dir, Config),
727+
ModuleDir = filename:join(PrivDir, "custom_modules"),
728+
ok = filelib:ensure_dir(filename:join(ModuleDir, "dummy")),
729+
730+
%% Write a simple Python module
731+
ModulePath = filename:join(ModuleDir, "sample_module.py"),
732+
ModuleContent = <<"def greet(name):\n"
733+
" return f\"Hello, {name}!\"\n"
734+
"\n"
735+
"def add(a, b):\n"
736+
" return a + b\n"
737+
"\n"
738+
"VERSION = \"1.0.0\"\n">>,
739+
ok = file:write_file(ModulePath, ModuleContent),
740+
741+
%% Add path
742+
ok = py_import:add_path(ModuleDir),
743+
744+
%% Verify path is registered
745+
?assert(py_import:is_path_added(ModuleDir)),
746+
Paths = py_import:all_paths(),
747+
?assertEqual(1, length(Paths)),
748+
749+
%% Create a new context to apply paths
750+
{ok, Ctx} = py_context:new(#{mode => auto}),
751+
752+
%% Import and call the sample module
753+
{ok, Greeting} = py_context:call(Ctx, sample_module, greet, [<<"World">>], #{}),
754+
?assertEqual(<<"Hello, World!">>, Greeting),
755+
756+
{ok, Sum} = py_context:call(Ctx, sample_module, add, [2, 3], #{}),
757+
?assertEqual(5, Sum),
758+
759+
%% Clean up
760+
py_context:destroy(Ctx),
761+
ok = py_import:clear_paths(),
762+
763+
ct:pal("add_path successfully registers paths and enables module imports").

0 commit comments

Comments
 (0)