我有一个测试功能,可以在测试执行之前设置我的环境。
Setup environment before all tests
setup_environment() {
set -x # Enable verbose output
# Determine the OS type
OS_TYPE=$(uname 2>/dev/null || echo "Unknown")
case "$OS_TYPE" in
Darwin|Linux)
TMP_DIR="/tmp"
;;
CYGWIN*|MINGW*|MSYS*)
# Ensure cygpath is available before using it
if command -v cygpath >/dev/null 2>&1; then
TMP_DIR="$(cygpath -m "${TEMP:-/tmp}")"
else
echo "Error: cygpath not found. Is Cygwin or Git Bash installed?" >&2
return 1
fi
;;
*)
echo "Error: Unsupported OS: $OS_TYPE" >&2
return 1
;;
esac
# Use a unique directory for each test run to avoid conflicts
TEST_ID=$(date +%s)
export EXTRACT_DELTA_CONSOLE_LOG_PATH="$TMP_DIR/extract-delta-console-log-$TEST_ID"
export MINIMIZED_CHECKOUT_ROOT="$TMP_DIR/minimized-checkout-root-$TEST_ID"
export CHECKOUT_ROOT="$TMP_DIR/checkout-root-$TEST_ID"
export EXTRACTED_DELTA_ROOT="$TMP_DIR/extracted-delta-root-$TEST_ID"
# Clean up any pre-existing directories
rm -rf "$EXTRACT_DELTA_CONSOLE_LOG_PATH" "$MINIMIZED_CHECKOUT_ROOT" "$CHECKOUT_ROOT" "$EXTRACTED_DELTA_ROOT"
# Create necessary directories and handle errors
mkdir -p "$EXTRACT_DELTA_CONSOLE_LOG_PATH" || {
echo "Failed to create log dir at $EXTRACT_DELTA_CONSOLE_LOG_PATH"
return 1
}
mkdir -p "$MINIMIZED_CHECKOUT_ROOT" || {
echo "Failed to create checkout dir at $MINIMIZED_CHECKOUT_ROOT"
return 1
}
mkdir -p "$CHECKOUT_ROOT" || {
echo "Failed to create root checkout dir at $CHECKOUT_ROOT"
return 1
}
mkdir -p "$EXTRACTED_DELTA_ROOT" || {
echo "Failed to create extracted delta dir at $EXTRACTED_DELTA_ROOT"
return 1
}
# Git operations
mkdir -p "$CHECKOUT_ROOT/default-branch"
cd "$CHECKOUT_ROOT/default-branch" || {
echo "Failed to change directory to $CHECKOUT_ROOT/default-branch"
return 1
}
git init || {
echo "Failed to initialize Git"
return 1
}
git config core.autocrlf false || {
echo "Failed to set Git config"
return 1
}
# Create and commit an initial file
echo "Initial file content" >file1.txt
git add file1.txt || {
echo "Failed to add file1.txt"
return 1
}
git commit -m "Initial commit" || {
echo "Failed to commit"
return 1
}
# Checkout the main branch
git checkout -b main || {
echo "Failed to checkout branch 'main'"
return 1
}
echo "Setup completed for test: $TEST_ID"
}
一旦执行,就会失败并出现以下错误
a) 错误:在所有钩子 'setup_environment || 之前发生错误失败“安装失败,正在终止”(退出状态:0)
在 after all 函数中也给出了类似的错误。
b) 错误:在钩子“teardown_environment ||”之后发生错误失败“拆卸失败”(退出状态:0)
+ eval 'set -- "$1" "${SHELLSPEC_AFTER_ALL_1#*:}" "${SHELLSPEC_AFTER_ALL_1%%:*}"'
++ set -- AFTER_ALL 'teardown_environment || fail "Teardown failed"' 1#105
+ SHELLSPEC_HOOK_BLOCK_NO=1
+ SHELLSPEC_HOOK_LINENO=105
+ case $1 in
+ '[' 1 = 1 ']'
+ shellspec_is_marked_group 1
+ eval '[ "$SHELLSPEC_MARK_1" ] &&:'
++ '[' 1 ']'
++ :
+ :
+ eval 'SHELLSPEC_HOOK=$2 && teardown_environment || fail "Teardown failed" &&:'
++ SHELLSPEC_HOOK='teardown_environment || fail "Teardown failed"'
++ teardown_environment
++ echo 'Tearing down environment...'
++ rm -rf C:/extract-delta-console-log-1725509273 C:/minimized-checkout-root-1725509273 C://checkout-root-1725509273 C:/extracted-delta-root-1725509273
++ :
+ :
+ SHELLSPEC_HOOK_STATUS=0
+ return 0
+ return 0
我添加了调试日志,但找不到任何其他错误。 SHELLSPEC_HOOK_STATUS=0 我可以在日志中找到,这意味着没有发生错误。但我的测试也没有被执行。
我使用的shell规范版本如下
$ shellspec -v
0.28.1
上面的脚本是在Windows操作系统中执行的,我也在检查操作系统的兼容性。
# Determine OS type
OS_TYPE=$(uname 2>/dev/null || echo "Unknown")
case "$OS_TYPE" in
Darwin|Linux)
TMP_DIR="/tmp"
;;
CYGWIN*|MINGW*|MSYS*)
if command -v cygpath >/dev/null 2>&1; then
TMP_DIR="$(cygpath -m "${TEMP:-/tmp}")"
else
echo "Error: cygpath not found" >&2
exit 1
fi
;;
*)
echo "Error: Unsupported OS: $OS_TYPE" >&2
exit 1
;;
esac
我更喜欢使用 then
if...then...elif...else...
结构而不是逻辑连接词,即 ||
或 &&
,因为在自动化测试框架中使用时绑定更紧密 - 这对于 BATS 来说当然是正确的,我想知道它是否也适用于想知道这是 Shellspec 吗?您是否使用类似的方法观察到相同的错误/不需要的行为?