Explorar o código

【新增】增强重品牌脚本并添加测试用例

- 在 rebrand.bash 中添加用户界面安装功能
- 优化配置文件替换逻辑
- 新增 test-rebrand.sh 脚本用于测试 rebrand 功能
- 测试用例覆盖不同重品牌场景,包括更改 UI ID、登录页面和系统名称
PJ568 hai 1 ano
pai
achega
bfe663e182
Modificáronse 2 ficheiros con 81 adicións e 4 borrados
  1. 14 4
      scripts/rebrand.bash
  2. 67 0
      scripts/test-rebrand.sh

+ 14 - 4
scripts/rebrand.bash

@@ -78,6 +78,16 @@ fi
 # 如果用户界面 ID 不为 pageos-ui 或登录页面路径不为空,替换 airootfs/etc/greetd/config.toml 中 pageos-session 的参数 | If the user interface ID is not pageos-ui or the login page path is not empty, replace the parameter pageos-session in airootfs/etc/greetd/config.toml
 if [ "$UI_ID" != "pageos-ui" ] || [ -n "$LOGIN_PATH" ]; then
   CONFIG_FILE="$REPO_DIR/airootfs/etc/greetd/config.toml"
+  # airootfs/usr/local/bin/pageos-pkgr repo install --repo airootfs/etc/skel/.local/share/pageos $UI_ID
+  if [ -n "$UI_ID" ] && [ "$UI_ID" != "pageos-ui" ]; then
+    recho "正在安装用户界面: $UI_ID" "Installing user interface: $UI_ID"
+    "$REPO_DIR/airootfs/usr/local/bin/pageos-pkgr" repo update --repo "$REPO_DIR/airootfs/etc/skel/.local/share/pageos"
+    "$REPO_DIR/airootfs/usr/local/bin/pageos-pkgr" repo install --repo "$REPO_DIR/airootfs/etc/skel/.local/share/pageos" "$UI_ID"
+    if [ $? -ne 0 ]; then
+      recho "错误: 无法安装用户界面 $UI_ID" "Error: Failed to install user interface $UI_ID"
+      exit 1
+    fi
+  fi
   
   # 检查配置文件是否存在 | Check if the configuration file exists
   if [ ! -f "$CONFIG_FILE" ]; then
@@ -86,13 +96,13 @@ if [ "$UI_ID" != "pageos-ui" ] || [ -n "$LOGIN_PATH" ]; then
   fi
   
   # 检查配置文件中是否存在要替换的行 | Check if the line to be replaced exists in the configuration file
-  if ! grep -q 'command = "pageos-session pageos-ui"' "$CONFIG_FILE"; then
+  if ! grep -q 'command = "pageos-session .*"' "$CONFIG_FILE"; then
     recho "错误: 配置文件中未找到要替换的命令行" "Error: Command line to replace not found in config file"
     exit 1
   fi
   
   # 替换配置文件中的命令 | Replace the command in the configuration file
-  sed -i "s|command = \"pageos-session pageos-ui\"|command = \"pageos-session $UI_ID $LOGIN_PATH\"|" "$CONFIG_FILE"
+  sed -i "s|command = \"pageos-session .*\"|command = \"pageos-session $UI_ID $LOGIN_PATH\"|" "$CONFIG_FILE"
   if [ $? -ne 0 ]; then
     recho "错误: 无法更新配置文件" "Error: Failed to update config file"
     exit 1
@@ -112,13 +122,13 @@ if [ -n "$SYS_NAME" ] && [ "$SYS_NAME" != "pageos" ]; then
   fi
   
   # 检查配置文件中是否存在要替换的行 | Check if the line to be replaced exists in the configuration file
-  if ! grep -q 'iso_name="pageos"' "$PROFILEDEF_FILE"; then
+  if ! grep -q 'iso_name="[^"]*"' "$PROFILEDEF_FILE"; then
     recho "错误: 配置文件中未找到要替换的 iso_name 行" "Error: iso_name line to replace not found in config file"
     exit 1
   fi
   
   # 替换配置文件中的 iso_name | Replace the iso_name in the configuration file
-  sed -i "s|iso_name=\"pageos\"|iso_name=\"$SYS_NAME\"|" "$PROFILEDEF_FILE"
+  sed -i "s|iso_name=\"[^\"]*\"|iso_name=\"$SYS_NAME\"|" "$PROFILEDEF_FILE"
   if [ $? -ne 0 ]; then
     recho "错误: 无法更新配置文件" "Error: Failed to update config file"
     exit 1

+ 67 - 0
scripts/test-rebrand.sh

@@ -0,0 +1,67 @@
+#!/usr/bin/env bash
+
+# 测试脚本,用于测试 rebrand.bash 的功能
+
+# 设置定量 | Quantities
+## 仓库所在目录 | Repository directory
+REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
+
+# 创建临时目录用于测试
+TEST_DIR=$(mktemp -d)
+echo "测试目录: $TEST_DIR"
+
+# 复制必要的文件到测试目录
+cp -r $REPO_DIR/scripts "$TEST_DIR/"
+cp -r $REPO_DIR/airootfs "$TEST_DIR/"
+cp $REPO_DIR/profiledef.sh "$TEST_DIR/"
+
+# 创建一个模拟的 pageos-pkgr 脚本
+cat > "$TEST_DIR/airootfs/usr/local/bin/pageos-pkgr" << 'EOF'
+#!/usr/bin/env bash
+echo "模拟安装包: $@"
+exit 0
+EOF
+
+chmod +x "$TEST_DIR/airootfs/usr/local/bin/pageos-pkgr"
+
+# 创建一个模拟的登录页面
+echo "<html><body>Test Login Page</body></html>" > "$TEST_DIR/login-page.html"
+
+# 测试 1: 仅更改 UI ID
+echo "=== 测试 1: 仅更改 UI ID ==="
+cd "$TEST_DIR" && ./scripts/rebrand.bash "test-ui"
+if [ $? -eq 0 ]; then
+  echo "测试 1 通过"
+else
+  echo "测试 1 失败"
+fi
+
+# 测试 2: 更改 UI ID 和登录页面
+echo "=== 测试 2: 更改 UI ID 和登录页面 ==="
+cd "$TEST_DIR" && ./scripts/rebrand.bash "test-ui-2" "file://$TEST_DIR/login-page.html"
+if [ $? -eq 0 ]; then
+  echo "测试 2 通过"
+else
+  echo "测试 2 失败"
+fi
+
+# 测试 3: 更改系统名称
+echo "=== 测试 3: 更改系统名称 ==="
+cd "$TEST_DIR" && ./scripts/rebrand.bash "" "" "test-os"
+if [ $? -eq 0 ]; then
+  echo "测试 3 通过"
+else
+  echo "测试 3 失败"
+fi
+
+# 测试 4: 同时更改所有参数
+echo "=== 测试 4: 同时更改所有参数 ==="
+cd "$TEST_DIR" && ./scripts/rebrand.bash "test-ui-3" "file://$TEST_DIR/login-page.html" "test-os-2"
+if [ $? -eq 0 ]; then
+  echo "测试 4 通过"
+else
+  echo "测试 4 失败"
+fi
+
+# 清理测试目录
+rm -rf "$TEST_DIR"