Skip to content

Commit 92f2cc0

Browse files
authored
fix: CLI error reporting for cmd? and cd with absolute paths (#129)
* feat: add CLI robustness test suite Add comprehensive automated test suite for milk-cli: - tests/cli/cli_robustness_tests.milk: 109 annotated test cases covering basic parsing, help/discovery, builtins, missing/wrong arguments, variables, arithmetic, flow control, user functions, command chaining, shell features (pipes, redirects, here-strings, command substitution), error conditions, set flags, readonly/declare. - tests/cli/run_cli_robustness_tests.sh: Bash test runner that feeds tests one-by-one to milk-cli, captures output, checks for crashes/hangs/missing error messages, and prints a summary report. - .agents/workflows/cli-robustness-test.md: Workflow for running the test suite. Initial run: 105 pass, 4 MISSING_ERROR (no error message printed for: cmd? with unknown command, cd to nonexistent directory, mem.mk2Dim with no args, mem.rm with no args). * fix: CLI error reporting for cmd? and cd with absolute paths Fix two CLI error reporting bugs found by the robustness test suite: 1. help_command() (cmd?) now returns RETURN_FAILURE when the queried command is not found. Previously it always returned RETURN_SUCCESS even when printing "does not exist", misleading the caller. 2. Extend tokenizer raw-string bypass to treat arguments starting with '/' as raw strings (same as '-' prefixed arguments). This fixes cd /absolute/path where the '/' was misinterpreted as division by cli_parse(), causing a silent parse error that skipped command execution entirely. Update test expectations: mem.mk2Dim and mem.rm with no arguments correctly create a local FPS with defaults — this is expected FPS behavior, not an error. Also add 'does not exist' to the runner grep pattern for error detection.
1 parent 5959de7 commit 92f2cc0

5 files changed

Lines changed: 1000 additions & 8 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
description: Run the CLI robustness test suite
3+
---
4+
5+
# CLI Robustness Test Suite
6+
7+
Run this workflow to verify that `milk-cli` correctly
8+
handles valid commands, invalid commands, missing arguments,
9+
and scripting constructs — without crashing, hanging, or
10+
silently failing.
11+
12+
## Steps
13+
14+
// turbo-all
15+
16+
1. Run the test suite from the repository root:
17+
18+
```bash
19+
cd /home/oguyon/src/milk && bash tests/cli/run_cli_robustness_tests.sh --verbose
20+
```
21+
22+
2. Review the summary output. Categorized results:
23+
- **PASS** — command worked as expected
24+
- **FAIL** — expected success but got error exit
25+
- **MISSING_ERROR** — expected error message but
26+
none was printed
27+
- **CRASH** — process killed by signal (segfault etc.)
28+
- **HANG** — command timed out
29+
30+
3. If any failures, examine the detailed report at:
31+
`tests/cli/cli_test_report.txt`
32+
33+
4. Fix issues in the CLI code, then re-run step 1.
34+
35+
5. When adding new CLI features or changing syntax,
36+
update `tests/cli/cli_robustness_tests.milk` with
37+
matching test cases.
38+
39+
## Test File Format
40+
41+
Each test block in `cli_robustness_tests.milk` has:
42+
43+
```
44+
#DESC: Short description
45+
#EXPECT:OK (or #EXPECT:ERR)
46+
command_line_1
47+
command_line_2 (for multi-line constructs)
48+
```
49+
50+
## Adding New Tests
51+
52+
1. Add a `#DESC:` + `#EXPECT:` annotation pair
53+
2. Follow with the command(s) to test
54+
3. Use `#EXPECT:ERR` for commands that should print
55+
an error message
56+
4. For multi-line constructs (if/while/for/function),
57+
include all lines in the block

src/cli/CLIcore/CLIcore/CLIcore_UI.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,16 +2944,21 @@ errno_t CLI_execute_line()
29442944

29452945
/* When a CLI command has already been
29462946
* identified and the current argument
2947-
* starts with '-', treat it as a raw
2948-
* string rather than running it through
2949-
* the arithmetic expression parser.
2950-
* This prevents flags like -n, -g, --since
2951-
* from triggering a parse error that would
2952-
* block the command from being called. */
2947+
* starts with '-' or '/', treat it as
2948+
* a raw string rather than running it
2949+
* through the arithmetic expression
2950+
* parser.
2951+
* '-' prevents flags like -n, -g,
2952+
* --since from triggering a parse
2953+
* error.
2954+
* '/' prevents absolute paths from
2955+
* being misinterpreted as division
2956+
* (e.g. cd /tmp). */
29532957
if(data.cmdNBarg > 0
29542958
&& data.cmdargtoken[0].type
29552959
== CMDARGTOKEN_TYPE_COMMAND
2956-
&& cmdargstring[0] == '-')
2960+
&& (cmdargstring[0] == '-'
2961+
|| cmdargstring[0] == '/'))
29572962
{
29582963
strncpy(
29592964
data.cmdargtoken[data.cmdNBarg]

src/cli/CLIcore/CLIcore/CLIcore_help.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,9 +1100,13 @@ errno_t help_command(
11001100
{
11011101
if(foundregexmatch == 0)
11021102
{
1103-
printf("\tNo substring or regex match to \"%s\"\n", cmdkey);
1103+
printf(
1104+
"\tNo substring or regex "
1105+
"match to \"%s\"\n",
1106+
cmdkey);
11041107
}
11051108
}
1109+
return RETURN_FAILURE;
11061110
}
11071111

11081112
return RETURN_SUCCESS;

0 commit comments

Comments
 (0)