From 891f4f3b2d1b2fcab1b64e0ea942d186f98b7406 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 21 Feb 2018 20:09:13 -0800 Subject: [PATCH] Upgrade more code-smell tests. (#36560) * Enhance no-dict-* code-smell tests. * Enhance no-basestring code-smell test. * Enhance no-get-exception code-smell test. * Enhance empty-init code-smell test. * Enhance required-and-default-attribute test. * Remove unused code-smell test. --- test/sanity/code-smell/empty-init.json | 11 +++++ test/sanity/code-smell/empty-init.py | 30 +++++++++++++ test/sanity/code-smell/empty-init.sh | 20 --------- .../code-smell/inappropriately-private.sh | 18 -------- test/sanity/code-smell/no-basestring.json | 6 +++ test/sanity/code-smell/no-basestring.py | 28 +++++++++++++ test/sanity/code-smell/no-basestring.sh | 18 -------- test/sanity/code-smell/no-dict-iteritems.json | 6 +++ test/sanity/code-smell/no-dict-iteritems.py | 28 +++++++++++++ test/sanity/code-smell/no-dict-iteritems.sh | 19 --------- test/sanity/code-smell/no-dict-iterkeys.json | 6 +++ test/sanity/code-smell/no-dict-iterkeys.py | 28 +++++++++++++ test/sanity/code-smell/no-dict-iterkeys.sh | 22 ---------- .../sanity/code-smell/no-dict-itervalues.json | 6 +++ test/sanity/code-smell/no-dict-itervalues.py | 28 +++++++++++++ test/sanity/code-smell/no-dict-itervalues.sh | 19 --------- test/sanity/code-smell/no-get-exception.json | 6 +++ test/sanity/code-smell/no-get-exception.py | 42 +++++++++++++++++++ test/sanity/code-smell/no-get-exception.sh | 34 --------------- .../required-and-default-attributes.json | 9 ++++ .../required-and-default-attributes.py | 27 ++++++++++++ .../required-and-default-attributes.sh | 10 ----- test/sanity/code-smell/skip.txt | 1 - 23 files changed, 261 insertions(+), 161 deletions(-) create mode 100644 test/sanity/code-smell/empty-init.json create mode 100755 test/sanity/code-smell/empty-init.py delete mode 100755 test/sanity/code-smell/empty-init.sh delete mode 100755 test/sanity/code-smell/inappropriately-private.sh create mode 100644 test/sanity/code-smell/no-basestring.json create mode 100755 test/sanity/code-smell/no-basestring.py delete mode 100755 test/sanity/code-smell/no-basestring.sh create mode 100644 test/sanity/code-smell/no-dict-iteritems.json create mode 100755 test/sanity/code-smell/no-dict-iteritems.py delete mode 100755 test/sanity/code-smell/no-dict-iteritems.sh create mode 100644 test/sanity/code-smell/no-dict-iterkeys.json create mode 100755 test/sanity/code-smell/no-dict-iterkeys.py delete mode 100755 test/sanity/code-smell/no-dict-iterkeys.sh create mode 100644 test/sanity/code-smell/no-dict-itervalues.json create mode 100755 test/sanity/code-smell/no-dict-itervalues.py delete mode 100755 test/sanity/code-smell/no-dict-itervalues.sh create mode 100644 test/sanity/code-smell/no-get-exception.json create mode 100755 test/sanity/code-smell/no-get-exception.py delete mode 100755 test/sanity/code-smell/no-get-exception.sh create mode 100644 test/sanity/code-smell/required-and-default-attributes.json create mode 100755 test/sanity/code-smell/required-and-default-attributes.py delete mode 100755 test/sanity/code-smell/required-and-default-attributes.sh diff --git a/test/sanity/code-smell/empty-init.json b/test/sanity/code-smell/empty-init.json new file mode 100644 index 00000000000..43d0bd35013 --- /dev/null +++ b/test/sanity/code-smell/empty-init.json @@ -0,0 +1,11 @@ +{ + "prefixes": [ + "lib/ansible/modules/", + "lib/ansible/module_utils/", + "test/units/" + ], + "extensions": [ + ".py" + ], + "output": "path-message" +} diff --git a/test/sanity/code-smell/empty-init.py b/test/sanity/code-smell/empty-init.py new file mode 100755 index 00000000000..f55817dc670 --- /dev/null +++ b/test/sanity/code-smell/empty-init.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import os +import re +import sys + + +def main(): + skip = set([ + 'test/sanity/code-smell/%s' % os.path.basename(__file__), + # facts is grandfathered in but will break namespacing + # the only way to fix it is to deprecate and eventually remove it + # six will break namespacing but because it is bundled we should not be overriding it + 'lib/ansible/module_utils/facts/__init__.py', + 'lib/ansible/module_utils/six/__init__.py', + ]) + + for path in sys.argv[1:]: + if path in skip: + continue + + if os.path.basename(path) != '__init__.py': + continue + + if os.path.getsize(path) > 0: + print('%s: empty __init__.py required' % path) + + +if __name__ == '__main__': + main() diff --git a/test/sanity/code-smell/empty-init.sh b/test/sanity/code-smell/empty-init.sh deleted file mode 100755 index 3ae810f5f9d..00000000000 --- a/test/sanity/code-smell/empty-init.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -found='' - -for path in lib/ansible/modules/ lib/ansible/module_utils test/units/; do - # facts is grandfathered in but will break namespacing. Only way to fix it - # is to deprecate and eventually remove. - # six will break namespacing but because it is bundled we should not be overriding it - files=$(find "${path}" -name __init__.py -size '+0' | sed '\!lib/ansible/module_utils/\(six\|facts\)/__init__.py!d') - - if [ "${files}" ]; then - echo "${files}" - found=1 - fi -done - -if [ "${found}" ]; then - echo "One or more __init__.py file(s) listed above are non-empty." - exit 1 -fi diff --git a/test/sanity/code-smell/inappropriately-private.sh b/test/sanity/code-smell/inappropriately-private.sh deleted file mode 100755 index 9ee3342179a..00000000000 --- a/test/sanity/code-smell/inappropriately-private.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -# -# Test that we do not access private attributes of other objects. -# -# * private attributes of ourself are okay: self._private. -# * Private attributes of other objects are not: self.other._private -# - -# Currently the code has many places where we're violating this test so we need -# to clean up the code before we can enable this. Maybe we'll need to -# selectively blacklist modules so that we can work on this a piece at a time. -# -# Also need to implement whitelist for certain things like bundled libraries -# that violate this. -# -# 23-10-2015: Count was 508 lines -grep -Pri '(?