|
40 | 40 | %% sys.modules verification tests |
41 | 41 | import_in_sys_modules_test/1, |
42 | 42 | 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 |
44 | 46 | ]). |
45 | 47 |
|
46 | 48 | all() -> |
@@ -72,7 +74,9 @@ groups() -> |
72 | 74 | %% sys.modules verification tests |
73 | 75 | import_in_sys_modules_test, |
74 | 76 | 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 |
76 | 80 | ]}]. |
77 | 81 |
|
78 | 82 | init_per_suite(Config) -> |
@@ -705,3 +709,55 @@ _sys_keys = list(sys.modules.keys()) |
705 | 709 | py_context:destroy(Ctx), |
706 | 710 |
|
707 | 711 | 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