• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding: utf-8
3
4"""
5Copyright (c) 2025 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18Description: update unit test config json field
19"""
20
21import argparse
22import json
23import os
24
25OBFUSCATION_RULES = "obfuscationRules"
26APPLY_NAME_CACHE = "applyNameCache"
27PRINT_NAME_CACHE = "printNameCache"
28
29
30def parse_args():
31    parser = argparse.ArgumentParser()
32    parser.add_argument("--project-dir",
33                        help="project root directory")
34    return parser.parse_args()
35
36
37def update_json_file(config_path, field_name, field_value):
38    fd = os.open(config_path, os.O_RDWR | os.O_CREAT, 0o644)
39    with os.fdopen(fd, 'r+', encoding='utf-8') as file:
40        data = json.load(file)
41
42        if OBFUSCATION_RULES not in data or not isinstance(data[OBFUSCATION_RULES], dict):
43            return
44
45        if field_name in data[OBFUSCATION_RULES]:
46            return
47
48        data[OBFUSCATION_RULES][field_name] = field_value
49        file.seek(0)
50        json.dump(data, file, indent=2, ensure_ascii=False)
51        file.truncate()
52
53
54def modify_json_file(base_path, config_name, cache_name, json_field):
55    config_path = os.path.join(base_path, config_name)
56    cache_path = os.path.join(base_path, cache_name)
57    update_json_file(config_path, json_field, cache_path)
58
59
60if __name__ == '__main__':
61    args = parse_args()
62    configsPath = os.path.join(args.project_dir, 'tests/unittest/configs')
63    modify_json_file(configsPath, 'context_test_01_config.json', 'context_test_01_name_cache.json', APPLY_NAME_CACHE)
64    modify_json_file(configsPath, 'name_cache_test_02_config.json', 'new_name_cache_test.json', PRINT_NAME_CACHE)
65