Deprecate rally.common.yamlutils and rally.common.fileutils

* rally.common.fileutils contains stuff that related only to
  rally.cli.envutils + openstack related helper

* rally.common.yamlutils was designed for CLI purpose and should not be
  places under rally.common.

Change-Id: I9d639d2baa9298a26e7670c5b4b356a8b56583a2
This commit is contained in:
Andrey Kurilin 2020-03-22 00:44:08 +02:00
parent 4a466f2bc5
commit 30d5a8ed04
18 changed files with 163 additions and 195 deletions

View File

@ -45,6 +45,11 @@ Deprecated
* Module *rally.common.sshutils* is deprecated. Use *rally.utils.sshutils*
instead.
* Module *rally.common.yamlutils* is deprecated. It was designed for CLI usage
and moves to right place.
* Module *rally.common.fileutils* is deprecated.
* All modules from *rally.plugins.common.contexts* are deprecated. Use
*rally.plugins.task.contexts* instead.

View File

@ -25,10 +25,9 @@ import jsonschema
from rally.cli import cliutils
from rally.cli import envutils
from rally.common import fileutils
from rally.cli import yamlutils as yaml
from rally.common import logging
from rally.common import utils
from rally.common import yamlutils as yaml
from rally.env import env_mgr
from rally import exceptions
from rally import plugins
@ -326,10 +325,10 @@ class DeploymentCommands(object):
return 1
print("Using deployment: %s" % deployment["uuid"])
fileutils.update_globals_file(envutils.ENV_DEPLOYMENT,
deployment["uuid"])
fileutils.update_globals_file(envutils.ENV_ENV,
deployment["uuid"])
envutils.update_globals_file(envutils.ENV_DEPLOYMENT,
deployment["uuid"])
envutils.update_globals_file(envutils.ENV_ENV,
deployment["uuid"])
if "openstack" in deployment["credentials"]:
creds = deployment["credentials"]["openstack"][0]

View File

@ -21,8 +21,7 @@ import traceback
from rally.cli import cliutils
from rally.cli import envutils
from rally.common import fileutils
from rally.common import yamlutils as yaml
from rally.cli import yamlutils as yaml
from rally.env import env_mgr
from rally import exceptions
@ -336,4 +335,4 @@ class EnvCommands(object):
def _use(self, env_uuid, to_json):
_print("Using environment: %s" % env_uuid, to_json)
fileutils.update_globals_file(envutils.ENV_ENV, env_uuid)
envutils.update_globals_file(envutils.ENV_ENV, env_uuid)

View File

@ -28,11 +28,10 @@ import jsonschema
from rally.cli import cliutils
from rally.cli import envutils
from rally.common import fileutils
from rally.cli import yamlutils as yaml
from rally.common import logging
from rally.common import utils as rutils
from rally.common import version
from rally.common import yamlutils as yaml
from rally import consts
from rally import exceptions
from rally import plugins
@ -926,7 +925,7 @@ class TaskCommands(object):
print("Using task: %s" % task_id)
api.task.get(task_id=task_id)
fileutils.update_globals_file("RALLY_TASK", task_id)
envutils.update_globals_file("RALLY_TASK", task_id)
@cliutils.args("--uuid", dest="tasks", nargs="+", type=str,
help="UUIDs of tasks or json reports of tasks")

View File

