Skip to content
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

{Packaging} Drop mock library #19024

Merged
merged 2 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/sphinx/azhelpgen/azhelpgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import argparse
import json
from mock import patch
from unittest.mock import patch
from os.path import expanduser
from docutils import nodes
from docutils.statemachine import ViewList
Expand Down Expand Up @@ -51,7 +51,7 @@ def make_rst(self):
if help_file.deprecate_info:
yield '{}:deprecated: {}'.format(INDENT, help_file.deprecate_info._get_message(help_file.deprecate_info))
if not is_command:
top_group_name = help_file.command.split()[0] if help_file.command else 'az'
top_group_name = help_file.command.split()[0] if help_file.command else 'az'
yield '{}:docsource: {}'.format(INDENT, doc_source_map[top_group_name] if top_group_name in doc_source_map else '')
else:
top_command_name = help_file.command.split()[0] if help_file.command else ''
Expand All @@ -61,7 +61,7 @@ def make_rst(self):

if is_command and help_file.parameters:
group_registry = ArgumentGroupRegistry(
[p.group_name for p in help_file.parameters if p.group_name])
[p.group_name for p in help_file.parameters if p.group_name])

for arg in sorted(help_file.parameters,
key=lambda p: group_registry.get_group_priority(p.group_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import unittest
import tempfile
import shutil
import mock
from unittest import mock


def get_test_data_file(filename):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import unittest
import shutil
import hashlib
import mock
from unittest import mock
import sys

from azure.cli.core.util import CLIError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import os
import mock
from unittest import mock
import unittest
from requests.exceptions import ConnectionError, HTTPError
from azure.cli.core.util import CLIError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# pylint: disable=line-too-long
import datetime
import unittest
import unittest.mock as mock
from unittest import mock
from unittest.mock import MagicMock

from azure.cli.core.adal_authentication import AdalAuthentication, _try_scopes_to_resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# --------------------------------------------------------------------------------------------

import unittest
import mock
from unittest import mock

from azure.cli.core.profiles import (ResourceType, PROFILE_TYPE, CustomResourceType,
get_api_version, supported_api_version, register_resource_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import unittest

import mock
from unittest import mock
import os
import tempfile

Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import tempfile
import unittest
import mock
from unittest import mock
import multiprocessing
import configparser

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_get_error_type(self):
self.assertEqual(result_error_type, expected_error_type.value)

def test_get_parameter_mappings(self):
import mock
from unittest import mock
from azure.cli.core import AzCommandsLoader
from azure.cli.core.mock import DummyCli
from azure.cli.core.command_recommender import get_parameter_mappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import sys
import logging
import mock
from unittest import mock
import unittest
from collections import namedtuple

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import os
import logging
import unittest
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from unittest.mock import MagicMock

import azure.cli.core._debug as _debug
import azure.cli.core.util as cli_util
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import shutil
import zipfile

import mock
from unittest import mock

from azure.cli.core.extension import (get_extensions, build_extension_path, extension_exists,
get_extension, get_extension_names, get_extension_modname, ext_compat_with_cli,
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from inspect import getmembers as inspect_getmembers

import unittest
import mock
from unittest import mock
import tempfile

from knack.help import GroupHelpFile, HelpAuthoringException
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os
import shutil
from knack.util import CLIError
import mock
from unittest import mock

from azure.cli.core.keys import generate_ssh_keys

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import mock
from unittest import mock
import unittest

from azure.cli.core.local_context import AzCLILocalContext, ALL
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import mock
from unittest import mock
import unittest
import difflib
from io import StringIO
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import sys
import unittest
import mock
from unittest import mock
import re

from copy import deepcopy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import sys
import unittest
import mock
from unittest import mock
import re

from copy import deepcopy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_extract_parameters_correctly(self):
self.assertEqual(['-g', '--name', '-d', '--debug'], AzCliCommandInvoker._extract_parameter_names(args))

def test_cloud_forbid_telemetry(self):
import mock
from unittest import mock
import azure.cli.core.telemetry as telemetry
from azure.cli.core.mock import DummyCli
from knack.completion import ARGCOMPLETE_ENV_NAME
Expand Down
42 changes: 21 additions & 21 deletions src/azure-cli-core/azure/cli/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import sys
import unittest
import mock
from unittest import mock
import tempfile
import json

Expand Down Expand Up @@ -270,20 +270,20 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
generated_client_request_id_name=None)

get_raw_token_mock.assert_not_called()
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
Copy link
Member Author

@jiasli jiasli Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args and kwargs are not available in Python 3.6.

https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args

Changed in version 3.8: Added args and kwargs properties.

self.assertEqual(request.method, 'PUT')
self.assertEqual(request.url, 'https://myaccount.blob.core.windows.net/mycontainer/myblob?timeout=30&sv=2019-02-02&srt=s&ss=bf')
self.assertEqual(request.body, '{"b1": "v1"}')
# Verify no Authorization header
self.assertDictEqual(dict(request.headers), expected_header)
self.assertEqual(send_mock.call_args.kwargs["verify"], not should_disable_connection_verify())
self.assertEqual(send_mock.call_args[1]["verify"], not should_disable_connection_verify())

# Test Authorization header is skipped
send_raw_request(cli_ctx, 'GET', full_arm_rest_url, body=test_body, skip_authorization_header=True,
generated_client_request_id_name=None)

get_raw_token_mock.assert_not_called()
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertDictEqual(dict(request.headers), expected_header)

# Test Authorization header is already provided
Expand All @@ -292,7 +292,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
generated_client_request_id_name=None)

get_raw_token_mock.assert_not_called()
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertDictEqual(dict(request.headers), {**expected_header, 'Authorization': 'Basic ABCDE'})

# Test Authorization header is auto appended
Expand All @@ -301,7 +301,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
generated_client_request_id_name=None)

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertDictEqual(dict(request.headers), expected_header_with_auth)

# Test ARM Subscriptions - List
Expand All @@ -311,7 +311,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
generated_client_request_id_name=None)

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.url, test_arm_endpoint.rstrip('/') + '/subscriptions?api-version=2020-01-01')
self.assertDictEqual(dict(request.headers), expected_header_with_auth)

