diff --git a/src/core/zowe/core_for_zowe_sdk/config_file.py b/src/core/zowe/core_for_zowe_sdk/config_file.py index 86b7195d..a67440ee 100644 --- a/src/core/zowe/core_for_zowe_sdk/config_file.py +++ b/src/core/zowe/core_for_zowe_sdk/config_file.py @@ -453,9 +453,13 @@ def save(self, secure_props=True): Returns: None """ - # Update the config file with any changes + # Updating the config file with any changes if self.profiles is None: - self.init_from_file() + try: + self.init_from_file() + except FileNotFoundError: + pass + elif any(self.profiles.values()): with open(self.filepath, 'w') as file: self.jsonc["profiles"] = self.profiles diff --git a/src/core/zowe/core_for_zowe_sdk/profile_manager.py b/src/core/zowe/core_for_zowe_sdk/profile_manager.py index 18dbbe49..43c8db3b 100644 --- a/src/core/zowe/core_for_zowe_sdk/profile_manager.py +++ b/src/core/zowe/core_for_zowe_sdk/profile_manager.py @@ -326,25 +326,34 @@ def get_highest_priority_layer(self, json_path: str) -> Optional[ConfigFile]: ] original_name = layers[0].get_profile_name_from_path(json_path) - - if not self._show_warnings: - warnings.simplefilter("ignore") for layer in layers: - layer.init_from_file() + try: + layer.init_from_file() + except FileNotFoundError: + continue parts = original_name.split(".") current_name = "" + while parts: current_name = ".".join(parts) profile = layer.find_profile(current_name, layer.profiles) + if profile is not None and len(current_name) > len(longest_match): highest_layer = layer longest_match = current_name + else: parts.pop() if original_name == longest_match: break - warnings.resetwarnings() + + if highest_layer is None: + highest_layer = layer + + if highest_layer is None: + raise FileNotFoundError(f"Could not find a valid layer for {json_path}") + return highest_layer @@ -360,10 +369,7 @@ def set_property(self, json_path, value, secure=None) -> None: # highest priority layer for the given profile name highest_priority_layer = self.get_highest_priority_layer(json_path) - # If the profile doesn't exist in any layer, use the project user config as the default location - if not highest_priority_layer: - highest_priority_layer = self.project_user_config - + # Set the property in the highest priority layer highest_priority_layer.set_property(json_path, value, secure=secure) @@ -378,8 +384,6 @@ def set_profile(self, profile_path: str, profile_data: dict) -> None: """ highest_priority_layer = self.get_highest_priority_layer(profile_path) - if not highest_priority_layer: - highest_priority_layer = self.project_user_config highest_priority_layer.set_profile(profile_path, profile_data) def save(self) -> None: diff --git a/tests/unit/test_zowe_core.py b/tests/unit/test_zowe_core.py index 9cc5044f..253f68bd 100644 --- a/tests/unit/test_zowe_core.py +++ b/tests/unit/test_zowe_core.py @@ -579,9 +579,7 @@ def test_profile_loading_with_env_variables(self, get_pass_func): } self.assertEqual(props, expected_props) - @mock.patch("warnings.simplefilter") - @mock.patch("warnings.resetwarnings") - def test_get_highest_priority_layer(self, mock_resetwarnings, mock_simplefilter): + def test_get_highest_priority_layer(self): """ Test that get_highest_priority_layer returns the highest priority layer with a valid profile data dictionary. """ @@ -593,15 +591,12 @@ def test_get_highest_priority_layer(self, mock_resetwarnings, mock_simplefilter) # Set up the ProfileManager profile_manager = ProfileManager() profile_manager.project_user_config = project_user_config - profile_manager._show_warnings = False project_user_config.get_profile_name_from_path.return_value = "zosmf" # Call the function being tested result_layer = profile_manager.get_highest_priority_layer("zosmf") # Assert the results self.assertEqual(result_layer, project_user_config) - mock_simplefilter.assert_called_with("ignore") - mock_resetwarnings.assert_called() @patch("zowe.core_for_zowe_sdk.ProfileManager.get_highest_priority_layer") def test_profile_manager_set_property(self, get_highest_priority_layer_mock):