from mu4web.util import ( MutableString, chain, cwd, find, force, split_path, ) import os from tempfile import TemporaryDirectory from math import sqrt import pytest def test_split_path() -> None: assert split_path("/home/hugo/test/something") == ['home', 'hugo', 'test', 'something'] def test_find(maildir: str) -> None: assert sorted([split_path(p)[-3:] for p in find(maildir, type='f')]) == [ [b'mail', b'cur', b'1691189956.3013485_5.gandalf:2,RS'], [b'mail', b'cur', b'1691189958.3013485_7.gandalf:2,S'], [b'mail', b'cur', b'1691314589.3192599_3.gandalf:2,S']] assert sorted([split_path(p)[-1] for p in find(maildir, type='d')]) \ == [b'cur', b'mail', b'new', b'tmp'] def test_cwd() -> None: outer = os.getcwd() with TemporaryDirectory() as dir: with cwd(dir): assert os.getcwd() == dir assert outer == os.getcwd() def test_chain() -> None: assert (chain(range(9)) @ sum # type: ignore @ sqrt).value == 6.0 # type: ignore def test_force() -> None: assert force(1) == 1 with pytest.raises(AssertionError): force(None) def test_mutable_string() -> None: def inner(s: str | MutableString, msg: str) -> None: s += msg s1: str = '' s2: MutableString = MutableString() inner(s1, "Hello") inner(s1, "World") inner(s2, "Hello") inner(s2, "World") # Control with regular string, to ensure that we actually mutate # the variable value, and not just the variable. assert s1 == '' assert str(s2) == "HelloWorld" assert repr(s2) == 'MutableString("HelloWorld")'