From 904a033ae738e1c351f8fef71e2ec2418fc4db3d Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 5 Feb 2021 12:27:32 +0100 Subject: Renaming src -> minishell_test for package name, Renaming main.py -> __main__.py for package execution with python -m --- minishell_test/__main__.py | 93 ++++++++ minishell_test/args.py | 87 +++++++ minishell_test/config.py | 120 ++++++++++ minishell_test/hooks.py | 123 ++++++++++ minishell_test/sandbox.py | 48 ++++ minishell_test/suite/__init__.py | 2 + minishell_test/suite/decorator.py | 47 ++++ minishell_test/suite/suite.py | 179 ++++++++++++++ minishell_test/suites/__init__.py | 17 ++ minishell_test/suites/builtin.py | 403 +++++++++++++++++++++++++++++++ minishell_test/suites/cmd.py | 331 ++++++++++++++++++++++++++ minishell_test/suites/flow.py | 290 ++++++++++++++++++++++ minishell_test/suites/misc.py | 100 ++++++++ minishell_test/suites/path.py | 137 +++++++++++ minishell_test/suites/preprocess.py | 463 ++++++++++++++++++++++++++++++++++++ minishell_test/test/__init__.py | 13 + minishell_test/test/captured.py | 56 +++++ minishell_test/test/result.py | 246 +++++++++++++++++++ minishell_test/test/test.py | 155 ++++++++++++ 19 files changed, 2910 insertions(+) create mode 100755 minishell_test/__main__.py create mode 100644 minishell_test/args.py create mode 100644 minishell_test/config.py create mode 100644 minishell_test/hooks.py create mode 100644 minishell_test/sandbox.py create mode 100644 minishell_test/suite/__init__.py create mode 100644 minishell_test/suite/decorator.py create mode 100644 minishell_test/suite/suite.py create mode 100644 minishell_test/suites/__init__.py create mode 100644 minishell_test/suites/builtin.py create mode 100644 minishell_test/suites/cmd.py create mode 100644 minishell_test/suites/flow.py create mode 100644 minishell_test/suites/misc.py create mode 100644 minishell_test/suites/path.py create mode 100644 minishell_test/suites/preprocess.py create mode 100644 minishell_test/test/__init__.py create mode 100644 minishell_test/test/captured.py create mode 100644 minishell_test/test/result.py create mode 100644 minishell_test/test/test.py (limited to 'minishell_test') diff --git a/minishell_test/__main__.py b/minishell_test/__main__.py new file mode 100755 index 0000000..fe48b5e --- /dev/null +++ b/minishell_test/__main__.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +# ############################################################################ # +# # +# ::: :::::::: # +# main.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 + +import config +import sandbox +from args import parse_args +from suite import Suite +from suites import * # noqa: F403,F401 + + +def main(): + args = parse_args() + if args.list: + Suite.list() + sys.exit(0) + + if config.MINISHELL_MAKE or args.make: + try: + print("{:=^{width}}".format("MAKE", width=config.TERM_COLS)) + subprocess.run(["make", "--no-print-directory", "-C", config.MINISHELL_DIR], + check=True, + env={"MINISHELL_TEST_FLAGS": "-DMINISHELL_TEST", **os.environ}) + print("=" * config.TERM_COLS) + except subprocess.CalledProcessError: + sys.exit(1) + if args.make: + sys.exit(0) + if os.path.exists(config.EXECUTABLES_PATH): + shutil.rmtree(config.EXECUTABLES_PATH) + os.mkdir(config.EXECUTABLES_PATH) + for cmd in config.AVAILABLE_COMMANDS: + cmd_path = distutils.spawn.find_executable(cmd) + if cmd_path is None: + raise RuntimeError + shutil.copy(cmd_path, + os.path.join(config.EXECUTABLES_PATH, cmd)) + + reference_args = os.environ.get("MINISHELL_TEST_ARGS") + if reference_args is not None: + config.REFERENCE_ARGS.extend(reference_args.split(',')) + + pager = os.environ.get("MINISHELL_TEST_PAGER") + if pager is not None: + config.PAGER = pager + + config.VERBOSE_LEVEL = args.verbose + if args.bonus or os.environ.get("MINISHELL_TEST_BONUS") == "yes": + config.BONUS = True + if args.no_bonus: + config.BONUS = False + config.EXIT_FIRST = args.exit_first + config.CHECK_LEAKS = args.check_leaks + config.RANGE = args.range + config.SHOW_RANGE = args.show_range + if config.RANGE is not None or config.CHECK_LEAKS: + config.SHOW_RANGE = True + + Suite.setup(args.suites) + try: + Suite.run_all() + except KeyboardInterrupt: + 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" + " (./run -h for more details)") + + if args.pager: + subprocess.run([config.PAGER, config.LOG_PATH]) + + +if __name__ == "__main__": + main() diff --git a/minishell_test/args.py b/minishell_test/args.py new file mode 100644 index 0000000..b7fcca6 --- /dev/null +++ b/minishell_test/args.py @@ -0,0 +1,87 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# args.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:24:32 by charles #+# #+# # +# Updated: 2021/01/11 22:20:16 by charles ### ########.fr # +# # +# ############################################################################ # + +import argparse +import textwrap + + +def parse_args(): + """Parse command line arguments""" + + parser = argparse.ArgumentParser( + description=textwrap.dedent(r""" + ___ ____ _ _ _ _ _ _ + | \/ (_) (_) | | | | | | | | | + | . . |_ _ __ _ ___| |__ ___| | | | |_ ___ ___| |_ + | |\/| | | '_ \| / __| '_ \ / _ \ | | | __/ _ \/ __| __| + | | | | | | | | \__ \ | | | __/ | | | || __/\__ \ |_ + \_| |_/_|_| |_|_|___/_| |_|\___|_|_| \__\___||___/\__| + """), + formatter_class=argparse.RawTextHelpFormatter, + epilog=textwrap.dedent("""\ + Signal handling is not tested + There is a commented glob suite in src/suites/preprocess.py. + Good luck handling `*'.*'`. + """) + ) + parser.add_argument( + "-k", "--check-leaks", action="store_true", + help="Run valgrind on tests (disable usual comparison with bash)" + ) + parser.add_argument( + "-r", "--range", nargs=2, type=int, metavar=("BEGIN", "END"), + help="Range of test index to run (imply --show-index)" + ) + parser.add_argument( + "--show-range", action="store_true", + help="Show test index (useful with --range)" + ) + parser.add_argument( + "-x", "--exit-first", action="store_true", + help="Exit on first fail" + ) + parser.add_argument( + "-v", "--verbose", action="count", + help="Increase verbosity level (e.g -vv == 2)" + ) + parser.add_argument( + "-b", "--bonus", action="store_true", + help="Enable bonus tests" + ) + parser.add_argument( + "-n", "--no-bonus", action="store_true", + help="Disable bonus tests" + ) + parser.add_argument( + "-l", "--list", action="store_true", + help="Print available test suites" + ) + parser.add_argument( + "-m", "--make", action="store_true", + help="Make minishell and exit" + ) + parser.add_argument( + "-g", "--pager", action="store_true", + help="After running the test, display the result in a pager of your choice" + ) + parser.add_argument( + "suites", nargs='*', metavar="suite", + help=textwrap.dedent("""\ + Test suites/group to run. + It tries to be smart and autocomplete the suite name + (e.g ./run int -> ./run preprocess/interpolation) + """) + ) + tmp = parser.parse_args() + if tmp.verbose is None: + tmp.verbose = 1 + return tmp diff --git a/minishell_test/config.py b/minishell_test/config.py new file mode 100644 index 0000000..493652c --- /dev/null +++ b/minishell_test/config.py @@ -0,0 +1,120 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# config.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:24:19 by charles #+# #+# # +# Updated: 2021/01/31 04:41:29 by charles ### ########.fr # +# # +# ############################################################################ # + +################################################################################ +# Minishell configuration file # +################################################################################ + +import os +import shutil +import distutils.spawn +from typing import List + +# run the bonus tests +# can be changed with `export MINISHELL_TEST_BONUS=yes` in your shell rc file. +BONUS = False + +# minishell dir path +MINISHELL_DIR = "../minishell" + +# minishell executable +MINISHELL_EXEC = "minishell" + +# make minishell before executing the test if set to True +MINISHELL_MAKE = True + +# path to reference shell (shell which will be compared minishell) +# has to support the -c option (sh, bash and zsh support it) +REFERENCE_PATH = "/bin/bash" +# can be changed with `export MINISHELL_TEST_ARGS=--poxix,--otherarg` +REFERENCE_ARGS: List[str] = [] # ["--posix"] + +# pager to use with --pager option +# can be changed with `export MINISHELL_TEST_PAGER=yourpager` +PAGER = "less" + +# log file path +LOG_PATH = "result.log" + +# path to the sandbox directory +# WARNING: will be rm -rf so be careful +SANDBOX_PATH = "sandbox" + +# where the availables commands are stored +EXECUTABLES_PATH = "./bin" + +# commands available in test" +AVAILABLE_COMMANDS = ["rmdir", "env", "cat", "touch", "ls", "grep", "sh", "head"] + +# $PATH environment variable passed to the shell +PATH_VARIABLE = os.path.abspath(EXECUTABLES_PATH) + +# test timeout +TIMEOUT = 0.5 + +# check leaks test timeout +CHECK_LEAKS_TIMEOUT = 10 + +LOREM = """ +Mollitia asperiores assumenda excepturi et ipsa. Nihil corporis facere aut a rem consequatur. +Quas molestiae corporis et quibusdam maiores. Molestiae sed unde aut at sed. +Deserunt quidem quidem aspernatur pariatur vel illum voluptatum. Culpa unde dolor aspernatur sit. +Mollitia tenetur sed eaque autem placeat a aut in. Ipsam ea consequuntur omnis. +Non et qui vel corrupti similique eum aut voluptatibus. Iste consequatur voluptatum et omnis debitis. +Sit quia neque nihil consequatur sint. Velit libero ut aut et et rerum. +Placeat cumque incidunt non repellat sunt perspiciatis ullam. +Repellendus repudiandae nostrum quia quis corrupti. +Rerum veniam earum cumque pariatur accusantium voluptatum omnis. +Alias ut et et adipisci. Tempore omnis numquam ullam et animi et eveniet. +Dolor itaque distinctio in. Magnam rerum quia est laboriosam repellat perspiciatis eos. +Consequuntur quae corrupti atque. Numquam enim ut ut. +Perspiciatis ut maxime et libero quo voluptas consequatur illum. Pariatur porro dolor cumque molestiae harum. +""" +LOREM = ' '.join(LOREM.split('\n')) + +############################################################################### +# You probably shouldn't edit after # +############################################################################### + +MINISHELL_PATH = os.path.abspath( + os.path.join(MINISHELL_DIR, MINISHELL_EXEC) +) + +VALGRIND_CMD: List[str] = [ + distutils.spawn.find_executable("valgrind") or "couldn't find valgrind", + # "valgrind", + "--trace-children=no", + "--leak-check=yes", + "--child-silent-after-fork=yes", + "--show-leak-kinds=definite", + MINISHELL_PATH, +] + +# 0, 1, 2 +VERBOSE_LEVEL = 1 + +MINISHELL_ERROR_BEGIN = os.path.basename(MINISHELL_PATH) + ": " +REFERENCE_ERROR_BEGIN = REFERENCE_PATH + ": line 0: " + +TERM_COLS = shutil.get_terminal_size().columns +if TERM_COLS < 40: + raise RuntimeError("You're terminal isn't wide enough") + +PLATFORM = os.uname().sysname + +EXIT_FIRST = False + +CHECK_LEAKS = False + +SHOW_RANGE = False + +RANGE = None diff --git a/minishell_test/hooks.py b/minishell_test/hooks.py new file mode 100644 index 0000000..e37f2aa --- /dev/null +++ b/minishell_test/hooks.py @@ -0,0 +1,123 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# hooks.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/11 16:10:20 by charles #+# #+# # +# Updated: 2020/11/25 21:36:18 by charles ### ########.fr # +# # +# ############################################################################ # + +import re +import sys +import os + +import config + + +def sort_lines(output): + """Sort lines of output""" + return '\n'.join(sorted(output.split('\n'))) + + +def error_line0(output): + """Replace "/bin/bash: -c: line 0:" by "minishell:" and delete the second line""" + error_message = os.environ.get("MINISHELL_TEST_DONT_CHECK_ERROR_MESSAGE") + if error_message is not None and error_message == "yes": + return "DISCARDED BY TEST" + + lines = output.split('\n') + if len(lines) != 3: + return output + prefix = "{}: -c: line 0: ".format(config.REFERENCE_PATH) + if lines[0].find(prefix) != 0: + return output + return lines[0].replace(prefix, "minishell: ") + "\n" + + +def discard(output): + """Discard the output""" + return "DISCARDED BY TEST" + + +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.REFERENCE_ARGS) else "declare -x " + return sort_lines( + '\n'.join([line for line in output.split('\n') + if re.match("^{}[a-zA-Z]+$".format(prefix), line) is None]) + ) + + +def replace_double_slash(output): + """Replace occurence of double slash by one""" + return output.replace("//", "/") + + +def replace_double_semi_colon(output): + """Replace occurence of double semi-colon by one""" + return output.replace(";;", ";") + + +def platform_status(darwin_status, linux_status, windows_status=None): + def hook(status): + if config.PLATFORM == "Darwin": + return status + elif config.PLATFORM == "Linux": + return (darwin_status if status == linux_status else status) + else: + raise RuntimeError("This platform exit codes are not supported yet," + "feel free to contact me to add it.") + sys.exit(2) + return status + return hook + + +def is_directory(output): + if config.PLATFORM == "Linux": + return output.replace("Is a directory", "is a directory") + else: + return output + + +# def no_cd_too_many_arguments(output): +# for i, line in output.split("\n"): +# if line.find("too many arguments") + + +def shlvl_0_to_1(output): + if config.PLATFORM == "Linux": + return output.replace("SHLVL=0", "SHLVL=1") + else: + return output + + +def delete_escape(output): + if config.PLATFORM == "Linux": + return output.replace("\\", "") + else: + return output + + +def error_eof_to_expected_token(output): + return output.replace( + "-c: line 1: syntax error: unexpected end of file", + "syntax error expected token" + ) + + +def linux_discard(output): + if config.PLATFORM == "Linux": + return "DISCARDED BY MINISHELL TEST" + else: + return output + + +def should_not_be(not_expected): + def hook(output): + if output == not_expected: + return "OUTPUT SHOULD NOT BE " + output + return "DISCARDED BY TEST" + return hook diff --git a/minishell_test/sandbox.py b/minishell_test/sandbox.py new file mode 100644 index 0000000..bd49d1e --- /dev/null +++ b/minishell_test/sandbox.py @@ -0,0 +1,48 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# sandbox.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/11 13:48:07 by charles #+# #+# # +# Updated: 2021/01/31 03:59:30 by charles ### ########.fr # +# # +# ############################################################################ # + +import os +import glob +import shutil +import subprocess +from contextlib import contextmanager + +import config + + +def create(): + """Create a new sandbox directory""" + try: + os.mkdir(config.SANDBOX_PATH) + except OSError: + pass + + +def remove(): + """Remove the sandbox directory + Brute force rm -rf if clean removal doesn't work due to permissions. + """ + try: + shutil.rmtree(config.SANDBOX_PATH) + except PermissionError: + subprocess.run(["chmod", "777", *glob.glob(config.SANDBOX_PATH + "/*")], check=True) + subprocess.run(["rm", "-rf", config.SANDBOX_PATH], check=True) + except FileNotFoundError: + pass + + +@contextmanager +def context(): + """Sandbox context manager""" + create() + yield + remove() diff --git a/minishell_test/suite/__init__.py b/minishell_test/suite/__init__.py new file mode 100644 index 0000000..6f7f321 --- /dev/null +++ b/minishell_test/suite/__init__.py @@ -0,0 +1,2 @@ +from suite.suite import Suite # noqa: F401 +from suite.decorator import suite # noqa: F401 diff --git a/minishell_test/suite/decorator.py b/minishell_test/suite/decorator.py new file mode 100644 index 0000000..fdc7fb6 --- /dev/null +++ b/minishell_test/suite/decorator.py @@ -0,0 +1,47 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# decorator.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/11 12:28:00 by charles #+# #+# # +# Updated: 2021/02/04 16:18:11 by charles ### ########.fr # +# # +# ############################################################################ # + +import inspect +from typing import List + +from suite import Suite +from test import Test + + +def suite(groups: List[str] = [], bonus: bool = False): # type: ignore + """Decorator generator for suites arguments""" + + def suite_wrapper(origin): + """Decorator for a suite function (fmt: suite_[name]) """ + + mod = inspect.getmodule(origin) + if mod is None: + raise NotImplementedError + mod_name = mod.__name__[len("suites."):] + name = "{}/{}".format(mod_name, origin.__name__[len("suite_"):]) + description = origin.__doc__ + if description is None: + print("You should had a doc string to the {} suite".format(name)) + description = "no description" + description = description.split("\n")[0].strip() + s = Suite(name, groups + [mod_name], bonus, description) + + def test_generator(): + def test(*args, **kwargs): + s.add(Test(*args, **kwargs)) + origin(test) + + s.generator_func = test_generator + Suite.available.append(s) + return test_generator + + return suite_wrapper diff --git a/minishell_test/suite/suite.py b/minishell_test/suite/suite.py new file mode 100644 index 0000000..836cac0 --- /dev/null +++ b/minishell_test/suite/suite.py @@ -0,0 +1,179 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# suite.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:24:29 by charles #+# #+# # +# Updated: 2021/02/04 16:13:08 by charles ### ########.fr # +# # +# ############################################################################ # + +import sys +from typing import List, Tuple, Optional, Callable + +import config +from test import Test + + +class Suite: + available: List['Suite'] = [] + + @classmethod + def run_all(cls): + """Run all available suites""" + for s in cls.available: + if not s.run() and config.EXIT_FIRST: + break + + @classmethod + def setup(cls, asked_names: List[str]) -> None: + """ Remove not asked suite from available suites + Tries to autocomplete the asked names + """ + if not config.BONUS: + cls.available = [s for s in cls.available if not s.bonus] + if len(asked_names) == 0: + asked_names = [s.name for s in cls.available] + + suite_names = [s.name for s in cls.available] + names = [] + for i, name in enumerate(asked_names): + if name in suite_names: + names.append(name) + continue + matches = [n for n in suite_names + if n.find("/") != -1 + and n[n.find("/") + 1:].startswith(name) + or n.startswith(name)] + if len(matches) == 1: + names.append(matches[0]) + elif len(matches) != 0 and all(n.startswith(name) for n in matches): + names.extend(matches) + elif len(matches) > 2: + print(("Ambiguous name `{}` match the following suites\n\t{}\n" + "Try to run with -l to see the available suites") + .format(name, ', '.join(matches))) + sys.exit(1) + elif len(matches) == 0: + print(("Name `{}` doesn't match any suite/group name\n\t" + "Try to run with -l to see the available suites") + .format(name)) + sys.exit(1) + + cls.available = list(set( + [s for s in cls.available if s.name in names] + + [s for s in cls.available if any(g for g in s.groups if g in names)] + )) + cls.available.sort(key=lambda s: s.name) + for s in cls.available: + if s.generator_func is not None: + s.generator_func() + + @classmethod + def available_names(cls) -> List[str]: + """List of available suites names""" + return [s.name for s in cls.available] + + @classmethod + def list(cls): + print("Groups:") + print("\n".join({" - " + ', '.join(s.groups) for s in Suite.available})) + print("The available suites are:") + max_name_width = max(len(s.name) for s in Suite.available) + 5 + lines = [ + " - {:.<{max_name_width}} {}".format( + s.name + " ", + s.description, + max_name_width=max_name_width + ) + for s in Suite.available + ] + print("\n".join(lines)) + + def __init__( + self, + name: str, + groups: List[str], + bonus: bool = False, + description: str = "no description", + ): + """Suite class + name: suite id + groups: list of suite groups + bonus: is this suite bonus + """ + self.name = name + self.groups = groups + self.description = description + self.bonus = bonus + self.generator_func: Optional[Callable] = None + self.tests: List[Test] = [] + + def add(self, test): + """Append a test to the suite""" + self.tests.append(test) + + BLUE_CHARS = "\033[34m" + CLOSE_CHARS = "\033[0m" + + def run(self) -> bool: + """Run all test in the suite""" + if config.VERBOSE_LEVEL == 0: + print(self.name + ": ", end="") + else: + print("{}{:#^{width}}{}".format( + self.BLUE_CHARS, + " " + self.name + " ", + self.CLOSE_CHARS, + width=config.TERM_COLS + )) + for i, t in enumerate(self.tests): + if config.RANGE is not None: + if not (config.RANGE[0] <= i <= config.RANGE[1]): + continue + t.run(i) + if config.EXIT_FIRST and t.result is not None and t.result.failed: + return False + if config.VERBOSE_LEVEL == 0: + print() + return True + + def total(self) -> Tuple[int, int]: + """Returns the total of passed and failed tests""" + passed_total = 0 + for t in self.tests: + if t.result is None: + return (-1, -1) + if t.result.passed: + passed_total += 1 + return passed_total, len(self.tests) - passed_total + + @classmethod + def summarize(cls): + """Print a summary of all runned suites""" + pass_sum = 0 + fail_sum = 0 + print("\nSummary:") + for s in cls.available: + (pass_total, fail_total) = s.total() + if pass_total == -1: + continue + pass_sum += pass_total + fail_sum += fail_total + print("{:.<{width}} \033[32m{:4} [PASS]\033[0m \033[31m{:4} [FAIL]\033[0m" + .format(s.name + " ", pass_total, fail_total, width=config.TERM_COLS - 24)) + print("{:.<{width}} \033[32m{:4} [PASS]\033[0m \033[31m{:4} [FAIL]\033[0m" + .format("TOTAL ", pass_sum, fail_sum, width=config.TERM_COLS - 24)) + + @classmethod + def save_log(cls): + """Save the result of all suites to a file""" + with open(config.LOG_PATH, "w") as log_file: + for s in cls.available: + for t in s.tests: + if t.result is not None and t.result.failed: + t.result.colored = False + t.result.set_colors() + log_file.write(t.result.full_diff() + '\n') diff --git a/minishell_test/suites/__init__.py b/minishell_test/suites/__init__.py new file mode 100644 index 0000000..b6b3b68 --- /dev/null +++ b/minishell_test/suites/__init__.py @@ -0,0 +1,17 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# __init__.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:24:48 by charles #+# #+# # +# Updated: 2020/09/11 13:25:26 by charles ### ########.fr # +# # +# ############################################################################ # + +import os +import glob + +modules = glob.glob(os.path.join(os.path.dirname(__file__), "*.py")) +__all__ = [os.path.basename(f)[:-3] for f in modules if os.path.isfile(f) and not f.endswith("__init__.py")] diff --git a/minishell_test/suites/builtin.py b/minishell_test/suites/builtin.py new file mode 100644 index 0000000..9ab2af8 --- /dev/null +++ b/minishell_test/suites/builtin.py @@ -0,0 +1,403 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# builtin.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: juligonz +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:24:43 by charles #+# #+# # +# Updated: 2020/11/28 06:17:19 by charles ### ########.fr # +# Updated: 2020/09/11 18:01:27 by juligonz ### ########.fr # +# # +# **************************************************************************** # + +import os + +import config +import hooks +from suite import suite +from hooks import linux_discard + + +@suite() +def suite_echo(test): + """ echo builtin tests """ + test("echo") + test("echo bonjour") + test("echo lalalala lalalalal alalalalal alalalala") + test("echo lalalala lalalalal alalalalal alalalala") + test("echo " + config.LOREM) + test("echo -n") + test("echo -n bonjour") + test("echo -n lalalala lalalalal alalalalal alalalala") + test("echo -n lalalala lalalalal alalalalal alalalala") + test("echo -n " + config.LOREM) + test("echo bonjour -n") + test("echo -n bonjour -n") + test(" echo bonjour je") + test(" echo -n bonjour je") + test("echo a '' b '' c '' d") + test('echo a "" b "" c "" d') + test("echo -n a '' b '' c '' d") + test('echo -n a "" b "" c "" d') + test("echo '' '' ''") + test("Echo bonjour") + test("eCho bonjour") + test("ecHo bonjour") + test("echO bonjour") + test("EchO bonjour") + test("eCHo bonjour") + test("EcHo bonjour") + test("eChO bonjour") + test("Echo bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("eCho bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("ecHo bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("echO bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("EchO bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("eCHo bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("EcHo bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("eChO bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("eChO -e 'bonjo\\nur'", exports={"PATH": "/bin:/usr/bin"}) + test("echo -n -n -n -n bonjour") + test("echo -nnnnnnnnnnnnnnnnnnnnn bonjour") + test("echo -nnnnnnnnnnnnnnnnnnnnn -n -n -n bonjour -n -n") + + +@suite() +def suite_export(test): + """ export builtin tests """ + test("export", hook=hooks.export_singleton) + test("export", exports={"A": ""}, hook=hooks.export_singleton) + test("export", exports={"A": "\""}, hook=hooks.export_singleton) + test("export", exports={"A": "\\"}, hook=hooks.export_singleton) + test("export", exports={"A": "$"}, hook=hooks.export_singleton) + test("export", exports={"A": "\t"}, hook=hooks.export_singleton) + test("export", exports={"A": "'"}, hook=hooks.export_singleton) + test("export", exports={"A": "a"}, hook=hooks.export_singleton) + test("export A=a; echo $A") + test("export A=a B=b C=c; echo $A$B$C") + test("export A=a B=b C=c D=d E=e F=f G=g H=h I=i J=j K=k L=l" + "M=m N=n O=o P=p Q=q R=r S=s T=t U=u V=v W=w X=x Y=y Z=z" + "; echo $A$B$C$D$E$F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$X$Y$Z") + test("export BONJOURJESUIS=a; echo $BONJOURJESUIS") + test("export bonjourjesuis=a; echo $bonjourjesuis") + test("export bonjour_je_suis=a; echo $bonjour_je_suis") + test("export BONJOURJESUIS1=a; echo $BONJOURJESUIS1") + test("export bO_nJq123o__1ju_je3234sui__a=a; echo $bO_nJq123o__1ju_je3234sui__a") + test("export a0123456789=a; echo $a0123456789") + test("export abcdefghijklmnopqrstuvwxyz=a; echo $abcdefghijklmnopqrstuvwxyz") + test("export ABCDEFGHIJKLMNOPQRSTUVWXYZ=a; echo $ABCDEFGHIJKLMNOPQRSTUVWXYZ") + test("export __________________________=a; echo $__________________________") + test("export _bonjour_=a; echo $_bonjour_") + test("export _=a; echo $_a") + test("export 1=a") + test("export BONJOURJESUIS =a") + test("export BONJOURJESUIS= a") + test(r"export BONJOUR\\JESUIS=a") + test(r"export BONJOUR\'JESUIS=a") + test(r'export BONJOUR\"JESUIS=a') + test(r"export BONJOUR\$JESUIS=a") + test(r"export BONJOUR\&JESUIS=a") + test(r"export BONJOUR\|JESUIS=a") + test(r"export BONJOUR\;JESUIS=a") + test(r"export BONJOUR\_JESUIS=a") + test(r"export BONJOUR\0JESUIS=a") + test(r"export \B\O\N\ \ \ \ \ \ \ JOURJESUIS=a") + test(r"export A=\B\O\N\ \ \ \ \ \ \ JOURJESUIS; echo $A") + test(r"export A='bonjour je suis charles'; echo $A") + test(r'export A="bonjour je suis charles"; echo $A') + test(r"export A==a; echo $A") + test(r"export A===a; echo $A") + test(r"export A====a; echo $A") + test(r"export A=====a; echo $A") + test(r"export A======a; echo $A") + test(r"export A=a=a=a=a=a; echo $A") + test("export A 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf ' C; echo $A$B$C") + test("export 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf ' C; echo $A$B$C") + test("export A 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf '; echo $A$B$C") + test("export A B C; echo $A$B$C") + test("export A B C; env", hook=hooks.sort_lines) + test("export 'AH@'=nop") + test("export \"AH'\"=nop") + test("export 'AH\"'=nop") + test("export 'AH$'=nop") + test("export 'AH!'=nop") + test("export 'AH|'=nop") + test("export 'AH;'=nop") + test("export 'AH&'=nop") + test("export 'AH\\'=nop") + test("export $TEST", exports={"TEST": "A=a"}) + test(r"export BONJOUR\\JESUIS") + test(r"export BONJOUR\'JESUIS") + test(r'export BONJOUR\"JESUIS') + test(r"export BONJOUR\$JESUIS") + test(r"export BONJOUR\&JESUIS") + test(r"export BONJOUR\|JESUIS") + test(r"export BONJOUR\;JESUIS") + test(r"export BONJOUR\_JESUIS") + test(r"export BONJOUR\0JESUIS") + test("Export B=bonjour") + test("exporT B=bonjour") + test("Export B=bonjour", exports={"PATH": "/bin:/usr/bin"}) + test("exporT B=bonjour", exports={"PATH": "/bin:/usr/bin"}) + + +@suite() +def suite_cd(test): + """ cd builtin tests """ + test("echo $PWD; echo $OLDPWD; cd .; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ..; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ../..; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ../../..; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ../../../..; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ../../../../..; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ../../../../../..; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd /; pwd; echo $PWD; echo $OLDPWD") + # /etc is a link to /etc/private + # hard and weird to implement with pwd + # test("cd /etc; pwd; echo $PWD") + test("echo $PWD; echo $OLDPWD; cd ''; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd '' ''; pwd; echo $PWD; echo $OLDPWD", hook=linux_discard) + test("echo $PWD; echo $OLDPWD; cd '' '' ''; pwd; echo $PWD; echo $OLDPWD", hook=linux_discard) + test("echo $PWD; echo $OLDPWD; cd ' '; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd d ''; pwd; echo $PWD; echo $OLDPWD", setup="mkdir d", hook=linux_discard) + test("echo $PWD; echo $OLDPWD; cd d d; pwd; echo $PWD; echo $OLDPWD", setup="mkdir d", hook=linux_discard) + test("echo $PWD; echo $OLDPWD; cd d ' '; pwd; echo $PWD; echo $OLDPWD", setup="mkdir d", hook=linux_discard) + test("echo $PWD; echo $OLDPWD; cd $HOME; pwd; echo $PWD; echo $OLDPWD", exports={"HOME": os.getenv("HOME")}) + test("echo $PWD; echo $OLDPWD; cd /; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd /.; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd /./; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd /././././; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd //; pwd; echo $PWD; echo $OLDPWD", hook=hooks.replace_double_slash) + test("echo $PWD; echo $OLDPWD; cd ///; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ////; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd //////////////////////////////////////////////////////; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd; echo $PWD; echo $OLDPWD", exports={"HOME": os.getenv("HOME")}) + test("echo $PWD; echo $OLDPWD; cd ' /'; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ' / '; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ' /'; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ' / '; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ' // '; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd //home; pwd; echo $PWD; echo $OLDPWD", hook=hooks.replace_double_slash) + test("echo $PWD; echo $OLDPWD; cd ' //home'; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd ' //home '; pwd; echo $PWD; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 001 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 002 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 003 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 004 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 005 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 006 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 007 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 010 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 020 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 030 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 040 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 050 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 060 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 070 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 100 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 200 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 300 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 400 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 500 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 600 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 700 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 755 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 644 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 311 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 111 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 222 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 333 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 0777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 1000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 2000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 3000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 4000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 5000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 6000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 7000 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 1777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 2777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 3777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 4777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 5777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 6777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 7777 d") + test("echo $PWD; echo $OLDPWD; cd d; echo $OLDPWD", setup="mkdir -m 0000 d") + test("echo $PWD; echo $OLDPWD; cd /; echo $PWD; echo $OLDPWD; unset PWD; cd /dev; echo $OLDPWD") + test("echo $PWD; echo $OLDPWD; cd /; echo $PWD; echo $OLDPWD; export PWD=bonjour; cd /dev; echo $OLDPWD") + # test("cd '\t'; pwd; echo $PWD"); + # test("cd '\t \t\t\t '; pwd; echo $PWD"); + # test("cd ~; pwd; echo $PWD"); # do we have to handle ~ ? + # test("cd ~/..; pwd; echo $PWD"); + # test("cd ~/../..; pwd; echo $PWD"); + # test("cd $HOME; pwd; echo $PWD") + test("Cd .") + test("cD .") + test("Cd .", exports={"PATH": "/bin:/usr/bin"}) + test("cD .", exports={"PATH": "/bin:/usr/bin"}) + + +@suite() +def suite_unset(test): + """ unset builtin tests """ + test("unset") + test("unset A; echo $A", exports={"A": "a"}) + test("unset 'A '; echo $A", exports={"A": "a"}) + test("unset 'A='; echo $A", exports={"A": "a"}) + test("unset A B C; echo $A$B$C", exports={"A": "a", "B": "b", "C": "c"}) + test("unset A 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf ' C; echo $A$B$C", + exports={"A": "a", "B": "b", "C": "c"}) + test("unset 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf ' C; echo $A$B$C", + exports={"A": "a", "B": "b", "C": "c"}) + test("unset A 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf '; echo $A$B$C", + exports={"A": "a", "B": "b", "C": "c"}) + test("unset A; echo $A$B$C", exports={"A": "a", "B": "b", "C": "c"}) + test("unset C; echo $A$B$C", exports={"A": "a", "B": "b", "C": "c"}) + test("unset A B C", exports={"A": "a", "B": "b", "C": "c"}) + test("unset A 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf ' C", + exports={"A": "a", "B": "b", "C": "c"}) + test("unset 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf ' C", + exports={"A": "a", "B": "b", "C": "c"}) + test("unset A 'asdf ' B ' asdf asdf asd f' ' asdf ' '' 'asdf '", + exports={"A": "a", "B": "b", "C": "c"}) + test("unset A", exports={"A": "a", "B": "b", "C": "c"}) + test("unset 'AH@'=nop") + test("unset \"AH'\"=nop") + test("unset 'AH\"'=nop") + test("unset 'AH$'=nop") + test("unset 'AH!'=nop") + test("unset 'AH|'=nop") + test("unset 'AH;'=nop") + test("unset 'AH&'=nop") + test("unset 'AH\\'=nop") + test("Unset TERM") + test("unseT TERM") + test("Unset TERM", exports={"PATH": "/bin:/usr/bin"}) + test("unseT TERM", exports={"PATH": "/bin:/usr/bin"}) + + +@suite() +def suite_pwd(test): + """ pwd builtin tests """ + test("pwd") + test("pwd", setup="cd ..") + test("pwd", setup="cd ../..") + test("pwd", setup="cd ../../..") + test("pwd", setup="cd /") + test("pwd", setup="cd $HOME") + test("pwd | cat -e") + test("pwd", exports={"PWD": "/etc"}) + test("unset PWD; pwd; echo $PWD") + test("export PWD=foo; pwd; echo $PWD") + # test("cd lnk; rmdir ../d; pwd", setup="mkdir d; ln -s d lnk") + test("Pwd") + test("pwD") + test("Pwd", exports={"PATH": "/bin:/usr/bin"}) + test("pwD", exports={"PATH": "/bin:/usr/bin"}) + + +@suite() +def suite_env(test): + """ env builtin tests """ + test("env", hook=[hooks.sort_lines, hooks.shlvl_0_to_1]) + test("env", setup="export A=a", hook=[hooks.sort_lines, hooks.shlvl_0_to_1]) + test("env", setup="export A=a B=b C=c", hook=[hooks.sort_lines, hooks.shlvl_0_to_1]) + test("env | cat -e", setup="export A=a B=b C=c", hook=[hooks.sort_lines, hooks.shlvl_0_to_1]) + test("Env") + test("enV") + test("Env", exports={"PATH": "/bin:/usr/bin"}) + test("enV", exports={"PATH": "/bin:/usr/bin"}) + + +@suite() +def suite_exit(test): + """ exit builtin tests """ + test("exit") + test("exit 1") + test("exit 2") + test("exit 3") + test("exit ' 3'") + test("exit '\t3'") + test("exit '\t\f\r 3'") + test("exit '3 '") + test("exit '3\t'") + test("exit '3\r'", hook_status=hooks.platform_status(255, 2)) + test("exit '3\t\f\r '", hook_status=hooks.platform_status(255, 2)) + test("exit '3 a'", hook_status=hooks.platform_status(255, 2)) + test("exit '3\t\t\ta'", hook_status=hooks.platform_status(255, 2)) + test("exit 0") + test("exit -0") + test("exit -1") + test("exit 255") + test("exit 256") + test("exit 2000000") + test("exit -2000000") + test("exit 2147483647") + test("exit -2147483648") + test("exit 2147483648") + test("exit -2147483649") + test("exit 3147483648") + test("exit -3147483649") + test("exit 4294967295") + test("exit 4294967296") + test("exit -9223372036854775808") + test("exit 9223372036854775807") + test("exit -9223372036854775809", hook_status=hooks.platform_status(255, 2)) + test("exit 9223372036854775808", hook_status=hooks.platform_status(255, 2)) + test("exit 18446744073709551615", hook_status=hooks.platform_status(255, 2)) + test("exit 18446744073709551616", hook_status=hooks.platform_status(255, 2)) + test("exit +1") + test("exit +2") + test("exit +3") + test("exit +0") + test("exit +255") + test("exit +256") + test("exit +2000000") + test("exit +2147483647") + test("exit ++1", hook_status=hooks.platform_status(255, 2)) + test("exit ++2", hook_status=hooks.platform_status(255, 2)) + test("exit ++3", hook_status=hooks.platform_status(255, 2)) + test("exit ++0", hook_status=hooks.platform_status(255, 2)) + test("exit ++255", hook_status=hooks.platform_status(255, 2)) + test("exit ++256", hook_status=hooks.platform_status(255, 2)) + test("exit ++2000000", hook_status=hooks.platform_status(255, 2)) + test("exit ++2147483647", hook_status=hooks.platform_status(255, 2)) + test("exit --1", hook_status=hooks.platform_status(255, 2)) + test("exit --2", hook_status=hooks.platform_status(255, 2)) + test("exit --3", hook_status=hooks.platform_status(255, 2)) + test("exit --0", hook_status=hooks.platform_status(255, 2)) + test("exit --255", hook_status=hooks.platform_status(255, 2)) + test("exit --256", hook_status=hooks.platform_status(255, 2)) + test("exit --2000000", hook_status=hooks.platform_status(255, 2)) + test("exit --2147483647", hook_status=hooks.platform_status(255, 2)) + test("exit bonjour", hook_status=hooks.platform_status(255, 2)) + test("exit 0_", hook_status=hooks.platform_status(255, 2)) + test("exit _0", hook_status=hooks.platform_status(255, 2)) + test("exit 0123456789") + test("exit -0123456789") + test("exit 00000000000000000000000000000000000000000000001") + test("exit 00000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000001") + test("exit 00000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000") + test("exit -00000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000001") + test("exit -99999999999999999999999999999999999999999999" + "99999999999999999999999999999999999999999999", hook_status=hooks.platform_status(255, 2)) + test("exit 99999999999999999999999999999999999999999999" + "99999999999999999999999999999999999999999999", hook_status=hooks.platform_status(255, 2)) + test("exit 0 bonjour") + test("exit bonjour 0", hook_status=hooks.platform_status(255, 2)) + test("exit 0 1") + test("exit 0 1 2 3 4 5 6 7 8 9") + test("exit " + config.LOREM, hook_status=hooks.platform_status(255, 2)) + test("exit bonjoru; echo should have exited", hook_status=hooks.platform_status(255, 2)) + test("exit 99999999999999999999999999999999999999999999999999999; echo should have exited", hook_status=hooks.platform_status(255, 2)) + test("exit 9999; echo should have exited") + test("Exit; echo a") + test("exiT; echo a") + test("Exit; echo a", exports={"PATH": "/bin:/usr/bin"}) + test("exiT; echo a", exports={"PATH": "/bin:/usr/bin"}) diff --git a/minishell_test/suites/cmd.py b/minishell_test/suites/cmd.py new file mode 100644 index 0000000..53b1f97 --- /dev/null +++ b/minishell_test/suites/cmd.py @@ -0,0 +1,331 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# cmd.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 15:11:46 by charles #+# #+# # +# Updated: 2021/02/04 16:14:54 by charles ### ########.fr # +# # +# ############################################################################ # + +import distutils + +import hooks +import config +from suite import suite + + +@suite() +def suite_redirection(test): + """ append/write/read redirections """ + test("echo bonjour > test", setup="", files=["test"]) + test("echo > test bonjour", setup="", files=["test"]) + test("> test echo bonjour", setup="", files=["test"]) + test("echo bonjour >> test", setup="", files=["test"]) + test("echo >> test bonjour", setup="", files=["test"]) + test(">> test echo bonjour", setup="", files=["test"]) + test("cat < test", setup="echo bonjour > test") + test("echo bonjour > test", setup="", files=["test"]) + test("echo > test'sticked' bonjour", setup="", files=["teststicked"]) + test("> test'sticked' echo bonjour", setup="", files=["teststicked"]) + test("echo bonjour >> test'sticked'", setup="", files=["teststicked"]) + test("echo >> test'sticked' bonjour", setup="", files=["teststicked"]) + test(">> test'sticked' echo bonjour", setup="", files=["teststicked"]) + test("cat < test'sticked'", setup="echo bonjour > test'sticked'") + test("< test'sticked' cat", setup="echo bonjour > test'sticked'") + test("echo > test\"sticked\" bonjour", setup="", files=["teststicked"]) + test("> test\"sticked\" echo bonjour", setup="", files=["teststicked"]) + test("echo bonjour >> test\"sticked\"", setup="", files=["teststicked"]) + test("echo >> test\"sticked\" bonjour", setup="", files=["teststicked"]) + test(">> test\"sticked\" echo bonjour", setup="", files=["teststicked"]) + test("cat < test\"sticked\"", setup="echo bonjour > test\"sticked\"") + test("< test\"sticked\" cat", setup="echo bonjour > test\"sticked\"") + test("echo > test'yo'\"sticked\" bonjour", setup="", files=["testyosticked"]) + test("> test'yo'\"sticked\" echo bonjour", setup="", files=["testyosticked"]) + test("echo bonjour >> test'yo'\"sticked\"", setup="", files=["testyosticked"]) + test("echo >> test'yo'\"sticked\" bonjour", setup="", files=["testyosticked"]) + test(">> test'yo'\"sticked\" echo bonjour", setup="", files=["testyosticked"]) + test("cat < test'yo'\"sticked\"", setup="echo bonjour > test'yo'\"sticked\"") + test("< test'yo'\"sticked\" cat", setup="echo bonjour > test'yo'\"sticked\"") + test("echo bonjour > test > je > suis", setup="", files=["test", "je", "suis"]) + test("echo > test > je bonjour > suis", setup="", files=["test", "je", "suis"]) + test("> test echo bonjour > je > suis", setup="", files=["test", "je", "suis"]) + test("echo bonjour >> test > je >> suis", setup="", files=["test", "je", "suis"]) + test("echo >> test bonjour > je > suis", setup="", files=["test", "je", "suis"]) + test(">> test echo > je bonjour > suis", setup="", files=["test", "je", "suis"]) + test("cat < test < je", setup="echo bonjour > test; echo salut > je") + test("echo bonjour>test>je>suis", setup="", files=["test", "je", "suis"]) + test(">test echo bonjour>je>suis", setup="", files=["test", "je", "suis"]) + test("echo bonjour>>test>je>>suis", setup="", files=["test", "je", "suis"]) + test("cat a'b'c'd'e'f'g'h'i'j'k'l'm'n'o'p'q'r's't'u'v'w'x'y'z'", + files=["abcdefghijklmnopqrstuvwxyz"]) + test('echo bonjour > a"b"c"d"e"f"g"h"i"j"k"l"m"n"o"p"q"r"s"t"u"v"w"x"y"z"', + files=["abcdefghijklmnopqrstuvwxyz"]) + test('echo bonjour > a\'b\'c"d"e\'f\'g"h"i\'j\'k"l"m\'n\'o"p\'q\'r"s\'t\'u"v"w"x"y\'z\'', + files=["abcdefghijklmnopqrstuvwxyz"]) + test("> file", files=["file"]) + test("< file", setup="echo bonjour > file") + test(">", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test(">>", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("<", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("echo >", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("echo >>", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("echo <", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("> test", files=["test"]) + test(">> test", files=["test"]) + test("< test", setup="touch test") + test("echo foo >>> bar", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("echo foo >>>> bar", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("echo foo >>>>> bar", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("cat << < bar", setup="echo bonjour > bar", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("cat << << bar", setup="echo bonjour > bar", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("cat <<<<< bar", setup="echo bonjour > bar", hook=hooks.error_line0, hook_status=hooks.platform_status(1, 2)) + test("cat < doesnotexist") + test("echo bonjour >> a", setup="echo a > a", files=["a"]) + test("echo bonjour >> a >> a", setup="echo a > a", files=["a"]) + test("echo bonjour > a", setup="echo a > a", files=["a"]) + test("echo bonjour > a >> a", setup="echo a > a", files=["a"]) + + +@suite() +def suite_cmd(test): + """ long cmd, cmd not found tests """ + test("notfound") + test("notfound a b c") + test('echo "\\"" >>a"b""c" ', files=["abc"]) + test("echo " + ''.join([chr(i) for i in range(1, 127) if chr(i) not in '\n`"\'()|&><'])) + test("echo foo>bar", files=["bar"]) + test("echo foo >bar", files=["bar"]) + test("echo foo> bar", files=["bar"]) + test("echo foo > bar", files=["bar"]) + test("echo a as df sad f as df qw e qwe as df asd f as df as d fas d f" + "asd f asd f asdf asdf asdf asd f asd f asd f asd f as df as df a" + "asd f asd f asdf asdf asdf asd f asd f asd f asd f as df as df a" + "asd f asd f asdf asdf asdf asd f asd f asd f asd f as df as df a") + test("echo " + config.LOREM * 10) + test("echo " + config.LOREM * 20) + + +@suite() +def suite_status(test): + """ $? tests """ + test("echo $?") + test("echo; echo $?") + test("notfound; echo $?") + test("cat < doesntexist; echo $?") + test("cat < noperm; echo $?", setup="echo bonjour > noperm; chmod 000 noperm") + test("echo") + test("notfound") + test("cat < doesntexist") + test("cat < noperm", setup="echo bonjour > noperm; chmod 000 noperm") + + +@suite() +def suite_cmd_path(test): + """ cmd is a relative path, permissions on executable """ + ls_path = distutils.spawn.find_executable("ls") + if ls_path is None: + print("Couldn't find `ls` in your PATH: Skipping suite") + return + cat_path = distutils.spawn.find_executable("cat") + if cat_path is None: + print("Couldn't find `cat` in your PATH: Skipping suite") + return + test(ls_path, setup="touch a b c") + test(ls_path + " -l", setup="touch a b c") + test("./bonjour", setup="touch a b c; cp {} bonjour".format(ls_path)) + test("./bonjour -l", setup="touch a b c; cp {} bonjour".format(ls_path)) + test("./somedir/bonjour -l", + setup="mkdir somedir; touch a b c; touch somedir/d somedir/e;" + "cp {} somedir/bonjour".format(ls_path)) + test("./ls . a b c", + setup="touch a b c; echo bonjour > a; cp {} ls".format(cat_path)) + test("ls . a b c", + setup="touch a b c; echo bonjour > a; cp {} ls".format(cat_path)) + test("./somefile", setup="echo > somefile; chmod 000 somefile") + test("./somefile", setup="echo > somefile; chmod 001 somefile") + test("./somefile", setup="echo > somefile; chmod 002 somefile") + test("./somefile", setup="echo > somefile; chmod 003 somefile") + test("./somefile", setup="echo > somefile; chmod 004 somefile") + test("./somefile", setup="echo > somefile; chmod 005 somefile") + test("./somefile", setup="echo > somefile; chmod 006 somefile") + test("./somefile", setup="echo > somefile; chmod 007 somefile") + test("./somefile", setup="echo > somefile; chmod 010 somefile") + test("./somefile", setup="echo > somefile; chmod 020 somefile") + test("./somefile", setup="echo > somefile; chmod 030 somefile") + test("./somefile", setup="echo > somefile; chmod 040 somefile") + test("./somefile", setup="echo > somefile; chmod 050 somefile") + test("./somefile", setup="echo > somefile; chmod 060 somefile") + test("./somefile", setup="echo > somefile; chmod 070 somefile") + test("./somefile", setup="echo > somefile; chmod 100 somefile") + test("./somefile", setup="echo > somefile; chmod 200 somefile") + test("./somefile", setup="echo > somefile; chmod 300 somefile") + test("./somefile", setup="echo > somefile; chmod 400 somefile") + test("./somefile", setup="echo > somefile; chmod 500 somefile") + test("./somefile", setup="echo > somefile; chmod 600 somefile") + test("./somefile", setup="echo > somefile; chmod 700 somefile") + test("./somefile", setup="echo > somefile; chmod 755 somefile") + test("./somefile", setup="echo > somefile; chmod 644 somefile") + test("./somefile", setup="echo > somefile; chmod 311 somefile") + test("./somefile", setup="echo > somefile; chmod 111 somefile") + test("./somefile", setup="echo > somefile; chmod 222 somefile") + test("./somefile", setup="echo > somefile; chmod 333 somefile") + test("somedir/", setup="mkdir somedir", hook=hooks.is_directory) + test("./somedir/", setup="mkdir somedir", hook=hooks.is_directory) + test("somedir", setup="mkdir somedir") + test("./somedir", setup="mkdir somedir", hook=hooks.is_directory) + test("somedir", setup="mkdir somedir") + test("somedirsoftlink/", setup="mkdir somedir; ln -s somedir somedirsoftlink", hook=hooks.is_directory) + test("./somedirsoftlink/", setup="mkdir somedir; ln -s somedir somedirsoftlink", hook=hooks.is_directory) + test("somedirsoftlink", setup="mkdir somedir; ln -s somedir somedirsoftlink") + test("./somedirsoftlink", setup="mkdir somedir; ln -s somedir somedirsoftlink", hook=hooks.is_directory) + test("somedirsoftlink", setup="mkdir somedir; ln -s somedir somedirsoftlink") + test("./someremovedlink", setup="touch somefile; ln -s somefile someremovedlink; rm -f somefile") + test("./somelink2", setup="touch somefile; ln -s somefile somelink1; ln -s somelink1 somelink2") + test("./somelink3", setup="touch somefile; ln -s somefile somelink1; ln -s somelink1 somelink2;" + "ln -s somelink2 somelink3") + test("./somelink4", setup="touch somefile; ln -s somefile somelink1; ln -s somelink1 somelink2;" + "ln -s somelink2 somelink3; ln -s somelink3 somelink4") + test("./somelink2ls", setup="cp " + ls_path + " somefile;" + "ln -s somefile somelink1; ln -s somelink1 somelink2") + test("./somelink3ls", setup="cp " + ls_path + " somefile;" + "ln -s somefile somelink1; ln -s somelink1 somelink2;" + "ln -s somelink2 somelink3") + test("./somelink4ls", setup="cp " + ls_path + " somefile;" + "ln -s somefile somelink1; ln -s somelink1 somelink2;" + "ln -s somelink2 somelink3; ln -s somelink3 somelink4") + test("_", setup="touch _") + test("'-'", setup="touch -") + test("./_", setup="touch _") + test("./-", setup="touch a; mv a ./-") + test("./.", setup="touch .", hook=hooks.is_directory) + test("./..", setup="touch ..", hook=hooks.is_directory) + test("./somefile", setup='echo > somefile && chmod 0777 somefile') + test("./somefile", setup='echo > somefile && chmod 1000 somefile') + test("./somefile", setup='echo > somefile && chmod 2000 somefile') + test("./somefile", setup='echo > somefile && chmod 3000 somefile') + test("./somefile", setup='echo > somefile && chmod 4000 somefile') + test("./somefile", setup='echo > somefile && chmod 5000 somefile') + test("./somefile", setup='echo > somefile && chmod 6000 somefile') + test("./somefile", setup='echo > somefile && chmod 7000 somefile') + test("./somefile", setup='echo > somefile && chmod 1777 somefile') + test("./somefile", setup='echo > somefile && chmod 2777 somefile') + test("./somefile", setup='echo > somefile && chmod 3777 somefile') + test("./somefile", setup='echo > somefile && chmod 4777 somefile') + test("./somefile", setup='echo > somefile && chmod 5777 somefile') + test("./somefile", setup='echo > somefile && chmod 6777 somefile') + test("./somefile", setup='echo > somefile && chmod 7777 somefile') + test("./somefile", setup='echo > somefile && chmod 0000 somefile') + test("./somedir", setup='mkdir -m 0777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 1000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 2000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 3000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 4000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 5000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 6000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 7000 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 1777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 2777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 3777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 4777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 5777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 6777 somedir', hook=hooks.is_directory) + test("./somedir", setup='mkdir -m 0000 somedir', hook=hooks.is_directory) + test("./Somefile", setup='echo > somefile && chmod 000 somefile') + test("./someFILE", setup='echo > somefile && chmod 000 somefile') + + +# @suite(bonus=True) +# def suite_cmd_variable(test): +# test("A=a sh -c 'echo $A'") +# test("A=a B=b sh -c 'echo $A$B'") +# test("A=a B=b C=c D=d E=e F=f G=g H=h sh -c 'echo $A$B$C$D$E$F$G$H'") +# test("A=a A=bonjour sh -c 'echo $A'") +# test("A=aA=bonjour sh -c 'echo $A'") +# test("BONJOURJESUIS=a sh -c 'echo $BONJOURJESUIS'") +# test("bonjourjesuis=a sh -c 'echo $bonjourjesuis'") +# test("bonjour_je_suis=a sh -c 'echo $bonjour_je_suis'") +# test("BONJOURJESUIS1=a sh -c 'echo $BONJOURJESUIS1'") +# test("bO_nJq123o__1ju_je3234sui__a=a sh -c 'echo $bO_nJq123o__1ju_je3234sui__a'") +# test("a0123456789=a sh -c 'echo $a0123456789'") +# test("abcdefghijklmnopqrstuvwxyz=a sh -c 'echo $abcdefghijklmnopqrstuvwxyz'") +# test("ABCDEFGHIJKLMNOPQRSTUVWXYZ=a sh -c 'echo $ABCDEFGHIJKLMNOPQRSTUVWXYZ'") +# test("__________________________=a sh -c 'echo $__________________________'") +# test("_bonjour_=a sh -c 'echo $_bonjour_'") +# test("_=a sh -c 'echo $_a'") +# test("1=a") +# test("BONJOURJESUIS =a sh -c 'echo $BONJOURJESUIS '") +# test("BONJOURJESUIS= a sh -c 'echo $BONJOURJESUIS'") +# test(r"BONJOUR\\JESUIS=a sh -c 'echo $BONJOUR\\JESUIS'") +# test(r'BONJOUR\'JESUIS=a sh -c "echo $BONJOUR\'JESUIS"') +# test(r'BONJOUR\"JESUIS=a sh -c "echo $BONJOUR\"JESUIS"') +# test(r"BONJOUR\$JESUIS=a sh -c 'echo $BONJOUR\$JESUIS'") +# test(r"BONJOUR\&JESUIS=a sh -c 'echo $BONJOUR\&JESUIS'") +# test(r"BONJOUR\|JESUIS=a sh -c 'echo $BONJOUR\|JESUIS'") +# test(r"BONJOUR\;JESUIS=a sh -c 'echo $BONJOUR\;JESUIS'") +# test(r"BONJOUR\_JESUIS=a sh -c 'echo $BONJOUR\_JESUIS'") +# test(r"BONJOUR\0JESUIS=a sh -c 'echo $BONJOUR\0JESUIS'") +# test(r"\B\O\N\ \ \ \ \ \ \ JOURJESUIS=a sh -c 'echo $\B\O\N\ \ \ \ \ \ \ JOURJESUIS'") +# test(r"A=\B\O\N\ \ \ \ \ \ \ JOURJESUIS sh -c 'echo $A'") +# test(r"A='bonjour je suis charles' sh -c 'echo $A'") +# test(r'A="bonjour je suis charles" sh -c "echo $A"') +# test(r"A==a sh -c 'echo $A'") +# test(r"A===a sh -c 'echo $A'") +# test(r"A====a sh -c 'echo $A'") +# test(r"A=====a sh -c 'echo $A'") +# test(r"A======a sh -c 'echo $A'") +# test(r"A=a=a=a=a=a sh -c 'echo $A'") +# +# test("A=a; echo $A") +# test("A=a B=b; echo $A$B") +# test("A=a B=b C=c D=d E=e F=f G=g H=h; echo $A$B$C$D$E$F$G$H") +# test("A=a A=bonjour; echo $A") +# test("A=aA=bonjour; echo $A") +# test("BONJOURJESUIS=a; echo $BONJOURJESUIS") +# test("bonjourjesuis=a; echo $bonjourjesuis") +# test("bonjour_je_suis=a; echo $bonjour_je_suis") +# test("BONJOURJESUIS1=a; echo $BONJOURJESUIS1") +# test("bO_nJq123o__1ju_je3234sui__a=a; echo $bO_nJq123o__1ju_je3234sui__a") +# test("a0123456789=a; echo $a0123456789") +# test("abcdefghijklmnopqrstuvwxyz=a; echo $abcdefghijklmnopqrstuvwxyz") +# test("ABCDEFGHIJKLMNOPQRSTUVWXYZ=a; echo $ABCDEFGHIJKLMNOPQRSTUVWXYZ") +# test("__________________________=a; echo $__________________________") +# test("_bonjour_=a; echo $_bonjour_") +# test("_=a; echo $_a") +# test("BONJOURJESUIS =a; echo $BONJOURJESUIS ") +# test("BONJOURJESUIS= a; echo $BONJOURJESUIS") +# test(r"BONJOUR\\JESUIS=a; echo $BONJOUR\\JESUIS") +# test(r"BONJOUR\'JESUIS=a; echo $BONJOUR\'JESUIS") +# test(r'BONJOUR\"JESUIS=a; echo $BONJOUR\"JESUIS') +# test(r"BONJOUR\$JESUIS=a; echo $BONJOUR\$JESUIS") +# test(r"BONJOUR\&JESUIS=a; echo $BONJOUR\&JESUIS") +# test(r"BONJOUR\|JESUIS=a; echo $BONJOUR\|JESUIS") +# test(r"BONJOUR\;JESUIS=a; echo $BONJOUR\;JESUIS") +# test(r"BONJOUR\_JESUIS=a; echo $BONJOUR\_JESUIS") +# test(r"BONJOUR\0JESUIS=a; echo $BONJOUR\0JESUIS") +# test(r"\B\O\N\ \ \ \ \ \ \ JOURJESUIS=a; echo $\B\O\N\ \ \ \ \ \ \ JOURJESUIS") +# test(r"A=\B\O\N\ \ \ \ \ \ \ JOURJESUIS; echo $A") +# test(r"A='bonjour je suis charles'; echo $A") +# test(r'A="bonjour je suis charles"; echo $A') +# test(r"A==a; echo $A") +# test(r"A===a; echo $A") +# test(r"A====a; echo $A") +# test(r"A=====a; echo $A") +# test(r"A======a; echo $A") +# test(r"A=a=a=a=a=a; echo $A") +# +# test("PATH=a ls") +# test("PATH=a echo aa") +# test("A=a echo $A") +# test("A=a B=b echo $A$B") +# test("A=a B=b C=c D=d E=e F=f G=g H=h echo $A$B$C$D$E$F$G$H") +# test("A=$PATH sh -c 'echo $A'") +# test("A=\"$PATH je suis\" sh -c 'echo $A'") +# test("A='$PATH je suis' sh -c 'echo $A'") +# test("$TEST sh -c 'echo $A'", setup="export TEST='A=a'") +# test("'BONJOURJESUIS''=''a' sh -c 'echo $BONJOURJESUIS'") +# test('"BONJOURJESUIS""=""a" sh -c "echo $BONJOURJESUIS"') +# test("./somedir", setup='mkdir somedir && chmod 0000 somedir') diff --git a/minishell_test/suites/flow.py b/minishell_test/suites/flow.py new file mode 100644 index 0000000..2c00b2f --- /dev/null +++ b/minishell_test/suites/flow.py @@ -0,0 +1,290 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# flow.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:24:52 by charles #+# #+# # +# Updated: 2020/11/10 13:16:28 by cacharle ### ########.fr # +# # +# ############################################################################ # + +import config +from suite import suite +from hooks import error_line0, platform_status, discard, replace_double_semi_colon, error_eof_to_expected_token + + +@suite() +def suite_end(test): + """ `;` tests """ + test("echo bonjour; echo je") + test("echo bonjour ;echo je") + test("echo bonjour ; echo je") + test("echo bonjour;") + test("echo; ") + test("echo ; ") + test("echo ;") + test("; echo", hook=error_line0, hook_status=platform_status(2, 1)) + test(" ;echo", hook=error_line0, hook_status=platform_status(2, 1)) + test(" ; echo", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo a; echo b; echo c; echo d; echo e; echo f; echo g; echo h; echo i;" + "echo j; echo k; echo l; echo m; echo c; echo c; echo c; echo c; echo c;" + "echo c; echo c; echo c; echo v; echo w; echo x; echo y; echo z") + test("echo a ; echo b; echo c ;echo d ; echo e ;echo f; echo g ;echo h; echo i;" + "echo j ; echo k; echo l; echo m; echo c ; echo c; echo c ; echo c; echo c;" + "echo c; echo c ; echo c; echo v ; echo w; echo x; echo y ; echo z") + test("ls doesnotexists ; echo bonjour") + test("ls doesnotexists; echo bonjour") + test("echo bonjour; ls doesnotexists") + test("echo a ; ;", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo a ; ;", hook=error_line0, hook_status=platform_status(2, 1)) + test(";", hook=error_line0, hook_status=platform_status(2, 1)) + test("; ;", hook=error_line0, hook_status=platform_status(2, 1)) + test("; ; ;", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo a ; ; echo b", hook=error_line0, hook_status=platform_status(2, 1)) + test(";;", hook=[error_line0, replace_double_semi_colon], hook_status=platform_status(2, 1)) + test(";;;", hook=[error_line0, replace_double_semi_colon], hook_status=platform_status(2, 1)) + test(";;;;;", hook=[error_line0, replace_double_semi_colon], hook_status=platform_status(2, 1)) + test("echo a ;; echo b", hook=[error_line0, replace_double_semi_colon], hook_status=platform_status(2, 1)) + test("echo a ;;;;; echo b", hook=[error_line0, replace_double_semi_colon], hook_status=platform_status(2, 1)) + test("ls " + 40 * " ; ls", setup="touch a b c") + test("ls " + 80 * " ; ls", setup="touch a b c") + test("ls " + 40 * " ; ls" + ";", setup="touch a b c") + test("ls " + 80 * " ; ls" + ";", setup="touch a b c") + + +@suite() +def suite_pipe(test): + """ `|` tests """ + test("cat /etc/shells | head -c 10") + test("cat -e /etc/shells | head -c 10") + test("cat -e /etc/shells | cat -e | head -c 10") + test("cat -e /etc/shells | cat -e | cat -e | head -c 10") + test("cat -e /dev/random | head -c 10", hook=discard) + test("cat -e /dev/random | cat -e | head -c 10", hook=discard) + test("cat -e /dev/random | cat -e | cat -e | head -c 10", hook=discard) + test("echo bonjour | cat") + test("echo bonjour | cat -e") + test("echo bonjour | cat -e | cat -e | cat -e | cat -e | cat -e | cat -e | cat -e") + test("ls | cat -e", setup="touch a b c d; mkdir m1 m2 m3") + test("ls -l | cat -e", setup="touch a b c d; mkdir m1 m2 m3") + test("ls -l | cat -e | cat | cat | cat", setup="touch a b c d; mkdir m1 m2 m3") + test("ls -l | cat -e | cat -e | cat -e | cat -e", setup="touch a b c d; mkdir m1 m2 m3") + test("ls -l | cat -e < a", setup="touch a b c d; mkdir m1 m2 m3; echo bonjour > a") + test("echo|", hook=discard, hook_status=platform_status(2, 1)) + test("echo |", hook=discard, hook_status=platform_status(2, 1)) + test("echo | ", hook=discard, hook_status=platform_status(2, 1)) + test("|cat", hook=error_line0, hook_status=platform_status(2, 1)) + test("| cat", hook=error_line0, hook_status=platform_status(2, 1)) + test(" | cat", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo a | export A=a; echo $A") + test("export A=a | cat; echo $A") + test("echo bonjour | | cat -e", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo bonjour | asdf") + test("asdf | echo bonjour") + test("echo a ||| echo b", hook=error_line0, hook_status=platform_status(2, 1)) + test("ls " + 40 * " | ls", setup="touch a b c") + test("ls " + 80 * " | ls", setup="touch a b c") + test("echo bonjour " + 40 * " | cat -e") + test("echo bonjour " + 80 * " | cat -e") + test("ls asdfasdf | echo a") + test("echo a | ls asdfasdf") + test("ls asdfasdf | echo a; echo b") + test("echo a | ls asdfasdf; echo b") + test("echo a > foo | cat -e", files=["foo"]) + test("echo a >> foo | cat -e", files=["foo"]) + test("echo a | cat -e < foo", setup="echo b > foo") + test("echo a > bar | cat -e < foo", setup="echo b > foo", files=["bar"]) + + +@suite(bonus=True) +def suite_and(test): + """ `&&` tests """ + test("echo bonjour&& echo je") + test("echo bonjour &&echo je") + test("echo bonjour && echo je") + test("echo bonjour&&", hook=discard, hook_status=platform_status(2, 1)) + test("echo&& ", hook=discard, hook_status=platform_status(2, 1)) + test("echo && ", hook=discard, hook_status=platform_status(2, 1)) + test("echo &&", hook=discard, hook_status=platform_status(2, 1)) + test("&&echo", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& echo", hook=error_line0, hook_status=platform_status(2, 1)) + test(" && echo", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo a&& echo b&& echo c&& echo d&& echo e&& echo f&& echo g&& echo h&& echo i&&" + "echo j&& echo k&& echo l&& echo m&& echo c&& echo c&& echo c&& echo c&& echo c&&" + "echo c&& echo c&& echo c&& echo v&& echo w&& echo x&& echo y&& echo z") + test("echo a && echo b&& echo c &&echo d && echo e &&echo f&& echo g &&echo h&& echo i&&" + "echo j && echo k&& echo l&& echo m&& echo c && echo c&& echo c && echo c&& echo c&&" + "echo c&& echo c && echo c&& echo v && echo w&& echo x&& echo y && echo z") + test("ls doesnotexists && echo bonjour") + test("ls doesnotexists&& echo bonjour") + test("echo bonjour&& ls doesnotexists") + test("ls " + 40 * " && ls", setup="touch a b c") + test("ls " + 80 * " && ls", setup="touch a b c") + + +@suite(bonus=True) +def suite_or(test): + """ `||` tests """ + test("echo bonjour|| echo je") + test("echo bonjour ||echo je") + test("echo bonjour || echo je") + test("echo bonjour||", hook=discard, hook_status=platform_status(2, 1)) + test("echo|| ", hook=discard, hook_status=platform_status(2, 1)) + test("echo || ", hook=discard, hook_status=platform_status(2, 1)) + test("echo ||", hook=discard, hook_status=platform_status(2, 1)) + test("||echo", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| echo", hook=error_line0, hook_status=platform_status(2, 1)) + test(" || echo", hook=error_line0, hook_status=platform_status(2, 1)) + test("echo a|| echo b|| echo c|| echo d|| echo e|| echo f|| echo g|| echo h|| echo i||" + "echo j|| echo k|| echo l|| echo m|| echo c|| echo c|| echo c|| echo c|| echo c||" + "echo c|| echo c|| echo c|| echo v|| echo w|| echo x|| echo y|| echo z") + test("echo a || echo b|| echo c ||echo d || echo e ||echo f|| echo g ||echo h|| echo i||" + "echo j || echo k|| echo l|| echo m|| echo c || echo c|| echo c || echo c|| echo c||" + "echo c|| echo c || echo c|| echo v || echo w|| echo x|| echo y || echo z") + test("ls doesnotexists || echo bonjour") + test("ls doesnotexists|| echo bonjour") + test("echo bonjour|| ls doesnotexists") + test("ls asdf" + 40 * " || ls asdf", setup="touch a b c") + test("ls asdf" + 80 * " || ls asdf", setup="touch a b c") + + +@suite(bonus=True) +def suite_parenthesis(test): + """ `(`, `)` tests """ + test("(echo bonjour)") + test("(echo bonjour )") + test("( echo bonjour )") + test("(echo a && echo b) && echo c") + test("(echo a || echo b) || echo c") + test("(ls doesnotexist || echo b) || echo c") + test("(echo a || ls doesnotexist) || echo c") + test("echo aa && (echo b && echo c)") + test("ls doesnotexist || (echo b && echo c)") + test("(echo bonjour > f1)", files=["f1"]) + test("(echo bonjour > f1 > f2 > f3)", files=["f1", "f2", "f3"]) + test("(echo bonjour > f1 > f2 > f3 > f4 > f5 > f6 > f7 > f8 > f9)", + files=["f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9"]) + test("(echo bonjour) > f1", files=["f1"]) + test("(echo bonjour) > f1 > f2 > f3", files=["f1", "f2", "f3"]) + test("(echo bonjour) > f1 > f2 > f3 > f4 > f5 > f6 > f7 > f8 > f9", + files=["f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9"]) + test("(cat -e < f1)", setup="echo bonjour > f1") + test("(cat -e < f1 < f2 < f3)", setup="touch f1 f2 f3 f4; echo bonjour > f3") + test("(cat -e < f1 < f2 < f3 < f4 < f5 < f6 < f7 < f8 < f9)", + setup="touch f1 f2 f3 f4 f5 f6 f7 f8 f9; echo bonjour > f9") + test("(cat -e) < f1", setup="echo bonjour > f1") + test("(cat -e) < f1 < f2 < f3", setup="touch f1 f2 f3 f4; echo bonjour > f3") + test("(cat -e) < f1 < f2 < f3 < f4 < f5 < f6 < f7 < f8 < f9", + setup="touch f1 f2 f3 f4 f5 f6 f7 f8 f9; echo bonjour > f9") + test("(echo bonjour > f1 > f2 > f3 > f4) > f5 > f6 > f7 > f8 > f9", + files=["f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9"]) + test("(cat -e < f1 < f2 < f3 < f4) < f5 < f6 < f7 < f8 < f9", + setup="touch f1 f2 f3 f4 f5 f6 f7 f8 f9; echo bonjour > f4") + test("(echo bonjour > f1) > f2", files=["f1", "f2"]) + test("(cat -e > f1) < f2", setup="ls -l / > f2", files=["f1"]) + test("(exit); echo bonjour") + test("(echo bonjour; exit; echo aurevoir)") + test("(ls && ls)") + test("(ls doesntexist || ls)") + test("(ls doesntexist && ls)") + test("(ls && ls) && echo $?") + test("(echo a; echo b) | cat -e") + test("echo bonjour | (cat -e; echo a)") + test("echo bonjour | (echo a; cat -e)") + test("(echo a) | (cat -e)") + test("(echo a; echo b) | (cat -e)") + test("(echo a) | (cat -e | cat -e)") + test("(echo a; echo b) | (cat -e | cat -e)") + test("(echo a; echo b) | cat -e | cat -e") + test("(echo a); (echo b) | (cat -e) | (cat -e)") + test("echo a | (cat -e | cat -e | cat -e)") + test("echo a | (cat -e | cat -e | cat -e) | cat -e") + test("(echo a) | (cat -e | cat -e | cat -e) | cat -e") + test("(echo a) | (cat -e | cat -e | cat -e) | (cat -e)") + test("(echo bonjour ; echo aurevoir) | (cat -e | cat -e) | cat -e") + test("( echo salut && echo bonjours ) ; echo comment ca va") + test("(cd /; echo $PWD; pwd); echo $PWD; pwd") + test("(export A=a; echo $A); echo $A") + test("(cat /etc/shells) | (cat -e) | (cat -e) | (cat -e)") + test("(cat /etc/shells) | (cat -e) | (cat -e) | (cat -e) | (cat -e) | (cat -e) | (cat -e) | (cat -e) | (cat -e)") + test("(cat /etc/shells | (cat -e) | (cat -e) | (cat -e)", + hook=[error_line0, error_eof_to_expected_token], hook_status=platform_status(2, 1)) + test("(cat /etc/shells) | (cat -e) | (cat -e | (cat -e)", + hook=[error_line0, error_eof_to_expected_token], hook_status=platform_status(2, 1)) + + +@suite() +def suite_syntax_error(test): + """ separator syntax error test """ + test("< | a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> | a", hook=error_line0, hook_status=platform_status(2, 1)) + test(">> | a", hook=error_line0, hook_status=platform_status(2, 1)) + test("< ; a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> ; a", hook=error_line0, hook_status=platform_status(2, 1)) + test(">> ; a", hook=error_line0, hook_status=platform_status(2, 1)) + test("; | a", hook=error_line0, hook_status=platform_status(2, 1)) + test("; < a", hook=error_line0, hook_status=platform_status(2, 1)) + test("; > a", hook=error_line0, hook_status=platform_status(2, 1)) + test("; >> a", hook=error_line0, hook_status=platform_status(2, 1)) + test("| ; a", hook=error_line0, hook_status=platform_status(2, 1)) + test("| < a", hook=error_line0, hook_status=platform_status(2, 1)) + test("| > a", hook=error_line0, hook_status=platform_status(2, 1)) + test("| >> a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> a ; a", hook=error_line0) + test("< a ; a", hook=error_line0) + test(">> a ; a", hook=error_line0) + test(config.LOREM + " > >" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " < <" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " ; |" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " | ;" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + + +@suite(bonus=True) +def suite_syntax_error_bonus(test): + """ separator syntax error bonus test """ + test("< && a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> && a", hook=error_line0, hook_status=platform_status(2, 1)) + test(">> && a", hook=error_line0, hook_status=platform_status(2, 1)) + test("< || a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> || a", hook=error_line0, hook_status=platform_status(2, 1)) + test(">> || a", hook=error_line0, hook_status=platform_status(2, 1)) + test("< ( a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> ( a", hook=error_line0, hook_status=platform_status(2, 1)) + test(">> ( a", hook=error_line0, hook_status=platform_status(2, 1)) + test("< ) a", hook=error_line0, hook_status=platform_status(2, 1)) + test("> ) a", hook=error_line0, hook_status=platform_status(2, 1)) + test(">> ) a", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& < a", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& > a", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& >> a", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& || a", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& ( a", hook=error_line0, hook_status=platform_status(2, 1)) + test("&& ) a", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| < a", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| > a", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| >> a", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| && a", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| ( a", hook=error_line0, hook_status=platform_status(2, 1)) + test("|| ) a", hook=error_line0, hook_status=platform_status(2, 1)) + test("( < a", hook=[error_line0, error_eof_to_expected_token], hook_status=platform_status(2, 1)) + test("( > a", hook=[error_line0, error_eof_to_expected_token], hook_status=platform_status(2, 1)) + test("( >> a", hook=[error_line0, error_eof_to_expected_token], hook_status=platform_status(2, 1)) + test(") < a", hook=error_line0, hook_status=platform_status(2, 1)) + test(") > a", hook=error_line0, hook_status=platform_status(2, 1)) + test(") >> a", hook=error_line0, hook_status=platform_status(2, 1)) + test("( && a", hook=error_line0, hook_status=platform_status(2, 1)) + test("( || a", hook=error_line0, hook_status=platform_status(2, 1)) + test("( ) a", hook=error_line0, hook_status=platform_status(2, 1)) + test(") && a", hook=error_line0, hook_status=platform_status(2, 1)) + test(") || a", hook=error_line0, hook_status=platform_status(2, 1)) + test(") ( a", hook=error_line0, hook_status=platform_status(2, 1)) + test("() a", hook=error_line0, hook_status=platform_status(2, 1)) + test("( a", hook=[error_line0, error_eof_to_expected_token], hook_status=platform_status(2, 1)) + test(") a", hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " && &&" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " || ||" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " ( (" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test(config.LOREM + " ) )" + config.LOREM, hook=error_line0, hook_status=platform_status(2, 1)) + test("(); () ;() ;() ;() ;() ;() ;() ;() ;() ;a", hook=error_line0, hook_status=platform_status(2, 1)) diff --git a/minishell_test/suites/misc.py b/minishell_test/suites/misc.py new file mode 100644 index 0000000..a6b9bf2 --- /dev/null +++ b/minishell_test/suites/misc.py @@ -0,0 +1,100 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# misc.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/10/10 13:06:19 by cacharle #+# #+# # +# Updated: 2020/11/28 06:10:12 by charles ### ########.fr # +# # +# ############################################################################ # + +from suite import suite + + +@suite() +def suite_shlvl(test): + """ test for the SHLVL variable """ + test("echo $SHLVL") + test("echo $SHLVL", exports={"SHLVL": ""}) + test("echo $SHLVL", exports={"SHLVL": " 1"}) + test("echo $SHLVL", exports={"SHLVL": " 2"}) + test("echo $SHLVL", exports={"SHLVL": " 3"}) + test("echo $SHLVL", exports={"SHLVL": " ' 3'"}) + test("echo $SHLVL", exports={"SHLVL": " '\t3'"}) + test("echo $SHLVL", exports={"SHLVL": " '\t\f\r 3'"}) + test("echo $SHLVL", exports={"SHLVL": " '3 '"}) + test("echo $SHLVL", exports={"SHLVL": " '3\t'"}) + test("echo $SHLVL", exports={"SHLVL": " '3\r'"}) + test("echo $SHLVL", exports={"SHLVL": " '3\t\f\r '"}) + test("echo $SHLVL", exports={"SHLVL": " '3 a'"}) + test("echo $SHLVL", exports={"SHLVL": " '3\t\t\ta'"}) + test("echo $SHLVL", exports={"SHLVL": " 0"}) + test("echo $SHLVL", exports={"SHLVL": " -0"}) + test("echo $SHLVL", exports={"SHLVL": " -1"}) + test("echo $SHLVL", exports={"SHLVL": " 255"}) + test("echo $SHLVL", exports={"SHLVL": " 256"}) + test("echo $SHLVL", exports={"SHLVL": " 2000000"}) + test("echo $SHLVL", exports={"SHLVL": " -2000000"}) + test("echo $SHLVL", exports={"SHLVL": " 2147483647"}) + test("echo $SHLVL", exports={"SHLVL": " -2147483648"}) + test("echo $SHLVL", exports={"SHLVL": " 2147483648"}) + test("echo $SHLVL", exports={"SHLVL": " -2147483649"}) + test("echo $SHLVL", exports={"SHLVL": " 3147483648"}) + test("echo $SHLVL", exports={"SHLVL": " -3147483649"}) + test("echo $SHLVL", exports={"SHLVL": " 4294967295"}) + test("echo $SHLVL", exports={"SHLVL": " 4294967296"}) + test("echo $SHLVL", exports={"SHLVL": " -9223372036854775808"}) + test("echo $SHLVL", exports={"SHLVL": " 9223372036854775807"}) + test("echo $SHLVL", exports={"SHLVL": " -9223372036854775809"}) + test("echo $SHLVL", exports={"SHLVL": " 9223372036854775808"}) + test("echo $SHLVL", exports={"SHLVL": " 18446744073709551615"}) + test("echo $SHLVL", exports={"SHLVL": " 18446744073709551616"}) + test("echo $SHLVL", exports={"SHLVL": " +1"}) + test("echo $SHLVL", exports={"SHLVL": " +2"}) + test("echo $SHLVL", exports={"SHLVL": " +3"}) + test("echo $SHLVL", exports={"SHLVL": " +0"}) + test("echo $SHLVL", exports={"SHLVL": " +255"}) + test("echo $SHLVL", exports={"SHLVL": " +256"}) + test("echo $SHLVL", exports={"SHLVL": " +2000000"}) + test("echo $SHLVL", exports={"SHLVL": " +2147483647"}) + test("echo $SHLVL", exports={"SHLVL": " ++1"}) + test("echo $SHLVL", exports={"SHLVL": " ++2"}) + test("echo $SHLVL", exports={"SHLVL": " ++3"}) + test("echo $SHLVL", exports={"SHLVL": " ++0"}) + test("echo $SHLVL", exports={"SHLVL": " ++255"}) + test("echo $SHLVL", exports={"SHLVL": " ++256"}) + test("echo $SHLVL", exports={"SHLVL": " ++2000000"}) + test("echo $SHLVL", exports={"SHLVL": " ++2147483647"}) + test("echo $SHLVL", exports={"SHLVL": " --1"}) + test("echo $SHLVL", exports={"SHLVL": " --2"}) + test("echo $SHLVL", exports={"SHLVL": " --3"}) + test("echo $SHLVL", exports={"SHLVL": " --0"}) + test("echo $SHLVL", exports={"SHLVL": " --255"}) + test("echo $SHLVL", exports={"SHLVL": " --256"}) + test("echo $SHLVL", exports={"SHLVL": " --2000000"}) + test("echo $SHLVL", exports={"SHLVL": " --2147483647"}) + test("echo $SHLVL", exports={"SHLVL": " bonjour"}) + test("echo $SHLVL", exports={"SHLVL": " 0_"}) + test("echo $SHLVL", exports={"SHLVL": " _0"}) + test("echo $SHLVL", exports={"SHLVL": " 0123456789"}) + test("echo $SHLVL", exports={"SHLVL": " -0123456789"}) + test("echo $SHLVL", exports={"SHLVL": " 00000000000000000000000000000000000000000000001"}) + test("echo $SHLVL", exports={"SHLVL": " 00000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000001"}) + + +@suite() +def suite_lastcmd(test): + """ test for $_, the last executed command """ + test("echo $_") + test("echo; echo $_") + test("env; echo $_") + test("export A=a; echo $_") + test("unset A; echo $_") + test("echo a b c d; echo $_") + test("cat -e /etc/shells; echo $_") + test("echo a; echo \"$_\"") + test("echo a; echo '$_'") + test("echo a; echo \"@$_@\"") diff --git a/minishell_test/suites/path.py b/minishell_test/suites/path.py new file mode 100644 index 0000000..93d4232 --- /dev/null +++ b/minishell_test/suites/path.py @@ -0,0 +1,137 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# path.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/09 15:12:58 by charles #+# #+# # +# Updated: 2021/02/04 16:14:20 by charles ### ########.fr # +# # +# ############################################################################ # + +import distutils.spawn + +from suite import suite + + +@suite() +def suite_path(test): + """ searching a command in the path tests """ + whoami_path = distutils.spawn.find_executable("which") + if whoami_path is None: + print("Couldn't find `whoami` in your PATH: Skipping suite") + return + mode_fmt = ("mkdir path && cp " + + whoami_path + + " ./path/a && chmod {} ./path/a") + test("a", setup=mode_fmt.format("000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("001"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("002"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("003"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("004"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("005"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("006"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("007"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("010"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("020"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("030"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("040"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("050"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("060"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("070"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("100"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("200"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("300"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("400"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("500"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("600"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("700"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("755"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("644"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("311"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("111"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("222"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("333"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("0777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("1000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("2000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("3000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("4000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("5000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("6000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("7000"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("1777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("2777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("3777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("4777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("5777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("6777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("7777"), exports={"PATH": "path"}) + test("a", setup=mode_fmt.format("0000"), exports={"PATH": "path"}) + test("A", setup=mode_fmt.format("000"), exports={"PATH": "path"}) + # test("b", setup="mkdir path && cp " + whoami_path + " ./path/a && ln -s ./path/a ./path/b", + # exports={"PATH": "path"}) + test("b", setup="mkdir path && ln -s " + whoami_path + " ./path/b", exports={"PATH": "path"}) + test("a", setup="mkdir path && mkfifo path/a") + test("a", setup="mkdir path && mkfifo path/a && chmod 777 path/a") + # test("a", setup="mkdir path1 path2 && cp " + whoami_path + " path1/a" + # "&& cp " + whoami_path + " path2/a && chmod 000 path1/a", exports={"PATH": "path1:path2"}) + test("a", setup="mkdir path1 path2 && cp " + whoami_path + " path1/a" + "&& cp " + whoami_path + " path2/a && chmod 000 path1/a", exports={"PATH": "path2:path1"}) + test("a/b", setup="mkdir -p path/a; cp " + whoami_path + " path/a/b", exports={"PATH": "path"}) + + +@suite() +def suite_path_variable(test): + """ $PATH environment variable tests """ + test("echo $PATH", exports={"PATH": "doesnotexits"}) + test("echo $PATH", exports={"PATH": "doesnotexits:asdfasdfas"}) + test("echo $PATH", exports={"PATH": "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z"}) + test("echo $PATH", exports={"PATH": "________"}) + test("echo $PATH", exports={"PATH": " "}) + test("echo $PATH", exports={"PATH": " : "}) + test("echo $PATH", exports={"PATH": " /bin "}) + test("echo $PATH", exports={"PATH": " /sbin "}) + test("echo $PATH", exports={"PATH": "/bin:/bin:/bin:/bin"}) + test("echo $PATH", exports={"PATH": "/sbin:/sbin:/sbin:/sbin"}) + test("echo $PATH", exports={"PATH": ""}) + test("echo $PATH", exports={"PATH": ":"}) + test("echo $PATH", exports={"PATH": ":::::::::::::::::::"}) + test("echo $PATH", exports={"PATH": "/asdfasdf"}) + test("echo $PATH", exports={"PATH": "/usr/asdf:/usr/lib/asdfasdf"}) + test("whoami", exports={"PATH": "doesnotexits"}) + test("whoami", exports={"PATH": "doesnotexits:asdfasdfas"}) + test("whoami", exports={"PATH": "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z"}) + test("whoami", exports={"PATH": "________"}) + test("whoami", exports={"PATH": " "}) + test("whoami", exports={"PATH": " : "}) + test("whoami", exports={"PATH": " /usr/bin "}) + test("whoami", exports={"PATH": "/usr/bin:/usr/bin:/usr/bin:/usr/bin"}) + test("whoami", exports={"PATH": " /sbin "}) + test("whoami", exports={"PATH": "/sbin:/sbin:/sbin:/sbin"}) + test("whoami", exports={"PATH": ""}) # error message explicit enough + test("unset PATH; whoami") # error message explicit enough + test("whoami", exports={"PATH": ":"}) + test("whoami", exports={"PATH": ":::::::::::::::::::"}) + test("whoami", exports={"PATH": "/asdfasdf"}) + test("whoami", exports={"PATH": "/usr/asdf:/usr/lib/asdfasdf"}) + test("whoami", setup="unset PATH") + create_cmd_setup = "echo '#!/bin/sh\necho bonjour' > somecmd; chmod +x somecmd" + test("somecmd", setup=create_cmd_setup, exports={"PATH": ""}) + test("somecmd", setup=create_cmd_setup, exports={"PATH": ":"}) + test("somecmd", setup=create_cmd_setup, exports={"PATH": "::::::::"}) + test("somecmd", setup=create_cmd_setup, exports={"PATH": "/asdfasdf"}) + test("somecmd", setup=create_cmd_setup, exports={"PATH": "/usr/asdf:/usr/lib/asdfasdf"}) + test("somecmd", setup=create_cmd_setup + "; unset PATH") + test("somecmd", setup=create_cmd_setup, exports={"PATH": "/bin:"}) + test("somecmd", setup=create_cmd_setup, exports={"PATH": ":/bin"}) + test("somecmd", setup=create_cmd_setup, exports={"PATH": ":/bin:"}) + test("Whoami", exports={"PATH": "/usr/bin"}) + test("wHoAMi", exports={"PATH": "/usr/bin"}) + test("whoami", exports={"PATH": "/usr/bIn"}) + test("whoami", exports={"PATH": "/Usr/bin"}) + test("Whoami", exports={"PATH": "/usr/bIn"}) + test("wHoami", exports={"PATH": "/Usr/bin"}) + test("Whoami", exports={"PATH": ""}) + test("wHoami", exports={"PATH": ""}) diff --git a/minishell_test/suites/preprocess.py b/minishell_test/suites/preprocess.py new file mode 100644 index 0000000..0176399 --- /dev/null +++ b/minishell_test/suites/preprocess.py @@ -0,0 +1,463 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# preprocess.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: juligonz +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/07/15 18:25:00 by charles #+# #+# # +# Updated: 2021/01/21 13:06:55 by charles ### ########.fr # +# # +# **************************************************************************** # + +import config +import hooks +from suite import suite + + +@suite() +def suite_quote(test): + """ double and single quote test, escape in quotes, quote missmatch """ + test("'echo' 'bonjour'") + test("'echo' 'je' 'suis' 'charles'") + test('"echo" "bonjour"') + test('"echo" "je" "suis" "charles"') + test('echo je\'suis\'"charles"') + test('echo "je"suis\'charles\'') + test('echo \'je\'"suis"charles') + test('echo "\\""') + test('echo "\\$"') + test('echo "\\\\"') + test('echo "\\a"') + test('echo "\\A"') + test('ls ""') + test("ls ''") + test('ls "" "" "" \'\' """"') + test("ls '' '' '''' ''") + test("' echo' bonjour") + test("'echo ' bonjour") + test('" echo" bonjour') + test('"echo " bonjour') + test("''echo bonjour") + test('""echo bonjour') + test("''''''''''''''''''''''''''''''''''''''''''''''''''''''''''echo bonjour") + test('""""""""""""""""""""""""""""""""""""""""""""""""""""""""""echo bonjour') + test("echo'' bonjour") + test('echo"" bonjour') + test("echo'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' bonjour") + test('echo"""""""""""""""""""""""""""""""""""""""""""""""""""""""""" bonjour') + test("ec''ho bonjour") + test('ec""ho bonjour') + test("ec''''''''''''''''''''''''''''''''''''''''''''''''''''''''''ho bonjour") + test('ec""""""""""""""""""""""""""""""""""""""""""""""""""""""""""ho bonjour') + test("'''''''e''''''''''c''''''''''''h''''''''o''''''''''''''''''''' bonjour") + test('"""""""e""""""""""c""""""""""""h""""""""o""""""""""""""""""""" bonjour') + test("echo '\"'") + test("echo '\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"'") + test('echo "\'"') + test('echo "\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'"') + test("echo '", hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test('echo "', hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test("echo '''", hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test('echo """', hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test("echo '''''''''''''''''''''''''''''''''''''''''''", hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test('echo """""""""""""""""""""""""""""""""""""""""""', hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test("echo 'AH\\'") + test('echo "AH\\"', hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test('echo "AH\\""') + test("echo '\\''", hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test('echo "\\""') + test('echo "\\\\""', hook=hooks.error_line0, hook_status=hooks.platform_status(2, 1)) + test('echo bonjour > "fi le"', files=['fi le']) + test("echo bonjour > 'fi le'", files=['fi le']) + + +@suite() +def suite_interpolation(test): + """environment variable interpolation tests, valid name, escape in variable""" + test("echo $TEST", exports={"TEST": "bonjour"}) + test("echo $TES", exports={"TEST": "bonjour"}) + test("echo $TEST_", exports={"TEST": "bonjour"}) + test('echo "|$TEST|"', exports={"TEST": "bonjour"}) + test('echo "|$TES|"', exports={"TEST": "bonjour"}) + test('echo "|$TEST_|"', exports={"TEST": "bonjour"}) + test("echo '|$TEST|'", exports={"TEST": "bonjour"}) + test("echo '|$TES|'", exports={"TEST": "bonjour"}) + test("echo '|$TEST_|'", exports={"TEST": "bonjour"}) + test("echo $A$B$C", exports={"A": "foo", "B": "bar", "C": "baz"}) + test('echo "$A$B$C"', exports={"A": "foo", "B": "bar", "C": "baz"}) + test("echo '$A$B$C'", exports={"A": "foo", "B": "bar", "C": "baz"}) + test("echo $A,$B,$C", exports={"A": "foo", "B": "bar", "C": "baz"}) + test('echo "$A,$B,$C"', exports={"A": "foo", "B": "bar", "C": "baz"}) + test("echo '$A,$B,$C'", exports={"A": "foo", "B": "bar", "C": "baz"}) + test('echo $A"$B"$C"A"$B"$C"', exports={"A": "foo", "B": "bar", "C": "baz"}) + test("echo $A'$B'$C'A'$B'$C'", exports={"A": "foo", "B": "bar", "C": "baz"}) + test('echo $A"$B"$C"A"$B"$C"', exports={"A": "foo ", "B": " bar ", "C": "baz "}) + test("echo $A'$B'$C'A'$B'$C'", exports={"A": "foo ", "B": " bar ", "C": "baz "}) + test("echo $A") + test("echo $A$B") + test("echo $A$B$C") + test("echo $A$B$C$D") + test("echo [$A]", exports={"A": r"bonjour\je"}) + test("echo [$A]", exports={"A": r"\b\\o\\\nj\\\\\\\our\\je\\\\"}) + test("echo [$A]", exports={"A": r" \b\\o\\\nj\\\\\\\our\\je\\\\"}) + test("echo [$A]", exports={"A": r"\b\\o\\\nj\\\\\\\our\\je\\\\ "}) + test("echo [$A]", exports={"A": r" \b\\o\\\nj\\\\\\\our\\je\\\\ "}) + test("echo [$A]", exports={"A": r"a \b\\o\\\nj\\\\\\\our\\je\\\\ b"}) + test("echo [$A]", exports={"A": r" a \b\\o\\\nj\\\\\\\our\\je\\\\ b "}) + test("echo [$A]", exports={"A": r" "}) + test("echo [$A]", exports={"A": r" "}) + test("echo [$A]", exports={"A": r"\ "}) + test("echo [$A]", exports={"A": r" \ "}) + test(r"echo \ \ \ \ \ \ \ $A\ \ \ \ \ \ ", exports={"A": "bonjour"}) + test(r"echo \ \ \ \ \ \ \ $A\ \ \ \ \ \ ", exports={"A": "bonjour je suis"}) + test(r"echo \ \ \ \ \ \ \ $A\ \ \ \ \ \ ", exports={"A": " bonjour je suis "}) + test('echo $A', exports={"A": "bonjour je suis splited"}) + test('echo $A', exports={"A": "bonjour je suis splited"}) + test('echo $A', exports={"A": " bonjour je suis splited "}) + test('echo [$A]', exports={"A": "bonjour je suis splited"}) + test('echo [$A]', exports={"A": "bonjour je suis splited"}) + test('echo [$A]', exports={"A": " bonjour je suis splited "}) + test('echo "[$A]"', exports={"A": "bonjour je suis splited"}) + test('echo "[$A]"', exports={"A": "bonjour je suis splited"}) + test('echo "[$A]"', exports={"A": " bonjour je suis splited "}) + test('echo \\ $A', exports={"A": "bonjour je suis splited"}) + test('echo \\ $A', exports={"A": "bonjour je suis splited"}) + test('echo \\ $A', exports={"A": " bonjour je suis splited "}) + test('echo $A\\ ', exports={"A": "bonjour je suis splited"}) + test('echo $A\\ ', exports={"A": "bonjour je suis splited"}) + test('echo $A\\ ', exports={"A": " bonjour je suis splited "}) + test('echo $A$A$A', exports={"A": " bonjour je suis splited "}) + test("echo $A", exports={"A": "'" + config.LOREM + "'"}) + test('echo "$A"', exports={"A": "'" + config.LOREM + "'"}) + test("echo '$A'", exports={"A": "'" + config.LOREM + "'"}) + test("$ECHO $ECHO", exports={"ECHO": "echo"}) + test("$A$B bonjour", exports={"A": "ec", "B": "ho"}) + test("$LS", exports={"LS": "ls -l"}, setup="touch a b c") + test("echo $") + test("echo \\$") + test("echo \\$\\$\\$\\$") + test("echo \\$$\\$$") + test("echo $\\A $\\B", exports={"A": "a", "B": "b"}) + test("echo $\\A$\\B", exports={"A": "a", "B": "b"}) + test("echo $A", exports={"A": " "}) + test("echo $A", exports={"A": " "}) + test("echo $A", exports={"A": " "}) + test("echo $A", exports={"A": " "}) + test("echo $A", exports={"A": " a "}) + test("echo $A", exports={"A": " "}) + test("echo $A", exports={"A": " a "}) + test("echo @$A@", exports={"A": " "}) + test("echo @ $A@", exports={"A": " "}) + test("echo @$A @", exports={"A": " "}) + test("echo @$A@", exports={"A": " "}) + test("echo '@'$A'@'", exports={"A": " "}) + test("echo '@' $A'@'", exports={"A": " "}) + test("echo '@'$A '@'", exports={"A": " "}) + test('echo "@"$A"@"', exports={"A": " "}) + test('echo "@" $A"@"', exports={"A": " "}) + test('echo "@"$A "@"', exports={"A": " "}) + test('echo @"$A"@', exports={"A": " "}) + test('echo @ "$A"@', exports={"A": " "}) + test('echo @"$A" @', exports={"A": " "}) + test('echo @"$A"@', exports={"A": " "}) + test("echo '@'\"$A\"'@'", exports={"A": " "}) + test("echo '@' \"$A\"'@'", exports={"A": " "}) + test("echo '@'\"$A\" '@'", exports={"A": " "}) + test('echo "@""$A""@"', exports={"A": " "}) + test('echo "@" "$A""@"', exports={"A": " "}) + test('echo "@""$A" "@"', exports={"A": " "}) + test('echo $A$B$C', exports={"A": "", "B": "", "C": ""}) + test('echo bonjour > $A', exports={"A": 'file'}, files=['file']) + test('echo bonjour > $A', exports={"A": 'fi le'}, files=['fi le'], hook=hooks.error_line0) + test('echo bonjour > "$A"', exports={"A": 'fi le'}, files=['fi le']) + test("echo bonjour > '$A'", exports={"A": 'fi le'}, files=['fi le']) + + +@suite() +def suite_escape(test): + """ escape test, in command, with space, before quote """ + test(r"echo \a") + test(r"\e\c\h\o bonjour") + test(r"echo charles\ ") + test(r"echo \ \ jesuis\ \ charles") + test(r"echo \ \ jesuis\; \ charles") + test(r"echo \ \ jesuis\&\& \ charles") + test(r"echo \ \ jesuis\|\| \ charles") + test(r"echo \ \ jesuis \|\| \ charles") + test(r"echo \ \ jesuis\; \ charles") + test(r"echo \ \ \ \ \ \ \ \ ") + test(r"echo \ \ \ \ \ \ \ \ \ \ \ \ \ \ ") + test(r"echo \$PATH") + test(r"echo \$\P\A\T\H") + test(r"echo\ bonjour") + test(r"\ echo bonjour") + test(r" \ echo bonjour") + test(r" \ echo bonjour") + test(r" \ echo bonjour") + test(r" \ echo bonjour") + test(r'/bin/echo " \ "') + test(r'/bin/echo " \" "') + test(r'/bin/echo " \' "') + test(r'/bin/echo " \a "') + test(r'/bin/echo " \b "') + test(r'/bin/echo " \| "') + test(r'/bin/echo " \! "') + test(r'/bin/echo " \@ "') + test(r'/bin/echo " \$ "') + test(r'/bin/echo " \$LESS "') + test(r'/bin/echo " \$? "') + test(r'/bin/echo " \\ "') + test(r'/bin/echo " \\\ "') + test(r'/bin/echo " \\\\ "') + test(r'/bin/echo " \\\\\ "') + test(r'/bin/echo " \\\\\\ "') + test(r'/bin/echo " \\\\\\\ "') + test(r'/bin/echo " \\\\\\\\ "') + test(r"/bin/echo ' \ '") + test(r"/bin/echo ' \" '") + test(r"/bin/echo ' \a '") + test(r"/bin/echo ' \b '") + test(r"/bin/echo ' \| '") + test(r"/bin/echo ' \! '") + test(r"/bin/echo ' \@ '") + test(r"/bin/echo ' \$ '") + test(r"/bin/echo ' $LESS '") + test(r"/bin/echo ' \$? '") + test(r"/bin/echo ' \\ '") + test(r"/bin/echo ' \\\ '") + test("echo \\", hook=hooks.delete_escape) + test("echo \"\\\"\"'bonjour'") + + +@suite() +def suite_spaces(test): + """ field splitting with spaces and tabs """ + test("echo foo") + test("echo foo") + test(" echo foo") + test("echo foo ") + test(" echo foo ") + test("echo\t\t\t\t\t\t\t\t\t\tfoo") + test("\t\t\t\t\t\techo\tfoo") + test("echo\tfoo\t\t\t\t\t\t") + test("\t\t\t\techo\t\t\t\tfoo\t\t\t\t") + test("\fecho\ffoo", hook=hooks.should_not_be("foo\n")) + test("\necho\nfoo", hook=hooks.should_not_be("foo\n")) + test("\recho\rfoo", hook=hooks.should_not_be("foo\n")) + test("\vecho\vfoo", hook=hooks.should_not_be("foo\n")) + test("\t\r\v\fecho\v\t\r\vfoo", hook=hooks.should_not_be("foo\n")) + test("") + test(" ") + test("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t") + test(" \t\t\t \t\t\t ") + + +# @suite(bonus=True) +# def suite_glob(test): +# test("echo *") +# test("echo *", setup="touch a b c") +# test("echo *.c", setup="touch a b c foo.c bar.c") +# test("echo src/*.c", setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c") +# test("echo */*.c", setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c") +# test("echo */*.c", +# setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.c inc/bar.c") +# test("echo */*.h", +# setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# test("echo l1/*/l3/*/*", +# setup="mkdir -p l1/l2_1/l3; mkdir -p l1/l2_2; cd l1/l2_1/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h;\ +# cd ../../..; cd l1/l2_2; touch bonjour je suis") +# +# test("echo */*/*/*/*.c", +# setup="mkdir -p l1/l2/l3; cd l1/l2/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# test("echo */*/*/*/*.h", +# setup="mkdir -p l1/l2/l3; cd l1/l2/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# +# test("echo */*/*/*.c", +# setup="mkdir -p l1/l2/l3; cd l1/l2/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# test("echo */*/*/*.h", +# setup="mkdir -p l1/l2/l3; cd l1/l2/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# +# test("echo */*/*/*/*/*.c", +# setup="mkdir -p l1/l2/l3; cd l1/l2/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# test("echo */*/*/*/*/*.h", +# setup="mkdir -p l1/l2/l3; cd l1/l2/l3;\ +# mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.h inc/bar.h") +# +# test("echo /*") +# test("echo /etc/*") +# test("echo /usr/include/*.h") +# # test("echo /*/*", timeout=10) +# # test("echo /usr/*/*", timeout=10) +# test("echo /usr/*") +# test("echo /dev/*") +# test("echo /etc/*") +# test("echo /root/*") +# test("echo /usr*") +# test("echo /dev*") +# test("echo /etc*") +# test("echo /root*") +# +# test("echo *nothing") +# test("echo nothing*") +# test("echo *nothing*") +# +# test("echo a*b", setup="touch ab aab aaaaab aaaaaaaab acccccb acb abbbb") +# test("echo a**b", setup="touch ab aab aaaaab aaaaaaaab acccccb acb abbbb") +# test("echo a***b", setup="touch ab aab aaaaab aaaaaaaab acccccb acb abbbb") +# test("echo a****b", setup="touch ab aab aaaaab aaaaaaaab acccccb acb abbbb") +# +# test("echo **") +# test("echo **", setup="touch a b c") +# test("echo **", setup="mkdir d; touch d/a d/b d/c") +# test("echo */*", setup="mkdir d; touch d/a d/b d/c") +# test("echo */a", setup="mkdir d; touch d/a d/b d/c") +# test("echo d/*", setup="mkdir d; touch d/a d/b d/c") +# +# test("*") +# test("*", setup="touch a b c") +# test("*.c", setup="touch a b c foo.c bar.c") +# test("src/*.c", setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c") +# test("*/*.c", setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c") +# test("*/*.c", +# setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.c inc/bar.c") +# +# test("export A=*; echo $A") +# test("A=*; echo $A") +# +# test("echo *", setup="mkdir d1; touch d1/a d1/b d1/c; ln -s d1 d1link") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; ln -s d1 d1link") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; ln -s d1 .d1link") +# test("echo */*", setup="mkdir .d1; touch .d1/a .d1/b .d1/c; ln -s .d1 d1link") +# test("echo .*/*", setup="mkdir d1; touch d1/a d1/b d1/c; ln -s d1 .d1link") +# test("echo .*/*", setup="mkdir .d1; touch .d1/a .d1/b .d1/c; ln -s .d1 d1link") +# +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 001 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 002 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 003 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 004 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 005 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 006 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 007 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 010 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 020 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 030 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 040 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 050 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 060 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 070 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 100 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 200 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 300 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 400 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 500 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 600 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 700 d1/a") +# +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 755 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 644 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 311 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 111 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 222 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 333 d1/a") +# +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 001 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 002 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 003 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 004 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 005 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 006 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 007 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 010 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 020 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 030 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 040 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 050 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 060 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 070 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 100 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 200 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 300 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 400 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 500 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 600 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 700 d1") +# +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 755 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 644 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 311 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 111 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 222 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 333 d1") +# +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 0777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 1000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 2000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 3000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 4000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 5000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 6000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 7000 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 1777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 2777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 3777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 4777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 5777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 6777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 7777 d1/a") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 0000 d1/a") +# +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 0777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 1000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 2000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 3000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 4000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 5000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 6000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 7000 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 1777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 2777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 3777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 4777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 5777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 6777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 7777 d1") +# test("echo */*", setup="mkdir d1; touch d1/a d1/b d1/c; chmod 0000 d1") +# +# test("echo *", setup="touch a; ln -s a b") +# test("echo *", setup="touch a; ln -s a b; ln -s b c") +# test("echo *", setup="touch a; ln -s a b; ln -s b c; ln -s c d") +# test("echo d/*", setup="mkdir d; touch a b c d/d d/e d/f") +# test("echo d/*", setup="mkdir d; touch a b c d/d d/e d/f; chmod 000 d") +# test(r"echo \*", setup="touch a b c") +# test(r"echo \*\*", setup="touch a b c") +# test(r"echo \ *", setup="touch a b c") +# test(r"echo *\.c", setup="touch a.c b.c c.c") +# test(r"echo *.\c", setup="touch a.c b.c c.c") +# test(r"echo *.c\ ", setup="touch a.c b.c c.c") +# test("echo $A$B", +# setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.c inc/bar.c", +# exports={"A": "*", "B": "/*.c"}) +# test("echo $A$B", +# setup="mkdir src; touch src/a src/b src/c src/foo.c src/bar.c;\ +# mkdir inc; touch inc/a inc/b inc/c inc/foo.c inc/bar.c", +# exports={"A": "*/.", "B": "*.c"}) diff --git a/minishell_test/test/__init__.py b/minishell_test/test/__init__.py new file mode 100644 index 0000000..cf9949f --- /dev/null +++ b/minishell_test/test/__init__.py @@ -0,0 +1,13 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# __init__.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/11 12:18:14 by charles #+# #+# # +# Updated: 2020/09/11 20:18:10 by charles ### ########.fr # +# # +# ############################################################################ # + +from test.test import Test # noqa: F401 diff --git a/minishell_test/test/captured.py b/minishell_test/test/captured.py new file mode 100644 index 0000000..f7dae3e --- /dev/null +++ b/minishell_test/test/captured.py @@ -0,0 +1,56 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# captured.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/11 12:16:25 by charles #+# #+# # +# Updated: 2021/02/04 15:52:19 by charles ### ########.fr # +# # +# ############################################################################ # + +from typing import List, Optional + +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, 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) + + 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) diff --git a/minishell_test/test/result.py b/minishell_test/test/result.py new file mode 100644 index 0000000..eff7b8b --- /dev/null +++ b/minishell_test/test/result.py @@ -0,0 +1,246 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# result.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/11 12:17:34 by charles #+# #+# # +# Updated: 2021/02/05 01:36:44 by charles ### ########.fr # +# # +# ############################################################################ # + +import sys +import re +from typing import Match, List, Optional + +import config +from test.captured import Captured + + +class BaseResult: + RED_CHARS = "\033[31m" + GREEN_CHARS = "\033[32m" + BLUE_CHARS = "\033[34m" + BOLD_CHARS = "\033[1m" + CLOSE_CHARS = "\033[0m" + + def __init__(self, cmd: str): + self.cmd = cmd + self.colored = True + self.set_colors() + + @property + def passed(self): + """Check if the result passed""" + raise NotImplementedError + + @property + def failed(self): + """Check if the result failed""" + return not self.passed + + 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") + + 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() + + def indicator(self, title: str, prefix: str) -> str: + return self.bold(self.blue(prefix + " " + title)) + + def full_diff(self) -> str: + raise NotImplementedError + + @property + def _escaped_cmd(self): + """Escape common control characters""" + c = self.cmd + c = c.replace("\t", "\\t") + c = c.replace("\n", "\\n") + c = c.replace("\v", "\\v") + c = c.replace("\r", "\\r") + c = c.replace("\f", "\\f") + return c + + @property + def _header_with(self): + return self.indicator("WITH {}".format(self._escaped_cmd), "|>") + '\n' + + def set_colors(self): + """Set colors strings on or off based on self.colored""" + if self.colored: + self.color_red = self.RED_CHARS + self.color_green = self.GREEN_CHARS + self.color_blue = self.BLUE_CHARS + self.color_bold = self.BOLD_CHARS + self.color_close = self.CLOSE_CHARS + else: + self.color_red = "" + self.color_green = "" + self.color_blue = "" + self.color_bold = "" + self.color_close = "" + + def green(self, s): + return self.color_green + s + self.color_close + + def red(self, s): + return self.color_red + s + self.color_close + + def blue(self, s): + return self.color_blue + s + self.color_close + + def bold(self, s): + return self.color_bold + s + self.color_close + + +class Result(BaseResult): + def __init__( + self, + cmd: str, + file_names: List[str], + expected: Captured, + actual: Captured, + ): + """Result class + cmd: runned command + file_names: names of watched files + expected: expected capture + actual: actual capture + """ + self.file_names = file_names + self.expected = expected + self.actual = actual + super().__init__(cmd) + + @property + def passed(self): + return self.actual == self.expected + + def header(self, title: str) -> str: + return self.bold("|---------------------------------------{:-<40}".format(title)) + + @property + def expected_header(self) -> str: + return self.green(self.header("EXPECTED")) + '\n' + + @property + def actual_header(self) -> str: + return self.red(self.header("ACTUAL")) + '\n' + + def cat_e(self, s: Optional[str]) -> str: + """Pass a string through a cat -e like output""" + if s is None: + return "FROM TEST: File not created\n" + s = s.replace("\n", "$\n") + if len(s) < 2: + return s + if s[-1] != '\n': + s += '\n' + return s + + def file_diff(self, file_name: str, expected: Optional[str], actual: Optional[str]) -> str: + """Difference between 2 files""" + if expected == actual: + return "" + file_header = self.indicator("FILE {}".format(file_name), "|#") + '\n' + return ( + file_header + + self.expected_header + + self.cat_e(expected) + + self.actual_header + + self.cat_e(actual) + ) + + def files_diff(self): + """Difference between watched files""" + return '\n'.join([self.file_diff(n, e, a) for n, e, a in + zip(self.file_names, + self.expected.files_content, + self.actual.files_content) + if e != a]) + + def output_diff(self) -> str: + """Difference in command output""" + out = "" + if self.actual.is_timeout: + return "TIMEOUT\n" + if self.expected.status != self.actual.status: + out += self.indicator( + "STATUS: expected {} actual {}" + .format(self.expected.status, self.actual.status), "| " + ) + '\n' + if self.expected.output != self.actual.output: + out += (self.expected_header + + self.cat_e(self.expected.output) + + self.actual_header + + self.cat_e(self.actual.output)) + return out + + def full_diff(self) -> str: + """Concat all difference reports""" + return self._header_with + self.output_diff() + self.files_diff() + "=" * 80 + '\n' + + +class LeakResult(BaseResult): + def __init__(self, cmd: str, captured: Captured): + self.captured = captured + super().__init__(cmd) + + def _search_leak_kind(self, kind: str) -> Match: + match = re.search( + r"==\d+==\s+" + kind + r" lost: (?P[0-9,]+) bytes in [0-9,]+ blocks", + self.captured.output + ) + if match is None: + raise RuntimeError( + "valgrind output parsing failed for `{}`:\n{}" + .format(self.cmd, self.captured.output) + ) + return match + + @property + def _lost_bytes(self): + if self.captured.output.find("All heap blocks were freed -- no leaks are possible") != -1: + definite_bytes = 0 + indirect_bytes = 0 + else: + definite_match = self._search_leak_kind("definitely") + indirect_match = self._search_leak_kind("indirectly") + definite_bytes = int(definite_match.group("bytes").replace(",", "")) + indirect_bytes = int(indirect_match.group("bytes").replace(",", "")) + return definite_bytes + indirect_bytes + + @property + def passed(self): + """Check if the result passed""" + if self.captured.is_timeout: + return False + return self._lost_bytes == 0 + + def full_diff(self) -> str: + """Concat all difference reports""" + return self._header_with + self.captured.output diff --git a/minishell_test/test/test.py b/minishell_test/test/test.py new file mode 100644 index 0000000..ab68d1e --- /dev/null +++ b/minishell_test/test/test.py @@ -0,0 +1,155 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# test.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/06/16 21:48:50 by charles #+# #+# # +# Updated: 2021/02/05 01:37:44 by charles ### ########.fr # +# # +# ############################################################################ # + +import os +import sys +import subprocess +from typing import Optional, List, Dict, Union, Callable + +import config +from test.captured import Captured +from test.result import Result, LeakResult +import sandbox + +HookType = Union[Callable[[str], str], List[Callable[[str], str]]] +HookStatusType = Union[Callable[[int], int], List[Callable[[int], int]]] + + +class Test: + def __init__( + self, + cmd: str, + setup: str = "", + files: List[str] = [], + exports: Dict[str, str] = {}, + timeout: float = config.TIMEOUT, + hook: HookType = [], + hook_status: HookStatusType = [], + ): + """ Test class + cmd: command to execute + setup: command to execute before tested command + files: files to watch (check content after test) + exports: exported variables + timeout: maximum amount of time taken by the test + hook: function to execute on the output of the test + hook_status: function to execute on status code + """ + self.cmd = cmd + self.setup = setup + self.files = files + self.exports = exports + self.result: Optional[Union[Result, LeakResult]] = None + self.timeout = timeout + if type(hook) is not list: + hook = [hook] # type: ignore + if type(hook_status) is not list: + hook_status = [hook_status] # type: ignore + self.hook: List[Callable[[str], str]] = hook # type: ignore + self.hook_status: List[Callable[[int], int]] = hook_status # type: ignore + + def run(self, index: int) -> None: + """ Run the test for minishell and the reference shell and print the result out """ + + if config.CHECK_LEAKS: + 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"]) + self.result = Result(self.full_cmd, self.files, expected, actual) + self.result.put(index) + + def _run_sandboxed(self, shell_cmd: List[str]) -> Captured: + """ Run the command in a sandbox environment """ + with sandbox.context(): + if self.setup != "": + try: + subprocess.run( + self.setup, + shell=True, + cwd=config.SANDBOX_PATH, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + check=True + ) + except subprocess.CalledProcessError as e: + print("Error: `{}` setup command failed for `{}`\n\twith '{}'" + .format(self.setup, + self.cmd, + "no stderr" if e.stdout is None + else e.stdout.decode().strip())) + sys.exit(1) + return self._run_capture(shell_cmd) + + def _run_capture(self, shell_cmd: List[str]) -> Captured: + """ Capture the output (stdout and stderr) + Capture the content of the watched files after the command is run + """ + # run the command in the sandbox + process = subprocess.Popen( + [*shell_cmd, self.cmd], + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + cwd=config.SANDBOX_PATH, + env={ + 'PATH': config.PATH_VARIABLE, + 'TERM': 'xterm-256color', + **self.exports, + }, + ) + + # 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)) + except subprocess.TimeoutExpired: + process.kill() + # _, _ = process.communicate(timeout=2) + return Captured.timeout() + try: + output = stdout.decode() + except UnicodeDecodeError: + output = "UNICODE ERROR: {}".format(process.stdout) + + # capture watched files content + 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: + files_content.append(f.read().decode()) + except FileNotFoundError: + files_content.append(None) + + # apply output/status hooks + for hook in self.hook: + output = hook(output) + for hook_status in self.hook_status: + process.returncode = hook_status(process.returncode) + return Captured(output, process.returncode, files_content) + + @property + def full_cmd(self): + """ Return the command prefixed by the setup and exports """ + s = self.cmd + if len(self.exports) != 0: + s = "[EXPORTS {}] {}".format( + ' '.join(["{}='{}'".format(k, v) for k, v in self.exports.items()]), s) + if self.setup != "": + s = "[SETUP {}] {}".format(self.setup, s) + return s -- cgit