|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import os |
| 4 | +import textwrap |
| 5 | + |
| 6 | +from pathlib import Path |
3 | 7 | from typing import TYPE_CHECKING
|
4 | 8 |
|
5 | 9 | import pytest
|
6 | 10 |
|
| 11 | +from cleo.testers.application_tester import ApplicationTester |
| 12 | + |
| 13 | +from poetry.console.application import Application |
7 | 14 | from poetry.console.commands.version import VersionCommand
|
8 | 15 |
|
9 | 16 |
|
10 | 17 | if TYPE_CHECKING:
|
11 | 18 | from cleo.testers.command_tester import CommandTester
|
| 19 | + from pytest_mock import MockerFixture |
12 | 20 |
|
13 | 21 | from poetry.poetry import Poetry
|
14 | 22 | from tests.types import CommandTesterFactory
|
@@ -132,3 +140,67 @@ def test_dry_run(tester: CommandTester) -> None:
|
132 | 140 | new_pyproject = tester.command.poetry.file.path.read_text(encoding="utf-8")
|
133 | 141 | assert tester.io.fetch_output() == "Bumping version from 1.2.3 to 2.0.0\n"
|
134 | 142 | assert old_pyproject == new_pyproject
|
| 143 | + |
| 144 | + |
| 145 | +def test_version_with_project_parameter( |
| 146 | + fixture_dir: FixtureDirGetter, mocker: MockerFixture |
| 147 | +) -> None: |
| 148 | + app = Application() |
| 149 | + tester = ApplicationTester(app) |
| 150 | + |
| 151 | + orig_version_command = VersionCommand.handle |
| 152 | + |
| 153 | + def mock_handle(command: VersionCommand) -> int: |
| 154 | + exit_code = orig_version_command(command) |
| 155 | + |
| 156 | + command.io.write_line(f"ProjectPath: {command.poetry.pyproject_path.parent}") |
| 157 | + command.io.write_line(f"WorkingDirectory: {os.getcwd()}") |
| 158 | + |
| 159 | + return exit_code |
| 160 | + |
| 161 | + mocker.patch("poetry.console.commands.version.VersionCommand.handle", mock_handle) |
| 162 | + |
| 163 | + source_dir = fixture_dir("scripts") |
| 164 | + tester.execute(f"--project {source_dir} version") |
| 165 | + |
| 166 | + output = tester.io.fetch_output() |
| 167 | + expected = textwrap.dedent(f"""\ |
| 168 | + scripts 0.1.0 |
| 169 | + ProjectPath: {source_dir} |
| 170 | + WorkingDirectory: {os.getcwd()} |
| 171 | + """) |
| 172 | + |
| 173 | + assert source_dir != Path(os.getcwd()) |
| 174 | + assert output == expected |
| 175 | + |
| 176 | + |
| 177 | +def test_version_with_directory_parameter( |
| 178 | + fixture_dir: FixtureDirGetter, mocker: MockerFixture |
| 179 | +) -> None: |
| 180 | + app = Application() |
| 181 | + tester = ApplicationTester(app) |
| 182 | + |
| 183 | + orig_version_command = VersionCommand.handle |
| 184 | + |
| 185 | + def mock_handle(command: VersionCommand) -> int: |
| 186 | + exit_code = orig_version_command(command) |
| 187 | + |
| 188 | + command.io.write_line(f"ProjectPath: {command.poetry.pyproject_path.parent}") |
| 189 | + command.io.write_line(f"WorkingDirectory: {os.getcwd()}") |
| 190 | + |
| 191 | + return exit_code |
| 192 | + |
| 193 | + mocker.patch("poetry.console.commands.version.VersionCommand.handle", mock_handle) |
| 194 | + |
| 195 | + source_dir = fixture_dir("scripts") |
| 196 | + tester.execute(f"--directory {source_dir} version") |
| 197 | + |
| 198 | + output = tester.io.fetch_output() |
| 199 | + expected = textwrap.dedent(f"""\ |
| 200 | + scripts 0.1.0 |
| 201 | + ProjectPath: {source_dir} |
| 202 | + WorkingDirectory: {source_dir} |
| 203 | + """) |
| 204 | + |
| 205 | + assert source_dir != Path(os.getcwd()) |
| 206 | + assert output == expected |
0 commit comments