17
17
from ansible_creator .output import Output
18
18
from ansible_creator .subcommands .init import Init
19
19
from ansible_creator .utils import TermFeatures
20
- from tests .defaults import FIXTURES_DIR
20
+ from tests .defaults import FIXTURES_DIR , UUID_LENGTH
21
21
22
22
23
23
class ConfigDict (TypedDict ):
@@ -51,7 +51,7 @@ def fixture_cli_args(tmp_path: Path, output: Output) -> ConfigDict:
51
51
"""Create a dict to use for a Init class object as fixture.
52
52
53
53
Args:
54
- tmp_path: App configuration object .
54
+ tmp_path: Temporary directory path .
55
55
output: Output class object.
56
56
57
57
Returns:
@@ -94,18 +94,34 @@ def test_run_success_for_collection(
94
94
capsys : pytest .CaptureFixture [str ],
95
95
tmp_path : Path ,
96
96
cli_args : ConfigDict ,
97
+ monkeypatch : pytest .MonkeyPatch ,
97
98
) -> None :
98
99
"""Test Init.run().
99
100
100
101
Args:
101
102
capsys: Pytest fixture to capture stdout and stderr.
102
103
tmp_path: Temporary directory path.
103
104
cli_args: Dictionary, partial Init class object.
105
+ monkeypatch: Pytest monkeypatch fixture.
104
106
"""
105
107
cli_args ["project" ] = "collection"
106
108
init = Init (
107
109
Config (** cli_args ),
108
110
)
111
+
112
+ # Mock the "unique_name_in_devfile" method
113
+ def mock_unique_name_in_devfile (self : Init ) -> str :
114
+ coll_namespace = self ._namespace
115
+ coll_name = self ._collection_name
116
+ return f"{ coll_namespace } .{ coll_name } "
117
+
118
+ # Apply the mock
119
+ monkeypatch .setattr (
120
+ Init ,
121
+ "unique_name_in_devfile" ,
122
+ mock_unique_name_in_devfile ,
123
+ )
124
+
109
125
init .run ()
110
126
result = capsys .readouterr ().out
111
127
@@ -140,6 +156,7 @@ def test_run_success_ansible_project(
140
156
capsys : pytest .CaptureFixture [str ],
141
157
tmp_path : Path ,
142
158
cli_args : ConfigDict ,
159
+ monkeypatch : pytest .MonkeyPatch ,
143
160
) -> None :
144
161
"""Test Init.run().
145
162
@@ -149,6 +166,7 @@ def test_run_success_ansible_project(
149
166
capsys: Pytest fixture to capture stdout and stderr.
150
167
tmp_path: Temporary directory path.
151
168
cli_args: Dictionary, partial Init class object.
169
+ monkeypatch: Pytest monkeypatch fixture.
152
170
"""
153
171
cli_args ["collection" ] = ""
154
172
cli_args ["project" ] = "ansible-project"
@@ -158,6 +176,20 @@ def test_run_success_ansible_project(
158
176
init = Init (
159
177
Config (** cli_args ),
160
178
)
179
+
180
+ # Mock the "unique_name_in_devfile" method
181
+ def mock_unique_name_in_devfile (self : Init ) -> str :
182
+ coll_namespace = self ._scm_org
183
+ coll_name = self ._scm_project
184
+ return f"{ coll_namespace } .{ coll_name } "
185
+
186
+ # Apply the mock
187
+ monkeypatch .setattr (
188
+ Init ,
189
+ "unique_name_in_devfile" ,
190
+ mock_unique_name_in_devfile ,
191
+ )
192
+
161
193
init .run ()
162
194
result = capsys .readouterr ().out
163
195
@@ -292,3 +324,81 @@ def test_is_file_error(tmp_path: Path) -> None:
292
324
with pytest .raises (CreatorError ) as exc_info :
293
325
init .run ()
294
326
assert "but is a file" in str (exc_info .value )
327
+
328
+
329
+ @pytest .fixture (name = "cli_args_collection" )
330
+ def fixture_collection_project (tmp_path : Path , output : Output ) -> Config :
331
+ """Fixture for Config object with collection project.
332
+
333
+ Args:
334
+ tmp_path: Temporary directory path.
335
+ output: Output class object.
336
+
337
+ Returns:
338
+ Config: Config class object.
339
+ """
340
+ return Config (
341
+ subcommand = "init" ,
342
+ namespace = "testns" ,
343
+ collection_name = "testname" ,
344
+ init_path = str (tmp_path / "test_path" ),
345
+ force = False ,
346
+ creator_version = "1.0.0" ,
347
+ project = "collection" ,
348
+ scm_org = "" ,
349
+ scm_project = "" ,
350
+ output = output ,
351
+ )
352
+
353
+
354
+ @pytest .fixture (name = "cli_args_playbook" )
355
+ def fixture_playbook_project (tmp_path : Path , output : Output ) -> Config :
356
+ """Fixture for Config object with ansible-project.
357
+
358
+ Args:
359
+ tmp_path: Temporary directory path.
360
+ output: Output class object.
361
+
362
+ Returns:
363
+ Config: Config class object.
364
+ """
365
+ return Config (
366
+ subcommand = "init" ,
367
+ namespace = "" ,
368
+ collection_name = "" ,
369
+ init_path = str (tmp_path / "test_path" ),
370
+ force = False ,
371
+ creator_version = "1.0.0" ,
372
+ project = "ansible-project" ,
373
+ scm_org = "foo" ,
374
+ scm_project = "bar" ,
375
+ output = output ,
376
+ )
377
+
378
+
379
+ def test_name_in_devfile_collection (cli_args_collection : Config ) -> None :
380
+ """Test unique_name_in_devfile method for collection project.
381
+
382
+ Args:
383
+ cli_args_collection: Configuration object for collection project.
384
+ """
385
+ init = Init (cli_args_collection )
386
+ unique_name = init .unique_name_in_devfile ()
387
+ assert unique_name .startswith ("testns.testname-" )
388
+ uuid_part = unique_name .split ("-" )[- 1 ] # Extract the UUID part
389
+ assert len (uuid_part ) == UUID_LENGTH , "UUID part length mismatch"
390
+
391
+
392
+ def test_name_in_devfile_playbook (
393
+ cli_args_playbook : Config ,
394
+ ) -> None :
395
+ """Test unique_name_in_devfile method for playbook project.
396
+
397
+ Args:
398
+ cli_args_playbook: Configuration object for playbook project.
399
+ """
400
+ init = Init (cli_args_playbook )
401
+ unique_name = init .unique_name_in_devfile ()
402
+ assert unique_name .startswith ("foo.bar-" )
403
+ uuid_part = unique_name .split ("-" )[- 1 ] # Extract the UUID part
404
+ assert len (uuid_part ) == UUID_LENGTH , "UUID part length mismatch"
0 commit comments