5
5
6
6
from pathlib import Path
7
7
from typing import TYPE_CHECKING
8
+ from typing import ClassVar
8
9
9
10
import pytest
10
11
13
14
from cleo .testers .application_tester import ApplicationTester
14
15
15
16
from poetry .console .application import Application
17
+ from poetry .console .commands .command import Command
16
18
from poetry .console .commands .version import VersionCommand
19
+ from poetry .plugins import ApplicationPlugin
20
+ from tests .helpers import mock_metadata_entry_points
17
21
from tests .helpers import switch_working_directory
18
22
19
23
27
31
NO_PYPROJECT_TOML_ERROR = "Poetry could not find a pyproject.toml file in"
28
32
29
33
34
+ class CheckProjectPathCommand (Command ):
35
+ name = "check-project-path"
36
+
37
+ description = "Check Project Path Command"
38
+
39
+ def handle (self ) -> int :
40
+ if not self .poetry .pyproject_path .exists ():
41
+ raise RuntimeError (
42
+ f"Wrong project path in handle: { self .poetry .pyproject_path } \n Working directory: { Path .cwd ()} "
43
+ )
44
+
45
+ return 0
46
+
47
+
48
+ class EarlyPoetryAccessPlugin (ApplicationPlugin ):
49
+ commands : ClassVar [list [type [Command ]]] = [CheckProjectPathCommand ]
50
+
51
+ def activate (self , application : Application ) -> None :
52
+ super ().activate (application )
53
+
54
+ # access application.poetry
55
+ # see https://github.com/nat-n/poethepoet/issues/288
56
+ if not application .poetry .pyproject_path .exists ():
57
+ raise RuntimeError (
58
+ f"Wrong project path in activate: { application .poetry .pyproject_path } \n Working directory: { Path .cwd ()} "
59
+ )
60
+
61
+
62
+ @pytest .fixture
63
+ def with_early_poetry_access_plugin (mocker : MockerFixture ) -> None :
64
+ mock_metadata_entry_points (mocker , EarlyPoetryAccessPlugin )
65
+
66
+
30
67
@pytest .fixture
31
68
def project_source_directory (fixture_copier : FixtureCopier ) -> Path :
32
69
return fixture_copier ("up_to_date_lock" )
33
70
34
71
72
+ @pytest .fixture
73
+ def relative_project_source_directory (project_source_directory : Path ) -> Path :
74
+ # ensure pre-conditions are met
75
+ cwd = Path .cwd ()
76
+ assert project_source_directory .is_relative_to (cwd )
77
+
78
+ # construct relative path
79
+ relative_source_directory = project_source_directory .relative_to (cwd )
80
+ assert relative_source_directory .as_posix () != project_source_directory .as_posix ()
81
+ assert not relative_source_directory .is_absolute ()
82
+
83
+ return relative_source_directory
84
+
85
+
35
86
@pytest .fixture
36
87
def tester () -> ApplicationTester :
37
88
return ApplicationTester (Application ())
@@ -149,20 +200,14 @@ def test_application_with_context_parameters(
149
200
def test_application_with_relative_project_parameter (
150
201
tester : ApplicationTester ,
151
202
project_source_directory : Path ,
203
+ relative_project_source_directory : Path ,
152
204
with_mocked_version_command : None ,
153
205
tmp_path_factory : TempPathFactory ,
154
206
) -> None :
155
- # ensure pre-conditions are met
156
207
cwd = Path .cwd ()
157
- assert project_source_directory .is_relative_to (cwd )
158
-
159
- # construct relative path
160
- relative_source_directory = project_source_directory .relative_to (cwd )
161
- assert relative_source_directory .as_posix () != project_source_directory .as_posix ()
162
- assert not relative_source_directory .is_absolute ()
163
-
164
- # we expect application run to be executed within current cwd but project to be a subdirectory
165
- args = f"--directory '{ cwd } ' --project { relative_source_directory } version"
208
+ # we expect application run to be executed within current cwd
209
+ # but project to be a subdirectory
210
+ args = f"--directory '{ cwd } ' --project { relative_project_source_directory } version"
166
211
167
212
# we switch cwd to a new temporary directory unrelated to the project directory
168
213
new_working_dir = tmp_path_factory .mktemp ("unrelated-working-directory" )
@@ -181,6 +226,19 @@ def test_application_with_relative_project_parameter(
181
226
""" )
182
227
183
228
229
+ def test_application_with_relative_directory_parameter_and_early_poetry_access_plugin (
230
+ tester : ApplicationTester ,
231
+ with_early_poetry_access_plugin : None ,
232
+ relative_project_source_directory : Path ,
233
+ ) -> None :
234
+ """see https://github.com/nat-n/poethepoet/issues/288"""
235
+ tester .execute (
236
+ f"--directory { relative_project_source_directory } check-project-path"
237
+ )
238
+
239
+ assert tester .status_code == 0 , tester .io .fetch_error ()
240
+
241
+
184
242
@pytest .mark .parametrize (
185
243
("parameter" , "check" , "result" ),
186
244
[
0 commit comments