aboutsummaryrefslogtreecommitdiff
path: root/minishell_test/test/captured.py
blob: 8c217a3a83a5b19c3e7ecbc46539f2403364bfb7 (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
# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    captured.py                                        :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: charles <me@cacharle.xyz>                  +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/09/11 12:16:25 by charles           #+#    #+#              #
#    Updated: 2021/02/27 16:15:13 by cacharle         ###   ########.fr        #
#                                                                              #
# ############################################################################ #

import re
from typing import List, Optional

from minishell_test.config import Config


class Captured:
    def __init__(
        self,
        output: str,
        status: int,
        files_content: List[Optional[str]],
        is_timeout: bool = False
    ):
        """Captured class
           output:        captured content
           status:        command status
           files_content: content of the files altered by the command
           is_timeout:    the command has timed out
        """

        lines = output.split('\n')
        for i, line in enumerate(lines):
            lines[i] = line = re.sub("line [01]: ", "", lines[i], 1)
            if line.startswith(Config.shell_reference_prefix):
                lines[i] = Config.minishell_prefix + line[len(Config.shell_reference_prefix):]
        self.output = '\n'.join(lines)

        self.status = status
        self.files_content = files_content
        self.is_timeout = is_timeout

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Captured):
            raise NotImplementedError
        if self.is_timeout:
            return self.is_timeout == other.is_timeout
        return (self.output == other.output and
                self.status == other.status and
                all(x == y for x, y in zip(self.files_content, other.files_content)))

    @staticmethod
    def timeout():
        """Create a new captured timeout"""
        return Captured("", 0, [], is_timeout=True)