Expand All @@ -322,7 +322,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
generated_client_request_id_name=None)

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.url, test_arm_endpoint.rstrip('/') + '/tenants?api-version=2020-01-01')
self.assertDictEqual(dict(request.headers), expected_header_with_auth)

Expand All @@ -332,7 +332,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
generated_client_request_id_name=None)

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.url, full_arm_rest_url)
self.assertDictEqual(dict(request.headers), expected_header_with_auth)

Expand All @@ -341,7 +341,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
send_raw_request(cli_ctx, 'GET', full_arm_rest_url)

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.url, full_arm_rest_url)

# Test full ARM URL with port
Expand All @@ -351,7 +351,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
send_raw_request(cli_ctx, 'GET', full_arm_rest_url_with_port)

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.url, 'https://management.azure.com:443/subscriptions/00000001-0000-0000-0000-000000000000/resourcegroups/02?api-version=2019-07-01')

# Test non-ARM APIs
Expand All @@ -360,15 +360,15 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
url = 'https://graph.windows.net/00000002-0000-0000-0000-000000000000/applications/00000003-0000-0000-0000-000000000000?api-version=1.6'
send_raw_request(cli_ctx, 'PATCH', url, body=test_body, generated_client_request_id_name=None)
get_raw_token_mock.assert_called_with(mock.ANY, 'https://graph.windows.net/')
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.method, 'PATCH')
self.assertEqual(request.url, url)

# Test MS Graph API https://graph.microsoft.com/beta/appRoleAssignments/01
url = 'https://graph.microsoft.com/beta/appRoleAssignments/01'
send_raw_request(cli_ctx, 'PATCH', url, body=test_body, generated_client_request_id_name=None)
get_raw_token_mock.assert_called_with(mock.ANY, 'https://graph.microsoft.com/')
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.method, 'PATCH')
self.assertEqual(request.url, url)

Expand All @@ -377,7 +377,7 @@ def test_send_raw_requests(self, send_mock, get_raw_token_mock):
send_raw_request(cli_ctx, 'GET', full_arm_rest_url, headers={'user-agent=ARG-UA'})

get_raw_token_mock.assert_called_with(mock.ANY, test_arm_active_directory_resource_id, subscription=subscription_id)
request = send_mock.call_args.args[1]
request = send_mock.call_args[0][1]
self.assertEqual(request.headers['User-Agent'], get_az_rest_user_agent() + ' env-ua ARG-UA')

def test_scopes_to_resource(self):
Expand Down Expand Up @@ -496,7 +496,7 @@ def test_handle_exception_clierror(self, mock_logger_error):

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(err_msg, mock_logger_error.call_args.args[0])
self.assertIn(err_msg, mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
Expand All @@ -514,7 +514,7 @@ def test_handle_exception_clouderror(self, mock_logger_error):

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(mock_cloud_error.args[0], mock_logger_error.call_args.args[0])
self.assertIn(mock_cloud_error.args[0], mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
Expand All @@ -531,8 +531,8 @@ def test_handle_exception_httpoperationerror_typical_response_error(self, mock_l

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(err_msg, mock_logger_error.call_args.args[0])
self.assertIn(err_code, mock_logger_error.call_args.args[0])
self.assertIn(err_msg, mock_logger_error.call_args[0][0])
self.assertIn(err_code, mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
Expand All @@ -552,7 +552,7 @@ def test_handle_exception_httpoperationerror_error_key_has_string_value(self, mo

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(expected_message, mock_logger_error.call_args.args[0])
self.assertIn(expected_message, mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
Expand All @@ -570,7 +570,7 @@ def test_handle_exception_httpoperationerror_no_error_key(self, mock_logger_erro

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(str(mock.call(mock_http_error).args[0]), mock_logger_error.call_args.args[0])
self.assertIs(str(mock_http_error), mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@mock.patch('azure.cli.core.azclierror.logger.error', autospec=True)
Expand All @@ -587,7 +587,7 @@ def test_handle_exception_httpoperationerror_no_response_text(self, mock_logger_

# test behavior
self.assertTrue(mock_logger_error.called)
self.assertIn(str(mock.call(mock_http_error).args[0]), mock_logger_error.call_args.args[0])
self.assertIn(str(mock_http_error), mock_logger_error.call_args[0][0])
self.assertEqual(ex_result, 1)

@staticmethod
Expand Down
7 changes: 0 additions & 7 deletions src/azure-cli-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@
if not sys.platform.startswith('cygwin'):
DEPENDENCIES.append('psutil~=5.8')

TESTS_REQUIRE = [
'mock'
]

with open('README.rst', 'r', encoding='utf-8') as f:
README = f.read()
Expand All @@ -87,9 +84,5 @@
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests", "azure", "azure.cli"]),
install_requires=DEPENDENCIES,
python_requires='>=3.6.0',
extras_require={
"test": TESTS_REQUIRE,
},
tests_require=TESTS_REQUIRE,
package_data={'azure.cli.core': ['auth_landing_pages/*.html']}
)
1 change: 0 additions & 1 deletion src/azure-cli-core/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ skip_missing_interpreters = True

[testenv]
deps = pytest
mock
pip
-e ../azure-cli-telemetry
commands = pytest
Loading