Skip to content

Commit

Permalink
refactored the code, and added error handling support
Browse files Browse the repository at this point in the history
Signed-off-by: samadpls <[email protected]>
  • Loading branch information
samadpls committed Sep 14, 2023
1 parent 08f2897 commit 0f27841
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
8 changes: 6 additions & 2 deletions src/core/zowe/core_for_zowe_sdk/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 461 in src/core/zowe/core_for_zowe_sdk/config_file.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/config_file.py#L458-L461

Added lines #L458 - L461 were not covered by tests

elif any(self.profiles.values()):
with open(self.filepath, 'w') as file:
self.jsonc["profiles"] = self.profiles
Expand Down
26 changes: 15 additions & 11 deletions src/core/zowe/core_for_zowe_sdk/profile_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 334 in src/core/zowe/core_for_zowe_sdk/profile_manager.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/profile_manager.py#L333-L334

Added lines #L333 - L334 were not covered by tests
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

Check warning on line 352 in src/core/zowe/core_for_zowe_sdk/profile_manager.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/profile_manager.py#L351-L352

Added lines #L351 - L352 were not covered by tests

if highest_layer is None:
raise FileNotFoundError(f"Could not find a valid layer for {json_path}")

Check warning on line 355 in src/core/zowe/core_for_zowe_sdk/profile_manager.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/profile_manager.py#L355

Added line #L355 was not covered by tests

return highest_layer


Expand All @@ -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)
Expand All @@ -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:
Expand Down
7 changes: 1 addition & 6 deletions tests/unit/test_zowe_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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):
Expand Down

0 comments on commit 0f27841

Please sign in to comment.