Skip to content

Commit 6322aa7

Browse files
committed
feat: added tests
1 parent 3703a21 commit 6322aa7

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/console/commands/test_version.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
from __future__ import annotations
22

3+
import os
4+
import textwrap
5+
6+
from pathlib import Path
37
from typing import TYPE_CHECKING
48

59
import pytest
610

11+
from cleo.testers.application_tester import ApplicationTester
12+
13+
from poetry.console.application import Application
714
from poetry.console.commands.version import VersionCommand
815

916

1017
if TYPE_CHECKING:
1118
from cleo.testers.command_tester import CommandTester
19+
from pytest_mock import MockerFixture
1220

1321
from poetry.poetry import Poetry
1422
from tests.types import CommandTesterFactory
@@ -132,3 +140,67 @@ def test_dry_run(tester: CommandTester) -> None:
132140
new_pyproject = tester.command.poetry.file.path.read_text(encoding="utf-8")
133141
assert tester.io.fetch_output() == "Bumping version from 1.2.3 to 2.0.0\n"
134142
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

Comments
 (0)