Skip to content

Commit

Permalink
Merge pull request #91 from rosineygp/feat/requirements-verify
Browse files Browse the repository at this point in the history
Feat/requirements verify
  • Loading branch information
rosineygp authored Mar 8, 2020
2 parents c9b5d27 + b62ab97 commit 54d2334
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 3 deletions.
49 changes: 49 additions & 0 deletions .mkdkr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,53 @@ _pretty() {
echo -e "${colors[${key}]}${*:2}\e[0m" >&2
}

_command_exist() {
if ! [ -x "$(command -v ${1})" ]; then
_pretty "red" "command: ${1} not found, please install it"
exit 1
fi
}

# https://stackoverflow.com/questions/16989598/comparing-php-version-numbers-using-bash#24067243
_version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "${1}"
}

_verify_make_version() {
_CURRENT="${1}"
if _version_gt "3.82" "$_CURRENT"; then
_pretty "red" "\nrequirements: [ERROR] make version needs to be > 3.81"
_pretty "yellow" "current: $_CURRENT\n\n"
exit 1
fi
}

_verify_git_version() {
_CURRENT="${1}"
if _version_gt "2" "$_CURRENT"; then
_pretty "yellow" "\nrequirements: [WARNING] git work better with version >= 2"
_pretty "yellow" "current: $_CURRENT\n\n"
fi
}

_verify_bash_version() {
_CURRENT="${1}"
if _version_gt "4" "$_CURRENT"; then
_pretty "red" "\nrequirements: [ERROR] use bash release > 3"
_pretty "yellow" "current: $_CURRENT\n\n"
exit 1
fi
}

_local_requirements() {
_command_exist "git"
_command_exist "make"
_command_exist "bash"
_verify_bash_version "$(bash --version | head -n 1 | awk '{ print $4 }')"
_verify_make_version "$(make --version | head -n 1 | awk '{ print $3 }')"
_verify_git_version "$(git --version | awk '{ print $3 }')"
}

