From 10bd807423dfd7e8bb73b114de8c606047846c66 Mon Sep 17 00:00:00 2001
From: Goutham Pacha Ravi <gouthampravi@gmail.com>
Date: Tue, 19 Feb 2019 20:26:45 -0800
Subject: [PATCH] [pylint] Run pylint separately for code and tests

We use the mock library in our
unit tests which assigns mocked objects with
members at run time. This causes pylint to flag
"no-member" errors. We also test return values
on methods which return None explicitly, or
implicitly, this upsets pylint.

pylint is quite inflexible in the way it handles
ignores in code. We can add ignore statements all
over the test code, but that is quite infeasible.

So, this change lets us run pylint separately
for code and test modules. When running tests,
it adjusts the disabled pylint checks.

Change-Id: I85d3fe896ee95c52c3da55aedba8f4d72d0c299e
---
 tools/coding-checks.sh | 31 ++++++++++++++++++++++---------
 tox.ini                |  3 ++-
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/tools/coding-checks.sh b/tools/coding-checks.sh
index 709f24e2ed..8a8457dde1 100755
--- a/tools/coding-checks.sh
+++ b/tools/coding-checks.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 set -eu
 
@@ -26,23 +26,36 @@ process_options() {
 }
 
 run_pylint() {
+
     local target="${scriptargs:-HEAD~1}"
+    local concurrency=$(python -c 'import multiprocessing as mp; print(mp.cpu_count())')
+    CODE_OKAY=0
 
     if [[ "$target" = *"all"* ]]; then
         files="manila"
+        test_files="manila.tests"
     else
-        files=$(git diff --name-only --diff-filter=ACMRU $target "*.py")
+        files=$(git diff --name-only --diff-filter=ACMRU HEAD~1 ':!manila/tests/*' '*.py')
+        test_files=$(git diff --name-only --diff-filter=ACMRU HEAD~1 'manila/tests/*.py')
     fi
-
-    if [ -n "${files}" ]; then
-        echo "Running pylint against:"
-        printf "\t%s\n" "${files[@]}"
-        pylint --rcfile=.pylintrc --output-format=colorized ${files} -E \
-            -j `python -c 'import multiprocessing as mp; print(mp.cpu_count())'`
-    else
+    if [[ -z "${files}" || -z "${test_files}" ]]; then
         echo "No python changes in this commit, pylint check not required."
         exit 0
     fi
+    if [[ -n "${files}" ]]; then
+        echo "Running pylint against manila code modules:"
+        printf "\t%s\n" "${files[@]}"
+        pylint --rcfile=.pylintrc --output-format=colorized ${files} \
+            -E -j $concurrency || CODE_OKAY=1
+    fi
+    if [[ -n "${test_files}" ]]; then
+        echo "Running pylint against manila test modules:"
+        printf "\t%s\n" "${test_files[@]}"
+        pylint --rcfile=.pylintrc --output-format=colorized ${test_files} \
+            -E -d "no-member,assignment-from-no-return,assignment-from-none" \
+            -j $concurrency || CODE_OKAY=1
+    fi
+    exit $CODE_OKAY
 }
 
 scriptargs=
diff --git a/tox.ini b/tox.ini
index 6c28eade4c..bc3b807d27 100644
--- a/tox.ini
+++ b/tox.ini
@@ -56,7 +56,8 @@ commands =
          devstack/upgrade/shutdown.sh \
          devstack/upgrade/upgrade.sh \
          tools/cover.sh \
-         tools/check_logging.sh
+         tools/check_logging.sh \
+         tools/coding-checks.sh
   {toxinidir}/tools/check_exec.py {toxinidir}/manila
   {toxinidir}/tools/check_logging.sh {toxinidir}/manila