From 3702a796b84beb50d0e51da2669e00a6b9d89c7b Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 2 Aug 2020 19:28:09 +0200
Subject: [PATCH] CI: Update the CI

- Clean all the generated files and folders, even on failed pipeline
- Add the format checking test at initialization
---
 .gitlab-ci.yml        | 14 ++++++++++---
 scripts/astyle.bash   | 47 +++++++++++++++++++++++--------------------
 scripts/validate.bash | 28 +++++++++++++++++++++++---
 3 files changed, 61 insertions(+), 28 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7eb57990..c31d062c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -12,8 +12,8 @@ stages:
         - main
         - merge_requests
     variables:
-        SRC_DIR: $CI_PROJECT_DIR                # Where the sources are
-        WORK_DIR: "/home/gitlab-runner/CI_$CI_PIPELINE_ID"    # Where we will work, so that it's not erased
+        SRC_DIR: $CI_PROJECT_DIR                            # Where the sources are
+        WORK_DIR: "/home/gitlab-runner/CI_$CI_PIPELINE_ID"  # Where we will work, so that it's not erased
 
 .breaking_tests: &breaking_tests
     allow_failure: true
@@ -29,16 +29,24 @@ env_sanitize:
     <<: *main_tests
     stage: initialization
     script:
+        - cd $CI_PROJECT_DIR
         - export # For debug purpose
         - scripts/validate.bash --build=gcc --step=prepare
         - scripts/validate.bash --build=clang --step=prepare
         - echo "Env is intialized"
 
+style_verification:
+    <<: *main_tests
+    stage: initialization
+    script:
+        - cd $CI_PROJECT_DIR
+        - scripts/validate.bash --step=format
+
 env_finalize:
     <<: *main_tests
     stage: finalization
     allow_failure: true
-    when: on_success
+    when: always
     script: rm -rf $WORK_DIR
 
 #################################################
diff --git a/scripts/astyle.bash b/scripts/astyle.bash
index 6b170988..22a8bd73 100755
--- a/scripts/astyle.bash
+++ b/scripts/astyle.bash
@@ -1,26 +1,29 @@
-#!/bin/sh
+#!/bin/bash
 
 OPTIONS='
-  --style=linux -s4
-  --pad-oper --pad-header --pad-comma
-  --align-pointer=name --align-reference=name
-  --break-one-line-headers
-  --remove-braces
-  --break-return-type
-  --convert-tabs
-  --close-templates
-  --max-code-length=101
-  --mode=c
-  --lineend=linux
-  --attach-inlines
-  --indent-labels
-  --indent-col1-comments
-  --indent-preproc-block
-  --quiet'
+    --style=linux -s4
+    --pad-oper --pad-header --pad-comma
+    --align-pointer=name --align-reference=name
+    --break-one-line-headers
+    --remove-braces
+    --break-return-type
+    --convert-tabs
+    --close-templates
+    --max-code-length=101
+    --mode=c
+    --lineend=linux
+    --attach-inlines
+    --indent-labels
+    --indent-col1-comments
+    --indent-preproc-block'
 
+if [ $# -gt 0 ] && [ "$1" = "--check" ] ; then
+    astyle $OPTIONS --dry-run -R ./*.c | grep Formatted
+    [ $? -eq 1 ] && exit 0 || exit 1
+fi
 
-find . -iname '*.c'     -exec astyle $OPTIONS {} \;
-find . -iname '*.h'     -exec astyle $OPTIONS {} \;
-find . -iname '*.hpp'   -exec astyle $OPTIONS {} \;
-find . -iname '*.cpp'   -exec astyle $OPTIONS {} \;
-find . -iname '*.orig'  -exec rm {} \;
+find . -name '*.c'     -exec astyle $OPTIONS --quiet {} \;
+find . -name '*.h'     -exec astyle $OPTIONS --quiet {} \;
+find . -name '*.hpp'   -exec astyle $OPTIONS --quiet {} \;
+find . -name '*.cpp'   -exec astyle $OPTIONS --quiet {} \;
+find . -name '*.orig'  -exec rm {} \;
diff --git a/scripts/validate.bash b/scripts/validate.bash
index 1517ac0b..89eddcc4 100755
--- a/scripts/validate.bash
+++ b/scripts/validate.bash
@@ -25,7 +25,7 @@ LKT_BUILD=""                # Build folder of lektor
 
 function test_simple ()
 {
-    check_cd $LKT_INSTALL
+    check_cd $LKT_INSTALL/bin/
     ./lektord &
     local _child_pid=$!
     ./lkt pwd=hashire adm kil
@@ -33,7 +33,7 @@ function test_simple ()
     kill $_child_pid
     wait $_child_pid
     local _child_sta=$?
-    [ _child_sta -ne 0 ] && die "The lektord process exited with $_child_sta"
+    [ $_child_sta -ne 0 ] && die "The lektord process exited with $_child_sta"
 }
 
 ### Utility functions ########################################################
@@ -72,7 +72,8 @@ Usage validate.bash --build=* --step=* [option]
     --help, -h      Prints this help message.
     --build=comp    Specify the compiler.
     --step=AA,BB,.. Specify the steps to do. The list may contains the
-                    following items: configure, launch, build.
+                    following items: prepare, configure, launch, build,
+                    cleanup, format.
 
 Step configure options:
     --debug             Perform a debug build.
@@ -135,19 +136,24 @@ function do_steps ()
     local _build=""
     local _configure=""
     local _launch=""
+    local _cleanup=""
+    local _format=""
     for step in $LKT_STEP ; do
         case "$step" in
             build)      _build="yes";;
             configure)  _configure="yes";;
             launch)     _launch="yes";;
             prepare)    _prepare="yes";;
+            format)     _format="yes";;
         esac
     done
 
+    [ "x$_format"    = "xyes" ] && do_format
     [ "x$_prepare"   = "xyes" ] && do_prepare
     [ "x$_configure" = "xyes" ] && do_configure
     [ "x$_build"     = "xyes" ] && do_build
     [ "x$_launch"    = "xyes" ] && do_launch
+    [ "x$_cleanup"   = "xyes" ] && do_cleanup
 }
 
 ### PERFORM THE TESTS ########################################################
@@ -164,6 +170,22 @@ function do_launch ()
     done
 }
 
+### PERFORM THE FORMAT CHECK #################################################
+function do_format ()
+{
+    ./scripts/astyle.bash --check
+    ! [ $? -eq 0 ] && die "Format is incorrect, use 'scripts/astyle.bash'"
+}
+
+### CLEANUP THE BUILD AND INSTALL FOLDERS ####################################
+# Globals:                                                                   #
+# - LKT_WORK                The folder in which everything is compiled and   #
+#                           installed with tests and others                  #
+function do_cleanup ()
+{
+    rm -rf $LKT_WORK || die "Failed to clean directory $LKT_WORK"
+}
+
 ### PROCESS ARGS #############################################################
 # Globals:                                                                   #
 # - LKT_DEBUG               Whether or not to build in debug mode            #
-- 
GitLab