@ -25,9 +25,8 @@ import webbrowser
from rally.cli import cliutils
from rally.cli import envutils
from rally.common import fileutils
from rally.cli import yamlutils as yaml
from rally.common import logging
from rally.common import yamlutils as yaml
from rally import exceptions
from rally import plugins
@ -151,7 +150,7 @@ class VerifyCommands(object):
def use_verifier(self, api, verifier_id):
"""Choose a verifier to use for the future operations."""
verifier = api.verifier.get(verifier_id=verifier_id)
fileutils.update_globals_file(envutils.ENV_VERIFIER, verifier["uuid"])
envutils.update_globals_file(envutils.ENV_VERIFIER, verifier["uuid"])
print("Using verifier '%s' (UUID=%s) as the default verifier "
"for the future CLI operations."
% (verifier["name"], verifier["uuid"]))
@ -536,7 +535,7 @@ class VerifyCommands(object):
verification = api.verification.get(
verification_uuid=verification_uuid)
fileutils.update_globals_file(
envutils.update_globals_file(
envutils.ENV_VERIFICATION, verification["uuid"])
print("Using verification (UUID=%s) as the default verification "
"for the future operations." % verification["uuid"])

View File

@ -17,7 +17,6 @@ import os
import decorator
from rally.common import fileutils
from rally import exceptions
PATH_GLOBALS = "~/.rally/globals"
@ -31,10 +30,78 @@ ENVVARS = [ENV_ENV, ENV_DEPLOYMENT, ENV_TASK, ENV_VERIFIER, ENV_VERIFICATION]
MSG_MISSING_ARG = "Missing argument: --%(arg_name)s"
def _read_env_file(path, except_env=None):
"""Read the environment variable file.
:param path: the path of the file
:param except_env: the environment variable to avoid in the output
:returns: the content of the original file except the line starting with
the except_env parameter
"""
output = []
if os.path.exists(path):
with open(path, "r") as env_file:
content = env_file.readlines()
for line in content:
if except_env is None or not line.startswith("%s=" %
except_env):
output.append(line)
return output
def _load_env_file(path):
"""Load the environment variable file into os.environ.
:param path: the path of the file
"""
if os.path.exists(path):
content = _read_env_file(path)
for line in content:
(key, sep, value) = line.partition("=")
os.environ[key] = value.rstrip()
def _rewrite_env_file(path, initial_content):
"""Rewrite the environment variable file.
:param path: the path of the file
:param initial_content: the original content of the file
"""
with open(path, "w+") as env_file:
for line in initial_content:
env_file.write(line)
def _update_env_file(path, env_key, env_value):
"""Update the environment variable file.
:param path: the path of the file
:param env_key: the key to update
:param env_value: the value of the property to update
"""
output = _read_env_file(path, env_key)
output.append("%s=%s" % (env_key, env_value))
_rewrite_env_file(path, output)
def update_globals_file(key, value):
"""Update the globals variables file.
:param key: the key to update
:param value: the value to update
"""
dir = os.path.expanduser("~/.rally/")
if not os.path.exists(dir):
os.makedirs(dir)
expanded_path = os.path.join(dir, "globals")
_update_env_file(expanded_path, key, "%s\n" % value)
def clear_global(global_key):
path = os.path.expanduser(PATH_GLOBALS)
if os.path.exists(path):
fileutils.update_env_file(path, global_key, "\n")
_update_env_file(path, global_key, "\n")
if global_key in os.environ:
os.environ.pop(global_key)
@ -46,7 +113,7 @@ def clear_env():
def get_global(global_key, do_raise=False):
if global_key not in os.environ:
fileutils.load_env_file(os.path.expanduser(PATH_GLOBALS))
_load_env_file(os.path.expanduser(PATH_GLOBALS))
value = os.environ.get(global_key)
if not value and do_raise:
raise exceptions.InvalidArgumentsException("%s env is missing"

View File

@ -15,75 +15,14 @@
import os
import tempfile
import warnings
import zipfile
def _read_env_file(path, except_env=None):
"""Read the environment variable file.
:param path: the path of the file
:param except_env: the environment variable to avoid in the output
:returns: the content of the original file except the line starting with
the except_env parameter
"""
output = []
if os.path.exists(path):
with open(path, "r") as env_file:
content = env_file.readlines()
for line in content:
if except_env is None or not line.startswith("%s=" %
except_env):
output.append(line)
return output
def load_env_file(path):
"""Load the environment variable file into os.environ.
:param path: the path of the file
"""
if os.path.exists(path):
content = _read_env_file(path)
for line in content:
(key, sep, value) = line.partition("=")
os.environ[key] = value.rstrip()
def _rewrite_env_file(path, initial_content):
"""Rewrite the environment variable file.
:param path: the path of the file
:param initial_content: the original content of the file
"""
with open(path, "w+") as env_file:
for line in initial_content:
env_file.write(line)
def update_env_file(path, env_key, env_value):
"""Update the environment variable file.
:param path: the path of the file
:param env_key: the key to update
:param env_value: the value of the property to update
"""
output = _read_env_file(path, env_key)
output.append("%s=%s" % (env_key, env_value))
_rewrite_env_file(path, output)
def update_globals_file(key, value):
"""Update the globals variables file.
:param key: the key to update
:param value: the value to update
"""
dir = os.path.expanduser("~/.rally/")
if not os.path.exists(dir):
os.makedirs(dir)
expanded_path = os.path.join(dir, "globals")
update_env_file(expanded_path, key, "%s\n" % value)
warnings.warn(
f"Module `{__name__}` is deprecated since Rally v3.0.0 and may be "
f"removed in further releases."
)
def pack_dir(source_directory, zip_name=None):

View File

@ -156,6 +156,7 @@ class RAMInt(object):
self.__int.value = 0
@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0")
def get_method_class(func):
"""Return the class that defined the given method.
@ -178,6 +179,7 @@ def get_method_class(func):
return None
@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0")
def first_index(lst, predicate):
"""Return the index of the first element that matches a predicate.
@ -212,6 +214,7 @@ def retry(times, func, *args, **kwargs):
raise
@logging.log_deprecated("it is openstack specific.", rally_version="3.0.0")
def iterate_per_tenants(users):
"""Iterate of a single arbitrary user from each tenant
@ -425,6 +428,7 @@ def make_name_matcher(*names):
return CustomNameMatcher
@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0")
def merge(length, *sources):
"""Merge lists of lists.

View File

@ -243,8 +243,8 @@ class DeploymentCommandsTestCase(test.TestCase):
@mock.patch("os.remove")
@mock.patch("os.symlink")
@mock.patch("os.path.exists", return_value=True)
@mock.patch("rally.common.fileutils.update_env_file")
def test_use(self, mock_update_env_file, mock_path_exists,
@mock.patch("rally.cli.envutils._update_env_file")
def test_use(self, mock__update_env_file, mock_path_exists,
mock_symlink, mock_remove):
deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b"
self.fake_api.deployment.get.return_value = {
@ -262,7 +262,7 @@ class DeploymentCommandsTestCase(test.TestCase):
create=True) as mock_file:
self.deployment.use(self.fake_api, deployment_id)
self.assertEqual(3, mock_path_exists.call_count)
mock_update_env_file.assert_has_calls([
mock__update_env_file.assert_has_calls([
mock.call(os.path.expanduser("~/.rally/globals"),
"RALLY_DEPLOYMENT", "%s\n" % deployment_id),
mock.call(os.path.expanduser("~/.rally/globals"),
@ -286,8 +286,8 @@ class DeploymentCommandsTestCase(test.TestCase):
@mock.patch("os.remove")
@mock.patch("os.symlink")
@mock.patch("os.path.exists", return_value=True)
@mock.patch("rally.common.fileutils.update_env_file")
def test_use_with_v3_auth(self, mock_update_env_file, mock_path_exists,
@mock.patch("rally.cli.envutils._update_env_file")
def test_use_with_v3_auth(self, mock__update_env_file, mock_path_exists,
mock_symlink, mock_remove):
deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b"
@ -308,7 +308,7 @@ class DeploymentCommandsTestCase(test.TestCase):
create=True) as mock_file:
self.deployment.use(self.fake_api, deployment_id)
self.assertEqual(3, mock_path_exists.call_count)
mock_update_env_file.assert_has_calls([
mock__update_env_file.assert_has_calls([
mock.call(os.path.expanduser("~/.rally/globals"),
"RALLY_DEPLOYMENT", "%s\n" % deployment_id),
mock.call(os.path.expanduser("~/.rally/globals"),
@ -334,7 +334,7 @@ class DeploymentCommandsTestCase(test.TestCase):
@mock.patch("rally.cli.commands.deployment.DeploymentCommands."
"_update_openrc_deployment_file")
@mock.patch("rally.common.fileutils.update_globals_file")
@mock.patch("rally.cli.envutils.update_globals_file")
def test_use_by_name(self, mock_update_globals_file,
mock__update_openrc_deployment_file):
fake_credentials = {"admin": "foo_admin", "users": ["foo_user"]}

View File

@ -494,7 +494,7 @@ class EnvCommandsTestCase(test.TestCase):
mock__use.assert_has_calls(
[mock.call("aa", False), mock.call("bb", True)])
@mock.patch("rally.cli.commands.env.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.env.envutils.update_globals_file")
@mock.patch("rally.cli.commands.env.print")
def test__use(self, mock_print, mock_update_globals_file):
self.env._use("aa", True)

View File

@ -26,7 +26,7 @@ import rally
from rally import api
from rally.cli import cliutils
from rally.cli.commands import task
from rally.common import yamlutils as yaml
from rally.cli import yamlutils as yaml
from rally import consts
from rally import exceptions
from tests.unit import fakes
@ -859,7 +859,7 @@ class TaskCommandsTestCase(test.TestCase):
deployment="deployment",
config=mock__load_and_validate_task.return_value)
@mock.patch("rally.common.fileutils._rewrite_env_file")
@mock.patch("rally.cli.envutils._rewrite_env_file")
def test_use(self, mock__rewrite_env_file):
task_id = "80422553-5774-44bd-98ac-38bd8c7a0feb"
self.task.use(self.fake_api, task_id)

View File

@ -129,7 +129,7 @@ class VerifyCommandsTestCase(test.TestCase):
self.fake_api.verifier.list_plugins.assert_called_once_with(
platform="some")
@mock.patch("rally.cli.commands.verify.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.envutils.update_globals_file")
def test_create_verifier(self, mock_update_globals_file):
self.fake_api.verifier.create.return_value = self.verifier_uuid
self.fake_api.verifier.get.return_value = self.verifier_data
@ -146,7 +146,7 @@ class VerifyCommandsTestCase(test.TestCase):
mock_update_globals_file.assert_called_once_with(
envutils.ENV_VERIFIER, self.verifier_uuid)
@mock.patch("rally.cli.commands.verify.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.envutils.update_globals_file")
def test_use_verifier(self, mock_update_globals_file):
self.fake_api.verifier.get.return_value = self.verifier_data
self.verify.use_verifier(self.fake_api, self.verifier_uuid)
@ -380,7 +380,7 @@ class VerifyCommandsTestCase(test.TestCase):
self.fake_api.verifier.delete_extension.assert_called_once_with(
verifier_id=self.verifier_uuid, name="ext_name")
@mock.patch("rally.cli.commands.verify.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.envutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.os.path.exists")
def test_start(self, mock_exists, mock_update_globals_file):
self.verify.start(self.fake_api, self.verifier_uuid,
@ -462,7 +462,7 @@ class VerifyCommandsTestCase(test.TestCase):
self.assertFalse(mock_update_globals_file.called)
@mock.patch("rally.cli.commands.verify.os.path.exists")
@mock.patch("rally.cli.commands.verify.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.envutils.update_globals_file")
def test_start_on_unfinished_deployment(self, mock_update_globals_file,
mock_exists):
deployment_id = self.deployment_uuid
@ -476,7 +476,7 @@ class VerifyCommandsTestCase(test.TestCase):
1, self.verify.start(self.fake_api,
self.deployment_uuid, deployment_id))
@mock.patch("rally.cli.commands.verify.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.envutils.update_globals_file")
def test_use(self, mock_update_globals_file):
self.fake_api.verification.get.return_value = self.verification_data
self.verify.use(self.fake_api, self.verification_uuid)
@ -485,7 +485,7 @@ class VerifyCommandsTestCase(test.TestCase):
mock_update_globals_file.assert_called_once_with(
envutils.ENV_VERIFICATION, self.verification_uuid)
@mock.patch("rally.cli.commands.verify.fileutils.update_globals_file")
@mock.patch("rally.cli.commands.verify.envutils.update_globals_file")
def test_rerun(self, mock_update_globals_file):
self.fake_api.verification.rerun.return_value = {
"verification": self.verification_data,

View File

@ -24,6 +24,27 @@ from tests.unit import test
class EnvUtilsTestCase(test.TestCase):
@mock.patch("os.path.exists", return_value=True)
@mock.patch.dict("os.environ", values={}, clear=True)
def test_load_env_vile(self, mock_exists):
file_data = "FAKE_ENV=fake_env\n"
with mock.patch("rally.cli.envutils.open", mock.mock_open(
read_data=file_data), create=True) as mock_file:
envutils._load_env_file("path_to_file")
self.assertIn("FAKE_ENV", os.environ)
mock_file.return_value.readlines.assert_called_once_with()
@mock.patch("os.path.exists", return_value=True)
def test_update_env_file(self, mock_exists):
file_data = "FAKE_ENV=old_value\nFAKE_ENV2=any\n"
with mock.patch("rally.cli.envutils.open", mock.mock_open(
read_data=file_data), create=True) as mock_file:
envutils._update_env_file("path_to_file", "FAKE_ENV", "new_value")
calls = [mock.call("FAKE_ENV2=any\n"), mock.call(
"FAKE_ENV=new_value")]
mock_file.return_value.readlines.assert_called_once_with()
mock_file.return_value.write.assert_has_calls(calls)
def test_default_from_global(self):
@envutils.default_from_global("test_arg_name",
@ -46,18 +67,18 @@ class EnvUtilsTestCase(test.TestCase):
self.assertEqual("my_deployment_id", deployment_id)
@mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file")
def test_get_deployment_id_with_exception(self, mock_load_env_file):
@mock.patch("rally.cli.envutils._load_env_file")
def test_get_deployment_id_with_exception(self, mock__load_env_file):
self.assertRaises(exceptions.InvalidArgumentsException,
envutils.get_global, envutils.ENV_DEPLOYMENT, True)
mock_load_env_file.assert_called_once_with(os.path.expanduser(
mock__load_env_file.assert_called_once_with(os.path.expanduser(
"~/.rally/globals"))
@mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file")
def test_get_deployment_id_with_none(self, mock_load_env_file):
@mock.patch("rally.cli.envutils._load_env_file")
def test_get_deployment_id_with_none(self, mock__load_env_file):
self.assertIsNone(envutils.get_global(envutils.ENV_DEPLOYMENT))
mock_load_env_file.assert_called_once_with(os.path.expanduser(
mock__load_env_file.assert_called_once_with(os.path.expanduser(
"~/.rally/globals"))
@mock.patch.dict(os.environ, values={envutils.ENV_TASK: "my_task_id"},
@ -66,29 +87,29 @@ class EnvUtilsTestCase(test.TestCase):
self.assertEqual("my_task_id", envutils.get_global(envutils.ENV_TASK))
@mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file")
def test_get_task_id_with_exception(self, mock_load_env_file):
@mock.patch("rally.cli.envutils._load_env_file")
def test_get_task_id_with_exception(self, mock__load_env_file):
self.assertRaises(exceptions.InvalidArgumentsException,
envutils.get_global, envutils.ENV_TASK, True)
mock_load_env_file.assert_called_once_with(os.path.expanduser(
mock__load_env_file.assert_called_once_with(os.path.expanduser(
"~/.rally/globals"))
@mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file")
def test_get_task_id_with_none(self, mock_load_env_file):
@mock.patch("rally.cli.envutils._load_env_file")
def test_get_task_id_with_none(self, mock__load_env_file):
self.assertIsNone(envutils.get_global("RALLY_TASK"))
mock_load_env_file.assert_called_once_with(os.path.expanduser(
mock__load_env_file.assert_called_once_with(os.path.expanduser(
"~/.rally/globals"))
@mock.patch.dict(os.environ,
values={envutils.ENV_DEPLOYMENT: "test_deployment_id"},
clear=True)
@mock.patch("os.path.exists")
@mock.patch("rally.cli.envutils.fileutils.update_env_file",
@mock.patch("rally.cli.envutils._update_env_file",
return_value=True)
def test_clear_global(self, mock_update_env_file, mock_path_exists):
def test_clear_global(self, mock__update_env_file, mock_path_exists):
envutils.clear_global(envutils.ENV_DEPLOYMENT)
mock_update_env_file.assert_called_once_with(os.path.expanduser(
mock__update_env_file.assert_called_once_with(os.path.expanduser(
"~/.rally/globals"), envutils.ENV_DEPLOYMENT, "\n")
self.assertEqual({}, os.environ)
@ -97,8 +118,8 @@ class EnvUtilsTestCase(test.TestCase):
envutils.ENV_TASK: "test_task_id"},
clear=True)
@mock.patch("os.path.exists")
@mock.patch("rally.cli.envutils.fileutils.update_env_file",
@mock.patch("rally.cli.envutils._update_env_file",
return_value=True)
def test_clear_env(self, mock_update_env_file, mock_path_exists):
def test_clear_env(self, mock__update_env_file, mock_path_exists):
envutils.clear_env()
self.assertEqual({}, os.environ)

View File

@ -12,7 +12,7 @@
from yaml import constructor
from rally.common import yamlutils
from rally.cli import yamlutils
from tests.unit import test

View File

@ -1,64 +0,0 @@
# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
from unittest import mock
from rally.common import fileutils
from tests.unit import test
class FileUtilsTestCase(test.TestCase):
@mock.patch("os.path.exists", return_value=True)
@mock.patch.dict("os.environ", values={}, clear=True)
def test_load_env_vile(self, mock_exists):
file_data = "FAKE_ENV=fake_env\n"
with mock.patch("rally.common.fileutils.open", mock.mock_open(
read_data=file_data), create=True) as mock_file:
fileutils.load_env_file("path_to_file")
self.assertIn("FAKE_ENV", os.environ)
mock_file.return_value.readlines.assert_called_once_with()
@mock.patch("os.path.exists", return_value=True)
def test_update_env_file(self, mock_exists):
file_data = "FAKE_ENV=old_value\nFAKE_ENV2=any\n"
with mock.patch("rally.common.fileutils.open", mock.mock_open(
read_data=file_data), create=True) as mock_file:
fileutils.update_env_file("path_to_file", "FAKE_ENV", "new_value")
calls = [mock.call("FAKE_ENV2=any\n"), mock.call(
"FAKE_ENV=new_value")]
mock_file.return_value.readlines.assert_called_once_with()
mock_file.return_value.write.assert_has_calls(calls)
class PackDirTestCase(test.TestCase):
@mock.patch("os.walk")
@mock.patch("zipfile.ZipFile")
def test_pack_dir(self, mock_zip_file, mock_walk):
mock_walk.side_effect = [
[("foo_root", [], ["file1", "file2", "file3"])]]
fileutils.pack_dir("rally-jobs/extra/murano/HelloReporter",
"fake_dir/package.zip")
mock_zip_file.assert_called_once_with("fake_dir/package.zip",
mode="w")
mock_walk.assert_called_once_with(
"rally-jobs/extra/murano/HelloReporter")
mock_zip_file.return_value.assert_has_calls(
[mock.call.write("foo_root/file1", "../../../../foo_root/file1"),
mock.call.write("foo_root/file2", "../../../../foo_root/file2"),
mock.call.write("foo_root/file3", "../../../../foo_root/file3"),
mock.call.close()])

View File

@ -241,7 +241,7 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor):
self.generic_visit(node)
def check_name(self, arg, dec_vars):
return (dec_vars is not None and arg in dec_vars)
return dec_vars is not None and arg in dec_vars
def visit_FunctionDef(self, node):
self.generic_visit(node)
@ -263,19 +263,19 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor):
shortest_name = sorted_by_len.pop()
if len(shortest_name) <= self.SHORTEST_VARIANT_LEN_LIMIT:
error_msgs.append(
("Argument '%(arg)s' misnamed; should be either "
"of %(dec)s that is derived from the mock "
"decorator args.\n") % {"arg": arg,
"dec": dec_vars})
(f"Argument 'mock_{arg}' misnamed; should be "
f"either of {dec_vars} that is derived from the "
f"mock decorator args.\n")
)
elif not arg:
error_msgs.append(
"Missing or malformed argument for %s decorator."
% dec_vars)
f"Missing or malformed argument for {dec_vars} "
f"decorator.")
mismatched = True
elif not dec_vars:
error_msgs.append(
"Missing or malformed decorator for '%s' argument."
% arg)
f"Missing or malformed decorator for 'mock_{arg}' "
f"argument.")
mismatched = True
if error_msgs:

View File

@ -337,7 +337,7 @@ def test_func(self, mock_args, mock_args2, mock_some_longer_args):
{
"lineno": lineno,
"messages": [
"Argument 'bar_foo_misnamed' misnamed; should be "
"Argument 'mock_bar_foo_misnamed' misnamed; should be "
"either of %s that is derived from the mock decorator "
"args.\n" % variants
],
@ -377,10 +377,10 @@ def test_func(self, mock_args, mock_args2, mock_some_longer_args):
{
"lineno": lineno,
"messages": [
"Argument 'bar_foo_misnamed' misnamed; should be "
"Argument 'mock_bar_foo_misnamed' misnamed; should be "
"either of %s that is derived from the mock decorator "
"args.\n" % variants,
"Missing or malformed decorator for 'mismatched' "
"Missing or malformed decorator for 'mock_mismatched' "
"argument."
],
"args": self.visitor._get_mock_args.return_value,