From 85ec44826aaed382a8b279a18002cb6282e4179b Mon Sep 17 00:00:00 2001 From: Chris Chinchilla Date: Mon, 5 Aug 2019 13:00:30 +0200 Subject: [PATCH] Add style checker Rename files Changes from review Update test/docsCodeStyle.sh Co-Authored-By: chriseth Update test/docsCodeStyle.sh Co-Authored-By: chriseth Remove extraneous brackets --- .circleci/config.yml | 18 ++++++++++++++- scripts/isolate_tests.py | 6 +++-- test/.solhint.json | 8 +++++++ test/.solhintignore | 1 + test/cmdlineTests.sh | 2 ++ test/docsCodeStyle.sh | 49 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 test/.solhint.json create mode 100644 test/.solhintignore create mode 100755 test/docsCodeStyle.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 0f9e98d03..e10258cee 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -183,6 +183,22 @@ jobs: name: Check spelling command: ~/.local/bin/codespell -S "*.enc,.git" -I ./scripts/codespell_whitelist.txt + chk_docs_examples: + docker: + - image: circleci/node + environment: + TERM: xterm + steps: + - checkout + - attach_workspace: + at: build + - run: + name: JS deps + command: sudo npm install -g solhint + - run: + name: Test Docs examples + command: ./test/docsCodeStyle.sh + chk_coding_style: docker: - image: buildpack-deps:disco @@ -586,6 +602,7 @@ workflows: # basic checks - chk_spelling: *workflow_trigger_on_tags - chk_coding_style: *workflow_trigger_on_tags + - chk_docs_examples: *workflow_trigger_on_tags - chk_buglist: *workflow_trigger_on_tags - chk_proofs: *workflow_trigger_on_tags @@ -646,4 +663,3 @@ workflows: # Code Coverage enabled build and tests - b_ubu_codecov: *workflow_trigger_on_tags - t_ubu_codecov: *workflow_ubuntu1904_codecov - diff --git a/scripts/isolate_tests.py b/scripts/isolate_tests.py index 8a9aa0a73..06feb0fc6 100755 --- a/scripts/isolate_tests.py +++ b/scripts/isolate_tests.py @@ -59,8 +59,10 @@ def extract_docs_cases(path): def write_cases(f, tests): cleaned_filename = f.replace(".","_").replace("-","_").replace(" ","_").lower() for test in tests: - open('test_%s_%s.sol' % (hashlib.sha256(test).hexdigest(), cleaned_filename), 'wb').write(test) - + # When code examples are extracted they indented by 8 spaces, which violates the style guide, + # so before checking remove 4 spaces from each line. + remainder = re.sub(r'^ {4}', '', test, 0, re.MULTILINE) + open('test_%s_%s.sol' % (hashlib.sha256(test).hexdigest(), cleaned_filename), 'wb').write(remainder) def extract_and_write(f, path): if docs: diff --git a/test/.solhint.json b/test/.solhint.json new file mode 100644 index 000000000..1379d570d --- /dev/null +++ b/test/.solhint.json @@ -0,0 +1,8 @@ +{ + "extends": "solhint:default", + "plugins": [], + "rules": { + "compiler-fixed": false, + "no-inline-assembly": false + } +} \ No newline at end of file diff --git a/test/.solhintignore b/test/.solhintignore new file mode 100644 index 000000000..ce864af20 --- /dev/null +++ b/test/.solhintignore @@ -0,0 +1 @@ +*contributing_rst* diff --git a/test/cmdlineTests.sh b/test/cmdlineTests.sh index ba926d865..acda5ba04 100755 --- a/test/cmdlineTests.sh +++ b/test/cmdlineTests.sh @@ -308,6 +308,7 @@ SOLTMPDIR=$(mktemp -d) set -e cd "$SOLTMPDIR" "$REPO_ROOT"/scripts/isolate_tests.py "$REPO_ROOT"/docs/ docs + for f in *.sol do # The contributors guide uses syntax tests, but we cannot @@ -317,6 +318,7 @@ SOLTMPDIR=$(mktemp -d) continue fi echo "$f" + opts='' # We expect errors if explicitly stated, or if imports # are used (in the style guide) diff --git a/test/docsCodeStyle.sh b/test/docsCodeStyle.sh new file mode 100755 index 000000000..79487d916 --- /dev/null +++ b/test/docsCodeStyle.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +set -e + +## GLOBAL VARIABLES + +REPO_ROOT=$(cd $(dirname "$0")/.. && pwd) + +## FUNCTIONS + +if [ "$CIRCLECI" ] +then + function printTask() { echo "$(tput bold)$(tput setaf 2)$1$(tput setaf 7)"; } + function printError() { echo "$(tput setaf 1)$1$(tput setaf 7)"; } +else + function printTask() { echo "$(tput bold)$(tput setaf 2)$1$(tput sgr0)"; } + function printError() { echo "$(tput setaf 1)$1$(tput sgr0)"; } +fi + +printTask "Checking docs examples style" +SOLTMPDIR=$(mktemp -d) +( + set -e + cd "$SOLTMPDIR" + "$REPO_ROOT"/scripts/isolate_tests.py "$REPO_ROOT"/docs/ docs + + if npm -v >/dev/null 2>&1; then + if npm list -g | grep solhint >/dev/null 2>&1; then + echo "node is installed, setting up solhint" + cp "$REPO_ROOT"/test/.solhint.json "$SOLTMPDIR"/.solhint.json + cp "$REPO_ROOT"/test/.solhintignore "$SOLTMPDIR"/.solhintignore + + for f in *.sol + do + echo "$f" + # Only report errors + solhint -f unix "$SOLTMPDIR/$f" + done + else + echo "node is installed, but not solhint" + exit 1 + fi + else + echo "node not installed, skipping docs style checker" + exit 1 + fi +) +rm -rf "$SOLTMPDIR" +echo "Done."