swydd/tests/test_operators.py

60 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-10-22 11:53:19 -05:00
from swydd import SwyddPath, asset, get, geterr, pipe, proc, seq, sub
2024-07-22 15:17:58 -05:00
2024-10-22 11:53:19 -05:00
def test_sub():
assert sub("echo 'hello'")
2024-07-22 15:17:58 -05:00
def test_pipes():
2024-10-22 11:53:19 -05:00
assert sub(
pipe("cat ../src/swydd/__init__.py").pipe("grep '__version__'").pipe("wc -l")
2024-07-22 15:17:58 -05:00
)
2024-10-22 11:53:19 -05:00
assert sub(
proc("cat ../src/swydd/__init__.py").pipe("grep '__version__'").pipe("wc -l")
2024-07-22 15:17:58 -05:00
)
def test_seqs():
# -a is not an arg to cat so the subprocess should return false
2024-10-22 11:53:19 -05:00
assert not sub(seq("cat -a").then("echo hello"))
assert not sub(proc("cat -a").then("echo hello"))
2024-07-22 15:17:58 -05:00
def test_capture():
2024-10-22 11:53:19 -05:00
result = get("ls src not-src")
2024-07-22 15:17:58 -05:00
assert result == "src:\nswydd"
2024-10-22 11:53:19 -05:00
result = geterr("ls src not-src")
2024-07-22 15:17:58 -05:00
assert result == "ls: cannot access 'not-src': No such file or directory"
2024-10-22 11:53:19 -05:00
assert "hello part deux" == get(proc("echo 'hello'").then("echo hello part deux"))
commands = proc("cp").then("echo hello part deux")
assert "" == get(commands)
2024-07-22 15:17:58 -05:00
assert "cp: missing file operand\nTry 'cp --help' for more information." == (
2024-10-22 11:53:19 -05:00
geterr(commands)
2024-07-22 15:17:58 -05:00
)
def check_result_file(file: SwyddPath, text: str) -> bool:
if p := file._path:
return p.read_text() == text
return False
def test_write_to_path():
2024-10-22 11:53:19 -05:00
result_f = asset("products/result.txt")
2024-07-22 15:17:58 -05:00
result_txt = "text to file"
2024-10-22 11:53:19 -05:00
result_f.write(result_txt)
2024-07-22 15:17:58 -05:00
assert check_result_file(result_f, result_txt + "\n")
def test_copy_and_rename():
2024-10-22 11:53:19 -05:00
src_f = asset("fixtures/input.txt")
dst_f = asset("products/input.txt")
dst_f.copy(src_f)
2024-07-22 15:17:58 -05:00
assert check_result_file(dst_f, "data to copy to another file\n")
2024-10-22 11:53:19 -05:00
dst_f.rename("products/input2.txt")
2024-07-22 15:17:58 -05:00
assert check_result_file(
2024-10-22 11:53:19 -05:00
asset("products/input2.txt"), "data to copy to another file\n"
2024-07-22 15:17:58 -05:00
)