diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index b304190..bb4698f 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -8,3 +8,7 @@ RUN apk update \ && apk add --no-cache \ shellcheck \ shfmt + +USER vscode + +RUN sh -c 'wget -O- https://git.io/shellspec | sh -s -- --yes' diff --git a/.github/run-test.sh b/.github/run-test.sh new file mode 100755 index 0000000..fcc5644 --- /dev/null +++ b/.github/run-test.sh @@ -0,0 +1,65 @@ +#!/bin/sh +# ============================================================================= +# ShellSpec による動的/単体テストの実行スクリプト +# ============================================================================= + +# ----------------------------------------------------------------------------- +# Constants +# ----------------------------------------------------------------------------- +PATH_DIR_REPO="$(dirname "$(cd "$(dirname "${BASH_SOURCE:-$0}")" && pwd)")" +PATH_DIR_RETURN="$(cd . && pwd)" +SUCCESS=0 +FAILURE=1 + +echo '===============================================================================' +echo ' Requirement Check for ShellSpec' +echo '===============================================================================' + +# ShellSpec のインストールチェック +which shellspec 1>/dev/null 2>/dev/null || { + echo >&2 "shellspec がインストールされていません" + echo >&2 "参考文献: https://github.com/shellspec/shellspec#installation" + + exit $FAILURE +} +echo "- ShellCheck $(shellcheck --version | grep version:)" + +# ----------------------------------------------------------------------------- +# Functions +# ----------------------------------------------------------------------------- + +runShellSpec() { + printf "%s" '- ShellSpec ' + + result=$(shellspec 2>&1) || { + printf >&2 ": NG\n%s" "$result" + + return $FAILURE + } + + echo "$(echo "$result" | head -n 2 | tail -n 1)" 'OK' +} + +# ----------------------------------------------------------------------------- +# Main +# ----------------------------------------------------------------------------- +set -eu + +cd "$PATH_DIR_REPO" || { + echo >&2 "Failed to change dir to: ${PATH_DIR_REPO}" + + exit $FAILURE +} + +echo '-------------------------------------------------------------------------------' +echo ' Running unit tests' +echo '-------------------------------------------------------------------------------' +runShellSpec + +cd "$PATH_DIR_RETURN" || { + echo >&2 "Failed to change dir to: ${PATH_DIR_RETURN}" + + exit $FAILURE +} + +exit $SUCCESS diff --git a/.github/workflows/shellspec_linux.yml b/.github/workflows/shellspec_linux.yml new file mode 100644 index 0000000..f3b7469 --- /dev/null +++ b/.github/workflows/shellspec_linux.yml @@ -0,0 +1,19 @@ +name: shellspec + +on: + workflow_dispatch: + pull_request: + +jobs: + unit_test: + runs-on: ubuntu-latest + steps: + - name: Checkout the code + uses: actions/checkout@v2 + + - name: Install latest shellspec + run: wget -O- https://git.io/shellspec | sh -s -- --yes + + - name: Run shellspec + run: | + ./.github/run-test.sh diff --git a/.shellspec b/.shellspec new file mode 100644 index 0000000..b4fbcd3 --- /dev/null +++ b/.shellspec @@ -0,0 +1,11 @@ +# ============================================================================= +# ShellSpec Configuration +# ============================================================================= +# Default command options/flags of `shellspec`. +# See: `shellspec --help` + +--default-path "tests" +--helperdir "./tests" +--pattern "**/*_test.sh" +--require spec_helper +--shell bash diff --git a/bin/archive b/bin/archive index 7a05015..97808fb 100755 --- a/bin/archive +++ b/bin/archive @@ -13,12 +13,20 @@ # - 注意:利用前にスクリプトに実行権限を与えるのを忘れないでください。 # +# md5s は md5sum/md5 のラッパー関数です. md5s() { - if [ -e md5sum ]; then - md5sum <"$1" - elif [ -e md5 ]; then + if [ -e "$(which md5sum)" ]; then + echo "$1" | md5sum | awk '{ print $1 }' + return $? + fi + + if [ -e "$(which md5)" ]; then md5 -q -s "$1" + return $? fi + + echo >&2 'MD5 ハッシュ関数がありません。' + exit 1 } md5f() { diff --git a/bin/enc b/bin/enc index 11a7954..e85734b 100755 --- a/bin/enc +++ b/bin/enc @@ -30,12 +30,20 @@ if [[ $# -lt 2 ]]; then exit 1 fi +# md5s は md5sum/md5 のラッパー関数です. md5s() { - if [ -e md5sum ]; then - echo "$1" | md5sum - elif [ -e md5 ]; then + if [ -e "$(which md5sum)" ]; then + echo "$1" | md5sum | awk '{ print $1 }' + return $? + fi + + if [ -e "$(which md5)" ]; then md5 -q -s "$1" + return $? fi + + echo >&2 'MD5 ハッシュ関数がありません。' + exit 1 } # コマンド引数取得 diff --git a/bin/sign b/bin/sign index 0df3eb5..ccf0f31 100755 --- a/bin/sign +++ b/bin/sign @@ -32,12 +32,20 @@ if [[ $# -lt 2 ]]; then exit 1 fi +# md5s は md5sum/md5 のラッパー関数です. md5s() { - if [ -e md5sum ]; then - echo "$1" | md5sum - elif [ -e md5 ]; then + if [ -e "$(which md5sum)" ]; then + echo "$1" | md5sum | awk '{ print $1 }' + return $? + fi + + if [ -e "$(which md5)" ]; then md5 -q -s "$1" + return $? fi + + echo >&2 'MD5 ハッシュ関数がありません。' + exit 1 } # コマンド引数取得 diff --git a/bin/verify b/bin/verify index 5687e31..8a2ac17 100755 --- a/bin/verify +++ b/bin/verify @@ -30,12 +30,20 @@ if [[ $# -lt 2 ]]; then exit 1 fi +# md5s は md5sum/md5 のラッパー関数です. md5s() { - if [ -e md5sum ]; then - echo "$1" | md5sum - elif [ -e md5 ]; then + if [ -e "$(which md5sum)" ]; then + echo "$1" | md5sum | awk '{ print $1 }' + return $? + fi + + if [ -e "$(which md5)" ]; then md5 -q -s "$1" + return $? fi + + echo >&2 'MD5 ハッシュ関数がありません。' + exit 1 } # コマンド引数取得 diff --git a/tests/issues/issue25_test.sh b/tests/issues/issue25_test.sh new file mode 100644 index 0000000..612a1cd --- /dev/null +++ b/tests/issues/issue25_test.sh @@ -0,0 +1,26 @@ +#shellcheck shell=bash + +# md5s は md5sum/md5 のラッパー関数です. +md5s() { + if [ -e "$(which md5sum)" ]; then + echo "$1" | md5sum | awk '{ print $1 }' + return $? + fi + + if [ -e "$(which md5)" ]; then + md5 -q -s "$1" + return $? + fi + + echo >&2 'MD5 ハッシュ関数がありません。' + exit 1 +} + +Describe 'md5s' + It 'should return MD5 hash of the arg 1' + When call md5s 'hoge' + + The output should equal 'c59548c3c576228486a1f0037eb16a1b' + The status should be success + End +End diff --git a/tests/spec_helper.sh b/tests/spec_helper.sh new file mode 100644 index 0000000..12333c4 --- /dev/null +++ b/tests/spec_helper.sh @@ -0,0 +1,48 @@ +#shellcheck shell=sh +# ============================================================================= +# spec_helper.sh は各テストの実行前に呼び出されるスクリプトです +# ============================================================================= +# See: https://github.com/shellspec/shellspec#spec_helper + +# set -eu + +# テスト中に利用可能なグローバル変数 +PATH_DIR_REPO="$(cd "$(dirname "${SHELLSPEC_SPECDIR:?'SHELLSPEC_SPECDIR not set'}")" && pwd)" +PATH_DIR_BIN="${PATH_DIR_REPO}/bin" +PATH_DIR_TEST="${PATH_DIR_REPO}/tests" + +export PATH_DIR_REPO +export PATH_DIR_BIN +export PATH_DIR_TEST + +# ----------------------------------------------------------------------------- +# テスト中パスが通っていないため引数を丸ごと渡す同名の代替関数を定義 +# ----------------------------------------------------------------------------- + +archive() { + "${PATH_DIR_BIN}/archive" "$@" +} + +check() { + "${PATH_DIR_BIN}/check" "$@" +} + +dec() { + "${PATH_DIR_BIN}/dec" "$@" +} + +enc() { + "${PATH_DIR_BIN}/enc" "$@" +} + +keygen() { + "${PATH_DIR_BIN}/keygen" "$@" +} + +sign() { + "${PATH_DIR_BIN}/sign" "$@" +} + +verify() { + "${PATH_DIR_BIN}/verify" "$@" +}