Skip to content

Commit af98156

Browse files
committed
chore(test): refactor app context tests
1 parent aace0b2 commit af98156

File tree

2 files changed

+81
-108
lines changed

2 files changed

+81
-108
lines changed

tests/console/commands/test_version.py

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

3-
import os
4-
import textwrap
5-
6-
from pathlib import Path
73
from typing import TYPE_CHECKING
84

95
import pytest
106

11-
from cleo.testers.application_tester import ApplicationTester
12-
13-
from poetry.console.application import Application
147
from poetry.console.commands.version import VersionCommand
158

169

1710
if TYPE_CHECKING:
1811
from cleo.testers.command_tester import CommandTester
19-
from pytest_mock import MockerFixture
2012

2113
from poetry.poetry import Poetry
2214
from tests.types import CommandTesterFactory
@@ -140,103 +132,3 @@ def test_dry_run(tester: CommandTester) -> None:
140132
new_pyproject = tester.command.poetry.file.path.read_text(encoding="utf-8")
141133
assert tester.io.fetch_output() == "Bumping version from 1.2.3 to 2.0.0\n"
142134
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
207-
208-
209-
def test_version_with_directory_and_project_parameter(
210-
fixture_dir: FixtureDirGetter, mocker: MockerFixture
211-
) -> None:
212-
app = Application()
213-
tester = ApplicationTester(app)
214-
215-
orig_version_command = VersionCommand.handle
216-
217-
def mock_handle(command: VersionCommand) -> int:
218-
exit_code = orig_version_command(command)
219-
220-
command.io.write_line(f"ProjectPath: {command.poetry.pyproject_path.parent}")
221-
command.io.write_line(f"WorkingDirectory: {os.getcwd()}")
222-
223-
return exit_code
224-
225-
mocker.patch("poetry.console.commands.version.VersionCommand.handle", mock_handle)
226-
227-
source_dir = fixture_dir("scripts")
228-
working_directory = source_dir.parent
229-
project_path = "./scripts"
230-
231-
tester.execute(f"--directory {working_directory} --project {project_path} version")
232-
233-
output = tester.io.fetch_output()
234-
235-
expected = textwrap.dedent(f"""\
236-
scripts 0.1.0
237-
ProjectPath: {source_dir}
238-
WorkingDirectory: {working_directory}
239-
""")
240-
241-
assert source_dir != working_directory
242-
assert output == expected

tests/console/test_application_global_options.py

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

3+
import textwrap
4+
35
from pathlib import Path
46
from typing import TYPE_CHECKING
57

@@ -8,9 +10,14 @@
810
from cleo.testers.application_tester import ApplicationTester
911

1012
from poetry.console.application import Application
13+
from poetry.console.commands.version import VersionCommand
14+
from tests.helpers import switch_working_directory
1115

1216

1317
if TYPE_CHECKING:
18+
from pytest import TempPathFactory
19+
from pytest_mock import MockerFixture
20+
1421
from tests.types import FixtureCopier
1522

1623

@@ -24,6 +31,21 @@ def tester() -> ApplicationTester:
2431
return ApplicationTester(Application())
2532

2633

34+
@pytest.fixture
35+
def with_mocked_version_command(mocker: MockerFixture) -> None:
36+
orig_version_command = VersionCommand.handle
37+
38+
def mock_handle(command: VersionCommand) -> int:
39+
exit_code = orig_version_command(command)
40+
41+
command.io.write_line(f"ProjectPath: {command.poetry.pyproject_path.parent}")
42+
command.io.write_line(f"WorkingDirectory: {Path.cwd()}")
43+
44+
return exit_code
45+
46+
mocker.patch("poetry.console.commands.version.VersionCommand.handle", mock_handle)
47+
48+
2749
@pytest.mark.parametrize("parameter", ["-C", "--directory", "-P", "--project"])
2850
def test_application_global_option_position_does_not_matter(
2951
parameter: str, tester: ApplicationTester, project_source_directory: Path
@@ -58,3 +80,62 @@ def test_application_global_option_position_does_not_matter(
5880

5981
assert "certifi" in stdout
6082
assert len(stdout.splitlines()) == 8
83+
84+
85+
@pytest.mark.parametrize("parameter", ["project", "directory"])
86+
def test_application_with_context_parameters(
87+
parameter: str,
88+
tester: ApplicationTester,
89+
project_source_directory: Path,
90+
with_mocked_version_command: None,
91+
) -> None:
92+
# ensure pre-conditions are met
93+
assert project_source_directory != Path.cwd()
94+
95+
is_directory_param = parameter == "directory"
96+
97+
tester.execute(f"--{parameter} {project_source_directory} version")
98+
assert tester.io.fetch_error() == ""
99+
assert tester.status_code == 0
100+
101+
output = tester.io.fetch_output()
102+
assert output == textwrap.dedent(f"""\
103+
foobar 0.1.0
104+
ProjectPath: {project_source_directory}
105+
WorkingDirectory: {project_source_directory if is_directory_param else Path.cwd()}
106+
""")
107+
108+
109+
def test_application_with_relative_project_parameter(
110+
tester: ApplicationTester,
111+
project_source_directory: Path,
112+
with_mocked_version_command: None,
113+
tmp_path_factory: TempPathFactory,
114+
) -> None:
115+
# ensure pre-conditions are met
116+
cwd = Path.cwd()
117+
assert project_source_directory.is_relative_to(cwd)
118+
119+
# construct relative path
120+
relative_source_directory = project_source_directory.relative_to(cwd)
121+
assert relative_source_directory.as_posix() != project_source_directory.as_posix()
122+
assert not relative_source_directory.is_absolute()
123+
124+
# we expect application run to be executed within current cwd but project to be a subdirectory
125+
args = f"--directory '{cwd}' --project {relative_source_directory} version"
126+
127+
# we switch cwd to a new temporary directory unrelated to the project directory
128+
new_working_dir = tmp_path_factory.mktemp("unrelated-working-directory")
129+
with switch_working_directory(new_working_dir):
130+
assert Path.cwd() == new_working_dir
131+
132+
tester.execute(args)
133+
assert tester.io.fetch_error() == ""
134+
assert tester.status_code == 0
135+
136+
output = tester.io.fetch_output()
137+
assert output == textwrap.dedent(f"""\
138+
foobar 0.1.0
139+
ProjectPath: {project_source_directory}
140+
WorkingDirectory: {cwd}
141+
""")

0 commit comments

Comments
 (0)