• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#  Copyright (c) 2023-2024 Huawei Device Co., Ltd.
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16
17import re
18import os
19import platform
20
21
22def read_declaration(path):
23    start_pattern = re.compile(r'^\/\*\*\-*')
24    end_pattern = re.compile(r'^\s*\-+\*\/')
25    context = ""
26    with open(path, 'r', encoding='utf-8', errors='ignore') as f:
27        declaration_begin = False
28        while True:
29            line = f.readline()
30            if not line:
31                break
32            if start_pattern.match(line):
33                declaration_begin = True
34                continue
35            if end_pattern.match(line):
36                declaration_begin = False
37                break
38            if declaration_begin:
39                context += line
40    return context
41
42
43def list_root_directorys_new(directorys_in_root, filtered_directories, max_ts_version):
44    # directorys_in_root like: ['2.0', '2.1', '2.2', ... '4.9', 'spec']
45    if max_ts_version is None:
46        filtered_directories = directorys_in_root
47    else:
48        max_ts_version = float(max_ts_version)
49        for f_item in directorys_in_root:
50            if f_item == 'spec':
51                filtered_directories.append(f_item)
52                continue
53            try:
54                f_num = float(f_item)
55                if f_num <= max_ts_version:
56                    filtered_directories.append(f_item)
57            except Exception as e:
58                print(e)
59
60    return filtered_directories
61
62
63def list_all_test_files_in_dir(dir_path, all_file_path=None, limit_version=None):
64    if all_file_path is None:
65        all_file_path = []
66
67    if dir_path.endswith("test_ts_cases") or dir_path.endswith("test_ts_cases/"):
68        is_root = True
69    else:
70        is_root = False
71
72    items_in_dir = os.listdir(dir_path)
73    filtered_directories = []
74
75    if is_root:
76        filtered_directories = list_root_directorys_new(items_in_dir, filtered_directories, limit_version)
77    else:
78        filtered_directories = items_in_dir
79
80    for file_dir in filtered_directories:
81        file_or_dir_path = os.path.join(dir_path, file_dir)
82        if platform.system().lower() != 'windows' and '\\' in file_or_dir_path:
83            file_or_dir_path = file_or_dir_path.replace('\\', '/')
84
85        if os.path.isdir(file_or_dir_path):
86            list_all_test_files_in_dir(file_or_dir_path, all_file_path, None)
87        else:
88            all_file_path.append(file_or_dir_path)
89
90    return all_file_path
91
92
93def get_disable_list(file_path):
94    disable_list = []
95    with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
96        while True:
97            line = f.readline()
98            if not line:
99                break
100            if platform.system().lower() == 'windows':
101                line = line.replace('/', '\\')
102            disable_list.append(line.strip())
103    return disable_list
104
105
106def is_disabled_case(file_path, disable_list):
107    if disable_list is None:
108        return False
109
110    for one in disable_list:
111        if file_path.endswith(one.strip()):
112            return True
113
114    return False