File: test_command_startproject.py

package info (click to toggle)
python-scrapy 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,308 kB
  • sloc: python: 55,321; xml: 199; makefile: 25; sh: 7
file content (292 lines) | stat: -rw-r--r-- 10,287 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import annotations

import os
import subprocess
import sys
from contextlib import contextmanager
from itertools import chain
from pathlib import Path
from shutil import copytree
from stat import S_IWRITE as ANYONE_WRITE_PERMISSION

import scrapy
from scrapy.commands.startproject import IGNORE
from scrapy.utils.test import get_testenv
from tests.utils.cmdline import call, proc


class TestStartprojectCommand:
    project_name = "testproject"

    @staticmethod
    def _assert_files_exist(project_dir: Path, project_name: str) -> None:
        assert (project_dir / "scrapy.cfg").exists()
        assert (project_dir / project_name).exists()
        assert (project_dir / project_name / "__init__.py").exists()
        assert (project_dir / project_name / "items.py").exists()
        assert (project_dir / project_name / "pipelines.py").exists()
        assert (project_dir / project_name / "settings.py").exists()
        assert (project_dir / project_name / "spiders" / "__init__.py").exists()

    def test_startproject(self, tmp_path: Path) -> None:
        # with no dir argument creates the project in the "self.project_name" subdir of cwd
        assert call("startproject", self.project_name, cwd=tmp_path) == 0
        self._assert_files_exist(tmp_path / self.project_name, self.project_name)

        assert call("startproject", self.project_name, cwd=tmp_path) == 1
        assert call("startproject", "wrong---project---name") == 1
        assert call("startproject", "sys") == 1

    def test_startproject_with_project_dir(self, tmp_path: Path) -> None:
        # with a dir arg creates the project in the specified dir
        project_dir = tmp_path / "project"
        assert (
            call("startproject", self.project_name, str(project_dir), cwd=tmp_path) == 0
        )
        self._assert_files_exist(project_dir, self.project_name)

        assert (
            call(
                "startproject", self.project_name, str(project_dir) + "2", cwd=tmp_path
            )
            == 0
        )

        assert (
            call("startproject", self.project_name, str(project_dir), cwd=tmp_path) == 1
        )
        assert (
            call(
                "startproject", self.project_name + "2", str(project_dir), cwd=tmp_path
            )
            == 1
        )
        assert call("startproject", "wrong---project---name") == 1
        assert call("startproject", "sys") == 1
        assert call("startproject") == 2
        assert (
            call("startproject", self.project_name, str(project_dir), "another_params")
            == 2
        )

    def test_existing_project_dir(self, tmp_path: Path) -> None:
        project_name = self.project_name + "_existing"
        project_path = tmp_path / project_name
        project_path.mkdir()

        assert call("startproject", project_name, cwd=tmp_path) == 0
        self._assert_files_exist(project_path, project_name)


def get_permissions_dict(
    path: str | os.PathLike, renamings=None, ignore=None
) -> dict[str, str]:
    def get_permissions(path: Path) -> str:
        return oct(path.stat().st_mode)

    path_obj = Path(path)

    renamings = renamings or ()
    permissions_dict = {
        ".": get_permissions(path_obj),
    }
    for root, dirs, files in os.walk(path_obj):
        nodes = list(chain(dirs, files))
        if ignore:
            ignored_names = ignore(root, nodes)
            nodes = [node for node in nodes if node not in ignored_names]
        for node in nodes:
            absolute_path = Path(root, node)
            relative_path = str(absolute_path.relative_to(path))
            for search_string, replacement in renamings:
                relative_path = relative_path.replace(search_string, replacement)
            permissions = get_permissions(absolute_path)
            permissions_dict[relative_path] = permissions
    return permissions_dict


