• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WenjieDu / PyPOTS / 4676447045

pending completion
4676447045

Pull #48

github

GitHub
Merge 30b47daba into ce199d307
Pull Request #48: Add the lint-code workflow and pypots-cli to help development

79 of 79 new or added lines in 7 files covered. (100.0%)

2854 of 3389 relevant lines covered (84.21%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pypots/utils/commands/dev.py
1
"""
2
CLI tools to help the development team build PyPOTS.
3
"""
4

5
# Created by Wenjie Du <wenjay.du@gmail.com>
6
# License: GLP-v3
7

8
import os
×
9
from argparse import ArgumentParser, Namespace
×
10

11
from pypots.utils.commands import BaseCommand
×
12
from pypots.utils.logging import logger
×
13

14
IMPORT_ERROR_MESSAGE = (
×
15
    " `pypots-cli dev` command is for PyPOTS developers to run tests easily. "
16
    "Therefore, you need a complete PyPOTS development environment. However, you are missing some dependencies. "
17
    "Please refer to https://github.com/WenjieDu/PyPOTS/blob/main/environment-dev.yml for dependency details. "
18
)
19

20

21
def dev_command_factory(args: Namespace):
×
22
    return DevCommand(
×
23
        args.run_tests,
24
        args.k,
25
        args.show_coverage,
26
        args.lint_code,
27
    )
28

29

30
class DevCommand(BaseCommand):
×
31
    @staticmethod
×
32
    def register_subcommand(parser: ArgumentParser):
×
33
        train_parser = parser.add_parser("dev", help="CLI tool to help development ")
×
34
        train_parser.add_argument(
×
35
            "--run_tests",
36
            dest="run_tests",
37
            action="store_true",
38
            help="run all test cases",
39
        )
40
        train_parser.add_argument(
×
41
            "--show_coverage",
42
            dest="show_coverage",
43
            action="store_true",
44
            help="show the code coverage report after running tests",
45
        )
46
        train_parser.add_argument(
×
47
            "-k",
48
            type=str,
49
            default="",
50
            required=False,
51
            help="the -k option of pytest. Description of -k option in pytest: "
52
            "only run tests which match the given substring expression. An expression is a python evaluatable "
53
            "expression where all names are substring-matched against test names and their parent classes. "
54
            "Example: -k 'test_method or test_other' matches all test functions and classes whose name contains "
55
            "'test_method' or 'test_other', while -k 'not test_method' matches those that don't contain "
56
            "'test_method' in their names. -k 'not test_method and not test_other' will eliminate the matches. "
57
            "Additionally keywords are matched to classes and functions containing extra names in their "
58
            "'extra_keyword_matches' set, as well as functions which have names assigned directly to them. The "
59
            "matching is case-insensitive.",
60
        )
61
        train_parser.add_argument(
×
62
            "--lint_code",
63
            dest="lint_code",
64
            action="store_true",
65
            help="run Black and Flake8 to lint code",
66
        )
67
        train_parser.set_defaults(func=dev_command_factory)
×
68

69
    def __init__(
×
70
        self,
71
        run_tests: bool,
72
        k: str,
73
        show_coverage: bool,
74
        lint_code: bool,
75
    ):
76
        self._run_tests = run_tests
×
77
        self._k = k
×
78
        self._show_coverage = show_coverage
×
79
        self._lint_code = lint_code
×
80

81
    def run(self):
×
82
        if self._run_tests:
×
83
            try:
×
84
                pytest_command = f"pytest -k {self._k}" if self._k else "pytest"
×
85
                command_to_run_test = (
×
86
                    f"coverage run -m {pytest_command}"
87
                    if self._show_coverage
88
                    else pytest_command
89
                )
90
                logger.info(f"Executing '{command_to_run_test}'...")
×
91

92
                os.system(command_to_run_test)
×
93
                if self._show_coverage:
×
94
                    os.system("coverage report -m")
×
95
                    os.system("rm -rf .coverage")
×
96
                else:
97
                    logger.info(
×
98
                        "Omit the code coverage report. Enable it by using --show_coverage if in need."
99
                    )
100
                os.system("rm -rf .pytest_cache")
×
101

102
            except ImportError:
×
103
                raise ImportError(IMPORT_ERROR_MESSAGE)
×
104
            except Exception as e:
×
105
                raise RuntimeError(e)
×
106
        elif self._lint_code:
×
107
            try:
×
108
                logger.info("Reformatting with Black...")
×
109
                os.system("black .")
×
110
                logger.info("Linting with Flake8...")
×
111
                os.system("flake8 .")
×
112
            except ImportError:
×
113
                raise ImportError(IMPORT_ERROR_MESSAGE)
×
114
            except Exception as e:
×
115
                raise RuntimeError(e)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc