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

Master #128

Closed
wants to merge 4 commits into from
Closed
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
62 changes: 62 additions & 0 deletions src/assert_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
# SPDX-License-Identifier: GPL-2.0-only
import os
import re
from time import sleep
from typing import Union

Expand Down Expand Up @@ -601,3 +602,64 @@ def assert_ocr_not_exist(
f"{pic if pic else GlobalConfig.SCREEN_CACHE}",
)
)

@staticmethod
def assert_text_exist(src, det):
"""
断言源字符串中的所有字符串在目标字符串中都存在
"""
if not isinstance(det, str):
raise TypeError("det 必须是字符串")
if (isinstance(src, str)) and (src not in det):
raise AssertionError(f"字符串 '{src}' 不在 '{det}' 中")
elif isinstance(src, list):
for s in src:
if not isinstance(s, str):
raise TypeError("src 中的所有元素必须是字符串")
if s not in det:
raise AssertionError(f"字符串数组{src}中的字符串 '{s}' 不在 '{det}' 中")
else:
raise TypeError("src 必须是字符串或字符串列表")
@staticmethod
def assert_text_exist_with_wildcards(src, det):
"""
断言目标字符串中是否存在于与正则表达式(数组)所匹配的字符串
:param src : 正则表达式字符串 or 正则表达式字符串数组
:param det : 目标字符串
"""
if not isinstance(det, str):
raise TypeError("det 必须是字符串")
if isinstance(src, str):
src = [src]
for pattern in src:
if not isinstance(src, str):
raise TypeError("src 的所有元素必须是字符串")
if not re.search(pattern, det):
raise AssertionError(f"正则表达式 '{pattern}' 在字符串 '{det}' 中没有匹配项")
logger.info(f"{src}中的所有正则表达式均可匹配{det}")
def assert_file_exists_with_wildcards(self, path_wildcards):
"""
断言是否存在与正则表达式所匹配的路径的文件
仅文件名能使用正则表达式,文件路径使用全匹配方式
:param path_wildcards : 例如 /home/luobeichen/(my|your|his)lovestory.txt
"""
logger.info(f"查找与'{path_wildcards}'所匹配的文件")
# 解析输入字符串
folder_path, file_name_pattern = os.path.split(path_wildcards)
if not folder_path:
folder_path = '.'
# 定义正则表达式模式
pattern = re.compile(file_name_pattern)
# 初始化匹配的文件列表
matching_files = []
# 遍历指定目录及其子目录
for root , dirs, files in os.walk(folder_path):
for file in files:
if pattern.match(file):
matching_files.append(os.path.join(root, file))
if matching_files :
for matching_file in matching_files:
self.assert_file_exist(matching_file)
logger.info(f"找到{matching_file}文件")
else :
raise AssertionError(f"失败,不存在与{path_wildcards}所匹配的文件")
Loading