aboutsummaryrefslogtreecommitdiff
path: root/minishell_test/hooks.py
blob: 54963c94a2c66224e0b2aa978b08e9f84fde4932 (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
95
96
97
98
99
100
101
102
# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    hooks.py                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: charles <me@cacharle.xyz>                  +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/09/11 16:10:20 by charles           #+#    #+#              #
#    Updated: 2021/02/28 12:06:14 by cacharle         ###   ########.fr        #
#                                                                              #
# ############################################################################ #

import re

from minishell_test.config import Config


DISCARDED_TEXT = "DISCARDED BY TEST"


def sort_lines(output):
    """Sort lines of output"""
    return '\n'.join(sorted(output.split('\n')))


def error_line0(output):
    """Replace "/bin/bash: -c: line n:" by "minishell:" and delete the second line"""
    if not Config.check_error_messages:
        return DISCARDED_TEXT

    lines = output.split('\n')
    if len(lines) != 3:
        return output
    prefix = Config.shell_reference_prefix + "-c: "
    if not lines[0].startswith(prefix):
        return output
    return lines[0].replace(prefix, Config.minishell_prefix, 1) + "\n"


def discard(output):
    """Discard the output"""
    return DISCARDED_TEXT


def export_singleton(output):
    """Remove variable that are not set to anything in a call to export without arguments"""
    prefix = "export " if ("--posix" in Config.shell_reference_args) else "declare -x "
    return sort_lines(
        '\n'.join([line for line in output.split('\n')
                   if re.match("^{}[a-zA-Z_][a-zA-Z0-9_]*$".format(prefix), line) is None])
    )


def replace_double(s):
    """Replace double occurence of a string by one"""
    def hook(output):
        return output.replace(s + s, s)
    return hook


def error_eof_to_expected_token(output):
    return output.replace(
        "-c: line 1: syntax error: unexpected end of file",
        "syntax error expected token"
    )


def should_not_be(not_expected):
    def hook(output):
        if output == not_expected:
            return "OUTPUT SHOULD NOT BE " + output
        return DISCARDED_TEXT
    return hook


def platform_status(darwin_status, linux_status):
    def hook(status):
        if Config.platform == "darwin":
            return status
        elif Config.platform == "linux":
            return (darwin_status if status == linux_status else status)
        return status
    return hook


def linux_replace(f, t):
    def hook(output):
        if not Config.platform == "linux":
            return output
        return output.replace(f, t)
    return hook


is_directory = linux_replace("Is a directory", "is a directory")
shlvl_0_to_1 = linux_replace("SHLVL=0", "SHLVL=1")
delete_escape = linux_replace("\\", "")


def linux_discard(output):
    if not Config.platform == "linux":
        return output
    return DISCARDED_TEXT