Skip to content

Commit

Permalink
Implemented shellcheck support (#3)
Browse files Browse the repository at this point in the history
- Added shellcheck to the list of required tests
- Run all existing code through shellcheck and implemented suggested fixes
  • Loading branch information
Dmytro Konstantinov authored May 12, 2019
1 parent 97d53af commit 9f9be02
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
24 changes: 10 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ FROM base AS build
RUN apt-get -y update \
&& apt-get -y install \
binutils-dev \
cabal-install \
cmake \
curl \
gcc \
Expand All @@ -44,6 +45,10 @@ RUN apt-get -y update \
&& apt-get --purge autoremove \
&& apt-get clean
WORKDIR /home
RUN git clone --depth 1 --branch v0.6.0 https://github.com/koalaman/shellcheck.git \
&& cd shellcheck \
&& cabal update \
&& cabal install --global
RUN git clone --depth 1 --branch v1.1.0 https://github.com/bats-core/bats-core.git \
&& cd bats-core \
&& ./install.sh /usr/local
Expand All @@ -55,28 +60,19 @@ RUN git clone --depth 1 --branch v36 https://github.com/SimonKagstrom/kcov.git \
&& make install
ENTRYPOINT ["/etc/entrypoint.d/login_shell"]

FROM build AS clean
FROM build AS final
WORKDIR /home
COPY --from=build --chown=root /usr/local /usr/local
COPY --chown=payload . .
RUN rm -Rf \
./docker-fs \
./bats-core \
./kcov
USER payload
RUN ./bin/test \
&& TZ=UTC git show \
--pretty=tformat:"%H%+D%+ad%n%+s" \
--date=format-local:"%c %Z" \
| head -5 > ./VERSION \
&& rm -Rf ./.git
VOLUME ["/mnt"]
ENTRYPOINT ["/etc/entrypoint.d/test_getopts_long"]

FROM clean AS final
USER root
COPY --from=build --chown=root /usr/local /usr/local
COPY --from=clean --chown=payload /home /home
USER payload
&& rm -Rf \
./.git \
./docker-fs
WORKDIR /mnt
VOLUME ["/mnt"]
ENTRYPOINT ["/etc/entrypoint.d/test_getopts_long"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![getopts_long logo](https://raw.githubusercontent.com/UmkaDK/getopts_long/master/images/logo.png)](#)<br>

[![stable branch](https://img.shields.io/badge/dynamic/json.svg?color=lightgrey&label=stable&query=%24.default_branch&url=https%3A%2F%2Fapi.github.com%2Frepos%2FUmkaDK%2Fgetopts_long&logo=github)](https://github.com/UmkaDK/getopts_long/tree/master)
[![latest release](https://img.shields.io/badge/dynamic/json.svg?color=blue&label=release&query=%24.name&url=https%3A%2F%2Fapi.github.com%2Frepos%2FUmkaDK%2Fgetopts_long%2Freleases%2Flatest&logo=docker)](https://cloud.docker.com/repository/docker/umkadk/getopts_long)
[![latest release](https://img.shields.io/badge/dynamic/json.svg?color=blue&label=release&query=%24.name&url=https%3A%2F%2Fapi.github.com%2Frepos%2FUmkaDK%2Fgetopts_long%2Freleases%2Flatest&logo=docker)](https://hub.docker.com/r/umkadk/getopts_long)
[![test coverage](https://codecov.io/gh/UmkaDK/getopts_long/graph/badge.svg)](https://codecov.io/gh/UmkaDK/getopts_long)
[![donate link](https://img.shields.io/badge/donate-coinbase-gold.svg?colorB=ff8e00&logo=bitcoin)](https://commerce.coinbase.com/checkout/17ae30c2-9c3f-45fb-a911-36d01a3c81b6)

Expand Down
4 changes: 2 additions & 2 deletions bin/docker
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ CMD="${1}" && shift
case "${CMD}" in
'b'|'build')
if [[ -z "${*}" ]]; then docker image build --tag "${CONTAINER}" "${PWD}"
else docker image build ${@} --tag "${CONTAINER}" "${PWD}"; fi
else docker image build "${@}" --tag "${CONTAINER}" "${PWD}"; fi
;;
'r'|'run')
docker container run --rm --init -it -v "${PWD}:/mnt" "${CONTAINER}" "${@}"
;;
*)
echo "${0}: Unknown command -- ${cmd}" >&2
echo "${0}: Unknown command -- ${CMD}" >&2
exit 1
;;
esac
40 changes: 26 additions & 14 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -e

: "${GETOPTS_LONG_BIN:=./bin}"
: "${GETOPTS_LONG_LIB:=./lib}"
: "${GETOPTS_LONG_TEST:=./test}"
: "${GETOPTS_LONG_COVERAGE:=./coverage}"
Expand All @@ -11,29 +12,40 @@ __exit() {
exit "${2:-1}"
}

path_exists?() {
path_exists() {
: "${1:?Missing required argument -- directory path}"
[[ -e "${1}" ]] || __exit "Path not found -- ${1}"
}

color_diff() {
: "${1:?Missing required argument -- label color}"
: "${2:?Missing required argument -- coverage diff}"
: "${3:?Missing required argument -- version sha}"
printf "\e[01;%sm%+.2f%%\e[00m compared to %s" "${1}" "${2}" "${3}"
}

print_coverage_diff() {
: "${1:?Missing required argument -- new code coverage}"

local commit="$(head -c7 /home/VERSION)"
local coverage="$(jq -r .percent_covered "/home/coverage/coverage.json")"
local diff=$(echo "${coverage} - ${1}" | bc -l)

local label="%+.2f%% compared to ${commit}"
local color_label="\e[01;%sm%+.2f%%\e[00m compared to ${commit}"

if tty -s && [[ "${diff}" -gt '0' ]]; then printf "${color_label}" 32 "${diff}"
elif tty -s && [[ "${diff}" -lt '0' ]]; then printf "${color_label}" 31 "${diff}"
elif [[ "${diff}" == '0' ]]; then printf "unchanged compared to ${commit}"
else printf "${label}" "${diff}"; fi
local sha
sha="$(head -c7 /home/VERSION)"
local coverage
coverage="$(jq -r .percent_covered "/home/coverage/coverage.json")"
local diff
diff=$(echo "${coverage} - ${1}" | bc -l)

if tty -s && [[ "${diff}" -gt '0' ]]; then color_diff 32 "${diff}" "${sha}"
elif tty -s && [[ "${diff}" -lt '0' ]]; then color_diff 31 "${diff}" "${sha}"
elif [[ "${diff}" == '0' ]]; then printf "unchanged compared to %s" "${sha}"
else printf "%+.2f%% compared to %s" "${diff}" "${sha}"; fi
}

path_exists? "${GETOPTS_LONG_LIB}"
path_exists? "${GETOPTS_LONG_TEST}"
path_exists "${GETOPTS_LONG_LIB}"
path_exists "${GETOPTS_LONG_TEST}"

# Run shellcheck
echo "Checking code style: ${GETOPTS_LONG_BIN}/* ${GETOPTS_LONG_LIB}/*"
shellcheck -f tty -s bash -a "${GETOPTS_LONG_BIN}"/* "${GETOPTS_LONG_LIB}"/*

# Run bats
TEST_DIR="${*:-${GETOPTS_LONG_TEST}}"
Expand Down
2 changes: 1 addition & 1 deletion lib/getopts_long.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ getopts_long() {
builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1
[[ "${!optvar}" == '-' ]] || return 0

printf -v "${optvar}" "${OPTARG%%=*}"
printf -v "${optvar}" "%s" "${OPTARG%%=*}"

if [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}:([[:space:]]|$) ]]; then
OPTARG="${OPTARG#${!optvar}}"
Expand Down

0 comments on commit 9f9be02

Please sign in to comment.