diff --git a/misc/CMakeLists.txt b/misc/CMakeLists.txt index 0fe620310..2d01872a3 100644 --- a/misc/CMakeLists.txt +++ b/misc/CMakeLists.txt @@ -190,6 +190,11 @@ if(ENABLE_TESTING) ${CMAKE_COMMAND} -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/tests/environment -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/environment_test.cmake) + + add_test( + NAME release_version_validation + COMMAND /bin/bash ${CMAKE_CURRENT_SOURCE_DIR}/tests/release_version_test.sh + ${PROJECT_SOURCE_DIR}) endif() # polkit actions diff --git a/misc/tests/release_version_test.sh b/misc/tests/release_version_test.sh new file mode 100644 index 000000000..c2ebd6184 --- /dev/null +++ b/misc/tests/release_version_test.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +# +# SPDX-License-Identifier: LGPL-3.0-or-later + +set -eu + +source_dir=$1 +test_dir=$(mktemp -d) +trap 'rm -rf "${test_dir}"' EXIT +unset GIT_DIR GIT_WORK_TREE + +cp "${source_dir}/release.bash" "${test_dir}/release.bash" +cat > "${test_dir}/CMakeLists.txt" <<'EOF' +project( + linglong + VERSION 1.15.0) +EOF + +cd "${test_dir}" +git init --quiet +git config user.name "Linyaps Test" +git config user.email "test@linyaps.invalid" +git config commit.gpgsign false +git config core.hooksPath /dev/null +git add CMakeLists.txt release.bash +git commit --quiet -m "initial" + +head_before=$(git rev-parse HEAD) +tags_before=$(git tag) + +if printf '1.2.invalid\n' | bash ./release.bash; then + printf 'release.bash accepted an invalid version\n' >&2 + exit 1 +fi + +if [ -n "$(git status --porcelain)" ]; then + printf 'release.bash changed the worktree for an invalid version:\n' >&2 + git status --short >&2 + exit 1 +fi + +if [ "$(git rev-parse HEAD)" != "${head_before}" ]; then + printf 'release.bash created a commit for an invalid version\n' >&2 + exit 1 +fi + +if [ "$(git tag)" != "${tags_before}" ]; then + printf 'release.bash created a tag for an invalid version\n' >&2 + exit 1 +fi + +printf 'Release version validation test passed\n' diff --git a/release.bash b/release.bash index 14458a5cc..d1f6df328 100755 --- a/release.bash +++ b/release.bash @@ -42,6 +42,11 @@ if [[ -z "$NEW_VERSION" ]]; then echo "自动递增小版本号: $NEW_VERSION" fi +if [[ ! "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "错误: 版本号必须使用 major.minor.patch 格式!" >&2 + exit 1 +fi + # 更新 CMakeLists.txt 文件中的 VERSION 字段 sed -E -i "/project\s*\(/,/\)/{ s/(VERSION\s+)[0-9]+\.[0-9]+\.[0-9]+/\1${NEW_VERSION}/