-
Notifications
You must be signed in to change notification settings - Fork 127
Add generate and verify commands
#542
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
Open
DanielMicrosoft
wants to merge
7
commits into
Azure:dev
Choose a base branch
from
DanielMicrosoft:xxxxx-add-cmdidx-azdev-commands
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
22abe4b
feature: add commands for generate/verify json
DanielMicrosoft 58b445a
fix: formatting error in .rst
DanielMicrosoft 06a68e7
feature: add version bump and history msg
DanielMicrosoft 7cacc9e
fix: remove python 3.9 and add 3.13 to test matrix
DanielMicrosoft 20e8a8c
fix: readme formatting
DanielMicrosoft 61c4b81
readme
DanielMicrosoft 21c6779
readme
DanielMicrosoft 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
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
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
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
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,71 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| from knack.util import CLIError | ||
|
|
||
| from azdev.utilities import display, heading, py_cmd | ||
| from azdev.utilities.path import get_cli_repo_path | ||
|
|
||
|
|
||
| _LATEST_INDEX_SCRIPT = os.path.join('scripts', 'generate_latest_indices.py') | ||
|
|
||
|
|
||
| def _resolve_cli_repo_path(cli_path): | ||
| if cli_path: | ||
| resolved = os.path.abspath(os.path.expanduser(cli_path)) | ||
| else: | ||
| resolved = get_cli_repo_path() | ||
|
|
||
| if not resolved or resolved == '_NONE_': | ||
| raise CLIError('Azure CLI repo path is not configured. Specify `--cli` or run `azdev setup`.') | ||
|
|
||
| if not os.path.isdir(resolved): | ||
| raise CLIError('Azure CLI repo path does not exist: {}'.format(resolved)) | ||
|
|
||
| return resolved | ||
|
|
||
|
|
||
| def _run_latest_index(mode, cli_path=None, profile='latest', all_profiles=False): | ||
| if all_profiles: | ||
| raise CLIError('`--all-profiles` is not supported yet. Use `--profile latest`.') | ||
|
|
||
| if profile != 'latest': | ||
| raise CLIError("Unsupported profile '{}'. Only `latest` is currently supported.".format(profile)) | ||
|
|
||
| repo_path = _resolve_cli_repo_path(cli_path) | ||
| script_path = os.path.join(repo_path, _LATEST_INDEX_SCRIPT) | ||
| if not os.path.isfile(script_path): | ||
| raise CLIError('Unable to find azure-cli script: {}'.format(script_path)) | ||
|
|
||
| heading('Latest Index: {}'.format(mode.capitalize())) | ||
| display('Azure CLI repo: {}'.format(repo_path)) | ||
|
|
||
| command = '{} {}'.format(_LATEST_INDEX_SCRIPT, mode) | ||
| result = py_cmd(command, is_module=False, cwd=repo_path) | ||
|
|
||
| output = result.result | ||
| if isinstance(output, bytes): | ||
| output = output.decode('utf-8', errors='replace') | ||
| if output: | ||
| output = output.replace( | ||
| 'python scripts/generate_latest_indices.py generate', | ||
| 'azdev latest-index generate' | ||
| ) | ||
| display(output) | ||
|
|
||
| if result.exit_code: | ||
| sys.exit(result.exit_code) | ||
|
|
||
|
|
||
| def generate_latest_index(cli_path=None, profile='latest', all_profiles=False): | ||
| _run_latest_index('generate', cli_path=cli_path, profile=profile, all_profiles=all_profiles) | ||
|
|
||
|
|
||
| def verify_latest_index(cli_path=None, profile='latest', all_profiles=False): | ||
| _run_latest_index('verify', cli_path=cli_path, profile=profile, all_profiles=all_profiles) | ||
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,78 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| from unittest import mock | ||
| import os | ||
|
|
||
| from knack.util import CLIError, CommandResultItem | ||
|
|
||
| from azdev.operations.latest_index import generate_latest_index, verify_latest_index | ||
|
|
||
|
|
||
| class LatestIndexTestCase(unittest.TestCase): | ||
|
|
||
| @mock.patch('azdev.operations.latest_index.py_cmd') | ||
| @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) | ||
| @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) | ||
| def test_generate_with_explicit_repo_path(self, _, __, mock_py_cmd): | ||
| mock_py_cmd.return_value = CommandResultItem('generated', exit_code=0, error=None) | ||
|
|
||
| generate_latest_index(cli_path='/fake/azure-cli') | ||
|
|
||
| self.assertTrue(mock_py_cmd.called) | ||
| command = mock_py_cmd.call_args.args[0] | ||
| self.assertIn('generate_latest_indices.py generate', command) | ||
| self.assertEqual(os.path.abspath('/fake/azure-cli'), mock_py_cmd.call_args.kwargs['cwd']) | ||
| self.assertFalse(mock_py_cmd.call_args.kwargs['is_module']) | ||
|
|
||
| @mock.patch('azdev.operations.latest_index.py_cmd') | ||
| @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) | ||
| @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) | ||
| @mock.patch('azdev.operations.latest_index.get_cli_repo_path', return_value='/configured/azure-cli') | ||
| def test_verify_uses_configured_repo_path(self, _, __, ___, mock_py_cmd): | ||
| mock_py_cmd.return_value = CommandResultItem('verified', exit_code=0, error=None) | ||
|
|
||
| verify_latest_index() | ||
|
|
||
| self.assertEqual('/configured/azure-cli', mock_py_cmd.call_args.kwargs['cwd']) | ||
|
|
||
| @mock.patch('azdev.operations.latest_index.py_cmd') | ||
| @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) | ||
| @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) | ||
| def test_verify_non_zero_exit_is_propagated(self, _, __, mock_py_cmd): | ||
| # simulate bytes output as returned by py_cmd on failure | ||
| mock_py_cmd.return_value = CommandResultItem(b'stale\r\n', exit_code=1, error='mismatch') | ||
|
|
||
| with self.assertRaises(SystemExit) as ex: | ||
| verify_latest_index(cli_path='/fake/azure-cli') | ||
|
|
||
| self.assertEqual(1, ex.exception.code) | ||
|
|
||
| def test_non_latest_profile_is_rejected(self): | ||
| with self.assertRaises(CLIError): | ||
| generate_latest_index(cli_path='/fake/azure-cli', profile='2019-03-01-hybrid') | ||
|
|
||
| def test_all_profiles_flag_is_rejected(self): | ||
| with self.assertRaises(CLIError): | ||
| verify_latest_index(cli_path='/fake/azure-cli', all_profiles=True) | ||
|
|
||
| @mock.patch('azdev.operations.latest_index.display') | ||
| @mock.patch('azdev.operations.latest_index.py_cmd') | ||
| @mock.patch('azdev.operations.latest_index.os.path.isfile', return_value=True) | ||
| @mock.patch('azdev.operations.latest_index.os.path.isdir', return_value=True) | ||
| def test_bytes_output_decoded_and_hint_replaced(self, _, __, mock_py_cmd, mock_display): | ||
| raw = b'files are out of date\r\nRun:\r\n python scripts/generate_latest_indices.py generate\r\n' | ||
| mock_py_cmd.return_value = CommandResultItem(raw, exit_code=1, error='mismatch') | ||
|
|
||
| with self.assertRaises(SystemExit): | ||
| verify_latest_index(cli_path='/fake/azure-cli') | ||
|
|
||
| displayed = mock_display.call_args.args[0] | ||
| self.assertNotIn("b'", displayed) | ||
| self.assertNotIn('\\r\\n', displayed) | ||
| self.assertNotIn('python scripts/generate_latest_indices.py generate', displayed) | ||
| self.assertIn('azdev latest-index generate', displayed) |
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.
CLIError seems deprecated somehow. https://github.com/Azure/azure-cli/blob/4ce682d944dacd2b650399ef6ed8cd14e9814409/doc/error_handling_guidelines.md#L5