Currently running SWI-Prolog (threaded, 64 bits, version 8.3.28-20-g6f8a68f2b)
Problem: Using load_files/2 with the option imports([])
and directive :- load_test_files([]).
and predicate of same name in loaded file.
- works as expected
if the file being loaded does not have the directive:- load_test_files([]).
- throws error - No permission to redefine imported_procedure
if the file being loaded has the directive:- load_test_files([]).
My thought is that the addition of :- load_test_files([]).
when none of the predicates are being imported because of imports([])
should not cause an error.
Minimal reproduceable example.
All five files are in the same directory.
File: my_app.pl
:- module(my_app,
[
load_check_with_tests_loaded/0,
load_check_without_tests_loaded/0,
my_predicate/1
]).
load_check_with_tests_loaded :-
load_files(my_module_with_tests_loaded,[imports([])]).
load_check_without_tests_loaded :-
load_files(my_module_without_tests_loaded,[imports([])]).
my_predicate(Result) :-
Result = 'Hello from my_app.'.
File: my_module_with_tests_loaded.pl
:- module(my_module_with_tests_loaded,
[
my_predicate/1
]).
:- load_test_files([]).
my_predicate(Result) :-
Result = 'Hello from my_module_with_tests_loaded.'.
File: my_module_with_tests_loaded.plt
:- begin_tests(my_tests_with_tests_loaded).
:- use_module(my_module_with_tests_loaded).
test(hello) :-
my_predicate('Hello example').
:- end_tests(my_tests_with_tests_loaded).
File: my_module_without_tests_loaded.pl
:- module(my_module_without_tests_loaded,
[
my_predicate/1
]).
my_predicate(Result) :-
Result = 'Hello from my_module_without_tests_loaded.'.
File: my_module_withpit_tests_loaded.plt
:- begin_tests(my_test_without_tests_loaded).
:- use_module(my_module_without_tests_loaded).
test(hello) :-
my_predicate('Hello example').
:- end_tests(my_test_without_tests_loaded).
Example run.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.28-20-g6f8a68f2b)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- working_directory(_,'C:/Users/Groot').
true.
?- [my_app].
true.
?- load_check_without_tests_loaded.
true.
?- load_check_with_tests_loaded.
ERROR: c:/users/groot/my_module_with_tests_loaded.pl:25:
ERROR: No permission to redefine imported_procedure `my_app:my_predicate/1' % ` to rebalance
true.
Just to show that skipping the check load_check_without_tests_loaded
also creates the error.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.28-20-g6f8a68f2b)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- working_directory(_,'C:/Users/Groot').
true.
?- [my_app].
true.
?- load_check_with_tests_loaded.
ERROR: c:/users/groot/my_module_with_tests_loaded.pl:25:
ERROR: No permission to redefine imported_procedure `my_app:my_predicate/1' % ` to rebalance
true.
EDIT
Here is a workaround which just removes the directive from the source code before the source code is loaded.
asserta((system:term_expansion((:-load_test_files(Options)),'')),Ref),
load_files(some_source_file,[imports([])]),
erase(Ref)