• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2024 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import logging
19import os
20from pathlib import Path
21
22from pytest import FixtureRequest, Parser, fixture
23
24from .ark_config import str2bool
25from .logs import ARK_ERR, ARK_OUT
26
27
28def dev_log_filters():
29    from .mirrors.type_cache import LOG as cache_log
30    from .runtime import LOG as runtime_log
31
32    return {
33        None: logging.INFO,
34        "trio-websocket": logging.WARNING,
35        "trio_cdp": logging.DEBUG,
36        runtime_log.name: min(ARK_ERR, ARK_OUT),
37        cache_log.name: logging.DEBUG,
38    }
39
40
41DEV_LOG_FILTERS = dev_log_filters()
42
43
44@fixture(scope="session", autouse=True)
45def dev_log(request: FixtureRequest):
46    opt = request.config.getoption("dev_log", request.config.getini("dev_log"), skip=True)
47    if opt:
48        levels = DEV_LOG_FILTERS
49        for name, level in levels.items():
50            logger = logging.getLogger(name)
51            if logger.level:
52                logger.setLevel(min(level, logger.level))
53            else:
54                logger.setLevel(level)
55
56
57def pytest_addoption(parser: Parser):
58
59    # --ark-build-path
60    parser.addini(
61        name="ark_build_path",
62        default=os.environ.get("ARK_BUILD", "."),
63        type="paths",
64        help="Default value for --ark-build-path",
65    )
66    parser.getgroup("run").addoption(
67        "--ark-build-path",
68        dest="ark_build_path",
69        type=Path,
70        help="Ark compiler build directory. Default: $ARK_BUILD or .",
71    )
72
73    # --dev-log
74    parser.addini(
75        name="dev_log",
76        default=False,
77        type="bool",
78        help="Default value for --dev-log",
79    )
80    parser.getgroup("dev").addoption(
81        "--dev-log",
82        dest="dev_log",
83        help="Enable dev log level filter",
84        type=str2bool,
85    )
86