[gnome-contacts/feature/style-checks] ci: Do some basic style checks




commit 2bc4ab39a0605f632b8276bc9dc6d700530a3e9d
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Thu Dec 3 22:04:14 2020 +0100

    ci: Do some basic style checks

 .gitlab-ci.yml                                   | 19 ++++++
 {.gitlab-ci => .gitlab/ci}/meson-junit-report.py |  0
 {.gitlab-ci => .gitlab/ci}/run-tests.sh          |  0
 .gitlab/ci/style-check.sh                        | 77 ++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d7184820..c78259fb 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,5 +1,24 @@
 include: 'https://gitlab.gnome.org/GNOME/citemplates/raw/master/flatpak/flatpak_ci_initiative.yml'
 
+stages:
+  - review
+  - test
+  - 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"
+
+
 flatpak:
   image: registry.gitlab.gnome.org/gnome/gnome-runtime-images/gnome:master
   variables:
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 100%
rename from .gitlab-ci/run-tests.sh
rename to .gitlab/ci/run-tests.sh
diff --git a/.gitlab/ci/style-check.sh b/.gitlab/ci/style-check.sh
new file mode 100755
index 00000000..9167fb19
--- /dev/null
+++ b/.gitlab/ci/style-check.sh
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+
+###############################################################################
+# JUNIT HELPERS
+###############################################################################
+
+JUNIT_REPORT_TESTS_FILE=$(mktemp)
+
+# We need this to make sure we don't send funky stuff into the XML report
+function escape_xml() {
+  echo "$1" | sed -e 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
+}
+
+function append_failed_test_case() {
+  test_name="$1"
+  test_message="$2"
+
+  escaped_test_message="$(escape_xml "$test_message")"
+  echo "<testcase name=\"$test_name\"><failure message=\"$test_message\"/></testcase>" >> 
$JUNIT_REPORT_TESTS_FILE
+  echo >&2 "Test '$test_name' failed: $test_message"
+}
+
+function append_passed_test_case() {
+  test_name="$1"
+  commit="$2"
+
+  echo "<testcase name=\"$test_name\"></testcase>" >> $JUNIT_REPORT_TESTS_FILE
+}
+
+function generate_junit_report() {
+  junit_report_file="$1"
+  num_tests=$(cat "$JUNIT_REPORT_TESTS_FILE" | wc -l)
+  num_failures=$(grep '<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="style-review" tests="$num_tests" errors="0" failures="$num_failures" skipped="0">
+$(< $JUNIT_REPORT_TESTS_FILE)
+</testsuite>
+</testsuites>
+__EOF__
+}
+
+
+###############################################################################
+# STYLE CHECKS
+###############################################################################
+
+TESTNAME="No tabs"
+tabs_occurrences="$(fgrep -nR $'\t' src data)"
+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:]]$' src data)"
+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 the report
+# and fail this step if any failure occurred
+generate_junit_report style-check-junit-report.xml
+
+! grep -q '<failure' style-check-junit-report.xml
+exit $?


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