Skip to content

Commit 3c9c615

Browse files
da-liiiclaude
andcommitted
[0011] Auto-route tests directories to test command
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent a4d6ec3 commit 3c9c615

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

devel/0011.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [0011] gf CLI 自动路由 tests 目录到 test 命令
2+
3+
## 任务相关的代码文件
4+
- `src/goldfish.hpp`
5+
- `tests/goldfish/liii/goldtest-test.scm`
6+
7+
## 如何测试
8+
```bash
9+
# 1. 构建
10+
xmake b goldfish
11+
12+
# 2. 运行测试
13+
bin/gf tests/goldfish/liii/goldtest-test.scm
14+
15+
# 3. Adhoc 测试
16+
bin/gf tests/scheme
17+
# 预期行为:等价于 bin/gf test tests/scheme
18+
```
19+
20+
## 2026-05-07 gf CLI 自动路由 tests 目录到 test 命令
21+
### What
22+
`bin/gf` 检测到输入是一个目录,且该目录路径的第一级文件夹是 `tests` 时,会运行如下命令:
23+
24+
```
25+
bin/gf test <输入目录>
26+
```
27+
28+
例如 `bin/gf tests/scheme` 会自动路由为 `bin/gf test tests/scheme`
29+
30+
具体修改:
31+
1.`src/goldfish.hpp``repl_for_community_edition` 函数中,在命令路由逻辑前添加自动检测逻辑
32+
2. 检测条件:
33+
- `command` 不是内置命令(help, version, eval, load, repl, run, test 等)
34+
- `command` 对应的路径是一个存在的目录
35+
- 目录路径的第一级文件夹是 `tests`(例如 `tests/scheme`, `tests/goldfish/liii/string`
36+
3. 满足条件时,将 `command` 设为 `"test"`,并在 `command_args` 的对应位置插入 `"test"`,使 Scheme 侧看到的参数列表等价于 `gf test tests/...`
37+
38+
### Why
39+
提升 CLI 的易用性。用户习惯性地直接输入 `bin/gf tests/scheme` 来运行测试,而不需要显式输入 `test` 子命令。这符合直觉,因为 `tests` 目录下的内容天然与测试相关。
40+
41+
### How
42+
在 C++ 侧实现路由逻辑:
43+
- 使用 `std::filesystem::is_directory` 判断路径是否为目录
44+
- 使用字符串分割判断路径第一级是否为 `tests`
45+
- 通过修改 `command_args` vector,在原有路径参数前插入 `"test"`,确保 Scheme 侧的 `(command-line)` 返回正确的参数列表
46+
- 保持与现有 `command == "-e"` 时修改 `command_args` 的模式一致

src/goldfish.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5271,6 +5271,30 @@ repl_for_community_edition (s7_scheme* sc, int argc, char** argv) {
52715271
}
52725272
}
52735273

5274+
// 自动路由:如果参数是目录且第一级文件夹是 tests,自动视为 test 命令
5275+
if (!command.empty () && command != "help" && command != "version" &&
5276+
command != "eval" && command != "load" && command != "repl" &&
5277+
command != "run" && command != "test" && command != "-e") {
5278+
std::error_code ec;
5279+
if (fs::is_directory (command, ec)) {
5280+
fs::path p (command);
5281+
auto it= p.begin ();
5282+
if (it != p.end () && *it == "tests") {
5283+
if (command_index >= 0 && command_index <= static_cast<int> (command_args.size ())) {
5284+
command_args.insert (command_args.begin () + command_index, "test");
5285+
}
5286+
command= "test";
5287+
std::cerr << "[gf] Auto-routing: detected tests directory, routing to test command" << "\n";
5288+
std::cerr << "[gf] Executing: ";
5289+
for (size_t i= 0; i < command_args.size (); ++i) {
5290+
if (i > 0) std::cerr << " ";
5291+
std::cerr << command_args[i];
5292+
}
5293+
std::cerr << std::endl;
5294+
}
5295+
}
5296+
}
5297+
52745298
// 根据命令类型确定默认模式:
52755299
// - repl/load 命令默认使用 liii 模式
52765300
// - 其他命令(eval, run, 直接执行脚本)默认使用 r7rs 模式

0 commit comments

Comments
 (0)