Add pre-commit robot-framework tidy 22/81522/4
authorThanh Ha <zxiiro@gmail.com>
Wed, 10 Apr 2019 08:04:37 +0000 (16:04 +0800)
committerJamo Luhrsen <jluhrsen@gmail.com>
Tue, 14 May 2019 15:37:02 +0000 (15:37 +0000)
Use pre-commit to handle robot tidy tool rather than maintaining our
own. This tool is handy because it will automatically correct the
local files on "git commit" after pre-commit is installed for the
first time.

Install pre-commit using tox with `tox -e pre-commit`.

Change-Id: I64d802b2d0754160755bce3e50272a5f28af4d72
Signed-off-by: Thanh Ha <zxiiro@gmail.com>
.pre-commit-config.yaml [new file with mode: 0644]
tools/robot_check/README.markdown [deleted file]
tools/robot_check/requirements.txt [deleted file]
tools/robot_check/tidy.sh [deleted file]
tools/robot_check/tidytool.py [deleted file]
tox.ini

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644 (file)
index 0000000..cecdca2
--- /dev/null
@@ -0,0 +1,6 @@
+---
+- repo: git://github.com/guykisel/pre-commit-robotframework-tidy
+  rev: 'master'
+  hooks:
+  - id: robotframework-tidy-wrapper
+
diff --git a/tools/robot_check/README.markdown b/tools/robot_check/README.markdown
deleted file mode 100644 (file)
index 2701885..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-# Robot Tidy Tool
-
-Tool for checking and correcting Robot Framework code formatting.
-
-## Installation
-
-The `robot.tidy` Python module ships with the Robot Framework Python package.
-
-We recommend using a [Virtual Environment][1] to manage Python modules.
-
-    $ mkvirtualenv tidy
-    $ pip install -r requirements.txt
-
-## Usage
-
-The `tidy.sh` script is a wrapper around `robot.tidy` that checks all files in
-the Integration/Test repository.
-
-Use the `check` argument to report problems without correcting them.
-
-    $ ./tidy.sh check
-
-Use the `tidy` argument to automatically clean up all problems.
-
-    $ ./tidy.sh tidy
-
-[1]: https://virtualenvwrapper.readthedocs.io/en/latest/ "Virtualenvwrapper docs"
diff --git a/tools/robot_check/requirements.txt b/tools/robot_check/requirements.txt
deleted file mode 100644 (file)
index b779ff3..0000000
+++ /dev/null
@@ -1 +0,0 @@
-robotframework
diff --git a/tools/robot_check/tidy.sh b/tools/robot_check/tidy.sh
deleted file mode 100755 (executable)
index c9033db..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/usr/bin/env sh
-# Convenience script to run check on all .robot files in the project.
-# Run with the "quiet" argument to get rid of the non-error output.
-
-if test "${1-loud}" = "quiet"; then
-  COMMAND="quiet"
-elif test "${1-loud}" = "tidy"; then
-  COMMAND="tidy"
-else
-  COMMAND="check"
-fi
-BASE=`dirname ${0}`
-cd $BASE
-BASE=`pwd`
-cd ../..
-DIRLIST=""
-for Dir in *; do
-  if test -d $Dir; then
-    DIRLIST="$DIRLIST $Dir"
-  fi
-done
-python $BASE/tidytool.py $COMMAND $DIRLIST
diff --git a/tools/robot_check/tidytool.py b/tools/robot_check/tidytool.py
deleted file mode 100644 (file)
index 910e3a1..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-import difflib
-import os
-import sys
-import stat
-import robot.tidy
-
-
-def Error(FileSpec, Message):
-    global ErrorsReported
-    print "File", FileSpec + ":", Message
-    ErrorsReported = True
-
-
-def traverse_and_process(DirList, Processor):
-    """Traverse the directory and process all .robot files found"""
-    Stack = []
-    for Dir in DirList:
-        if Dir[-1] == "/":
-            Dir = Dir[:-1]
-        Stack.append(Dir)
-    while len(Stack) > 0:
-        Dir = Stack.pop() + "/"
-        try:
-            List = os.listdir(Dir)
-        except (IOError, OSError), e:
-            Error(Dir, "Directory not accessible: " + str(e))
-            continue
-        for Item in List:
-            Spec = Dir + Item
-            Stat = os.stat(Spec)
-            if stat.S_ISDIR(Stat.st_mode):
-                Stack.append(Spec)
-            elif Item.endswith(".robot"):
-                Processor(Spec)
-
-
-def check_quietly(FileSpec):
-    try:
-        Data = open(FileSpec).read()
-    except (IOError, OSError), e:
-        Error(FileSpec, "Not accessible: " + str(e))
-        return
-    try:
-        Data = Data.decode("utf8")
-    except:
-        Error(FileSpec, "Has invalid UTF8 data")
-        return
-    TidyTool = robot.tidy.Tidy()
-    CleanedData = TidyTool.file(FileSpec)
-    if Data != CleanedData:
-        Error(FileSpec, "Found to be untidy")
-
-
-def check(FileSpec):
-    Index = FileSpec.rfind("/")
-    FileName = FileSpec
-    if Index >= 0:
-        FileName = FileSpec[Index + 1:]
-    sys.stdout.write("  " + FileName + "\r")
-    sys.stdout.flush()
-    check_quietly(FileSpec)
-    sys.stdout.write(" " * (2 + len(FileName)) + "\r")
-
-
-def tidy(FileSpec):
-    print "Processing file:", FileSpec
-    TidyTool = robot.tidy.Tidy()
-    try:
-        CleanedData = TidyTool.file(FileSpec)
-        CleanedData = CleanedData.encode("utf8")
-        open(FileSpec, "w").write(CleanedData)
-    except (IOError, OSError), e:
-        Error(FileSpec, "Not accessible: " + str(e))
-
-
-def diff(FileSpec):
-    TidyTool = robot.tidy.Tidy()
-    try:
-        ActualLines = open(FileSpec, 'U').readlines()
-        CleanedData = TidyTool.file(FileSpec)
-        # Unified diff wants list of lines, and split on newline creates empty line at the end.
-        CleanedLines = [line + '\n' for line in CleanedData.encode("utf8").split('\n')[:-1]]
-        DiffText = "".join(tuple(difflib.unified_diff(ActualLines, CleanedLines, n=10)))
-        # TODO: If the last line does not contain \n, the output is ugly. Can we fix that without causing confsion?
-        if DiffText:
-            Error(FileSpec, "Tidy requires the following diff:\n" + DiffText)
-    except (IOError, OSError), e:
-        Error(FileSpec, "Not accessible: " + str(e))
-
-
-# TODO: Refactor the command line argument parsing to use argparse. Since I
-#       wanted to just quickly make this tool to get rid of manual robot.tidy
-#       runs I did not have time to create polished argparse based command
-#       line argument parsing code. Remember also to update the convenience
-#       scripts.
-
-
-def usage():
-    print "Usage:\ttidytool.py <command>k <dir1> [<dir2> <dir3> ...]"
-    print
-    print "where <command> is one of these:"
-    print
-    print "check\tCheck that the Robot test data is tidy."
-    print "quiet\tCheck quietly that the Robot test data is tidy."
-    print "tidy\tTidy the Robot test data."
-    print
-    print "The program traverses the specified directories, searching for"
-    print "Robot test data (.robot files) and performing the specified"
-    print "command on them."
-
-
-if __name__ == "__main__":
-    if len(sys.argv) < 2:
-        usage()
-        raise SystemExit
-    Command = sys.argv[1]
-    DirList = sys.argv[2:]
-    if Command == "check":
-        Processor = check
-    elif Command == "quiet":
-        Processor = check_quietly
-    elif Command == "tidy":
-        Processor = tidy
-    elif Command == "diff":
-        Processor = diff
-    else:
-        print "Unrecognized command:", Command
-        sys.exit(1)
-    ErrorsReported = False
-    traverse_and_process(DirList, Processor)
-    if ErrorsReported:
-        print "tidytool run FAILED !!!"
-        sys.exit(1)
diff --git a/tox.ini b/tox.ini
index 48e19fe1c918c9109574aeda44f3d7a03373d582..1e64b83267d270b33823e94809307c8d699c7072 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -1,6 +1,6 @@
 [tox]
 minversion = 1.6.0
-envlist = coala,pep8,tidy
+envlist = coala,pep8,pre-commit
 # TODO: Include docs when it starts performing strict checks.
 skipsdist = true
 
@@ -20,10 +20,11 @@ deps = -rdocs/requirements.txt
 commands =
     sphinx-build -W -n -b html -d {envtmpdir}/doctrees ./docs/ {toxinidir}/docs/_build/html
 
-[testenv:tidy]
-basepython = python2
-deps = robotframework
-commands = python ./tools/robot_check/tidytool.py diff csit tools
+[testenv:pre-commit]
+deps = pre-commit
+commands =
+    pre-commit install
+    pre-commit run --all-files
 
 [testenv:pep8]
 basepython = python2