From bb24320ba29efdd45b5e143a0b750a55c37901d0 Mon Sep 17 00:00:00 2001 From: Takashi NATSUME Date: Fri, 28 Jul 2017 16:06:25 +0900 Subject: [PATCH] Amend uuid4 hacking rule Starting from oslo.utils 3.20.0, oslo_utils.uuidutils.generate_uuid can return "uuid.uuid4().hex" string. So the following rule has been amended to use "oslo_utils.uuidutils.generate_uuid(dashed=False)" instead of "uuid.uuid4().hex". * N357: Use oslo_utils.uuidutils or uuidsentinel (in case of test cases) to generate UUID instead of uuid4(). Change-Id: I990cbfcefb33cc52d70e9be80c780593c536397a --- nova/hacking/checks.py | 6 ++---- nova/tests/unit/test_hacking.py | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index bc6fcddca9..0a460dfb30 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -97,6 +97,7 @@ doubled_words_re = re.compile( log_remove_context = re.compile( r"(.)*LOG\.(.*)\(.*(context=[_a-zA-Z0-9].*)+.*\)") return_not_followed_by_space = re.compile(r"^\s*return(?:\(|{|\"|'|#).*$") +uuid4_re = re.compile(r"uuid4\(\)($|[^\.]|\.hex)") class BaseASTChecker(ast.NodeVisitor): @@ -779,10 +780,7 @@ def check_uuid4(logical_line): msg = ("N357: Use oslo_utils.uuidutils or uuidsentinel(in case of test " "cases) to generate UUID instead of uuid4().") - if "uuid4()." in logical_line: - return - - if "uuid4()" in logical_line: + if uuid4_re.search(logical_line): yield (0, msg) diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 47b4ce518f..82b822a793 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -758,12 +758,12 @@ class HackingTestCase(test.NoDBTestCase): def test_check_uuid4(self): code = """ fake_uuid = uuid.uuid4() + hex_uuid = uuid.uuid4().hex """ - errors = [(1, 0, 'N357')] + errors = [(1, 0, 'N357'), (2, 0, 'N357')] self._assert_has_errors(code, checks.check_uuid4, expected_errors=errors) code = """ - hex_uuid = uuid.uuid4().hex int_uuid = uuid.uuid4().int urn_uuid = uuid.uuid4().urn variant_uuid = uuid.uuid4().variant