File: test_build__trim_file.py

package info (click to toggle)
python-briefcase 0.3.22-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,296 kB
  • sloc: python: 59,405; makefile: 57
file content (119 lines) | stat: -rw-r--r-- 3,813 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import pytest

from briefcase.console import Console
from briefcase.platforms.web.static import StaticWebBuildCommand

from ....utils import create_file


@pytest.fixture
def build_command(tmp_path):
    return StaticWebBuildCommand(
        console=Console(),
        base_path=tmp_path / "base_path",
        data_path=tmp_path / "briefcase",
    )


def test_trim_file(build_command, tmp_path):
    """A file can be trimmed at a sentinel."""
    filename = tmp_path / "dummy.txt"
    content = [
        "This is before the sentinel.",
        "This is also before the sentinel.",
        " ** This is the sentinel ** ",
        "This is after the sentinel.",
        "This is also after the sentinel.",
    ]

    create_file(filename, "\n".join(content))

    # Trim the file at the sentinel
    build_command._trim_file(filename, sentinel=" ** This is the sentinel ** ")

    # The file contains everything up to and including the sentinel.
    with filename.open(encoding="utf-8") as f:
        assert f.read() == "\n".join(content[:3]) + "\n"


def test_trim_no_sentinel(build_command, tmp_path):
    """A file that doesn't contain the sentinel is returned as-is."""
    filename = tmp_path / "dummy.txt"
    content = [
        "This is before the sentinel.",
        "This is also before the sentinel.",
        "NO SENTINEL HERE",
        "This is after the sentinel.",
        "This is also after the sentinel.",
    ]

    create_file(filename, "\n".join(content))

    # Trim the file at a sentinel
    build_command._trim_file(filename, sentinel=" ** This is the sentinel ** ")

    # The file is unmodified.
    with filename.open(encoding="utf-8") as f:
        assert f.read() == "\n".join(content)


def test_trim_file_multiple_sentinels(build_command, tmp_path):
    """A file with multiple sentinels is trimmed at the first one."""
    filename = tmp_path / "dummy.txt"
    content = [
        "This is before the sentinel.",
        "This is also before the sentinel.",
        " ** This is the sentinel ** ",
        "This is after the first sentinel.",
        "This is also after the first sentinel.",
        " ** This is the sentinel ** ",
        "This is after the second sentinel.",
        "This is also after the second sentinel.",
    ]

    create_file(filename, "\n".join(content))

    # Trim the file at the sentinel
    build_command._trim_file(filename, sentinel=" ** This is the sentinel ** ")

    # The file contains everything up to and including the sentinel.
    with filename.open(encoding="utf-8") as f:
        assert f.read() == "\n".join(content[:3]) + "\n"


def test_trim_sentinel_last_line(build_command, tmp_path):
    """A file with the sentinel as the last full line isn't a problem."""
    filename = tmp_path / "dummy.txt"
    content = [
        "This is before the sentinel.",
        "This is also before the sentinel.",
        " ** This is the sentinel ** ",
    ]

    create_file(filename, "\n".join(content) + "\n")

    # Trim the file at a sentinel
    build_command._trim_file(filename, sentinel=" ** This is the sentinel ** ")

    # The file is unmodified.
    with filename.open(encoding="utf-8") as f:
        assert f.read() == "\n".join(content) + "\n"


def test_trim_sentinel_EOF(build_command, tmp_path):
    """A file with the sentinel at EOF isn't a problem."""
    filename = tmp_path / "dummy.txt"
    content = [
        "This is before the sentinel.",
        "This is also before the sentinel.",
        " ** This is the sentinel ** ",
    ]

    create_file(filename, "\n".join(content))

    # Trim the file at a sentinel
    build_command._trim_file(filename, sentinel=" ** This is the sentinel ** ")

    # The file is unmodified.
    with filename.open(encoding="utf-8") as f:
        assert f.read() == "\n".join(content)