|
19 | 19 | # DEALINGS IN THE SOFTWARE.
|
20 | 20 | from __future__ import annotations
|
21 | 21 |
|
| 22 | +import copy |
22 | 23 | import os
|
23 | 24 |
|
| 25 | +from pathlib import Path |
24 | 26 | from typing import TYPE_CHECKING, Literal
|
25 | 27 |
|
26 | 28 | import pytest
|
@@ -84,16 +86,83 @@ def test_init_calls_validate( # noqa: D103
|
84 | 86 | patched_config_validate.assert_called_once_with()
|
85 | 87 |
|
86 | 88 |
|
| 89 | +def test_collection_directory_property( |
| 90 | + config_instance: config.Config, |
| 91 | + resources_folder_path: Path, |
| 92 | +) -> None: |
| 93 | + """Test collection_directory property. |
| 94 | +
|
| 95 | + Args: |
| 96 | + config_instance: Instance of Config. |
| 97 | + resources_folder_path: Path to resources directory holding a valid collection. |
| 98 | + """ |
| 99 | + # default path is not in a collection |
| 100 | + assert config_instance.collection_directory is None |
| 101 | + |
| 102 | + # Alter config_instance to start at path of a collection |
| 103 | + config_instance = copy.copy(config_instance) |
| 104 | + collection_path = resources_folder_path / "sample-collection" |
| 105 | + config_instance.project_directory = str(collection_path) |
| 106 | + assert config_instance.collection_directory == collection_path |
| 107 | + |
| 108 | + |
87 | 109 | def test_project_directory_property(config_instance: config.Config) -> None: # noqa: D103
|
88 |
| - assert os.getcwd() == config_instance.project_directory # noqa: PTH109 |
| 110 | + assert str(Path.cwd()) == config_instance.project_directory |
89 | 111 |
|
90 | 112 |
|
91 | 113 | def test_molecule_directory_property(config_instance: config.Config) -> None: # noqa: D103
|
92 |
| - x = os.path.join(os.getcwd(), "molecule") # noqa: PTH109, PTH118 |
| 114 | + x = str(Path.cwd() / "molecule") |
93 | 115 |
|
94 | 116 | assert x == config_instance.molecule_directory
|
95 | 117 |
|
96 | 118 |
|
| 119 | +def test_collection_property( |
| 120 | + config_instance: config.Config, |
| 121 | + resources_folder_path: Path, |
| 122 | +) -> None: |
| 123 | + """Test collection property. |
| 124 | +
|
| 125 | + Args: |
| 126 | + config_instance: Instance of Config. |
| 127 | + resources_folder_path: Path to resources directory holding a valid collection. |
| 128 | + """ |
| 129 | + modified_instance = copy.copy(config_instance) |
| 130 | + # default path is not in a collection |
| 131 | + assert config_instance.collection is None |
| 132 | + |
| 133 | + # Alter config_instance to start at path of a collection |
| 134 | + collection_path = resources_folder_path / "sample-collection" |
| 135 | + modified_instance.project_directory = str(collection_path) |
| 136 | + |
| 137 | + assert modified_instance.collection is not None |
| 138 | + assert modified_instance.collection["name"] == "goodies" |
| 139 | + assert modified_instance.collection["namespace"] == "acme" |
| 140 | + |
| 141 | + |
| 142 | +def test_collection_property_broken_collection( |
| 143 | + caplog: pytest.LogCaptureFixture, |
| 144 | + config_instance: config.Config, |
| 145 | + resources_folder_path: Path, |
| 146 | +) -> None: |
| 147 | + """Test collection property with a malformed galaxy.yml. |
| 148 | +
|
| 149 | + Args: |
| 150 | + caplog: pytest log capture fixture. |
| 151 | + config_instance: Instance of Config. |
| 152 | + resources_folder_path: Path to resources directory holding a valid collection. |
| 153 | + """ |
| 154 | + modified_instance = copy.copy(config_instance) |
| 155 | + |
| 156 | + # Alter config_instance to start at path of a collection |
| 157 | + collection_path = resources_folder_path / "broken-collection" |
| 158 | + modified_instance.project_directory = str(collection_path) |
| 159 | + |
| 160 | + assert modified_instance.collection is None |
| 161 | + |
| 162 | + msg = "missing mandatory field 'namespace'" |
| 163 | + assert msg in caplog.text |
| 164 | + |
| 165 | + |
97 | 166 | def test_dependency_property(config_instance: config.Config) -> None: # noqa: D103
|
98 | 167 | assert isinstance(config_instance.dependency, ansible_galaxy.AnsibleGalaxy)
|
99 | 168 |
|
|
0 commit comments