aboutsummaryrefslogtreecommitdiff
path: root/minishell_test/__main__.py
blob: 727e1679e611a9bd08a03184b0321e9f977f3c24 (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
#!/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.config 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.test import Test
from minishell_test.suites import *  # noqa: F403,F401


def main(argv=None):
    args = parse_args(sys.argv[1:])
    Config.init(args)

    if args.list:
        print(Suite.list(), end="")
        sys.exit(0)

    # running ``make`` in minishell directory
    if Config.make:
        print("{:=^{width}}".format("MAKE", width=Config.term_cols))
        try:
            subprocess.run(
                ["make", *Config.make_args, "--no-print-directory", "-C", Config.minishell_dir],
                check=True,
                env=os.environ,
            )
        except subprocess.CalledProcessError:
            sys.exit(1)
        print("=" * Config.term_cols)

    # 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.run(args.suites)
    except KeyboardInterrupt:
        pass
    finally:
        sandbox.remove()
    Suite.summarize()
    Suite.save()

    # 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()