File: test_snapshot_option_extension_pythonpath.py

package info (click to toggle)
python-syrupy 4.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,368 kB
  • sloc: python: 5,978; makefile: 3
file content (101 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import textwrap
from pathlib import Path

import pytest

import syrupy

SUBDIR = "subdir_not_on_default_path"


@pytest.fixture(autouse=True)
def cache_clear():
    syrupy.__import_extension.cache_clear()


@pytest.fixture
def testfile(pytester):
    subdir = pytester.mkpydir(SUBDIR)

    Path(
        subdir,
        "extension_file.py",
    ).write_text(
        data=textwrap.dedent(
            """
            from syrupy.extensions.single_file import SingleFileSnapshotExtension
            class MySingleFileExtension(SingleFileSnapshotExtension):
                pass
            """
        ),
        encoding="utf-8",
    )

    pytester.makepyfile(
        test_file=(
            """
            def test_default(snapshot):
                assert b"default extension serializer" == snapshot
            """
        )
    )

    return pytester


def test_snapshot_default_extension_option_success(testfile):
    testfile.makeini(
        f"""
        [pytest]
        pythonpath =
            {Path(testfile.path, SUBDIR).as_posix()}
    """
    )

    result = testfile.runpytest(
        "-v",
        "--snapshot-update",
        "--snapshot-default-extension",
        "extension_file.MySingleFileExtension",
    )
    result.stdout.re_match_lines((r"1 snapshot generated\.",))
    assert Path(
        testfile.path, "__snapshots__", "test_file", "test_default.raw"
    ).exists()
    assert not result.ret


def test_snapshot_default_extension_option_module_not_found(testfile):
    result = testfile.runpytest(
        "-v",
        "--snapshot-update",
        "--snapshot-default-extension",
        "extension_file.MySingleFileExtension",
    )
    result.stdout.re_match_lines((r".*: Module 'extension_file' does not exist.*",))
    assert not Path(
        testfile.path, "__snapshots__", "test_file", "test_default.raw"
    ).exists()
    assert result.ret


def test_snapshot_default_extension_option_failure(testfile):
    testfile.makeini(
        f"""
        [pytest]
        pythonpath =
            {Path(testfile.path, SUBDIR).as_posix()}
    """
    )

    result = testfile.runpytest(
        "-v",
        "--snapshot-update",
        "--snapshot-default-extension",
        "extension_file.DoesNotExistExtension",
    )
    result.stdout.re_match_lines((r".*: Member 'DoesNotExistExtension' not found.*",))
    assert not Path(
        testfile.path, "__snapshots__", "test_file", "test_default.raw"
    ).exists()
    assert result.ret