### Using shared_datadir Fixture for Global Test Data (Python) Source: https://context7.com/gabrielcnr/pytest-datadir/llms.txt Illustrates the usage of the `shared_datadir` fixture for accessing a global data directory shared across all tests. This is beneficial for common test assets. The example shows reading files and accessing subdirectories from this shared location. ```python from pathlib import Path def test_read_shared_data(shared_datadir: Path) -> None: """Read files from the global shared data directory.""" filename = shared_datadir / "spam.txt" with filename.open() as fp: contents = fp.read() assert contents == "eggs\n" def test_access_shared_subdirectories(shared_datadir: Path) -> None: """Access subdirectories in the shared data directory.""" assert shared_datadir.is_dir() filename = shared_datadir / "shared_directory" / "file.txt" assert filename.is_file() with filename.open() as fp: contents = fp.read() assert contents.strip() == "global contents" def test_file_override(shared_datadir: Path, datadir: Path) -> None: """Handle files with same name in both shared and module directories.""" # When the same filename exists in both directories, # they are kept separate in different temp directories shared_filepath = shared_datadir / "over.txt" private_filepath = datadir / "over.txt" assert shared_filepath.is_file() assert private_filepath.is_file() assert shared_filepath != private_filepath # Different paths ``` -------------------------------- ### Demonstrate lazy copying with lazy_datadir fixture Source: https://context7.com/gabrielcnr/pytest-datadir/llms.txt This snippet demonstrates the lazy copying behavior of the `lazy_datadir` fixture. Files are only copied to the temporary directory when they are accessed using the `/` operator or `joinpath` method. This improves test performance by avoiding unnecessary file operations. It shows how accessing a file triggers its copy, how modifications persist, and how new files can be created. ```python from pathlib import Path from pytest_datadir.plugin import LazyDataDir def test_lazy_copy_behavior(lazy_datadir: LazyDataDir) -> None: """Demonstrate lazy copying - files are copied only when accessed.""" # The temporary directory starts empty assert {x.name for x in lazy_datadir.tmp_path.iterdir()} == set() # Access a file - this triggers the copy hello = lazy_datadir / "hello.txt" assert {x.name for x in lazy_datadir.tmp_path.iterdir()} == {"hello.txt"} assert hello.read_text() == "Hello, world!\n" # Modify the file - subsequent access returns modified content hello.write_text("Hello world, hello world.") hello = lazy_datadir / Path("hello.txt") # Also works with Path objects assert hello.read_text() == "Hello world, hello world." def test_lazy_copy_directories(lazy_datadir: LazyDataDir) -> None: """Lazy copy entire directories when accessed.""" # Access a directory - copies the entire directory tree local_dir = lazy_datadir / "local_directory" assert local_dir.is_dir() is True assert local_dir.joinpath("file.txt").read_text() == "local contents" def test_lazy_create_new_files(lazy_datadir: LazyDataDir) -> None: """Create new files that don't exist in the original data directory.""" # Accessing a non-existent file doesn't fail - it returns a path fn = lazy_datadir / "new-file.txt" assert fn.exists() is False # Write to the new file fn.write_text("new contents") assert fn.exists() is True def test_lazy_copy_subdirectory_file(lazy_datadir: LazyDataDir) -> None: """Access files in subdirectories directly.""" # Accessing a file in a subdirectory copies the parent directory fn = lazy_datadir / "local_directory/file.txt" assert fn.read_text() == "local contents" ``` -------------------------------- ### Using datadir Fixture for Module-Specific Test Data (Python) Source: https://context7.com/gabrielcnr/pytest-datadir/llms.txt Demonstrates how to use the `datadir` fixture to access and modify module-specific test data. Files are copied to a temporary directory, ensuring that original data remains untouched. This fixture is ideal for data unique to a particular test module. ```python from pathlib import Path def test_read_hello(datadir: Path) -> None: """Read a file from the module-specific data directory.""" with (datadir / "hello.txt").open() as fp: contents = fp.read() assert contents == "Hello, world!\n" def test_modify_files_safely(datadir: Path, original_datadir: Path) -> None: """Modify test files without affecting originals.""" filename = datadir / "hello.txt" # Write to the temporary copy with filename.open("w") as fp: fp.write("Modified text!\n") # Verify the original is unchanged original_filename = original_datadir / "hello.txt" with original_filename.open() as fp: assert fp.read() == "Hello, world!\n" # Verify our modification with filename.open() as fp: assert fp.read() == "Modified text!\n" def test_access_subdirectories(datadir: Path) -> None: """Access files in subdirectories within the data directory.""" directory = datadir / "local_directory" assert directory.is_dir() filename = directory / "file.txt" assert filename.is_file() with filename.open() as fp: contents = fp.read() assert contents.strip() == "local contents" ``` -------------------------------- ### Compare original data with temporary copy using original_datadir fixture Source: https://context7.com/gabrielcnr/pytest-datadir/llms.txt This snippet illustrates the use of the `original_datadir` fixture, which provides read-only access to the original data directory without making a copy. This is useful for comparing the original files with modified versions in the temporary `datadir` or for verifying that operations on the temporary data do not affect the source. The test demonstrates modifying a file in `datadir` and asserting that the corresponding file in `original_datadir` remains unchanged. ```python from pathlib import Path def test_compare_original_and_copy(datadir: Path, original_datadir: Path) -> None: """Compare original data with the temporary copy.""" # Modify the temporary copy temp_file = datadir / "hello.txt" temp_file.write_text("Modified content") # Original remains unchanged original_file = original_datadir / "hello.txt" assert original_file.read_text() == "Hello, world!\n" assert temp_file.read_text() == "Modified content" ``` -------------------------------- ### Read Global Data Directory with shared_datadir Fixture Source: https://github.com/gabrielcnr/pytest-datadir/blob/master/README.md Demonstrates how to read the content of a file from the global 'data' directory using the 'shared_datadir' fixture. This fixture provides a pathlib.Path object representing the shared data directory. ```python def test_read_global(shared_datadir): contents = (shared_datadir / "hello.txt").read_text() assert contents == "Hello World!\n" ``` -------------------------------- ### Read Module-Specific Data Directory with datadir Fixture Source: https://github.com/gabrielcnr/pytest-datadir/blob/master/README.md Shows how to read the content of a file from a module-specific data directory using the 'datadir' fixture. This fixture provides a pathlib.Path object for the module's data directory. ```python def test_read_module(datadir): contents = (datadir / "spam.txt").read_text() assert contents == "eggs\n" ``` -------------------------------- ### Trigger GitHub Deploy Workflow Source: https://github.com/gabrielcnr/pytest-datadir/blob/master/RELEASING.rst Manually triggers the 'deploy.yml' GitHub workflow for a specific release branch and version. This is typically done after tests pass on the release branch. ```bash gh workflow run deploy.yml --ref release-VERSION --field version=VERSION ``` -------------------------------- ### Lazy Read Module Data with lazy_datadir Fixture Source: https://github.com/gabrielcnr/pytest-datadir/blob/master/README.md Illustrates using the 'lazy_datadir' fixture to read file content. This fixture lazily copies data only when accessed, improving performance for tests that don't need all data upfront. It mimics 'datadir' functionality but with lazy loading. ```python def test_read_module(lazy_datadir): contents = (lazy_datadir / "spam.txt").read_text() assert contents == "eggs\n" ``` -------------------------------- ### Lazy Read Shared Data with lazy_shared_datadir Fixture Source: https://github.com/gabrielcnr/pytest-datadir/blob/master/README.md Demonstrates the use of 'lazy_shared_datadir' for lazily accessing files from the global data directory. Similar to 'lazy_datadir', it copies files only when they are accessed via 'joinpath' or the '/' operator. ```python def test_read_global(lazy_shared_datadir): contents = (lazy_shared_datadir / "hello.txt").read_text() assert contents == "Hello World!\n" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.