• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2021 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20
21
22##############################################################################
23##############################################################################
24
25def get_result_dir(testsuit_path):
26    result_rootpath = os.environ.get('PYTEST_RESULTPATH')
27    findkey = os.sep + "tests" + os.sep
28    filedir, _ = os.path.split(testsuit_path)
29    pos = filedir.find(findkey)
30    if -1 != pos:
31        subpath = filedir[pos + len(findkey):]
32        pos1 = subpath.find(os.sep)
33        if -1 != pos1:
34            subpath = subpath[pos1 + len(os.sep):]
35            result_path = os.path.join(result_rootpath, "result", subpath)
36        else:
37            result_path = os.path.join(result_rootpath, "result")
38    else:
39        result_path = os.path.join(result_rootpath, "result")
40
41    if not os.path.exists(result_path):
42        os.makedirs(result_path)
43
44    return result_path
45
46
47def get_resource_dir(phone_res_dir, device_type_name):
48    if device_type_name.startswith("PHONE"):
49        product_form_name = "phone"
50    elif device_type_name.startswith("IVI"):
51        product_form_name = "ivi"
52    elif device_type_name.startswith("TV"):
53        product_form_name = "intellitv"
54    elif device_type_name.startswith("WATCH"):
55        product_form_name = "wearable"
56    else:
57        product_form_name = "phone"
58
59    pos = phone_res_dir.find(os.sep + "tests" + os.sep)
60    if pos != -1:
61        prefix_path = phone_res_dir[:pos]
62        suffix_path = phone_res_dir[pos+1:]
63        prefix_path = os.path.abspath(os.path.dirname(prefix_path))
64        current_dir = os.path.join(prefix_path, product_form_name,
65            suffix_path)
66        if not os.path.exists(current_dir):
67            current_dir = phone_res_dir
68    else:
69        current_dir = phone_res_dir
70    return current_dir
71
72
73def create_empty_result_file(savepath, filename, message=""):
74    message = str(message)
75    message = message.replace("\"", "")
76    message = message.replace("<", "")
77    message = message.replace(">", "")
78    message = message.replace("&", "")
79    if filename.endswith(".hap"):
80        filename = filename.split(".")[0]
81    if not os.path.exists(savepath):
82        with open(savepath, "w", encoding='utf-8') as file_desc:
83            time_stamp = time.strftime("%Y-%m-%d %H:%M:%S",
84                                       time.localtime())
85            file_desc.write('<?xml version="1.0" encoding="UTF-8"?>\n')
86            file_desc.write(
87                '<testsuites tests="0" failures="0" '
88                'disabled="0" errors="0" timestamp="%s" '
89                'time="0" name="AllTests">\n' % time_stamp)
90            file_desc.write(
91                '  <testsuite name="%s" tests="0" failures="0" '
92                'disabled="0" errors="0" time="0.0" '
93                'unavailable="1" message="%s">\n' %
94                (filename, message))
95            file_desc.write('  </testsuite>\n')
96            file_desc.write('</testsuites>\n')
97    return
98
99
100##############################################################################
101##############################################################################
102
103