[seahorse/nielsdg/add-style-check] ci: Add style checks




commit 72887d7fbec7b4d85af491593414395d2d96e8d6
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sun Mar 21 21:38:41 2021 +0100

    ci: Add style checks
    
    We're a bit too far off from a consistent style to be able to use a
    formatter like uncrustify, but we can start with simple stuff like
    trailing whitespaces.

 .gitlab-ci.yml                   |  18 +++++++
 .gitlab/ci/junit-report.sh       |  70 +++++++++++++++++++++++++
 .gitlab/ci/meson-junit-report.py | 109 +++++++++++++++++++++++++++++++++++++++
 .gitlab/ci/run-tests.sh          |  17 ++++++
 .gitlab/ci/style-check.sh        |  21 ++++++++
 5 files changed, 235 insertions(+)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 39265f28..3fd10989 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -4,6 +4,24 @@ variables:
   BUNDLE: 'seahorse.flatpak'
   GIT_SUBMODULE_STRATEGY: recursive
 
+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/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
new file mode 100644
index 00000000..248ef6e2
--- /dev/null
+++ b/.gitlab/ci/meson-junit-report.py
@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+
+# Turns a Meson testlog.json file into a JUnit XML report
+#
+# Copyright 2019  GNOME Foundation
+#
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# Original author: Emmanuele Bassi
+
+import argparse
+import datetime
+import json
+import os
+import sys
+import xml.etree.ElementTree as ET
+
+aparser = argparse.ArgumentParser(description='Turns a Meson test log into a JUnit report')
+aparser.add_argument('--project-name', metavar='NAME',
+                     help='The project name',
+                     default='unknown')
+aparser.add_argument('--job-id', metavar='ID',
+                     help='The job ID for the report',
+                     default='Unknown')
+aparser.add_argument('--branch', metavar='NAME',
+                     help='Branch of the project being tested',
+                     default='master')
+aparser.add_argument('--output', metavar='FILE',
+                     help='The output file, stdout by default',
+                     type=argparse.FileType('w', encoding='UTF-8'),
+                     default=sys.stdout)
+aparser.add_argument('infile', metavar='FILE',
+                     help='The input testlog.json, stdin by default',
+                     type=argparse.FileType('r', encoding='UTF-8'),
+                     default=sys.stdin)
+
+args = aparser.parse_args()
+
+outfile = args.output
+
+testsuites = ET.Element('testsuites')
+testsuites.set('id', '{}/{}'.format(args.job_id, args.branch))
+testsuites.set('package', args.project_name)
+testsuites.set('timestamp', datetime.datetime.utcnow().isoformat(timespec='minutes'))
+
+suites = {}
+for line in args.infile:
+    data = json.loads(line)
+    (full_suite, unit_name) = data['name'].split(' / ')
+    (project_name, suite_name) = full_suite.split(':')
+
+    duration = data['duration']
+    return_code = data['returncode']
+    log = data['stdout']
+
+    unit = {
+        'suite': suite_name,
+        'name': unit_name,
+        'duration': duration,
+        'returncode': return_code,
+        'stdout': log,
+    }
+
+    units = suites.setdefault(suite_name, [])
+    units.append(unit)
+
+for name, units in suites.items():
+    print('Processing suite {} (units: {})'.format(name, len(units)))
+
+    def if_failed(unit):
+        if unit['returncode'] != 0:
+            return True
+        return False
+
+    def if_succeded(unit):
+        if unit['returncode'] == 0:
+            return True
+        return False
+
+    successes = list(filter(if_succeded, units))
+    failures = list(filter(if_failed, units))
+    print(' - {}: {} pass, {} fail'.format(name, len(successes), len(failures)))
+
+    testsuite = ET.SubElement(testsuites, 'testsuite')
+    testsuite.set('name', '{}/{}'.format(args.project_name, name))
+    testsuite.set('tests', str(len(units)))
+    testsuite.set('errors', str(len(failures)))
+    testsuite.set('failures', str(len(failures)))
+
+    for unit in successes:
+        testcase = ET.SubElement(testsuite, 'testcase')
+        testcase.set('classname', '{}/{}'.format(args.project_name, unit['suite']))
+        testcase.set('name', unit['name'])
+        testcase.set('time', str(unit['duration']))
+
+    for unit in failures:
+        testcase = ET.SubElement(testsuite, 'testcase')
+        testcase.set('classname', '{}/{}'.format(args.project_name, unit['suite']))
+        testcase.set('name', unit['name'])
+        testcase.set('time', str(unit['duration']))
+
+        failure = ET.SubElement(testcase, 'failure')
+        failure.set('classname', '{}/{}'.format(args.project_name, unit['suite']))
+        failure.set('name', unit['name'])
+        failure.set('type', 'error')
+        failure.text = unit['stdout']
+
+output = ET.tostring(testsuites, encoding='unicode')
+outfile.write(output)
diff --git a/.gitlab/ci/run-tests.sh b/.gitlab/ci/run-tests.sh
new file mode 100644
index 00000000..b2a30967
--- /dev/null
+++ b/.gitlab/ci/run-tests.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+set +e
+
+xvfb-run -a -s "-screen 0 1024x768x24" \
+         flatpak build app \
+         meson test -C _build
+
+exit_code=$?
+
+python3 .gitlab-ci/meson-junit-report.py \
+        --project-name=gnome-contacts \
+        --job-id "${CI_JOB_NAME}" \
+        --output "_build/${CI_JOB_NAME}-report.xml" \
+        _build/meson-logs/testlog.json
+
+exit $exit_code
diff --git a/.gitlab/ci/style-check.sh b/.gitlab/ci/style-check.sh
new file mode 100755
index 00000000..7e498d1d
--- /dev/null
+++ b/.gitlab/ci/style-check.sh
@@ -0,0 +1,21 @@
+#!/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 trailing whitespace"
+source_dirs="common data gkr libseahorse pgp pkcs11 src ssh"
+trailing_ws_occurrences="$(grep -nRI '[[:blank:]]$' $source_dirs)"
+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]