syrupy-project / syrupy

:pancakes: The sweeter pytest snapshot plugin
https://syrupy-project.github.io/syrupy/
Apache License 2.0
550 stars 36 forks source link

How would I go about saving a snapshot of a directory? #923

Open jcbhmr opened 1 day ago

jcbhmr commented 1 day ago

What's your question?

I am testing a CLI application that operates on files. I am using pytest and pytest-datadir like this:

tests/test_entire_app.py
import subprocess
def test_scenario_one(datadir, snapshot):
    d = datadir / "test_scenario_one"
    p = subprocess.run(["myapp", d], capture_output=True, text=True)
    assert <somehow_load_directory_to_compare(d)> == snapshot # somehow snapshot should save that directory

What I'm doing right now is manually with open(f): data1 = f.read() for each expected output test file. Then all those go in a dict object { "app.ts": appts, "app.js": appjs, ... } which gets mashed into a huge .ambr file. I'd prefer a regular real directory structure if possible.

I want it to result in something like this:

.
└── tests/
    ├── __snapshots__/
    │   └── test_entire_app/
    │       └── test_scenario_one/
    │           ├── app.ts
    │           ├── app.js
    │           ├── utils.ts
    │           └── utils.js
    ├── test_entire_app/
    │   └── test_scenario_one/
    │       ├── app.ts
    │       └── utils.ts
    └── test_entire_app.py

...but I'm not familiar with how to go about actually doing that. I would appreciate some guidance about how to do this. I see that there's some examples of how to use the syrupy.extensions.single_file which seems cool, but I don't know how to go about making an extension that creates a directory with multiple files.