From b298afeb9da27038cbd54802a2d70504909427a3 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 15 Jun 2020 18:08:00 +0200 Subject: Rewrite in python --- .gitignore | 1 + README.md | 4 +- config.py | 37 +++++++++++++++++ main.py | 78 ++++++++++++++++++++++++++++++++++++ minishell_test.config | 24 ----------- test.sh | 107 -------------------------------------------------- tests/base.sh | 0 tests/builtin.sh | 23 ----------- tests/redirection.sh | 0 tests/separator.sh | 0 10 files changed, 118 insertions(+), 156 deletions(-) create mode 100644 .gitignore create mode 100644 config.py create mode 100755 main.py delete mode 100644 minishell_test.config delete mode 100755 test.sh delete mode 100644 tests/base.sh delete mode 100644 tests/builtin.sh delete mode 100644 tests/redirection.sh delete mode 100644 tests/separator.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd4c22c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*__pycache__* diff --git a/README.md b/README.md index a73596b..fe6ea90 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ The reasons for this is are: ## Run -`> ./test.sh` +`> ./main.py` # Configuration -The default configuration can be changed in the `minishell\_test.config` file. +The default configuration can be changed in diff --git a/config.py b/config.py new file mode 100644 index 0000000..286ff36 --- /dev/null +++ b/config.py @@ -0,0 +1,37 @@ +# Minishell configuration file + +# minishell dir path +MINISHELL_DIR = "/home/charles/minishell" + +# minishell executable +MINISHELL_EXEC = "minishell" + +# path to reference shell (shell which will be compared minishell) +# has to support the -c option (sh, bash and zsh support it) +REFERENCE_SHELL_PATH = "/bin/bash" + +# string marker which show the test result +PASS_MARKER = '.' +FAIL_MARKER = '!' + +# log file path +LOG_FILE = "result.log" + +# path to the sandbox directory +SANDBOX_PATH = "sandbox" + +LONG_TEXT = """ +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. +""" diff --git a/main.py b/main.py new file mode 100755 index 0000000..addebf6 --- /dev/null +++ b/main.py @@ -0,0 +1,78 @@ +#!/usr/bin/python3 + +import os +import sys +import subprocess +import shutil + +import config + + +def green(s): + return "\033[32m{}\033[0m".format(s) + + +def red(s): + return "\033[31m{}\033[0m".format(s) + + +def run_sandboxed(program: str, cmd: str) -> str: + try: + os.mkdir(config.SANDBOX_PATH) + except OSError: + pass + # os.system(self.setup_cmd) + current_dir = os.getcwd() + os.chdir(config.SANDBOX_PATH) + output = "" + try: + output = subprocess.check_output([program, "-c", cmd], stderr=subprocess.STDOUT) + except: + pass + + os.chdir(current_dir) + shutil.rmtree(config.SANDBOX_PATH) + return output + + +ignored_suites = [] +suites = {} +current_suite = "default" + +def test(cmd, setup = None, *files): + if current_suite in ignored_suites: + return + expected = run_sandboxed(config.REFERENCE_SHELL_PATH, cmd) + actual = run_sandboxed(os.path.join(config.MINISHELL_DIR, config.MINISHELL_EXEC), cmd) + + if actual != expected: + sys.stdout.write(red(config.FAIL_MARKER)) + else: + sys.stdout.write(green(config.PASS_MARKER)) + sys.stdout.flush() + + if suites.get(current_suite) is None: + suites[current_suite] = [] + suites[current_suite].append((expected, actual)) + + +def test_echo(): + test("echo bonjour") + test("echo je suis charles") + test("echo je suis charles") + + test("'echo' 'bonjour'") + test("'echo' 'je' 'suis' 'charles'") + test("'echo' 'je' 'suis' 'charles'") + + test('"echo" "bonjour"') + test('"echo" "je" "suis" "charles"') + test('"echo" "je" "suis" "charles"') + + + +def main(): + test_echo() + +if __name__ == "__main__": + main() diff --git a/minishell_test.config b/minishell_test.config deleted file mode 100644 index dbb5632..0000000 --- a/minishell_test.config +++ /dev/null @@ -1,24 +0,0 @@ -# Minishell config file - -# minishell dir path -minishell_path=../minishell - -# minishell executable -minishell_exec=minishell - -# lorem (long text) path -lorem_path=lorem.txt - -# path to reference shell (shell which will be compared minishell) -# has to support the -c option (sh, bash and zsh support it) -reference_shell_path=/bin/bash - -# string marker which show the test result -pass_marker=. -fail_marker=! - -# log file path -log_file=result.log - -# path to the sandbox directory -sandbox_path=sandbox diff --git a/test.sh b/test.sh deleted file mode 100755 index e145905..0000000 --- a/test.sh +++ /dev/null @@ -1,107 +0,0 @@ -#!/bin/sh - -# ############################################################################ # -# # -# ::: :::::::: # -# test.sh :+: :+: :+: # -# +:+ +:+ +:+ # -# By: charles +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2020/06/07 14:02:56 by charles #+# #+# # -# Updated: 2020/06/07 14:02:56 by charles ### ########.fr # -# # -# ############################################################################ # - -case $# in - 0) config_file=minishell_test.config;; - 1) config_file=$1;; - *) echo "Usage: $0 [config file]"; exit 1;; -esac - -config_extract() { - grep $1 $config_file | cut -d '=' -f 2 -} - -minishell_path=`config_extract minishell_path` -minishell_exec=`config_extract minishell_exec` -minishell_exec_path=$minishell_path/$minishell_exec -reference_shell_path=`config_extract reference_shell_path` -lorem_path=`config_extract lorem_path` -lorem=`cat $lorem_path` -pass_marker=`config_extract pass_marker` -fail_marker=`config_extract fail_marker` -log_file=`config_extract log_file` -sandbox_path=`config_extract sandbox_path` - -# echo $minishell_path -# echo $minishell_exec -# echo $minishell_exec_path -# echo $lorem - -if [ ! -f $minishell_exec_path ]; then - echo "Error: $minishell_exec_path does not exist" - exit 1 -fi - -red() { echo -n "`tput setaf 1`$1`tput sgr 0`"; } -green() { echo -n "`tput setaf 2`$1`tput sgr 0`"; } -put_pass_marker () { green $pass_marker; } -put_fail_marker () { red $fail_marker; } - -append_fail () { - tested=$1 - expected=$2 - expected_status=$3 - actual=$4 - actual_status=$5 - echo "for $1" -} - -sandbox_new() { - init_cmd="$1" - mkdir -p $sandbox_path - cd $sandbox_path - sh -c "$init_cmd" -} - -sandbox_clean() { - cd - > /dev/null - rm -rf $sandbox_path -} - -expect() { - expect_init $1 "" -} - -expect_init() { - init_cmd="$2" - sandbox_new "$init_cmd" - expected=`$referecence_shell_path -c $1` - expected_status=$? - sandbox_clean - sandbox_new "$init_cmd" - actual=`$minishell_exec_path -c $1` - actual_status=$? - sandbox_clean - if [ $expected = $actual ] && [ $expected_status -eq $actual_status ] - then - put_pass_marker - else - put_fail_marker - cat <> $log_file -WITH { $1 } -STATUS: expected: $expected_status actual: $actual_status -----------------------------------------EXPECTED-------------------------------- -$expected -----------------------------------------ACTUAL---------------------------------- -$actual -================================================================================ - -EOF - fi -} - -rm -f $log_file - -source './tests/base.sh' -source './tests/builtin.sh' diff --git a/tests/base.sh b/tests/base.sh deleted file mode 100644 index e69de29..0000000 diff --git a/tests/builtin.sh b/tests/builtin.sh deleted file mode 100644 index b5e572a..0000000 --- a/tests/builtin.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -# ############################################################################ # -# # -# ::: :::::::: # -# builtin.sh :+: :+: :+: # -# +:+ +:+ +:+ # -# By: charles +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2020/06/07 14:02:42 by charles #+# #+# # -# Updated: 2020/06/07 14:02:42 by charles ### ########.fr # -# # -# ############################################################################ # - -############################################################################### -# Builtin -############################################################################### - -expect 'echo bonjour' -expect 'echo bonjour je suis' -expect "echo $lorem" -expect "echo $lorem $lorem $lorem $lorem" - diff --git a/tests/redirection.sh b/tests/redirection.sh deleted file mode 100644 index e69de29..0000000 diff --git a/tests/separator.sh b/tests/separator.sh deleted file mode 100644 index e69de29..0000000 -- cgit