|
1 | 1 | """Unit tests for the conan plugin install functionality""" |
2 | 2 |
|
3 | | -import subprocess |
4 | | -from pathlib import Path |
5 | 3 | from typing import Any |
6 | | -from unittest.mock import Mock |
7 | 4 |
|
8 | 5 | import pytest |
9 | | -from packaging.requirements import Requirement |
10 | | -from pytest_mock import MockerFixture |
11 | 6 |
|
12 | 7 | from cppython.plugins.conan.plugin import ConanProvider |
13 | | -from cppython.plugins.conan.schema import ConanDependency |
14 | 8 | from cppython.test.pytest.mixins import ProviderPluginTestMixin |
15 | | -from cppython.utility.exception import ProviderInstallationError |
16 | 9 |
|
17 | 10 | # Use shared fixtures |
18 | 11 | pytest_plugins = ['tests.fixtures.conan'] |
@@ -43,165 +36,3 @@ def fixture_plugin_type() -> type[ConanProvider]: |
43 | 36 | The type of the Provider |
44 | 37 | """ |
45 | 38 | return ConanProvider |
46 | | - |
47 | | - def test_with_dependencies( |
48 | | - self, |
49 | | - plugin: ConanProvider, |
50 | | - conan_temp_conanfile: Path, |
51 | | - conan_mock_dependencies: list[Requirement], |
52 | | - conan_setup_mocks: dict[str, Mock], |
53 | | - ) -> None: |
54 | | - """Test install method with dependencies and existing conanfile |
55 | | -
|
56 | | - Args: |
57 | | - plugin: The plugin instance |
58 | | - conan_temp_conanfile: Path to temporary conanfile.py |
59 | | - conan_mock_dependencies: List of mock dependencies |
60 | | - conan_setup_mocks: Dictionary containing all mocks |
61 | | - """ |
62 | | - # Setup dependencies |
63 | | - plugin.core_data.cppython_data.dependencies = conan_mock_dependencies |
64 | | - |
65 | | - # Execute |
66 | | - plugin.install() |
67 | | - |
68 | | - # Verify builder was called |
69 | | - conan_setup_mocks['builder'].generate_conanfile.assert_called_once() |
70 | | - assert ( |
71 | | - conan_setup_mocks['builder'].generate_conanfile.call_args[0][0] |
72 | | - == plugin.core_data.project_data.project_root |
73 | | - ) |
74 | | - assert len(conan_setup_mocks['builder'].generate_conanfile.call_args[0][1]) == EXPECTED_DEPENDENCY_COUNT |
75 | | - |
76 | | - # Verify dependency resolution was called |
77 | | - assert conan_setup_mocks['resolve_conan_dependency'].call_count == EXPECTED_DEPENDENCY_COUNT |
78 | | - |
79 | | - # Verify build path was created |
80 | | - assert plugin.core_data.cppython_data.build_path.exists() |
81 | | - |
82 | | - # Verify subprocess.run was called with conan install command |
83 | | - conan_setup_mocks['subprocess_run'].assert_called_once() |
84 | | - call_args = conan_setup_mocks['subprocess_run'].call_args[0][0] |
85 | | - assert call_args[0] == 'conan' |
86 | | - assert call_args[1] == 'install' |
87 | | - assert '--build' in call_args |
88 | | - assert 'missing' in call_args |
89 | | - assert '--profile:host' in call_args |
90 | | - assert '--profile:build' in call_args |
91 | | - assert '--output-folder' in call_args |
92 | | - |
93 | | - def test_conan_command_failure( |
94 | | - self, |
95 | | - plugin: ConanProvider, |
96 | | - conan_temp_conanfile: Path, |
97 | | - conan_mock_dependencies: list[Requirement], |
98 | | - mocker: MockerFixture, |
99 | | - ) -> None: |
100 | | - """Test install method when conan CLI command fails |
101 | | -
|
102 | | - Args: |
103 | | - plugin: The plugin instance |
104 | | - conan_temp_conanfile: Path to temporary conanfile.py |
105 | | - conan_mock_dependencies: List of mock dependencies |
106 | | - mocker: Pytest mocker fixture |
107 | | - """ |
108 | | - # Mock builder |
109 | | - mock_builder = mocker.Mock() |
110 | | - mock_builder.generate_conanfile = mocker.Mock() |
111 | | - plugin.builder = mock_builder # type: ignore[attr-defined] |
112 | | - |
113 | | - # Mock resolve_conan_dependency |
114 | | - def mock_resolve(requirement: Requirement) -> ConanDependency: |
115 | | - return ConanDependency(name=requirement.name) |
116 | | - |
117 | | - mocker.patch('cppython.plugins.conan.plugin.resolve_conan_dependency', side_effect=mock_resolve) |
118 | | - |
119 | | - # Mock subprocess.run to simulate command failure |
120 | | - mock_subprocess_run = mocker.patch('cppython.plugins.conan.plugin.subprocess.run') |
121 | | - mock_subprocess_run.side_effect = subprocess.CalledProcessError( |
122 | | - 1, ['conan', 'install'], stderr='Conan CLI error: package not found' |
123 | | - ) |
124 | | - |
125 | | - # Add a dependency |
126 | | - plugin.core_data.cppython_data.dependencies = [conan_mock_dependencies[0]] |
127 | | - |
128 | | - # Execute and verify exception is raised |
129 | | - with pytest.raises( |
130 | | - ProviderInstallationError, |
131 | | - match='Failed to install dependencies:.*Conan CLI error: package not found', |
132 | | - ): |
133 | | - plugin.install() |
134 | | - |
135 | | - # Verify builder was still called |
136 | | - mock_builder.generate_conanfile.assert_called_once() |
137 | | - |
138 | | - # Verify subprocess.run was attempted |
139 | | - mock_subprocess_run.assert_called_once() |
140 | | - |
141 | | - def test_with_update_flag( |
142 | | - self, |
143 | | - plugin: ConanProvider, |
144 | | - conan_temp_conanfile: Path, |
145 | | - conan_mock_dependencies: list[Requirement], |
146 | | - conan_setup_mocks: dict[str, Mock], |
147 | | - ) -> None: |
148 | | - """Test install method passes update flag to CLI when update=True |
149 | | -
|
150 | | - Args: |
151 | | - plugin: The plugin instance |
152 | | - conan_temp_conanfile: Path to temporary conanfile.py |
153 | | - conan_mock_dependencies: List of mock dependencies |
154 | | - conan_setup_mocks: Dictionary containing all mocks |
155 | | - """ |
156 | | - # Setup dependencies |
157 | | - plugin.core_data.cppython_data.dependencies = conan_mock_dependencies |
158 | | - |
159 | | - # Execute update instead of install |
160 | | - plugin.update() |
161 | | - |
162 | | - # Verify subprocess.run was called with --update flag |
163 | | - conan_setup_mocks['subprocess_run'].assert_called_once() |
164 | | - call_args = conan_setup_mocks['subprocess_run'].call_args[0][0] |
165 | | - assert '--update' in call_args |
166 | | - |
167 | | - def test_with_custom_profiles( |
168 | | - self, |
169 | | - plugin: ConanProvider, |
170 | | - conan_temp_conanfile: Path, |
171 | | - conan_mock_dependencies: list[Requirement], |
172 | | - conan_setup_mocks: dict[str, Mock], |
173 | | - mocker: MockerFixture, |
174 | | - ) -> None: |
175 | | - """Test install method uses custom profiles when specified |
176 | | -
|
177 | | - Args: |
178 | | - plugin: The plugin instance |
179 | | - conan_temp_conanfile: Path to temporary conanfile.py |
180 | | - conan_mock_dependencies: List of mock dependencies |
181 | | - conan_setup_mocks: Dictionary containing all mocks |
182 | | - mocker: Pytest mocker fixture |
183 | | - """ |
184 | | - # Mock custom profiles |
185 | | - custom_host_profile = mocker.Mock() |
186 | | - custom_build_profile = mocker.Mock() |
187 | | - |
188 | | - # Override profiles in the plugin data |
189 | | - plugin.data.host_profile = custom_host_profile |
190 | | - plugin.data.build_profile = custom_build_profile |
191 | | - |
192 | | - # Setup dependencies |
193 | | - plugin.core_data.cppython_data.dependencies = conan_mock_dependencies |
194 | | - |
195 | | - # Execute |
196 | | - plugin.install() |
197 | | - |
198 | | - # Verify subprocess.run was called with custom profiles |
199 | | - conan_setup_mocks['subprocess_run'].assert_called_once() |
200 | | - call_args = conan_setup_mocks['subprocess_run'].call_args[0][0] |
201 | | - |
202 | | - # Find profile arguments |
203 | | - profile_host_idx = call_args.index('--profile:host') |
204 | | - profile_build_idx = call_args.index('--profile:build') |
205 | | - |
206 | | - assert call_args[profile_host_idx + 1] == str(custom_host_profile) |
207 | | - assert call_args[profile_build_idx + 1] == str(custom_build_profile) |
0 commit comments