39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
"""Tests for ops.system.writefile 模块 (write_file_run 函数)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from pyflowx.ops.system.writefile import write_file_run
|
|
|
|
|
|
class TestWriteFileRun:
|
|
"""``write_file_run`` 函数测试."""
|
|
|
|
def test_write_file_writes_content(self, tmp_path: Path) -> None:
|
|
"""write_file_run 应将内容写入指定文件."""
|
|
f = tmp_path / "out.txt"
|
|
write_file_run(str(f), "hello world")
|
|
assert f.read_text(encoding="utf-8") == "hello world"
|
|
|
|
def test_write_file_with_encoding(self, tmp_path: Path) -> None:
|
|
"""write_file_run 应支持指定编码."""
|
|
f = tmp_path / "out.txt"
|
|
write_file_run(str(f), "中文", encoding="utf-8")
|
|
assert f.read_text(encoding="utf-8") == "中文"
|
|
|
|
def test_write_file_overwrites_existing(self, tmp_path: Path) -> None:
|
|
"""write_file_run 应覆盖已有文件."""
|
|
f = tmp_path / "out.txt"
|
|
f.write_text("old content", encoding="utf-8")
|
|
write_file_run(str(f), "new content")
|
|
assert f.read_text(encoding="utf-8") == "new content"
|
|
|
|
def test_write_file_failure_propagates(self, tmp_path: Path) -> None:
|
|
"""write_file_run 写入失败应抛出异常 (不吞异常)."""
|
|
missing = tmp_path / "no_such_dir" / "out.txt"
|
|
with pytest.raises(FileNotFoundError):
|
|
write_file_run(str(missing), "x")
|