-
Notifications
You must be signed in to change notification settings - Fork 3.3k
{Core} Provide error recommendation for validate_file_or_dict
#23121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/azure-cli-core/azure/cli/core/commands/tests/test_validators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| import os | ||
|
|
||
| from azure.cli.core.commands.validators import validate_file_or_dict, JSON_RECOMMENDATION_MESSAGE | ||
| from azure.cli.core.azclierror import InvalidArgumentValueError | ||
|
|
||
|
|
||
| class TestValidators(unittest.TestCase): | ||
|
|
||
| def test_validate_file_or_dict(self): | ||
| json_str = '{"name": "my-resource"}' | ||
| curr_dir = os.path.dirname(os.path.realpath(__file__)) | ||
| temp_file = os.path.join(curr_dir, 'temp.json') | ||
|
|
||
| # Parse JSON file | ||
| with open(temp_file, 'w') as f: | ||
| f.write(json_str) | ||
| json_read = validate_file_or_dict(temp_file) | ||
| assert json_read['name'] == 'my-resource' | ||
| os.remove(temp_file) | ||
|
|
||
| # Parse in-line JSON string | ||
| json_read = validate_file_or_dict(json_str) | ||
| assert json_read['name'] == 'my-resource' | ||
|
|
||
| # Test error 1: Parse JSON file with error | ||
| with open(temp_file, 'w') as f: | ||
| f.write(json_str) | ||
| f.write('error!') | ||
| with self.assertRaisesRegex(InvalidArgumentValueError, 'Failed to parse file') as ex: | ||
| validate_file_or_dict(temp_file) | ||
| assert ex.exception.recommendations[0] == JSON_RECOMMENDATION_MESSAGE | ||
| assert len(ex.exception.recommendations) == 1 | ||
| os.remove(temp_file) | ||
|
|
||
| # Test error 2: A non-existing file, but it looks like a JSON file | ||
| with self.assertRaisesRegex(InvalidArgumentValueError, 'JSON file does not exist') as ex: | ||
| validate_file_or_dict("not-exist.json") | ||
| assert ex.exception.recommendations[0] == JSON_RECOMMENDATION_MESSAGE | ||
| assert len(ex.exception.recommendations) == 1 | ||
|
|
||
| # Test error 3: A non-existing file, or invalid JSON string | ||
| with self.assertRaisesRegex(InvalidArgumentValueError, 'Failed to parse string as JSON') as ex: | ||
| validate_file_or_dict("invalid-string") | ||
| assert ex.exception.recommendations[0] == JSON_RECOMMENDATION_MESSAGE | ||
| assert 'The provided JSON string may have been parsed by the shell.' in ex.exception.recommendations[1] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extra spaces break JSON's indentation.