Compare commits

...

4 Commits

Author SHA1 Message Date
51c0bf4229 Merge pull request 'fix: support empty string literals in textfx' (#63) from pi-agent/konabot:fix/textfx-empty-string into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #63
2026-03-18 19:41:34 +08:00
5b1c6d446c fix: remove redundant whitespace check; echo no-arg outputs empty line 2026-03-18 19:40:02 +08:00
717b7a95e8 fix: echo should not read stdin (Unix semantics) 2026-03-18 19:30:55 +08:00
9bac2b8cdf fix: support empty string literals in textfx
- Fix tokenizer to emit empty string token when closing quote on empty buffer
- Add force parameter to flush_word() to handle empty quoted strings
- Add test case for echo "" and echo ''
2026-03-18 19:23:42 +08:00
3 changed files with 24 additions and 7 deletions

View File

@ -161,9 +161,9 @@ class PipelineRunner:
"'": "'",
}
def flush_word():
def flush_word(force: bool = False):
nonlocal buf
if buf:
if buf or force:
tokens.append(Token(TokenKind.WORD, buf))
buf = ""
@ -178,6 +178,7 @@ class PipelineRunner:
escape = True
elif c == quote:
quote = None
flush_word(force=True) # 引号闭合时强制 flush即使是空字符串
else:
buf += c
i += 1
@ -188,7 +189,7 @@ class PipelineRunner:
i += 1
continue
if c.isspace() or c in "":
if c.isspace():
flush_word()
i += 1
continue

View File

@ -13,10 +13,8 @@ class THEcho(TextHandler):
async def handle(
self, env: TextHandlerEnvironment, istream: str | None, args: list[str]
) -> TextHandleResult:
if len(args) == 0 and istream is None:
return TextHandleResult(1, "请在 echo 后面添加需要输出的文本")
if istream is not None:
return TextHandleResult(0, "\n".join([istream] + args))
# echo 不读 stdin只输出参数Unix 语义)
# 无参数时输出空行(与 Unix echo 行为一致)
return TextHandleResult(0, "\n".join(args))

View File

@ -205,3 +205,21 @@ async def test_while_body_can_use_if(runner: PipelineRunner):
assert not isinstance(parsed, str)
results = await runner.run_pipeline(parsed, None, TextHandlerEnvironment(False))
assert results[0].code == 1
@pytest.mark.asyncio
async def test_echo_empty_string(runner: PipelineRunner):
"""测试 echo 空字符串"""
# 双引号空字符串
parsed = runner.parse_pipeline('echo ""')
assert not isinstance(parsed, str)
results = await runner.run_pipeline(parsed, None, TextHandlerEnvironment(False))
assert results[0].code == 0
assert results[0].ostream == ''
# 单引号空字符串
parsed2 = runner.parse_pipeline("echo ''")
assert not isinstance(parsed2, str)
results2 = await runner.run_pipeline(parsed2, None, TextHandlerEnvironment(False))
assert results2[0].code == 0
assert results2[0].ostream == ''