-
Notifications
You must be signed in to change notification settings - Fork 157
Add -t flag for custom toplevel (replaces --halt-on-error) #3147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjtolton
wants to merge
10
commits into
mthom:master
Choose a base branch
from
jjtolton:error-termination-flag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
768f7ce
-t custom toplevel option
jjtolton 15d1124
Add comprehensive tests for -t custom toplevel flag
jjtolton 3124754
Fix bug where -t argument was processed as filename
jjtolton 617a551
Add g_caused_exception/2 for custom toplevel error handling
jjtolton b91ce76
Add comprehensive tests for g_caused_exception/2
jjtolton 95abc40
Update tests to use format/2 instead of write/1
jjtolton e7c288f
Move g_caused_exception/2 dynamic directive to toplevel.pl
jjtolton 5357ecf
Remove redundant unit test file
jjtolton 16fd7a8
Move -t and -g flags before filenames in tests
jjtolton 11d0215
Fix -t flag help text to reflect it accepts any goal
jjtolton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| % Helper predicates for testing custom toplevel functionality | ||
|
|
||
| success_toplevel :- | ||
| format("SUCCESS_TOPLEVEL_EXECUTED~n", []), | ||
| halt(0). | ||
|
|
||
| failure_toplevel :- | ||
| format("FAILURE_TOPLEVEL_EXECUTED~n", []), | ||
| halt(1). | ||
|
|
||
| exit_code_42 :- | ||
| format("EXIT_CODE_42~n", []), | ||
| halt(42). | ||
|
|
||
| write_and_exit :- | ||
| format("Output from custom toplevel~n", []), | ||
| halt(0). | ||
|
|
||
| % This one doesn't halt - to test what happens if toplevel doesn't halt | ||
| non_halting_toplevel :- | ||
| format("NON_HALTING_TOPLEVEL~n", []). | ||
|
|
||
| % Test that toplevel can access loaded predicates | ||
| test_file_loaded :- | ||
| format("LOADED_PREDICATE_CALLED~n", []), | ||
| halt(0). | ||
|
|
||
| helper_predicate :- | ||
| format("Helper predicate works~n", []). | ||
|
|
||
| % g_caused_exception/2 testing predicates | ||
| check_exception_halt_1 :- | ||
| ( '$toplevel':g_caused_exception(Goal, Exception) -> | ||
| format("EXCEPTION_CAUGHT~n", []), | ||
| format("Goal: ~w~n", [Goal]), | ||
| format("Exception: ~w~n", [Exception]), | ||
| halt(1) | ||
| ; format("NO_EXCEPTION~n", []), | ||
| halt(0) | ||
| ). | ||
|
|
||
| check_exception_halt_0 :- | ||
| ( '$toplevel':g_caused_exception(_, _) -> | ||
| format("UNEXPECTED_EXCEPTION~n", []), | ||
| halt(1) | ||
| ; format("SUCCESS_NO_EXCEPTION~n", []), | ||
| halt(0) | ||
| ). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # Custom Toplevel Tests | ||
|
|
||
| ## Basic -t halt functionality | ||
| Test that -t halt prevents entering REPL and exits cleanly | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -t halt | ||
| ``` | ||
|
|
||
| ## -t halt with successful goal | ||
| Test that -t halt exits after running a goal successfully | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "write('Goal executed')" -t halt | ||
| Goal executed | ||
| ``` | ||
|
|
||
| ## -t halt with failing goal | ||
| Test that -t halt still exits even when goal fails | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "fail" -t halt | ||
| % Warning: initialization failed for: fail | ||
|
|
||
| ``` | ||
|
|
||
| ## Custom toplevel with exit code 0 | ||
| Test custom toplevel that exits with code 0 | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -t success_toplevel tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| SUCCESS_TOPLEVEL_EXECUTED | ||
|
|
||
| ``` | ||
|
|
||
| ## Custom toplevel with file loading | ||
| Test that custom toplevel can access predicates from loaded file | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -t test_file_loaded tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| LOADED_PREDICATE_CALLED | ||
|
|
||
| ``` | ||
|
|
||
| ## Custom toplevel with -g goal | ||
| Test combining -g goal with custom toplevel | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "helper_predicate" -t halt tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| Helper predicate works | ||
|
|
||
| ``` | ||
|
|
||
| ## Multiple goals with custom toplevel | ||
| Test multiple -g goals before custom toplevel | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "write('First goal'), nl" -g "write('Second goal'), nl" -t halt tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| First goal | ||
| Second goal | ||
|
|
||
| ``` | ||
|
|
||
| ## File loading then custom toplevel | ||
| Test that files are loaded before toplevel runs | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -t write_and_exit tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| Output from custom toplevel | ||
|
|
||
| ``` | ||
|
|
||
| ## Undefined toplevel predicate | ||
| Test error handling when toplevel predicate doesn't exist | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -t undefined_predicate | ||
| ? failed | ||
| error(existence_error(procedure,undefined_predicate/0),undefined_predicate/0). | ||
|
|
||
| ``` | ||
|
|
||
| ## Test that default behavior unchanged | ||
| Without -t flag, a simple goal should still work (using halt to avoid REPL) | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "write('No custom toplevel'), nl, halt" | ||
| No custom toplevel | ||
|
|
||
| ``` | ||
|
|
||
| ## g_caused_exception/2 with exception thrown | ||
| Test that g_caused_exception/2 is asserted when -g goal throws exception | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "throw(test_error)" -t check_exception_halt_1 tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| ? 1 | ||
| throw(test_error) causes: test_error | ||
| EXCEPTION_CAUGHT | ||
| Goal: throw(test_error) | ||
| Exception: test_error | ||
|
|
||
| ``` | ||
|
|
||
| ## g_caused_exception/2 with no exception | ||
| Test that g_caused_exception/2 is not asserted when -g goal succeeds | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "write('Success')" -t check_exception_halt_0 tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| SuccessSUCCESS_NO_EXCEPTION | ||
|
|
||
| ``` | ||
|
|
||
| ## g_caused_exception/2 with error() term | ||
| Test that g_caused_exception/2 captures error/2 terms correctly | ||
|
|
||
| ```trycmd | ||
| $ scryer-prolog -f --no-add-history -g "throw(error(type_error(integer, foo), context))" -t check_exception_halt_1 tests/scryer/cli/fixtures/toplevel_test_helper.pl | ||
| ? 1 | ||
| throw(error(type_error(integer,foo),context)) causes: error(type_error(integer,foo),context) | ||
| EXCEPTION_CAUGHT | ||
| Goal: throw(error(type_error(integer,foo),context)) | ||
| Exception: error(type_error(integer,foo),context) | ||
|
|
||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a goal, the invoked predicate can have any arity.
For instance, if you only need an error code and not the type of error, you can use:
Does that work in your scripts? In that way, you do not have to implement a custom toplevel for this particular purpose.