Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 25 md5s #26

Merged
merged 10 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
65 changes: 65 additions & 0 deletions .github/run-test.sh
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions .github/workflows/shellspec_linux.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions .shellspec
Original file line number Diff line number Diff line change
@@ -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
14 changes: 11 additions & 3 deletions bin/archive
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 11 additions & 3 deletions bin/enc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

# コマンド引数取得
Expand Down
14 changes: 11 additions & 3 deletions bin/sign
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

# コマンド引数取得
Expand Down
14 changes: 11 additions & 3 deletions bin/verify
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

# コマンド引数取得
Expand Down
26 changes: 26 additions & 0 deletions tests/issues/issue25_test.sh
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions tests/spec_helper.sh
Original file line number Diff line number Diff line change
@@ -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" "$@"
}