import re
from pathlib import Path

from clarabot_nano.utility.file import temporary_file_by_target
from clarabot_nano.utility.path.validate import normalize_absolute_path
from clarabot_nano_tests.testing.file import temp_path


def test_temporary_file_by_target(tmpdir):
    root = Path(normalize_absolute_path(temp_path(tmpdir)))
    root.mkdir()

    for name, prefix, result_re in [
        ("asdf", "XYZ", r"^\.~XYZ\.asdf\.\w+#$"),
        ("asdf.txt", "XYZ", r"^\.~XYZ\.asdf\.\w+\.txt#$"),
        ("asdf.yes.txt", "XYZ", r"^\.~XYZ\.asdf\.yes\.\w+\.txt#$"),
    ]:
        f = root / name
        tmp = temporary_file_by_target(f, prefix)
        assert (root / tmp).is_file()
        assert re.match(result_re, tmp) is not None
