[folks/wip/nielsdg/ci-style-check] ci: Do some automatic style checking




commit 0027253a81c08f952bd81ea3d1c60bfdfb1b8bd2
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Dec 4 16:14:57 2020 +0100

    ci: Do some automatic style checking

 .gitlab-ci.yml                                   | 16 +++++-
 .gitlab/ci/junit-report.sh                       | 70 ++++++++++++++++++++++++
 {.gitlab-ci => .gitlab/ci}/meson-junit-report.py |  0
 {.gitlab-ci => .gitlab/ci}/run-tests.sh          |  2 +-
 .gitlab/ci/style-check.sh                        | 31 +++++++++++
 5 files changed, 117 insertions(+), 2 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index a298d10b..3907cf60 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,9 +1,23 @@
 image: fedora:latest
 
 stages:
+  - review
   - build
   - deploy
 
+style-check:
+  stage: review
+  script:
+    - ./.gitlab/ci/style-check.sh
+  artifacts:
+    expire_in: 1 week
+    name: "style-check-junit-report"
+    when: always
+    reports:
+      junit: style-check-junit-report.xml
+    paths:
+      - "style-check-junit-report.xml"
+
 build-folks:
   stage: build
   except:
@@ -22,7 +36,7 @@ build-folks:
     # Multiply the Meson test timeout by 3 (mostly for the stress tests)
     # For most tests it doesn't matter anyway, since we internally use
     # TestUtils.loop_run_with_timeout()
-    - bash +x ./.gitlab-ci/run-tests.sh -t 3
+    - bash +x ./.gitlab/ci/run-tests.sh -t 3
   artifacts:
     reports:
       junit: "_build/${CI_JOB_NAME}-report.xml"
diff --git a/.gitlab/ci/junit-report.sh b/.gitlab/ci/junit-report.sh
new file mode 100755
index 00000000..47792e44
--- /dev/null
+++ b/.gitlab/ci/junit-report.sh
@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+#
+# junit-report.sh: JUnit report helpers
+#
+# Source this file into your CI scripts to get a nice JUnit report file which
+# can be shown in the GitLab UI.
+
+JUNIT_REPORT_TESTS_FILE=$(mktemp)
+
+# We need this to make sure we don't send funky stuff into the XML report,
+# making it invalid XML (and thus unparsable by CI)
+function escape_xml() {
+  echo "$1" | sed -e 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
+}
+
+# Append a failed test case with the given name and message
+function append_failed_test_case() {
+  test_name="$1"
+  test_message="$2"
+
+  # Escape both fields before putting them into the xml
+  test_name_esc="$(escape_xml "$test_name")"
+  test_message_esc="$(escape_xml "$test_message")"
+
+  echo "<testcase name=\"$test_name_esc\">" >> $JUNIT_REPORT_TESTS_FILE
+  echo "    <failure message=\"$test_message_esc\"/>" >> $JUNIT_REPORT_TESTS_FILE
+  echo "</testcase>" >> $JUNIT_REPORT_TESTS_FILE
+
+  # Also output to stderr, so it shows up in the job output
+  echo >&2 "Test '$test_name' failed: $test_message"
+}
+
+# Append a successful test case with the given name
+function append_passed_test_case() {
+  test_name="$1"
+  test_name_esc="$(escape_xml "$test_name")"
+
+  echo "<testcase name=\"$test_name_esc\"></testcase>" >> $JUNIT_REPORT_TESTS_FILE
+
+  # Also output to stderr, so it shows up in the job output
+  echo >&2 "Test '$test_name' succeeded"
+}
+
+# Aggregates the test cases into a proper JUnit report XML file
+function generate_junit_report() {
+  junit_report_file="$1"
+  testsuite_name="$2"
+
+  num_tests=$(fgrep '<testcase' -- "$JUNIT_REPORT_TESTS_FILE" | wc -l)
+  num_failures=$(fgrep '<failure' -- "$JUNIT_REPORT_TESTS_FILE" | wc -l )
+
+  echo Generating JUnit report \"$(pwd)/$junit_report_file\" with $num_tests tests and $num_failures 
failures.
+
+  cat > $junit_report_file << __EOF__
+<?xml version="1.0" encoding="utf-8"?>
+<testsuites tests="$num_tests" errors="0" failures="$num_failures">
+<testsuite name="$testsuite_name" tests="$num_tests" errors="0" failures="$num_failures" skipped="0">
+$(< $JUNIT_REPORT_TESTS_FILE)
+</testsuite>
+</testsuites>
+__EOF__
+}
+
+# Returns a non-zero exit status if any of the tests in the given JUnit report failed
+# You probably want to call this at the very end of your script.
+function check_junit_report() {
+  junit_report_file="$1"
+
+  ! fgrep -q '<failure' -- "$junit_report_file"
+}
diff --git a/.gitlab-ci/meson-junit-report.py b/.gitlab/ci/meson-junit-report.py
similarity index 100%
rename from .gitlab-ci/meson-junit-report.py
rename to .gitlab/ci/meson-junit-report.py
diff --git a/.gitlab-ci/run-tests.sh b/.gitlab/ci/run-tests.sh
similarity index 84%
rename from .gitlab-ci/run-tests.sh
rename to .gitlab/ci/run-tests.sh
index dfb828c5..c66e344e 100755
--- a/.gitlab-ci/run-tests.sh
+++ b/.gitlab/ci/run-tests.sh
@@ -6,7 +6,7 @@ meson test -C _build $*
 
 exit_code=$?
 
-python3 .gitlab-ci/meson-junit-report.py \
+python3 .gitlab/ci/meson-junit-report.py \
         --project-name=folks \
         --job-id "${CI_JOB_NAME}" \
         --output "_build/${CI_JOB_NAME}-report.xml" \
diff --git a/.gitlab/ci/style-check.sh b/.gitlab/ci/style-check.sh
new file mode 100755
index 00000000..7fad5631
--- /dev/null
+++ b/.gitlab/ci/style-check.sh
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+#
+# style-check.sh: Performs some basic style checks
+
+# Source the JUnit helpers
+scriptdir="$(dirname "$BASH_SOURCE")"
+source "$scriptdir/junit-report.sh"
+
+
+TESTNAME="No tabs"
+tabs_occurrences="$(fgrep -nR $'\t' folks backends)"
+if [[ -z "$tabs_occurrences" ]]; then
+  append_passed_test_case "$TESTNAME"
+else
+  append_failed_test_case "$TESTNAME" \
+    $'Please remove the tabs found at the following places:\n\n'"$tabs_occurrences"
+fi
+
+
+TESTNAME="No trailing whitespace"
+trailing_ws_occurrences="$(grep -nri '[[:blank:]]$' folks backends)"
+if [[ -z "$trailing_ws_occurrences" ]]; then
+  append_passed_test_case "$TESTNAME"
+else
+  append_failed_test_case "$TESTNAME" \
+    $'Please remove the trailing whitespace at the following places:\n\n'"$trailing_ws_occurrences"
+fi
+
+
+generate_junit_report "$CI_JOB_NAME-junit-report.xml" "$CI_JOB_NAME"
+check_junit_report "$CI_JOB_NAME-junit-report.xml"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]