# https://github.com/dylanaraps/pure-bash-bible#generate-a-uuid-v4
_uuid() {
C="89ab"
Expand Down Expand Up @@ -283,6 +330,8 @@ run:() {

if [ "$1" == "init" ]; then

_local_requirements

MKDKR_BRANCH_NAME="$(_branch_or_tag_name)"
MKDKR_BRANCH_NAME_SLUG="$(_branch_or_tag_name_slug ${MKDKR_BRANCH_NAME})"

Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ lint.shellcheck:
run: shellcheck -e SC2181 -e SC2086 -e SC1091 test/unit_branch_or_tag_name
run: shellcheck -e SC2181 -e SC2086 -e SC1091 test/unit_branch_or_tag_name_slug
run: shellcheck -e SC2181 -e SC2086 -e SC1091 test/unit_remote_include
run: shellcheck -e SC2181 -e SC2086 -e SC1091 test/unit_requirements
run: shellcheck test/cover

test.unit:
@$(dkr)
dind: docker:19 --workdir $(PWD)/test
run: apk add bash jq git
run: apk add bash jq git make
run: ./unit

DOCKER_BIN=https://download.docker.com/linux/static/stable/x86_64/docker-19.03.5.tgz
Expand Down Expand Up @@ -62,7 +63,7 @@ _coverage.report:
@$(dkr)
dind: kcov/kcov:v31 --workdir $(PWD)/test
run: rm -rf coverage
run: 'apt-get update && apt-get install -y curl jq bc git'
run: 'apt-get update && apt-get install -y curl jq bc git make'
$(call _docker_cli)
run: kcov --include-path=.mkdkr coverage unit
run: './cover > coverage/coverage.json'
Expand Down
3 changes: 2 additions & 1 deletion test/unit
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
./unit_run_command
./unit_branch_or_tag_name
./unit_branch_or_tag_name_slug
./unit_remote_include
./unit_remote_include
./unit_requirements
187 changes: 187 additions & 0 deletions test/unit_requirements
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#!/bin/bash

_compare_true() {
_version_gt "${1}" "${2}"
GT=$?
assertEquals "$GT" "1"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert ${1} ${3} ${2} [ OK ]"
}

_compare_false() {
_version_gt "${1}" "${2}"
GT=$?
assertEquals "$GT" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert ${1} ${3} ${2} [ OK ]"
}

test_mkdkr_version_gt() {
# shellcheck disable=SC1091
source .mkdkr

_compare_true "1.0.0" "1.0.1" "<"
_compare_true "1.0.0" "1.0.0" "=="
_compare_true "0.0.1" "0.0.2" "<"
_compare_true "0" "0" "=="
_compare_true "1.0.0" "2.0.0-devel" "<"
_compare_true "0.20.1" "0.200.1" "<"
_compare_true "2" "20" "<"
_compare_true "19.04" "20.04" "<"
_compare_true "01" "1" "<"

_compare_false "1.0.1" "1.0.0" ">"
_compare_false "0.0.2" "0.0.1" ">"
_compare_false "2.0.0-devel" "1.0.0" ">"
_compare_false "0.200.1" "0.20.1" ">"
_compare_false "20" "2" ">"
_compare_false "20.04" "19.04" ">"
_compare_false "1" "01" ">"

echo -e "\nRan\e[36m 16 \e[0masserts."
}

test_mkdkr_command_exist() {
# shellcheck disable=SC1091
source .mkdkr

_command_exist "bash"
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert bash exist [ OK ]"

_command_exist "make"
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert make exist [ OK ]"

_command_exist "git"
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert git exist [ OK ]"

(_command_exist "foo")
[[ "$?" == "1" ]] &&
_pretty "light_purple" "assert foo not exist [ OK ]"

(_command_exist "bar")
[[ "$?" == "1" ]] &&
_pretty "light_purple" "assert bar not exist [ OK ]"

echo -e "\nRan\e[36m 5 \e[0masserts."
}

test_mkdkr_verify_make_version() {
# shellcheck disable=SC1091
source .mkdkr

(_verify_make_version "4.3")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.3 [ OK ]"

(_verify_make_version "4.2")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.2 [ OK ]"

(_verify_make_version "4.1")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.1 [ OK ]"

(_verify_make_version "4.0")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.0 [ OK ]"

(_verify_make_version "3.82")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 3.82 [ OK ]"

(_verify_make_version "3.81")
assertEquals "$?" "1"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 3.81 [ OK ]"

echo -e "\nRan\e[36m 6 \e[0masserts."

}

test_mkdkr_verify_git_version() {
# shellcheck disable=SC1091
source .mkdkr

(_verify_git_version "2")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 2 [ OK ]"

(_verify_git_version "1.9")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 1.9 [ OK ]"

(_verify_git_version "1.8")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 1.8 [ OK ]"

echo -e "\nRan\e[36m 3 \e[0masserts."
}

test_mkdkr_verify_bash_version() {
# shellcheck disable=SC1091
source .mkdkr

(_verify_bash_version "5.0")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 5.0 [ OK ]"

(_verify_bash_version "4.4")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.4 [ OK ]"

(_verify_bash_version "4.3")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.3 [ OK ]"

(_verify_bash_version "4.2")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.2 [ OK ]"

(_verify_bash_version "4.1")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.1 [ OK ]"

(_verify_bash_version "4.0")
assertEquals "$?" "0"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 4.0 [ OK ]"

(_verify_bash_version "3.0")
assertEquals "$?" "1"
[[ "$?" == "0" ]] &&
_pretty "light_purple" "assert version 3.0 [ OK ]"

echo -e "\nRan\e[36m 7 \e[0masserts."
}

test_mkdkr_local_requirements() {
# shellcheck disable=SC1091
source .mkdkr

# just for code coverage, all tested above
(_local_requirements)

echo -e "\nRan\e[36m 1 \e[0masserts."
}

# shellcheck disable=SC1091
. ./shunit2

0 comments on commit 54d2334

Please sign in to comment.