|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from cleo.testers import CommandTester |
| 7 | + |
| 8 | +from tests.helpers import get_package |
| 9 | + |
| 10 | +from ..conftest import Application |
| 11 | +from ..conftest import Path |
| 12 | +from ..conftest import Poetry |
| 13 | + |
| 14 | + |
| 15 | +PYPROJECT_CONTENT = """\ |
| 16 | +[tool.poetry] |
| 17 | +name = "simple-project" |
| 18 | +version = "1.2.3" |
| 19 | +description = "Some description." |
| 20 | +authors = [ |
| 21 | + "Sébastien Eustace <[email protected]>" |
| 22 | +] |
| 23 | +license = "MIT" |
| 24 | +
|
| 25 | +readme = "README.rst" |
| 26 | +
|
| 27 | +homepage = "https://poetry.eustace.io" |
| 28 | +repository = "https://github.com/sdispater/poetry" |
| 29 | +documentation = "https://poetry.eustace.io/docs" |
| 30 | +
|
| 31 | +keywords = ["packaging", "dependency", "poetry"] |
| 32 | +
|
| 33 | +classifiers = [ |
| 34 | + "Topic :: Software Development :: Build Tools", |
| 35 | + "Topic :: Software Development :: Libraries :: Python Modules" |
| 36 | +] |
| 37 | +
|
| 38 | +# Requirements |
| 39 | +[tool.poetry.dependencies] |
| 40 | +python = "~2.7 || ^3.6" |
| 41 | +foo = "^1.0" |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def poetry(repo, tmp_dir): |
| 47 | + with (Path(tmp_dir) / "pyproject.toml").open("w", encoding="utf-8") as f: |
| 48 | + f.write(PYPROJECT_CONTENT) |
| 49 | + |
| 50 | + p = Poetry.create(Path(tmp_dir)) |
| 51 | + |
| 52 | + p.pool.remove_repository("pypi") |
| 53 | + p.pool.add_repository(repo) |
| 54 | + p._locker.write() |
| 55 | + |
| 56 | + yield p |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def app(poetry): |
| 61 | + return Application(poetry) |
| 62 | + |
| 63 | + |
| 64 | +def test_export_exports_requirements_txt_file_locks_if_no_lock_file(app, repo): |
| 65 | + command = app.find("export") |
| 66 | + tester = CommandTester(command) |
| 67 | + |
| 68 | + assert not app.poetry.locker.lock.exists() |
| 69 | + |
| 70 | + repo.add_package(get_package("foo", "1.0.0")) |
| 71 | + |
| 72 | + tester.execute([("command", command.get_name()), ("--format", "requirements.txt")]) |
| 73 | + |
| 74 | + requirements = app.poetry.file.parent / "requirements.txt" |
| 75 | + assert requirements.exists() |
| 76 | + |
| 77 | + with requirements.open(encoding="utf-8") as f: |
| 78 | + content = f.read() |
| 79 | + |
| 80 | + assert app.poetry.locker.lock.exists() |
| 81 | + |
| 82 | + expected = """\ |
| 83 | +foo==1.0.0 |
| 84 | +""" |
| 85 | + |
| 86 | + assert expected == content |
| 87 | + assert "The lock file does not exist. Locking." in tester.get_display(True) |
| 88 | + |
| 89 | + |
| 90 | +def test_export_exports_requirements_txt_uses_lock_file(app, repo): |
| 91 | + repo.add_package(get_package("foo", "1.0.0")) |
| 92 | + |
| 93 | + command = app.find("lock") |
| 94 | + tester = CommandTester(command) |
| 95 | + tester.execute([("command", "lock")]) |
| 96 | + |
| 97 | + assert app.poetry.locker.lock.exists() |
| 98 | + |
| 99 | + command = app.find("export") |
| 100 | + tester = CommandTester(command) |
| 101 | + |
| 102 | + tester.execute([("command", command.get_name()), ("--format", "requirements.txt")]) |
| 103 | + |
| 104 | + requirements = app.poetry.file.parent / "requirements.txt" |
| 105 | + assert requirements.exists() |
| 106 | + |
| 107 | + with requirements.open(encoding="utf-8") as f: |
| 108 | + content = f.read() |
| 109 | + |
| 110 | + assert app.poetry.locker.lock.exists() |
| 111 | + |
| 112 | + expected = """\ |
| 113 | +foo==1.0.0 |
| 114 | +""" |
| 115 | + |
| 116 | + assert expected == content |
| 117 | + assert "The lock file does not exist. Locking." not in tester.get_display(True) |
| 118 | + |
| 119 | + |
| 120 | +def test_export_fails_on_invalid_format(app, repo): |
| 121 | + repo.add_package(get_package("foo", "1.0.0")) |
| 122 | + |
| 123 | + command = app.find("lock") |
| 124 | + tester = CommandTester(command) |
| 125 | + tester.execute([("command", "lock")]) |
| 126 | + |
| 127 | + assert app.poetry.locker.lock.exists() |
| 128 | + |
| 129 | + command = app.find("export") |
| 130 | + tester = CommandTester(command) |
| 131 | + |
| 132 | + with pytest.raises(ValueError): |
| 133 | + tester.execute([("command", command.get_name()), ("--format", "invalid")]) |
0 commit comments