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 ''
This commit is contained in:
2026-03-18 19:23:42 +08:00
parent bfb8ebab29
commit 9bac2b8cdf
2 changed files with 22 additions and 3 deletions

View File

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

View File

@ -205,3 +205,21 @@ async def test_while_body_can_use_if(runner: PipelineRunner):
assert not isinstance(parsed, str) assert not isinstance(parsed, str)
results = await runner.run_pipeline(parsed, None, TextHandlerEnvironment(False)) results = await runner.run_pipeline(parsed, None, TextHandlerEnvironment(False))
assert results[0].code == 1 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 == ''