aboutsummaryrefslogtreecommitdiff
path: root/minishell_test/__main__.py
blob: 7da33cd76dd4fdaa6c74d6c79a3a92bc7644b6ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3

# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    main.py                                            :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/07/15 15:11:52 by charles           #+#    #+#              #
#    Updated: 2020/07/15 15:11:52 by charles          ###   ########.fr        #
#                                                                              #
# ############################################################################ #

import os
import sys
import shutil
import distutils.spawn
import subprocess

from minishell_test import config
from minishell_test import sandbox
from minishell_test.args import parse_args
from minishell_test.suite.suite import Suite, SuiteException
from minishell_test.suites import *  # noqa: F403,F401
from minishell_test.test import Test


def main(argv=None):
    args = parse_args()

    if args.list:
        Suite.list()
        sys.exit(0)

    # running ``make`` in minishell directory
    if config.MAKE or args.make:
        print("{:=^{width}}".format("MAKE", width=config.TERM_COLS))
        try:
            subprocess.run(
                ["make", "--no-print-directory", "-C", config.MINISHELL_DIR],
                check=True,
                env={"MINISHELL_TEST_FLAGS": "-DMINISHELL_TEST", **os.environ}
            )
        except subprocess.CalledProcessError:
            sys.exit(1)
        print("=" * config.TERM_COLS)
        if args.make:
            sys.exit(0)

    # setup available commands
    if not config.SHELL_AVAILABLE_COMMANDS_DIR.exists():
        config.SHELL_AVAILABLE_COMMANDS_DIR.mkdir(parents=True, exist_ok=True)
    for cmd in config.SHELL_AVAILABLE_COMMANDS:
        copied_path = config.SHELL_AVAILABLE_COMMANDS_DIR / cmd
        if copied_path.exists():
            continue
        cmd_path = distutils.spawn.find_executable(cmd)
        if cmd_path is None:
            raise RuntimeError(f"Command not found {cmd}")
        shutil.copy(cmd_path, copied_path)

    if args.try_cmd is not None:
        print("Output")
        print(Test.try_run(args.try_cmd))
        sys.exit(0)

    try:
        Suite.setup(args.suites)
    except SuiteException as e:
        print(e)
        sys.exit(1)

    try:
        Suite.run_all()
    except KeyboardInterrupt:
        pass
    finally:
        sandbox.remove()

    Suite.summarize()
    Suite.save_log()
    print("See", config.LOG_PATH, "for more information")
    if config.CHECK_LEAKS:
        print("HELP: Valgrind is really slow the -x and --range options could be useful"
              " ({} -h for more details)".format(sys.argv[0]))

    if config.PAGER:
        # TODO {} replaced by filename in pager config var
        subprocess.run([config.PAGER_PROG, config.LOG_PATH])


if __name__ == "__main__":
    main()