| 1 | import os |
| 2 | from pathlib import Path |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | |
| 7 | @pytest.fixture(autouse=True, scope="module") |
| 8 | def create_long_file_path(): |
| 9 | """ |
| 10 | Create a very long file name to ensure datadir can copy it correctly. |
| 11 | |
| 12 | We don't just create this file in the repository because this makes it |
| 13 | problematic to clone on Windows without LongPaths enabled in the system. |
| 14 | """ |
| 15 | d = Path(__file__).with_suffix("") |
| 16 | old_cwd = os.getcwd() |
| 17 | try: |
| 18 | os.chdir(d) |
| 19 | Path("a" * 250 + ".txt").touch() |
| 20 | finally: |
| 21 | os.chdir(old_cwd) |
| 22 | |
| 23 | |
| 24 | def test_read_hello(datadir): |
| 25 | assert set(os.listdir(str(datadir))) == { |
| 26 | "local_directory", |
| 27 | "hello.txt", |
| 28 | "over.txt", |
| 29 | "a" * 250 + ".txt", |
| 30 | } |
| 31 | with (datadir / "hello.txt").open() as fp: |
| 32 | contents = fp.read() |
| 33 | assert contents == "Hello, world!\n" |
| 34 | |
| 35 | |
| 36 | def test_change_test_files(datadir, original_datadir, shared_datadir, request): |
| 37 | filename = datadir / "hello.txt" |
| 38 | with filename.open("w") as fp: |
| 39 | fp.write("Modified text!\n") |
| 40 | |
| 41 | original_filename = original_datadir / "hello.txt" |
| 42 | with original_filename.open() as fp: |
| 43 | assert fp.read() == "Hello, world!\n" |
| 44 | |
| 45 | with filename.open() as fp: |
| 46 | assert fp.read() == "Modified text!\n" |
| 47 | |
| 48 | shared_filename = shared_datadir / "over.txt" |
| 49 | with shared_filename.open("w") as fp: |
| 50 | fp.write("changed") |
| 51 | shared_original = os.path.join(request.fspath.dirname, "data", "over.txt") |
| 52 | with open(shared_original) as fp: |
| 53 | assert fp.read().strip() == "8000" |
| 54 | |
| 55 | |
| 56 | def test_read_spam_from_other_dir(shared_datadir): |
| 57 | filename = shared_datadir / "spam.txt" |
| 58 | with filename.open() as fp: |
| 59 | contents = fp.read() |
| 60 | assert contents == "eggs\n" |
| 61 | |
| 62 | |
| 63 | def test_file_override(shared_datadir, datadir): |
| 64 | """The same file is in the module dir and global data. |
| 65 | Shared files are kept in a different temp directory""" |
| 66 | shared_filepath = shared_datadir / "over.txt" |
| 67 | private_filepath = datadir / "over.txt" |
| 68 | assert shared_filepath.is_file() |
| 69 | assert private_filepath.is_file() |
| 70 | assert shared_filepath != private_filepath |
| 71 | |
| 72 | |
| 73 | def test_local_directory(datadir): |
| 74 | directory = datadir / "local_directory" |
| 75 | assert directory.is_dir() |
| 76 | filename = directory / "file.txt" |
| 77 | assert filename.is_file() |
| 78 | with filename.open() as fp: |
| 79 | contents = fp.read() |
| 80 | assert contents.strip() == "local contents" |
| 81 | |
| 82 | |
| 83 | def test_shared_directory(shared_datadir): |
| 84 | assert shared_datadir.is_dir() |
| 85 | filename = shared_datadir / "shared_directory" / "file.txt" |
| 86 | assert filename.is_file() |
| 87 | with filename.open() as fp: |
| 88 | contents = fp.read() |
| 89 | assert contents.strip() == "global contents" |