From 348da189c00bdef866d2b597ea0250fc0f0e88dc Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 27 Feb 2021 20:55:16 +0100 Subject: Added some test for hooks, Refectoring hooks module --- docs/config.rst | 10 +++ minishell_test/args.py | 2 +- minishell_test/hooks.py | 59 ++++++-------- minishell_test/suites/builtin.py | 6 +- minishell_test/suites/flow.py | 14 ++-- setup.cfg | 2 +- tests/test_hooks.py | 169 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 217 insertions(+), 45 deletions(-) create mode 100644 tests/test_hooks.py diff --git a/docs/config.rst b/docs/config.rst index edd88d5..a8933d5 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -8,6 +8,16 @@ Configuration file It looks for a ``minishell_test.cfg`` file in your project directory. +Here is what the default configuration looks like: + +.. code-block:: cfg + + .. include:: ../minishell_test/data/default.cfg + +Format used for the configuration file: + +https://docs.python.org/3/library/configparser.html#supported-ini-file-structure + Global ------ diff --git a/minishell_test/args.py b/minishell_test/args.py index 3f56b8c..8aca64b 100644 --- a/minishell_test/args.py +++ b/minishell_test/args.py @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/07/15 18:24:32 by charles #+# #+# # -# Updated: 2021/02/27 12:08:55 by cacharle ### ########.fr # +# Updated: 2021/02/27 20:16:56 by cacharle ### ########.fr # # # # ############################################################################ # diff --git a/minishell_test/hooks.py b/minishell_test/hooks.py index 6356080..a1447cd 100644 --- a/minishell_test/hooks.py +++ b/minishell_test/hooks.py @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/11 16:10:20 by charles #+# #+# # -# Updated: 2021/02/27 15:40:25 by cacharle ### ########.fr # +# Updated: 2021/02/27 20:54:14 by cacharle ### ########.fr # # # # ############################################################################ # @@ -44,18 +44,15 @@ def export_singleton(output): prefix = "export " if ("--posix" in config.SHELL_REFERENCE_ARGS) else "declare -x " return sort_lines( '\n'.join([line for line in output.split('\n') - if re.match("^{}[a-zA-Z]+$".format(prefix), line) is None]) + if re.match("^{}[a-zA-Z_][a-zA-Z0-9_]*$".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 replace_double(s): + """Replace double occurence of a string by one""" + def hook(output): + return output.replace(s + s, s) + return hook def platform_status(darwin_status, linux_status, windows_status=None): @@ -68,30 +65,33 @@ def platform_status(darwin_status, linux_status, windows_status=None): return hook -def is_directory(output): - if config.PLATFORM == "linux": - return output.replace("Is a directory", "is a directory") - else: - return output +def linux_only(func): + """ Decorator for hooks that only need to be executed on linux """ + def hook(output): + if not config.PLATFORM == "linux": + return output + return func(output) + return hook -# def no_cd_too_many_arguments(output): -# for i, line in output.split("\n"): -# if line.find("too many arguments") +@linux_only +def is_directory(output): + return output.replace("Is a directory", "is a directory") +@linux_only def shlvl_0_to_1(output): - if config.PLATFORM == "linux": - return output.replace("SHLVL=0", "SHLVL=1") - else: - return output + return output.replace("SHLVL=0", "SHLVL=1") +@linux_only def delete_escape(output): - if config.PLATFORM == "linux": - return output.replace("\\", "") - else: - return output + return output.replace("\\", "") + + +@linux_only +def linux_discard(output): + return "DISCARDED BY MINISHELL TEST" def error_eof_to_expected_token(output): @@ -101,13 +101,6 @@ def error_eof_to_expected_token(output): ) -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: diff --git a/minishell_test/suites/builtin.py b/minishell_test/suites/builtin.py index c373ce6..2591bc3 100644 --- a/minishell_test/suites/builtin.py +++ b/minishell_test/suites/builtin.py @@ -6,7 +6,7 @@ # By: juligonz +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/07/15 18:24:43 by charles #+# #+# # -# Updated: 2021/02/27 12:07:46 by cacharle ### ########.fr # +# Updated: 2021/02/27 20:36:27 by cacharle ### ########.fr # # Updated: 2020/09/11 18:01:27 by juligonz ### ########.fr # # # # **************************************************************************** # @@ -168,7 +168,7 @@ def suite_cd(test): 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", hook=hooks.replace_double("/")) 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") @@ -179,7 +179,7 @@ def suite_cd(test): 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", hook=hooks.replace_double("/")) 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") diff --git a/minishell_test/suites/flow.py b/minishell_test/suites/flow.py index 2adbb4b..aa6d395 100644 --- a/minishell_test/suites/flow.py +++ b/minishell_test/suites/flow.py @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/07/15 18:24:52 by charles #+# #+# # -# Updated: 2021/02/27 12:06:58 by cacharle ### ########.fr # +# Updated: 2021/02/27 20:35:46 by cacharle ### ########.fr # # # # ############################################################################ # @@ -16,7 +16,7 @@ from minishell_test.hooks import ( error_line0, platform_status, discard, - replace_double_semi_colon, + replace_double, error_eof_to_expected_token ) @@ -49,11 +49,11 @@ def suite_end(test): 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(";;", hook=[error_line0, replace_double(";")], hook_status=platform_status(2, 1)) + test(";;;", hook=[error_line0, replace_double(";")], hook_status=platform_status(2, 1)) + test(";;;;;", hook=[error_line0, replace_double(";")], hook_status=platform_status(2, 1)) + test("echo a ;; echo b", hook=[error_line0, replace_double(";")], hook_status=platform_status(2, 1)) + test("echo a ;;;;; echo b", hook=[error_line0, replace_double(";")], 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") diff --git a/setup.cfg b/setup.cfg index fe52ac0..4a8a202 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,7 +29,7 @@ packages = find: python_requires = >=3.6 [options.package_data] -c_formatter_42/data = * +minishell_test/data = * [options.entry_points] console_scripts = diff --git a/tests/test_hooks.py b/tests/test_hooks.py new file mode 100644 index 0000000..c7685ef --- /dev/null +++ b/tests/test_hooks.py @@ -0,0 +1,169 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# test_hooks.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2021/02/27 20:03:52 by cacharle #+# #+# # +# Updated: 2021/02/27 20:44:22 by cacharle ### ########.fr # +# # +# ############################################################################ # + +import pytest + +from minishell_test import config + +from minishell_test.hooks import ( + sort_lines, + error_line0, + discard, + export_singleton, + replace_double, + platform_status, + is_directory, + shlvl_0_to_1, + delete_escape, + error_eof_to_expected_token, + linux_discard, + should_not_be, +) + + +def test_sort_lines(): + assert "a\nb\nc" == sort_lines("a\nb\nc") + assert "a\nb\nc" == sort_lines("c\nb\na") + assert "a\nb\nc" == sort_lines("b\na\nc") + assert """\ +EDITOR=vim +GNUPGHOME=/home/cacharle/.local/share/gnupg +JUPYTER_CONFIG_DIR=/home/cacharle/.config/jupyter +LESSHISTFILE=- +LESS_TERMCAP_se=[0m +LESS_TERMCAP_so=[01;33m +SHELL=/usr/bin/zsh +XDG_CONFIG_HOME=/home/cacharle/.config +XDG_DATA_HOME=/home/cacharle/.local/share +XMONAD_CONFIG_HOME=/home/cacharle/.config/xmonad +XMONAD_DATA_HOME=/home/cacharle/.local/share/xmonad +_=/usr/bin/env\ +""" == sort_lines("""\ +SHELL=/usr/bin/zsh +LESSHISTFILE=- +JUPYTER_CONFIG_DIR=/home/cacharle/.config/jupyter +XMONAD_CONFIG_HOME=/home/cacharle/.config/xmonad +XMONAD_DATA_HOME=/home/cacharle/.local/share/xmonad +LESS_TERMCAP_se=[0m +LESS_TERMCAP_so=[01;33m +XDG_DATA_HOME=/home/cacharle/.local/share +XDG_CONFIG_HOME=/home/cacharle/.config +GNUPGHOME=/home/cacharle/.local/share/gnupg +EDITOR=vim +_=/usr/bin/env\ +""") + + +@pytest.mark.skip() +def test_error_line0(): + pass + + +def test_discard(): + assert "DISCARDED BY TEST" == discard("") + assert "DISCARDED BY TEST" == discard("foo") + + +def test_export_singleton(): + assert "" == export_singleton("declare -x IGOTNUMBERS42") + assert "" == export_singleton("declare -x IGOTUNDERSCORE__") + assert "" == export_singleton("declare -x I") + assert "" == export_singleton("declare -x _") + assert """\ +declare -x XDG_SESSION_TYPE="tty" +declare -x XDG_VTNR="1" +declare -x XINITRC="/home/cacharle/.config/x11/xinitrc" +declare -x XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad" +declare -x XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad" +declare -x YSU_MESSAGE_POSITION="after" +declare -x YSU_VERSION="1.7.3" +declare -x ZDOTDIR="/home/cacharle/.config/zsh"\ +""" == export_singleton("""\ +declare -x XDG_SESSION_ID +declare -x XDG_SESSION_TYPE="tty" +declare -x XDG_VTNR="1" +declare -x XINITRC="/home/cacharle/.config/x11/xinitrc" +declare -x XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad" +declare -x XMONAD_CONFIG_HOME +declare -x XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad" +declare -x YSU_MESSAGE_POSITION="after" +declare -x YSU_VERSION="1.7.3" +declare -x ZDOTDIR="/home/cacharle/.config/zsh"\ +""") + + prev = config.SHELL_REFERENCE_ARGS + config.SHELL_REFERENCE_ARGS = ['--posix'] + try: + assert "" == export_singleton("export IGOTNUMBERS42") + assert "" == export_singleton("export IGOTUNDERSCORE__") + assert "" == export_singleton("export I") + assert "" == export_singleton("export _") + assert """\ +export XDG_SESSION_TYPE="tty" +export XDG_VTNR="1" +export XINITRC="/home/cacharle/.config/x11/xinitrc" +export XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad" +export XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad" +export YSU_MESSAGE_POSITION="after" +export YSU_VERSION="1.7.3" +export ZDOTDIR="/home/cacharle/.config/zsh"\ +""" == export_singleton("""\ +export XDG_SESSION_ID +export XDG_SESSION_TYPE="tty" +export XDG_VTNR="1" +export XINITRC="/home/cacharle/.config/x11/xinitrc" +export XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad" +export XMONAD_CONFIG_HOME +export XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad" +export YSU_MESSAGE_POSITION="after" +export YSU_VERSION="1.7.3" +export ZDOTDIR="/home/cacharle/.config/zsh"\ +""") + finally: + config.SHELL_REFERENCE_ARGS = prev + + +def test_replace_double(): + assert "/" == replace_double("/")("//") + assert "//" == replace_double("/")("////") + assert "////" == replace_double("/")("////////") + assert ";" == replace_double(";")(";;") + assert ";;" == replace_double(";")(";;;;") + assert ";;;;" == replace_double(";")(";;;;;;;;") + + +@pytest.mark.skip() +def test_platform_status(): + pass + +@pytest.mark.skip() +def test_is_directory(): + pass + +@pytest.mark.skip() +def test_shlvl_0_to_1(): + pass + +@pytest.mark.skip() +def test_delete_escape(): + pass + +def test_error_eof_to_expected_token(): + assert "syntax error expected token" == error_eof_to_expected_token("-c: line 1: syntax error: unexpected end of file") + +@pytest.mark.skip() +def test_linux_discard(): + pass + +@pytest.mark.skip() +def test_should_not_be(): + pass -- cgit