File: test_command_export.py

package info (click to toggle)
poetry-plugin-export 1.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 908 kB
  • sloc: python: 4,062; makefile: 6
file content (325 lines) | stat: -rw-r--r-- 9,515 bytes parent folder | download | duplicates (2)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from __future__ import annotations

import shutil

from typing import TYPE_CHECKING
from unittest.mock import Mock

import pytest

from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.package import Package

from poetry_plugin_export.exporter import Exporter
from tests.markers import MARKER_PY


if TYPE_CHECKING:
    from pathlib import Path

    from _pytest.monkeypatch import MonkeyPatch
    from cleo.testers.command_tester import CommandTester
    from poetry.poetry import Poetry
    from poetry.repositories import Repository
    from pytest_mock import MockerFixture

    from tests.types import CommandTesterFactory
    from tests.types import ProjectFactory


PYPROJECT_CONTENT = """\
[tool.poetry]
name = "simple-project"
version = "1.2.3"
description = "Some description."
authors = [
    "Sébastien Eustace <sebastien@eustace.io>"
]
license = "MIT"

readme = "README.rst"

homepage = "https://python-poetry.org"
repository = "https://github.com/python-poetry/poetry"
documentation = "https://python-poetry.org/docs"

keywords = ["packaging", "dependency", "poetry"]

classifiers = [
    "Topic :: Software Development :: Build Tools",
    "Topic :: Software Development :: Libraries :: Python Modules"
]

# Requirements
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"
foo = "^1.0"
bar = { version = "^1.1", optional = true }
qux = { version = "^1.2", optional = true }

[tool.poetry.group.dev.dependencies]
baz = "^2.0"

[tool.poetry.group.opt]
optional = true

[tool.poetry.group.opt.dependencies]
opt = "^2.2"


[tool.poetry.extras]
feature_bar = ["bar"]
feature_qux = ["qux"]
"""


@pytest.fixture(autouse=True)
def setup(repo: Repository) -> None:
    repo.add_package(Package("foo", "1.0.0"))
    repo.add_package(Package("bar", "1.1.0"))
    repo.add_package(Package("baz", "2.0.0"))
    repo.add_package(Package("opt", "2.2.0"))
    repo.add_package(Package("qux", "1.2.0"))


@pytest.fixture
def poetry(project_factory: ProjectFactory) -> Poetry:
    return project_factory(name="export", pyproject_content=PYPROJECT_CONTENT)


@pytest.fixture
def tester(
    command_tester_factory: CommandTesterFactory, poetry: Poetry
) -> CommandTester:
    return command_tester_factory("export", poetry=poetry)


def _export_requirements(tester: CommandTester, poetry: Poetry, tmp_path: Path) -> None:
    from tests.helpers import as_cwd

    with as_cwd(tmp_path):
        tester.execute("--format requirements.txt --output requirements.txt")

    requirements = tmp_path / "requirements.txt"
    assert requirements.exists()

    with requirements.open(encoding="utf-8") as f:
        content = f.read()

    assert poetry.locker.lock.exists()

    expected = f"""\
foo==1.0.0 ; {MARKER_PY}
"""

    assert content == expected


def test_export_exports_requirements_txt_file_locks_if_no_lock_file(
    tester: CommandTester, poetry: Poetry, tmp_path: Path
) -> None:
    assert not poetry.locker.lock.exists()
    _export_requirements(tester, poetry, tmp_path)
    assert "The lock file does not exist. Locking." in tester.io.fetch_error()


def test_export_exports_requirements_txt_uses_lock_file(
    tester: CommandTester, poetry: Poetry, tmp_path: Path, do_lock: None
) -> None:
    _export_requirements(tester, poetry, tmp_path)
    assert "The lock file does not exist. Locking." not in tester.io.fetch_error()


def test_export_fails_on_invalid_format(tester: CommandTester, do_lock: None) -> None:
    with pytest.raises(ValueError):
        tester.execute("--format invalid")


def test_export_fails_if_lockfile_is_not_fresh(
    tester: CommandTester,
    poetry: Poetry,
    tmp_path: Path,
    do_lock: None,
    mocker: MockerFixture,
) -> None:
    mocker.patch.object(poetry.locker, "is_fresh", return_value=False)
    assert tester.execute() == 1
    assert "pyproject.toml changed significantly" in tester.io.fetch_error()


def test_export_prints_to_stdout_by_default(
    tester: CommandTester, do_lock: None
) -> None:
    tester.execute("--format requirements.txt")
    expected = f"""\
foo==1.0.0 ; {MARKER_PY}
"""
    assert tester.io.fetch_output() == expected


def test_export_uses_requirements_txt_format_by_default(
    tester: CommandTester, do_lock: None
) -> None:
    tester.execute()
    expected = f"""\
foo==1.0.0 ; {MARKER_PY}
"""
    assert tester.io.fetch_output() == expected


