[gnome-contacts/feature/improve-ci] ci: Improve script for style checks a bit




commit 13e97de66c5476136d94599c4c04f19925052c7f
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Dec 4 11:17:13 2020 +0100

    ci: Improve script for style checks a bit
    
    Put the JUnit helper functions in a separate script and make them more
    generic.

 .gitlab/ci/junit-report.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++
 .gitlab/ci/style-check.sh  | 60 +++++------------------------------------
 src/contacts-app.vala      |  2 +-
 3 files changed, 74 insertions(+), 54 deletions(-)
---
diff --git a/.gitlab/ci/junit-report.sh b/.gitlab/ci/junit-report.sh
new file mode 100755
index 00000000..3001015e
--- /dev/null
+++ b/.gitlab/ci/junit-report.sh
@@ -0,0 +1,66 @@
+#!/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"
+
+  echo "<testcase name=\"$test_name\"></testcase>" >> $JUNIT_REPORT_TESTS_FILE
+}
+
+# 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/style-check.sh b/.gitlab/ci/style-check.sh
index f0997ad7..7c3d868b 100755
--- a/.gitlab/ci/style-check.sh
+++ b/.gitlab/ci/style-check.sh
@@ -1,53 +1,11 @@
 #!/usr/bin/env bash
+#
+# style-check.sh: Performs some basic style checks
 
-###############################################################################
-# JUNIT HELPERS
-###############################################################################
+# Source the JUnit helpers
+scriptdir="$(dirname "$BASH_SOURCE")"
+source "$scriptdir/junit-report.sh"
 
-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"
-
-  test_message_esc="$(escape_xml "$test_message")"
-  echo "<testcase name=\"$test_name\"><failure message=\"$test_message_esc\"/></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)"
@@ -69,9 +27,5 @@ else
 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 $?
+generate_junit_report "$CI_JOB_NAME-junit-report.xml" "$CI_JOB_NAME"
+check_junit_report "$CI_JOB_NAME-junit-report.xml"
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index 082449ad..6927438e 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -15,7 +15,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-using Folks;
+using Folks; 
 
 public class Contacts.App : Gtk.Application {
   private Settings settings;


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