对规范EDA测试用例源文件的思考
在EDA行业,对于测试用例而言,每个用例都有自己的数据来源,在公司内部,并不是所有研发/测试都会重复使用已存在的资源,每次遇到这种情况,研发/测试首选的是建造一个新的文件夹,并在测试用例中指向这一部分的文件。
Git自然是首选,问题是当这一部分文件变得相当庞大之后,Git或许并不是最高效的手段(存在大量流程),同时对文件的更新也无法及时的体现在对应的文件中(有一定延迟)
重新思考文件的管理规则
首先想到的是重新建造一套规则,满足以下几个条件
利用shell脚本重新构造一个命令,对测试目录具有直接管辖权
除开shell脚本,其他所有人员一律不能对测试目录进行操作
shell脚本具有完整的对测试目录添加,删除,锁定功能
基于以上的想法,构建了一套大概框架如下
代码时间
根据前一小节的设计思想,直接冲shell脚本。
编辑器抽风,格式乱的,理解概念就行咯~
#!/bin/bash
option=$1 # 选项(add/delete/lock/show)
source_path=$2 # 源路径
relative_d_path=$3 # 相对于 user_destination 的目标路径(可选)
destination="/home/piper/test" # 根目录
# 获取当前用户名
username=$(whoami)
if [ -z "$option" ]; then
echo "Usage: $0 <option> <source_path> [<relative_path>]"
echo "Options : "
echo " <add> : copy <source_path> to '$destination/$username'"
echo " <delete> : delete <source_path> from '$destination/$username'"
echo " <lock> : lock <source_path> in '$destination/$username'"
echo " <show> : show directory structure in '$destination/$username/[<source_path>]'"
exit 1
fi
if [ "$option" != "show" ]; then
if [ -z "$source_path" ]; then
echo -e "\033[31mError\033[0m: option '$option' requires specified path/file"
exit 1
fi
fi
# 在目标路径中创建基于用户名的文件夹
user_destination="$destination/$username"
mkdir -p "$user_destination"
if [ -z "$relative_d_path" ]; then
relative_path=$(basename "$source_path")
fi
full_source_path="$source_path"
full_destination_path="$user_destination/$relative_path"
#检查输入参数是否存在 '..'
checkpath="$full_destination_path/$full_source_path"
if [[ $checkpath =~ (^|\/)\.\.|\.(\/|$)|/^\.\.$ ]]; then
echo -e "\033[31mError\033[0m: File path not support '..' symbol"
exit 1
fi
echo full_source_path $full_source_path
echo full_destination_path $full_destination_path
if [ "$option" == "show" ]; then
if [ -z "$source_path" ]; then
source_path="$user_destination"
else
source_path="$user_destination/$source_path"
fi
if [ ! -e "$source_path" ]; then
echo -e "\033[31mError\033[0m: Source path does not exist."
exit 1
fi
tree -p "$source_path" # 使用tree命令显示目标路径的结构
elif [ "$option" == "add" ]; then
if [ ! -e "$full_source_path" ]; then
echo -e "\033[31mError\033[0m: Source does not exist."
exit 1
fi
if [ -d "$full_source_path" ]; then
if [ -e "$full_destination_path" ]; then
if [ ! -w "$full_destination_path" ]; then
echo -e "\033[31mError\033[0m: Destination file is locked (read-only) and cannot be overwritten."
exit 1
fi
echo -e "\033[36mInfo\033[0m: Merging folders..."
cp -Rn "$full_source_path/." "$full_destination_path" # 合并文件夹内容(不覆盖已存在文件)
echo -e "\033[36mInfo\033[0m: Folders merged successfully."
else
# sudo -u $USER
cp -Rn "$full_source_path" "$full_destination_path" # 复制整个文件夹
echo -e "\033[36mInfo\033[0m: Folder copied to $full_destination_path successfully."
fi
else
if [ -e "$full_destination_path" ]; then
if [ ! -w "$full_destination_path" ]; then
echo -e "\033[31mError\033[0m: Destination file is locked (read-only) and cannot be overwritten."
exit 1
fi
fi
# 检查目标文件夹是否为只读
if [ -w "$(dirname "$full_destination_path")" ]; then
cp "$full_source_path" "$full_destination_path" # 复制文件
echo -e "\033[36mInfo\033[0m: File copied to $full_destination_path successfully."
else
echo -e "\033[31mError\033[0m: Destination folder is locked (read-only) and cannot be written to."
exit 1
fi
fi
elif [ "$option" == "delete" ]; then
if [ -z "$relative_d_path" ]; then
full_path="$user_destination/$source_path"
else
full_path="$user_destination/$relative_d_path/$source_path"
fi
echo full_path $full_path
if [ ! -e "$full_path" ]; then
echo -e "\033[31mError\033[0m: File or path does not exist."
exit 1
fi
# 只读文件判定
if [ -d "$full_path" ]; then
# 使用find命令查找只读文件
readonly_files=$(find "$full_path" -type f ! -perm /u+w)
if [ -n "$readonly_files" ]; then
echo -e "\033[36mInfo\033[0m: $full_path have readonly files:"
echo "$readonly_files"
echo -e "\033[31mError\033[0m: Delete $full_path failed"
exit 1
fi
else
if [ ! -w "$full_path" ]; then
echo -e "\033[36mInfo\033[0m: $full_path is readonly file"
echo -e "\033[31mError\033[0m: Delete $full_path failed"
exit 1
fi
fi
rm -r "$full_path"
echo -e "\033[36mInfo\033[0m: Path deleted from $full_path successfully."
elif [ "$option" == "lock" ]; then
if [ -z "$relative_d_path" ]; then
full_path="$user_destination/$source_path"
else
full_path="$user_destination/$relative_d_path/$source_path"
fi
# echo full_path $full_path
if [ ! -e "$full_path" ]; then
echo -e "\033[31mError\033[0m: File or path '$full_path' does not exist."
exit 1
fi
if [ -w "$full_path" ]; then
if [ -d "$full_path" ]; then
# 递归设置目录及其子目录为只读权限
find "$full_path" -type f -exec chmod 444 {} +
find "$full_path" -type d -exec chmod 555 {} +
echo -e "\033[36mInfo\033[0m: Path '$full_path' now is read-only"
else
chmod 444 "$full_path"
echo -e "\033[36mInfo\033[0m: File '$full_path' now is read-only"
fi
else
echo -e "\033[36mInfo\033[0m: Path is already locked (read-only)."
exit 1
fi
else
echo -e "Invalid option: $option"
exit 1
fi
'''
不足的思考
这个脚本在实际运用中很难将testdata目录锁定,即用户只能通过脚本方式更改,这是我在测试中没有解决的问题,另外这其实并不能非常好的管理测试目录,只能说是一个临时的解决方案。
对于巨量的测试源数据,目前并没有发现好的方法,如何高效的管理这个庞大的复杂文件系统,是后面需要考虑的事情