@pytest.mark.parametrize(
    "options, expected",
    [
        ("", f"foo==1.0.0 ; {MARKER_PY}\n"),
        ("--with dev", f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\n"),
        ("--with opt", f"foo==1.0.0 ; {MARKER_PY}\nopt==2.2.0 ; {MARKER_PY}\n"),
        (
            "--with dev,opt",
            (
                f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\nopt==2.2.0 ;"
                f" {MARKER_PY}\n"
            ),
        ),
        (f"--without {MAIN_GROUP}", "\n"),
        ("--without dev", f"foo==1.0.0 ; {MARKER_PY}\n"),
        ("--without opt", f"foo==1.0.0 ; {MARKER_PY}\n"),
        (f"--without {MAIN_GROUP},dev,opt", "\n"),
        (f"--only {MAIN_GROUP}", f"foo==1.0.0 ; {MARKER_PY}\n"),
        ("--only dev", f"baz==2.0.0 ; {MARKER_PY}\n"),
        (
            f"--only {MAIN_GROUP},dev",
            f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\n",
        ),
    ],
)
def test_export_groups(
    tester: CommandTester, do_lock: None, options: str, expected: str
) -> None:
    tester.execute(options)
    assert tester.io.fetch_output() == expected


@pytest.mark.parametrize(
    "extras, expected",
    [
        (
            "feature_bar",
            f"""\
bar==1.1.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
""",
        ),
        (
            "feature_bar feature_qux",
            f"""\
bar==1.1.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
qux==1.2.0 ; {MARKER_PY}
""",
        ),
    ],
)
def test_export_includes_extras_by_flag(
    tester: CommandTester, do_lock: None, extras: str, expected: str
) -> None:
    tester.execute(f"--format requirements.txt --extras '{extras}'")
    assert tester.io.fetch_output() == expected


def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) -> None:
    with pytest.raises(ValueError) as error:
        tester.execute("--format requirements.txt --extras 'SUS AMONGUS'")
    expected = "Extra [amongus, sus] is not specified."
    assert str(error.value) == expected


def test_export_with_all_extras(tester: CommandTester, do_lock: None) -> None:
    tester.execute("--format requirements.txt --all-extras")
    output = tester.io.fetch_output()
    assert f"bar==1.1.0 ; {MARKER_PY}" in output
    assert f"qux==1.2.0 ; {MARKER_PY}" in output


def test_extras_conflicts_all_extras(tester: CommandTester, do_lock: None) -> None:
    tester.execute("--extras bar --all-extras")

    assert tester.status_code == 1
    assert (
        "You cannot specify explicit `--extras` while exporting using `--all-extras`.\n"
        in tester.io.fetch_error()
    )


def test_export_with_all_groups(tester: CommandTester, do_lock: None) -> None:
    tester.execute("--format requirements.txt --all-groups")
    output = tester.io.fetch_output()
    assert f"baz==2.0.0 ; {MARKER_PY}" in output
    assert f"opt==2.2.0 ; {MARKER_PY}" in output


@pytest.mark.parametrize("flag", ["--with", "--without", "--only"])
def test_with_conflicts_all_groups(
    tester: CommandTester, do_lock: None, flag: str
) -> None:
    tester.execute(f"{flag}=bar --all-groups")

    assert tester.status_code == 1
    assert (
        "You cannot specify explicit `--with`, `--without`,"
        " or `--only` while exporting using `--all-groups`.\n"
        in tester.io.fetch_error()
    )


def test_export_with_urls(
    monkeypatch: MonkeyPatch, tester: CommandTester, poetry: Poetry
) -> None:
    """
    We are just validating that the option gets passed. The option itself is tested in
    the Exporter test.
    """
    mock_export = Mock()
    monkeypatch.setattr(Exporter, "with_urls", mock_export)
    tester.execute("--without-urls")
    mock_export.assert_called_once_with(False)


def test_export_exports_constraints_txt_with_warnings(
    tmp_path: Path,
    fixture_root: Path,
    project_factory: ProjectFactory,
    command_tester_factory: CommandTesterFactory,
) -> None:
    # On Windows we have to make sure that the path dependency and the pyproject.toml
    # are on the same drive, otherwise locking fails.
    # (in our CI fixture_root is on D:\ but temp_path is on C:\)
    editable_dep_path = tmp_path / "project_with_nested_local"
    shutil.copytree(fixture_root / "project_with_nested_local", editable_dep_path)

    pyproject_content = f"""\
[tool.poetry]
name = "simple-project"
version = "1.2.3"
description = "Some description."
authors = [
    "Sébastien Eustace <sebastien@eustace.io>"
]

[tool.poetry.dependencies]
python = "^3.6"
baz = ">1.0"
project-with-nested-local = {{ path = "{editable_dep_path.as_posix()}", \
develop = true }}
"""
    poetry = project_factory(name="export", pyproject_content=pyproject_content)
    tester = command_tester_factory("export", poetry=poetry)
    tester.execute("--format constraints.txt")

    develop_warning = (
        "Warning: project-with-nested-local is locked in develop (editable) mode, which"
        " is incompatible with the constraints.txt format.\n"
    )
    expected = 'baz==2.0.0 ; python_version >= "3.6" and python_version < "4.0"\n'

    assert develop_warning in tester.io.fetch_error()
    assert tester.io.fetch_output() == expected