| 1034 | |
| 1035 | |
| 1036 | def test_tmpdir(): |
| 1037 | import pytest |
| 1038 | |
| 1039 | td = TmpDir() |
| 1040 | assert td.copy == [] |
| 1041 | env = {} |
| 1042 | td.prepare(env) |
| 1043 | td.prepare(env) # check subsequent call |
| 1044 | assert list(env.keys()) == ["tmpdir"] |
| 1045 | assert td.strpth == env["tmpdir"] |
| 1046 | assert td.pth.exists() |
| 1047 | td.cleanup() |
| 1048 | assert not td.pth.exists() |
| 1049 | |
| 1050 | prevdir = Path().cwd() |
| 1051 | with tempfile.TemporaryDirectory() as otd: |
| 1052 | os.chdir(otd) |
| 1053 | text_pth = Path("my file.txt") |
| 1054 | text_pth.write_text("some\nlines\n") |
| 1055 | bin_pth = Path("file.bits") |
| 1056 | bin_pth.write_bytes(b"\x02\x03\x04\x05") |
| 1057 | td = TmpDir(["my file.txt", "file.bits"]) |
| 1058 | assert td.copy == ["my file.txt", "file.bits"] |
| 1059 | td.prepare(env) |
| 1060 | assert td.pth.exists() |
| 1061 | assert set(pth.name for pth in td.pth.iterdir()) == {"my file.txt", "file.bits"} |
| 1062 | assert text_pth.read_text() == (td.pth / "my file.txt").read_text() |
| 1063 | assert bin_pth.read_bytes() == (td.pth / "file.bits").read_bytes() |
| 1064 | assert td.sub_var("$tmpdir/my file.txt") == f"{td.strpth}/my file.txt" |
| 1065 | assert td.sub_var("${tmpdir}/my file.txt") == "${tmpdir}/my file.txt" |
| 1066 | assert td.sub_var("$tmpdirs/my file.txt") == "$tmpdirs/my file.txt" |
| 1067 | td.cleanup() |
| 1068 | assert not td.pth.exists() |
| 1069 | os.chdir(prevdir) |
| 1070 | |
| 1071 | # errors |
| 1072 | with pytest.raises(ValueError, match="copy must be a list"): |
| 1073 | TmpDir("not a list") |
| 1074 | td = TmpDir(["/not/a/file.txt"]) |
| 1075 | with pytest.raises(ValueError, match="not prepared"): |
| 1076 | td.sub_var("xyz") |
| 1077 | with pytest.raises(FileNotFoundError, match=r"cannot find /not/a/file\.txt"): |
| 1078 | td.prepare({}) |
| 1079 | td = TmpDir(["$DIR/file.txt"]) |
| 1080 | with pytest.raises(FileNotFoundError, match=r"cannot find /nope/file\.txt"): |
| 1081 | td.prepare({"DIR": "/nope"}) |
| 1082 | |
| 1083 | |
| 1084 | def test_file(): |