From 47be547cfa85048ee511940dd3f2a599e4097d43 Mon Sep 17 00:00:00 2001 From: Manuel Streuhofer Date: Wed, 21 Dec 2016 19:30:28 +0100 Subject: [PATCH] extend acceptance test to check for code style (#81) * extend acceptance test to check for code style * cleanup following own style guide --- src/ini_val.sh | 10 +++++----- src/megamount.sh | 8 ++++---- src/parse_url.sh | 36 ++++++++++++++++----------------- src/templater.sh | 8 ++++---- test/acceptance.sh | 41 +++++++++++++++++++++++++++++++++----- test/style.pl | 39 ++++++++++++++++++++++++++++++++++++ website/_scripts/inject.sh | 2 +- 7 files changed, 107 insertions(+), 37 deletions(-) create mode 100755 test/style.pl diff --git a/src/ini_val.sh b/src/ini_val.sh index cc57cce..1750e63 100755 --- a/src/ini_val.sh +++ b/src/ini_val.sh @@ -36,7 +36,7 @@ function ini_val() { # Split on . for section. However, section is optional IFS='.' read -r section key <<< "${sectionkey}" - if [ -z "${key}" ]; then + if [[ ! "${key}" ]]; then key="${section}" section="" fi @@ -44,15 +44,15 @@ function ini_val() { local current current=$(awk -F "${delim}" "/^${key}${delim}/ {for (i=2; i> "${file}" else @@ -70,7 +70,7 @@ function ini_val() { fi } -if [ "${BASH_SOURCE[0]}" != "${0}" ]; then +if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then export -f ini_val else ini_val "${@}" diff --git a/src/megamount.sh b/src/megamount.sh index 6a0c853..cd68e32 100644 --- a/src/megamount.sh +++ b/src/megamount.sh @@ -53,13 +53,13 @@ function megamount () { (umount -lf "${target}" || umount -f "${target}") > /dev/null 2>&1 || true mkdir -p "${target}" - if [ "${proto}" = "smb://" ]; then + if [[ "${proto}" = "smb://" ]]; then mount -t cifs --verbose -o "username=${user},password=${pass},hard" "//${host}/${path}" "${target}" - elif [ "${proto}" = "afp://" ]; then + elif [[ "${proto}" = "afp://" ]]; then # start syslog-ng # afpfsd || echo "Unable to run afpfsd. Does /dev/log exist?" && exit 1 mount_afp "${url}" "${target}" - elif [ "${proto}" = "nfs://" ]; then + elif [[ "${proto}" = "nfs://" ]]; then mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" "${host}:/${path}" "${target}" else echo "ERR: Unknown protocol: '${proto}'" @@ -70,7 +70,7 @@ function megamount () { ls -al "${target}/" } -if [ "${BASH_SOURCE[0]}" != "${0}" ]; then +if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then export -f megamount else megamount "${@}" diff --git a/src/parse_url.sh b/src/parse_url.sh index e186dc7..a2c2d80 100644 --- a/src/parse_url.sh +++ b/src/parse_url.sh @@ -40,26 +40,26 @@ function parse_url() { local port local path - proto="$(echo "$parse" | grep :// | sed -e's,^\(.*://\).*,\1,g')" - url="${parse/$proto/}" - userpass="$(echo "$url" | grep @ | cut -d@ -f1)" - user="$(echo "$userpass" | grep : | cut -d: -f1)" - pass="$(echo "$userpass" | grep : | cut -d: -f2)" - hostport="$(echo "${url/$userpass@/}" | cut -d/ -f1)" - host="$(echo "$hostport" | grep : | cut -d: -f1)" - port="$(echo "$hostport" | grep : | cut -d: -f2)" - path="$(echo "$url" | grep / | cut -d/ -f2-)" + proto="$(echo "${parse}" | grep :// | sed -e's,^\(.*://\).*,\1,g')" + url="${parse/${proto}/}" + userpass="$(echo "${url}" | grep @ | cut -d@ -f1)" + user="$(echo "${userpass}" | grep : | cut -d: -f1)" + pass="$(echo "${userpass}" | grep : | cut -d: -f2)" + hostport="$(echo "${url/${userpass}@/}" | cut -d/ -f1)" + host="$(echo "${hostport}" | grep : | cut -d: -f1)" + port="$(echo "${hostport}" | grep : | cut -d: -f2)" + path="$(echo "${url}" | grep / | cut -d/ -f2-)" - [ -z "${user}" ] && user="${userpass}" - [ -z "${host}" ] && host="${hostport}" - if [ -z "${port}" ]; then - [ "${proto}" = "http://" ] && port="80" - [ "${proto}" = "https://" ] && port="443" - [ "${proto}" = "mysql://" ] && port="3306" - [ "${proto}" = "redis://" ] && port="6379" + [[ ! "${user}" ]] && user="${userpass}" + [[ ! "${host}" ]] && host="${hostport}" + if [[ ! "${port}" ]]; then + [[ "${proto}" = "http://" ]] && port="80" + [[ "${proto}" = "https://" ]] && port="443" + [[ "${proto}" = "mysql://" ]] && port="3306" + [[ "${proto}" = "redis://" ]] && port="6379" fi - if [ -n "${need}" ]; then + if [[ "${need}" ]]; then echo "${!need}" else echo "" @@ -76,7 +76,7 @@ function parse_url() { fi } -if [ "${BASH_SOURCE[0]}" != "${0}" ]; then +if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then export -f parse_url else parse_url "${@}" diff --git a/src/templater.sh b/src/templater.sh index f2155de..b4eb59f 100755 --- a/src/templater.sh +++ b/src/templater.sh @@ -30,11 +30,11 @@ function templater() { templateSrc="${1:-}" templateDst="${2:-}" - if [ ! -f "${templateSrc}" ]; then + if [[ ! -f "${templateSrc}" ]]; then echo "ERROR: Template source '${templateSrc}' needs to exist" exit 1 fi - if [ ! -n "${templateDst}" ]; then + if [[ ! "${templateDst}" ]]; then echo "ERROR: Template destination '${templateDst}' needs to be specified" exit 1 fi @@ -49,13 +49,13 @@ function templater() { # cat "${templateDst}" # shellcheck disable=SC2016 - if grep '${' "${templateDst}" && [ "${ALLOW_REMAINDERS}" = "0" ]; then + if grep '${' "${templateDst}" && [[ "${ALLOW_REMAINDERS}" = "0" ]]; then echo "ERROR: Unable to replace the above template vars" exit 1 fi } -if [ "${BASH_SOURCE[0]}" != "${0}" ]; then +if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then export -f templater else templater "${@}" diff --git a/test/acceptance.sh b/test/acceptance.sh index e7694d6..59cc586 100755 --- a/test/acceptance.sh +++ b/test/acceptance.sh @@ -44,7 +44,7 @@ trap cleanup_before_exit EXIT cmdSed=sed cmdTimeout=timeout -if [[ "${OSTYPE}" == "darwin"* ]]; then +if [[ "${OSTYPE}" = "darwin"* ]]; then cmdSed=gsed cmdTimeout=gtimeout fi @@ -206,10 +206,11 @@ if [[ "$(command -v shellcheck)" ]]; then echo -n " ${file}.. " - if ! shellcheck --shell=bash --external-sources "${file}" >> "${__accptstTmpDir}/shellcheck.err"; then - echo "✗" - failed="true" - continue + if ! shellcheck --shell=bash --external-sources --color=always \ + "${file}" >> "${__accptstTmpDir}/shellcheck.err"; then + echo "✗" + failed="true" + continue fi echo "✓" @@ -223,4 +224,34 @@ if [[ "$(command -v shellcheck)" ]]; then fi fi +# poor man's style guide checking +echo "==> b3bp style guide" +pushd "${__root}" > /dev/null + +failed="false" + +while IFS=$'\n' read -r file; do + [[ "${file}" =~ ^\./node_modules/ ]] && continue + [[ "${file}" =~ ^\./website/\.lanyon/ ]] && continue + + echo -n " ${file}.. " + + if ! "${__root}/test/style.pl" "${file}" >> "${__accptstTmpDir}/style.err"; then + echo "✗" + failed="true" + continue + fi + + echo "✓" +done <<< "$(find . -type f -iname '*.sh')" + +popd > /dev/null + +if [[ "${failed}" = "true" ]]; then + echo + cat "${__accptstTmpDir}/style.err" + echo + exit 1 +fi + exit 0 diff --git a/test/style.pl b/test/style.pl new file mode 100755 index 0000000..d8f6a16 --- /dev/null +++ b/test/style.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +die "usage: $0 \n" if (not @ARGV); + +my $rc = 0; +my $file = shift; + +open(my $fh, '<', $file) or die "Cannot open \`$file' for read: $!\n"; +while (<$fh>) { + next if (/^\s*#/); + + my $errors = 0; + + # remove everything between single quotes + # this will remove too much in case of: echo "var='$var'" + # and thus miss an opportunity to complain later on + # also it mangles the input line irreversible + s/'[^']+'/'___'/g; + + # highlight unbraced variables-- + # unless properly backslash'ed + $errors += s/((?:^|[^\\]))(((\\\\)+)?\$\w)/$1\033[31m$2\033[0m/g; + + # highlight single square brackets + $errors += s/((?:^|\s+))\[([^\[].+[^\]])\](\s*(;|&&|\|\|))/$1\033[31m\[\033[0m$2\033[31m\]\033[0m$3/g; + + # highlight double equal sign + $errors += s/(\[\[.*)(==)(.*\]\])/$1\033[31m$2\033[0m$3/g; + + next if (not $errors); + print "${file}[$.]: $_"; + $rc = 1; +} +close($fh); + +exit $rc; diff --git a/website/_scripts/inject.sh b/website/_scripts/inject.sh index c6caf19..6566178 100755 --- a/website/_scripts/inject.sh +++ b/website/_scripts/inject.sh @@ -69,7 +69,7 @@ EOF fi # Add a "<- Back Home" link, if any - echo -e $backLink >> "website/${targetName}.md" + echo -e "${backLink}" >> "website/${targetName}.md" echo "--> written website/${targetName}.md" done