class TestStartprojectTemplates:
    def test_startproject_template_override(self, tmp_path: Path) -> None:
        tmpl = tmp_path / "templates"
        tmpl_proj = tmpl / "project"
        project_name = "testproject"

        copytree(Path(scrapy.__path__[0], "templates"), tmpl)
        (tmpl_proj / "root_template").write_bytes(b"")

        args = ["--set", f"TEMPLATES_DIR={tmpl}"]
        _, out, _ = proc("startproject", project_name, *args, cwd=tmp_path)
        assert f"New Scrapy project '{project_name}', using template directory" in out
        assert str(tmpl_proj) in out
        assert (tmp_path / project_name / "root_template").exists()

    def test_startproject_permissions_from_writable(self, tmp_path: Path) -> None:
        """Check that generated files have the right permissions when the
        template folder has the same permissions as in the project, i.e.
        everything is writable."""
        scrapy_path = scrapy.__path__[0]
        project_template = Path(scrapy_path, "templates", "project")
        project_name = "startproject1"
        renamings = (
            ("module", project_name),
            (".tmpl", ""),
        )
        expected_permissions = get_permissions_dict(
            project_template,
            renamings,
            IGNORE,
        )

        destination = tmp_path / "proj"
        destination.mkdir()
        process = subprocess.Popen(
            (
                sys.executable,
                "-m",
                "scrapy.cmdline",
                "startproject",
                project_name,
            ),
            cwd=destination,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            env=get_testenv(),
        )
        process.wait()

        project_dir = destination / project_name
        actual_permissions = get_permissions_dict(project_dir)

        assert actual_permissions == expected_permissions

    def test_startproject_permissions_from_read_only(self, tmp_path: Path) -> None:
        """Check that generated files have the right permissions when the
        template folder has been made read-only, which is something that some
        systems do.

        See https://github.com/scrapy/scrapy/pull/4604
        """
        scrapy_path = scrapy.__path__[0]
        templates_dir = Path(scrapy_path, "templates")
        project_template = Path(templates_dir, "project")
        project_name = "startproject2"
        renamings = (
            ("module", project_name),
            (".tmpl", ""),
        )
        expected_permissions = get_permissions_dict(
            project_template,
            renamings,
            IGNORE,
        )

        def _make_read_only(path: Path):
            current_permissions = path.stat().st_mode
            path.chmod(current_permissions & ~ANYONE_WRITE_PERMISSION)

        read_only_templates_dir = tmp_path / "templates"
        copytree(templates_dir, read_only_templates_dir)

        for root, dirs, files in os.walk(read_only_templates_dir):
            for node in chain(dirs, files):
                _make_read_only(Path(root, node))

        destination = tmp_path / "proj"
        destination.mkdir()
        assert (
            call(
                "startproject",
                project_name,
                "--set",
                f"TEMPLATES_DIR={read_only_templates_dir}",
                cwd=destination,
            )
            == 0
        )

        project_dir = destination / project_name
        actual_permissions = get_permissions_dict(project_dir)

        assert actual_permissions == expected_permissions

    def test_startproject_permissions_unchanged_in_destination(
        self, tmp_path: Path
    ) -> None:
        """Check that preexisting folders and files in the destination folder
        do not see their permissions modified."""
        scrapy_path = scrapy.__path__[0]
        project_template = Path(scrapy_path, "templates", "project")
        project_name = "startproject3"
        renamings = (
            ("module", project_name),
            (".tmpl", ""),
        )
        expected_permissions = get_permissions_dict(
            project_template,
            renamings,
            IGNORE,
        )

        destination = tmp_path / "proj"
        project_dir = destination / project_name
        project_dir.mkdir(parents=True)

        existing_nodes = {
            f"{permissions:o}{extension}": permissions
            for extension in ("", ".d")
            for permissions in (
                0o444,
                0o555,
                0o644,
                0o666,
                0o755,
                0o777,
            )
        }
        for node, permissions in existing_nodes.items():
            path = project_dir / node
            if node.endswith(".d"):
                path.mkdir(mode=permissions)
            else:
                path.touch(mode=permissions)
            expected_permissions[node] = oct(path.stat().st_mode)

        assert call("startproject", project_name, ".", cwd=project_dir) == 0

        actual_permissions = get_permissions_dict(project_dir)

        assert actual_permissions == expected_permissions

    def test_startproject_permissions_umask_022(self, tmp_path: Path) -> None:
        """Check that generated files have the right permissions when the
        system uses a umask value that causes new files to have different
        permissions than those from the template folder."""

        @contextmanager
        def umask(new_mask):
            cur_mask = os.umask(new_mask)
            yield
            os.umask(cur_mask)

        scrapy_path = scrapy.__path__[0]
        project_template = Path(scrapy_path, "templates", "project")
        project_name = "umaskproject"
        renamings = (
            ("module", project_name),
            (".tmpl", ""),
        )
        expected_permissions = get_permissions_dict(
            project_template,
            renamings,
            IGNORE,
        )

        with umask(0o002):
            destination = tmp_path / "proj"
            destination.mkdir()
            assert call("startproject", project_name, cwd=destination) == 0

            project_dir = destination / project_name
            actual_permissions = get_permissions_dict(project_dir)

            assert actual_permissions == expected_permissions