From 7081b93b8ed4f98c628400e05d22d0523f41a842 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 27 Feb 2021 12:24:37 +0100 Subject: Fixing #16 - Adding support for custom config file in user directory --- minishell_test/test/captured.py | 20 +++++++++++--------- minishell_test/test/result.py | 34 ++++++++++------------------------ minishell_test/test/test.py | 30 ++++++++++++++---------------- 3 files changed, 35 insertions(+), 49 deletions(-) (limited to 'minishell_test/test') diff --git a/minishell_test/test/captured.py b/minishell_test/test/captured.py index 7db9739..a6141e8 100644 --- a/minishell_test/test/captured.py +++ b/minishell_test/test/captured.py @@ -6,13 +6,13 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/11 12:16:25 by charles #+# #+# # -# Updated: 2021/02/05 17:47:10 by charles ### ########.fr # +# Updated: 2021/02/27 12:20:00 by cacharle ### ########.fr # # # # ############################################################################ # from typing import List, Optional -import minishell_test.config as config +# from minishell_test import config class Captured: @@ -29,13 +29,15 @@ class Captured: files_content: content of the files altered by the command is_timeout: the command has timed out """ - lines = output.split('\n') - for i, l in enumerate(lines): - if l.find(config.REFERENCE_ERROR_BEGIN) == 0: - lines[i] = l.replace(config.REFERENCE_ERROR_BEGIN, config.MINISHELL_ERROR_BEGIN, 1) - elif l.find(config.REFERENCE_PATH + ": ") == 0: - lines[i] = l.replace(config.REFERENCE_PATH + ": ", config.MINISHELL_ERROR_BEGIN, 1) - self.output = '\n'.join(lines) + + # lines = output.split('\n') + # for i, l in enumerate(lines): + # if l.find(config.REFERENCE_ERROR_BEGIN) == 0: + # lines[i] = l.replace(config.REFERENCE_ERROR_BEGIN, config.MINISHELL_ERROR_BEGIN, 1) + # elif l.find(config.REFERENCE_PATH + ": ") == 0: + # lines[i] = l.replace(config.REFERENCE_PATH + ": ", config.MINISHELL_ERROR_BEGIN, 1) + + self.output = output # '\n'.join(lines) self.status = status self.files_content = files_content diff --git a/minishell_test/test/result.py b/minishell_test/test/result.py index fe465e5..b25face 100644 --- a/minishell_test/test/result.py +++ b/minishell_test/test/result.py @@ -6,15 +6,14 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/11 12:17:34 by charles #+# #+# # -# Updated: 2021/02/24 08:56:03 by cacharle ### ########.fr # +# Updated: 2021/02/27 12:10:13 by cacharle ### ########.fr # # # # ############################################################################ # -import sys import re from typing import Match, List, Optional -import minishell_test.config as config +from minishell_test import config from minishell_test.test.captured import Captured @@ -42,31 +41,18 @@ class BaseResult: def __repr__(self): """Returns a representation of the result based on the verbosity""" - if config.VERBOSE_LEVEL == 0: - return self.green('.') if self.passed else self.red('!') - if config.VERBOSE_LEVEL == 1: - printed = self._escaped_cmd[:] - if config.SHOW_RANGE: - printed = "{:2}: ".format(self.index) + printed - if len(printed) > config.TERM_COLS - 7: - printed = printed[:config.TERM_COLS - 10] + "..." - fmt = self.green("{:{width}} [PASS]") if self.passed else self.red("{:{width}} [FAIL]") - return fmt.format(printed, width=config.TERM_COLS - 7) - elif config.VERBOSE_LEVEL == 2: - return self.full_diff() - else: - raise RuntimeError("Invalid verbose level") + printed = self._escaped_cmd[:] + if config.SHOW_RANGE: + printed = "{:2}: ".format(self.index) + printed + if len(printed) > config.TERM_COLS - 7: + printed = printed[:config.TERM_COLS - 10] + "..." + fmt = self.green("{:{width}} [PASS]") if self.passed else self.red("{:{width}} [FAIL]") + return fmt.format(printed, width=config.TERM_COLS - 7) def put(self, index: int) -> None: """Print a summary of the result""" - if config.VERBOSE_LEVEL == 2 and self.passed: - return self.index = index - print(self, end="") - if config.VERBOSE_LEVEL == 0: - sys.stdout.flush() - else: - print() + print(self) def indicator(self, title: str, prefix: str) -> str: return self.bold(self.blue(prefix + " " + title)) diff --git a/minishell_test/test/test.py b/minishell_test/test/test.py index ff60522..c0e402f 100644 --- a/minishell_test/test/test.py +++ b/minishell_test/test/test.py @@ -6,19 +6,20 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/06/16 21:48:50 by charles #+# #+# # -# Updated: 2021/02/24 09:09:39 by cacharle ### ########.fr # +# Updated: 2021/02/27 12:19:15 by cacharle ### ########.fr # # # # ############################################################################ # import os import sys import subprocess +from pathlib import Path from typing import Optional, List, Dict, Union, Callable -import minishell_test.config as config +from minishell_test import config from minishell_test.test.captured import Captured from minishell_test.test.result import Result, LeakResult -import minishell_test.sandbox as sandbox +from minishell_test import sandbox HookType = Union[Callable[[str], str], List[Callable[[str], str]]] HookStatusType = Union[Callable[[int], int], List[Callable[[int], int]]] @@ -31,7 +32,7 @@ class Test: setup: str = "", files: List[str] = [], exports: Dict[str, str] = {}, - timeout: float = config.TIMEOUT, + timeout: float = config.TIMEOUT_TEST, hook: HookType = [], hook_status: HookStatusType = [], ): @@ -64,18 +65,16 @@ class Test: self.hook = [] self.hook_status = [] captured = self._run_sandboxed([*config.VALGRIND_CMD, "-c"]) - if config.VERBOSE_LEVEL == 2: - print(captured.output) self.result = LeakResult(self.full_cmd, captured) self.result.put(index) return - expected = self._run_sandboxed([config.REFERENCE_PATH, *config.REFERENCE_ARGS, "-c"]) - actual = self._run_sandboxed([config.MINISHELL_PATH, "-c"]) + expected = self._run_sandboxed([config.SHELL_REFERENCE_PATH, *config.SHELL_REFERENCE_ARGS, "-c"]) + actual = self._run_sandboxed([config.MINISHELL_EXEC_PATH, "-c"]) self.result = Result(self.full_cmd, self.files, expected, actual) self.result.put(index) - def _run_sandboxed(self, shell_cmd: List[str]) -> Captured: + def _run_sandboxed(self, shell_cmd: List[Union[str, Path]]) -> Captured: """ Run the command in a sandbox environment """ with sandbox.context(): if self.setup != "": @@ -83,7 +82,7 @@ class Test: subprocess.run( self.setup, shell=True, - cwd=config.SANDBOX_PATH, + cwd=config.SANDBOX_DIR, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, check=True @@ -97,7 +96,7 @@ class Test: sys.exit(1) return self._run_capture(shell_cmd) - def _run_capture(self, shell_cmd: List[str]) -> Captured: + def _run_capture(self, shell_cmd: List[Union[str, Path]]) -> Captured: """ Capture the output (stdout and stderr) Capture the content of the watched files after the command is run """ @@ -106,9 +105,9 @@ class Test: [*shell_cmd, self.cmd], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, - cwd=config.SANDBOX_PATH, + cwd=config.SANDBOX_DIR, env={ - 'PATH': config.PATH_VARIABLE, + 'PATH': config.SHELL_PATH_VARIABLE, 'TERM': 'xterm-256color', **self.exports, }, @@ -117,7 +116,7 @@ class Test: # https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate try: stdout, _ = process.communicate( - timeout=(self.timeout if not config.CHECK_LEAKS else config.CHECK_LEAKS_TIMEOUT)) + timeout=(self.timeout if not config.CHECK_LEAKS else config.TIMEOUT_LEAKS)) except subprocess.TimeoutExpired: process.kill() # _, _ = process.communicate(timeout=2) @@ -131,7 +130,7 @@ class Test: files_content: List[Optional[str]] = [] for file_name in self.files: try: - with open(os.path.join(config.SANDBOX_PATH, file_name), "rb") as f: + with open(os.path.join(config.SANDBOX_DIR, file_name), "rb") as f: files_content.append(f.read().decode()) except FileNotFoundError: files_content.append(None) @@ -156,7 +155,6 @@ class Test: @classmethod def try_run(cls, cmd: str) -> str: - config.VERBOSE_LEVEL = 2 test = Test(cmd) test.run(0) if isinstance(test.result, LeakResult): -- cgit From c92f2be21c6be2d44cd836dd7f362e545b9a1a90 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 27 Feb 2021 14:46:21 +0100 Subject: Added make_args and check_error_messages configuration options --- minishell_test/test/result.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'minishell_test/test') diff --git a/minishell_test/test/result.py b/minishell_test/test/result.py index b25face..87eeb5d 100644 --- a/minishell_test/test/result.py +++ b/minishell_test/test/result.py @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/11 12:17:34 by charles #+# #+# # -# Updated: 2021/02/27 12:10:13 by cacharle ### ########.fr # +# Updated: 2021/02/27 12:28:20 by cacharle ### ########.fr # # # # ############################################################################ # -- cgit From 0cf5f137f886bd4e80868dcf2cf74b3f3b2c28d3 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 27 Feb 2021 15:43:07 +0100 Subject: Fixing #10 - bash error line with number 0 or 1 --- minishell_test/test/captured.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'minishell_test/test') diff --git a/minishell_test/test/captured.py b/minishell_test/test/captured.py index a6141e8..e1ef29a 100644 --- a/minishell_test/test/captured.py +++ b/minishell_test/test/captured.py @@ -6,13 +6,14 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/11 12:16:25 by charles #+# #+# # -# Updated: 2021/02/27 12:20:00 by cacharle ### ########.fr # +# Updated: 2021/02/27 15:25:58 by cacharle ### ########.fr # # # # ############################################################################ # +import re from typing import List, Optional -# from minishell_test import config +from minishell_test import config class Captured: @@ -30,14 +31,12 @@ class Captured: is_timeout: the command has timed out """ - # lines = output.split('\n') - # for i, l in enumerate(lines): - # if l.find(config.REFERENCE_ERROR_BEGIN) == 0: - # lines[i] = l.replace(config.REFERENCE_ERROR_BEGIN, config.MINISHELL_ERROR_BEGIN, 1) - # elif l.find(config.REFERENCE_PATH + ": ") == 0: - # lines[i] = l.replace(config.REFERENCE_PATH + ": ", config.MINISHELL_ERROR_BEGIN, 1) - - self.output = output # '\n'.join(lines) + lines = output.split('\n') + for i, _ in enumerate(lines): + lines[i] = line = re.sub(f"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 -- cgit From 820d1ee00975d2e7859e4b2aded88dbf56a3b347 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 27 Feb 2021 16:12:34 +0100 Subject: Added configuration file section/key names check --- minishell_test/test/captured.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'minishell_test/test') diff --git a/minishell_test/test/captured.py b/minishell_test/test/captured.py index e1ef29a..d2fda8b 100644 --- a/minishell_test/test/captured.py +++ b/minishell_test/test/captured.py @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/11 12:16:25 by charles #+# #+# # -# Updated: 2021/02/27 15:25:58 by cacharle ### ########.fr # +# Updated: 2021/02/27 15:48:30 by cacharle ### ########.fr # # # # ############################################################################ # @@ -32,7 +32,7 @@ class Captured: """ lines = output.split('\n') - for i, _ in enumerate(lines): + for i, line in enumerate(lines): lines[i] = line = re.sub(f"line [01]: ", "", lines[i], 1) if line.startswith(config.SHELL_REFERENCE_PREFIX): lines[i] = config.MINISHELL_PREFIX + line[len(config.SHELL_REFERENCE_PREFIX):] -- cgit