aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml1
-rw-r--r--minishell_test/config.py6
-rw-r--r--minishell_test/hooks.py67
-rw-r--r--tests/test_hooks.py96
-rw-r--r--tox.ini2
5 files changed, 97 insertions, 75 deletions
diff --git a/.travis.yml b/.travis.yml
index 2e93a07..5c2486f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -30,5 +30,6 @@ install:
script:
- python -m flake8 minishell_test
- python -m mypy minishell_test
+ - python -m pytest -vvv
- python -m minishell_test -p /tmp/minishell
- python -m minishell_test -p /tmp/minishell -k pwd
diff --git a/minishell_test/config.py b/minishell_test/config.py
index db98ce5..00d147c 100644
--- a/minishell_test/config.py
+++ b/minishell_test/config.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/02/26 09:40:36 by cacharle #+# #+# #
-# Updated: 2021/02/28 10:53:38 by cacharle ### ########.fr #
+# Updated: 2021/02/28 11:19:13 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -78,7 +78,7 @@ class Config():
@classmethod
def init(cls, args):
if isinstance(args, list):
- args = parse_args(sys.argv[1:])
+ args = parse_args(args)
cls.minishell_dir = Path(args.path).resolve()
@@ -160,7 +160,7 @@ class Config():
cfg = ConfigParser()
cfg.read(DATA_DIR / 'default.cfg')
user_cfg = ConfigParser()
- user_cfg.read(cls.minishell_dir / CONFIG_FILENAME)
+ user_cfg.read(cls.minishell_dir / CONFIG_FILENAME) # if file doesn't exists, returns []
for section in user_cfg:
if section not in cfg:
diff --git a/minishell_test/hooks.py b/minishell_test/hooks.py
index 67c3f84..54963c9 100644
--- a/minishell_test/hooks.py
+++ b/minishell_test/hooks.py
@@ -6,7 +6,7 @@
# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/09/11 16:10:20 by charles #+# #+# #
-# Updated: 2021/02/27 20:54:14 by cacharle ### ########.fr #
+# Updated: 2021/02/28 12:06:14 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -15,6 +15,9 @@ import re
from minishell_test.config import Config
+DISCARDED_TEXT = "DISCARDED BY TEST"
+
+
def sort_lines(output):
"""Sort lines of output"""
return '\n'.join(sorted(output.split('\n')))
@@ -23,7 +26,7 @@ def sort_lines(output):
def error_line0(output):
"""Replace "/bin/bash: -c: line n:" by "minishell:" and delete the second line"""
if not Config.check_error_messages:
- return "DISCARDED BY TEST"
+ return DISCARDED_TEXT
lines = output.split('\n')
if len(lines) != 3:
@@ -36,7 +39,7 @@ def error_line0(output):
def discard(output):
"""Discard the output"""
- return "DISCARDED BY TEST"
+ return DISCARDED_TEXT
def export_singleton(output):
@@ -55,7 +58,22 @@ def replace_double(s):
return hook
-def platform_status(darwin_status, linux_status, windows_status=None):
+def error_eof_to_expected_token(output):
+ return output.replace(
+ "-c: line 1: syntax error: unexpected end of file",
+ "syntax error expected token"
+ )
+
+
+def should_not_be(not_expected):
+ def hook(output):
+ if output == not_expected:
+ return "OUTPUT SHOULD NOT BE " + output
+ return DISCARDED_TEXT
+ return hook
+
+
+def platform_status(darwin_status, linux_status):
def hook(status):
if Config.platform == "darwin":
return status
@@ -65,45 +83,20 @@ def platform_status(darwin_status, linux_status, windows_status=None):
return hook
-def linux_only(func):
- """ Decorator for hooks that only need to be executed on linux """
+def linux_replace(f, t):
def hook(output):
if not Config.platform == "linux":
return output
- return func(output)
+ return output.replace(f, t)
return hook
-@linux_only
-def is_directory(output):
- return output.replace("Is a directory", "is a directory")
-
-
-@linux_only
-def shlvl_0_to_1(output):
- return output.replace("SHLVL=0", "SHLVL=1")
+is_directory = linux_replace("Is a directory", "is a directory")
+shlvl_0_to_1 = linux_replace("SHLVL=0", "SHLVL=1")
+delete_escape = linux_replace("\\", "")
-@linux_only
-def delete_escape(output):
- return output.replace("\\", "")
-
-
-@linux_only
def linux_discard(output):
- return "DISCARDED BY MINISHELL TEST"
-
-
-def error_eof_to_expected_token(output):
- return output.replace(
- "-c: line 1: syntax error: unexpected end of file",
- "syntax error expected token"
- )
-
-
-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
+ if not Config.platform == "linux":
+ return output
+ return DISCARDED_TEXT
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index c7685ef..a811e55 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -6,13 +6,13 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/02/27 20:03:52 by cacharle #+# #+# #
-# Updated: 2021/02/27 20:44:22 by cacharle ### ########.fr #
+# Updated: 2021/02/28 12:05:58 by cacharle ### ########.fr #
# #
# ############################################################################ #
-import pytest
+import contextlib
-from minishell_test import config
+from minishell_test.config import Config
from minishell_test.hooks import (
sort_lines,
@@ -21,14 +21,25 @@ from minishell_test.hooks import (
export_singleton,
replace_double,
platform_status,
- is_directory,
- shlvl_0_to_1,
- delete_escape,
+ linux_replace,
error_eof_to_expected_token,
linux_discard,
should_not_be,
+ DISCARDED_TEXT,
)
+Config.init([])
+
+
+@contextlib.contextmanager
+def config_context(attr, value):
+ prev = getattr(Config, attr)
+ setattr(Config, attr, value)
+ try:
+ yield
+ finally:
+ setattr(Config, attr, prev)
+
def test_sort_lines():
assert "a\nb\nc" == sort_lines("a\nb\nc")
@@ -63,14 +74,19 @@ _=/usr/bin/env\
""")
-@pytest.mark.skip()
def test_error_line0():
- pass
+ assert "" == error_line0("")
+ assert "foo" == error_line0("foo")
+ assert "a\nb" == error_line0("a\nb")
+ assert "a\nb\nc" == error_line0("a\nb\nc")
+ assert "minishell: bonjour\n" == error_line0(Config.shell_reference_prefix + "-c: bonjour\nfoo\n")
+ assert "minishell: \n" == error_line0(Config.shell_reference_prefix + "-c: \nfoo\n")
+ assert Config.shell_reference_prefix + "-c:asdf\nfoo\n" == error_line0(Config.shell_reference_prefix + "-c:asdf\nfoo\n")
def test_discard():
- assert "DISCARDED BY TEST" == discard("")
- assert "DISCARDED BY TEST" == discard("foo")
+ assert DISCARDED_TEXT == discard("")
+ assert DISCARDED_TEXT == discard("foo")
def test_export_singleton():
@@ -100,9 +116,7 @@ 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:
+ with config_context("shell_reference_args", ["--posix"]):
assert "" == export_singleton("export IGOTNUMBERS42")
assert "" == export_singleton("export IGOTUNDERSCORE__")
assert "" == export_singleton("export I")
@@ -128,8 +142,6 @@ 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():
@@ -141,29 +153,43 @@ def test_replace_double():
assert ";;;;" == replace_double(";")(";;;;;;;;")
-@pytest.mark.skip()
-def test_platform_status():
- 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_is_directory():
- pass
-@pytest.mark.skip()
-def test_shlvl_0_to_1():
- pass
+def test_should_not_be():
+ assert "OUTPUT SHOULD NOT BE " == should_not_be("")("")
+ assert "OUTPUT SHOULD NOT BE bonjour" == should_not_be("bonjour")("bonjour")
+ assert DISCARDED_TEXT == should_not_be("foo")("")
+ assert DISCARDED_TEXT == should_not_be("bar")("bonjour")
-@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")
+def test_platform_status():
+ with config_context('platform', 'darwin'):
+ assert 0 == platform_status(0, 1)(0)
+ assert 1 == platform_status(42, 42)(1)
+ with config_context('platform', 'linux'):
+ assert 0 == platform_status(0, 1)(1)
+ assert 42 == platform_status(0, 1)(42)
+ with config_context('platform', 'foo'):
+ assert 0 == platform_status(42, 42)(0)
+
+
+def test_linux_replace():
+ with config_context('platform', 'darwin'):
+ assert "Is a directory" == linux_replace("Is a directory", "is a directory")("Is a directory")
+ assert "SHLVL=0" == linux_replace("SHLVL=0", "SHLVL=1")("SHLVL=0")
+ assert "\\" == linux_replace("\\", "")("\\")
+ with config_context('platform', 'linux'):
+ assert "is a directory" == linux_replace("Is a directory", "is a directory")("Is a directory")
+ assert "SHLVL=1" == linux_replace("SHLVL=0", "SHLVL=1")("SHLVL=0")
+ assert "" == linux_replace("\\", "")("\\")
-@pytest.mark.skip()
-def test_linux_discard():
- pass
-@pytest.mark.skip()
-def test_should_not_be():
- pass
+def test_linux_discard():
+ with config_context('platform', 'darwin'):
+ assert "" == linux_discard("")
+ assert "foo" == linux_discard("foo")
+ with config_context('platform', 'linux'):
+ assert DISCARDED_TEXT == linux_discard("")
+ assert DISCARDED_TEXT == linux_discard("foo")
diff --git a/tox.ini b/tox.ini
index 10af4a5..39d4ce6 100644
--- a/tox.ini
+++ b/tox.ini
@@ -9,6 +9,7 @@ passenv =
deps =
flake8
mypy
+ pytest
allowlist_externals =
git
@@ -22,6 +23,7 @@ commands_pre =
commands =
flake8 minishell_test
mypy minishell_test
+ pytest
python -m minishell_test -p /tmp/minishell
python -m minishell_test -p /tmp/minishell -k pwd