aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers.py27
-rw-r--r--tests/test/test_result.py130
-rw-r--r--tests/test/test_test.py97
-rw-r--r--tests/test_config.py25
-rw-r--r--tests/test_hooks.py82
-rw-r--r--tests/test_sandbox.py71
6 files changed, 200 insertions, 232 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
deleted file mode 100644
index f64be06..0000000
--- a/tests/helpers.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# ############################################################################ #
-# #
-# ::: :::::::: #
-# helpers.py :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2021/03/02 14:37:38 by cacharle #+# #+# #
-# Updated: 2021/03/02 17:45:28 by cacharle ### ########.fr #
-# #
-# ############################################################################ #
-
-import contextlib
-
-from minishell_test.config import Config
-
-
-@contextlib.contextmanager
-def config_context(**kwargs):
- prevs = {attr: getattr(Config, attr) for attr in kwargs.keys()}
- for attr, value in kwargs.items():
- setattr(Config, attr, value)
- try:
- yield
- finally:
- for attr, value in prevs.items():
- setattr(Config, attr, value)
diff --git a/tests/test/test_result.py b/tests/test/test_result.py
index f64511a..2e3afdb 100644
--- a/tests/test/test_result.py
+++ b/tests/test/test_result.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/03/01 16:26:34 by cacharle #+# #+# #
-# Updated: 2021/03/03 07:53:09 by cacharle ### ########.fr #
+# Updated: 2021/03/06 10:01:04 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -18,8 +18,6 @@ from minishell_test import colors
from minishell_test.test.result import BaseResult, Result, LeakResult, LeakResultException
from minishell_test.test.captured import CapturedCommand, CapturedTimeout
-from tests.helpers import config_context
-
colors.disable()
Config.init([])
@@ -152,42 +150,43 @@ class TestResult:
assert result_fail_file_not_exist_expected.failed
@pytest.mark.parametrize("term_cols", range(40, 300, 40))
- def test_summarize(self, result_pass, result_fail, result_fail_status, result_fail_file, result_fail_file_not_exist,
+ def test_summarize(self, monkeypatch, result_pass, result_fail, result_fail_status, result_fail_file, result_fail_file_not_exist,
result_fail_timeout, result_fail_file_not_exist_expected, result_pass_long_cmd, term_cols):
- with config_context(show_range=False, term_cols=term_cols):
- assert f"{'echo bonjour':{term_cols - 7}} [PASS]" == result_pass.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail_status.summarize(-1)
- assert f"{'echo bonjour > foo':{term_cols - 7}} [FAIL]" == result_fail_file.summarize(-1)
- assert f"{'echo bonjour > foo':{term_cols - 7}} [FAIL]" == result_fail_file_not_exist.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail_timeout.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail_file_not_exist_expected.summarize(-1)
- assert f"{('e' * 300)[:term_cols - 10]}... [PASS]" == result_pass_long_cmd.summarize(-1)
- with config_context(show_range=True, term_cols=term_cols):
- assert f" 1: {'echo bonjour':{term_cols - 11}} [PASS]" == result_pass.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_status.summarize(1)
- assert f" 1: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file.summarize(1)
- assert f" 1: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_timeout.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist_expected.summarize(1)
- assert f" 1: {('e' * 300)[:term_cols - 14]}... [PASS]" == result_pass_long_cmd.summarize(1)
- assert f"99: {'echo bonjour':{term_cols - 11}} [PASS]" == result_pass.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_status.summarize(99)
- assert f"99: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file.summarize(99)
- assert f"99: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_timeout.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist_expected.summarize(99)
- assert f"99: {('e' * 300)[:term_cols - 14]}... [PASS]" == result_pass_long_cmd.summarize(99)
- assert f"100: {'echo bonjour':{term_cols - 12}} [PASS]" == result_pass.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail_status.summarize(100)
- assert f"100: {'echo bonjour > foo':{term_cols - 12}} [FAIL]" == result_fail_file.summarize(100)
- assert f"100: {'echo bonjour > foo':{term_cols - 12}} [FAIL]" == result_fail_file_not_exist.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail_timeout.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail_file_not_exist_expected.summarize(100)
- assert f"100: {('e' * 300)[:term_cols - 15]}... [PASS]" == result_pass_long_cmd.summarize(100)
+ monkeypatch.setattr(Config, 'term_cols', term_cols)
+ monkeypatch.setattr(Config, 'show_range', False)
+ assert f"{'echo bonjour':{term_cols - 7}} [PASS]" == result_pass.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail_status.summarize(-1)
+ assert f"{'echo bonjour > foo':{term_cols - 7}} [FAIL]" == result_fail_file.summarize(-1)
+ assert f"{'echo bonjour > foo':{term_cols - 7}} [FAIL]" == result_fail_file_not_exist.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail_timeout.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_fail_file_not_exist_expected.summarize(-1)
+ assert f"{('e' * 300)[:term_cols - 10]}... [PASS]" == result_pass_long_cmd.summarize(-1)
+ monkeypatch.setattr(Config, 'show_range', True)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [PASS]" == result_pass.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_status.summarize(1)
+ assert f" 1: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file.summarize(1)
+ assert f" 1: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_timeout.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist_expected.summarize(1)
+ assert f" 1: {('e' * 300)[:term_cols - 14]}... [PASS]" == result_pass_long_cmd.summarize(1)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [PASS]" == result_pass.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_status.summarize(99)
+ assert f"99: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file.summarize(99)
+ assert f"99: {'echo bonjour > foo':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_timeout.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_fail_file_not_exist_expected.summarize(99)
+ assert f"99: {('e' * 300)[:term_cols - 14]}... [PASS]" == result_pass_long_cmd.summarize(99)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [PASS]" == result_pass.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail_status.summarize(100)
+ assert f"100: {'echo bonjour > foo':{term_cols - 12}} [FAIL]" == result_fail_file.summarize(100)
+ assert f"100: {'echo bonjour > foo':{term_cols - 12}} [FAIL]" == result_fail_file_not_exist.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail_timeout.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_fail_file_not_exist_expected.summarize(100)
+ assert f"100: {('e' * 300)[:term_cols - 15]}... [PASS]" == result_pass_long_cmd.summarize(100)
def test_repr(self, result_fail, result_fail_status, result_fail_file, result_fail_file_not_exist, result_fail_file_multiple,
result_fail_timeout, result_fail_file_not_exist_expected):
@@ -415,34 +414,35 @@ bonjour
assert result_leak_fail_timeout.failed
@pytest.mark.parametrize("term_cols", range(40, 300, 40))
- def test_summarize(self, result_leak_pass, result_leak_fail_indirect, result_leak_fail_definitive,
+ def test_summarize(self, monkeypatch, result_leak_pass, result_leak_fail_indirect, result_leak_fail_definitive,
result_leak_fail_both, result_leak_pass_no_count, result_leak_fail_timeout, term_cols):
- with config_context(show_range=False, term_cols=term_cols):
- assert f"{'echo bonjour':{term_cols - 7}} [PASS]" == result_leak_pass.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [PASS]" == result_leak_pass_no_count.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_definitive.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_indirect.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_both.summarize(-1)
- assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_timeout.summarize(-1)
- with config_context(show_range=True, term_cols=term_cols):
- assert f" 1: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass_no_count.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_definitive.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_indirect.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_both.summarize(1)
- assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_timeout.summarize(1)
- assert f"99: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass_no_count.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_definitive.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_indirect.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_both.summarize(99)
- assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_timeout.summarize(99)
- assert f"100: {'echo bonjour':{term_cols - 12}} [PASS]" == result_leak_pass.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [PASS]" == result_leak_pass_no_count.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_definitive.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_indirect.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_both.summarize(100)
- assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_timeout.summarize(100)
+ monkeypatch.setattr(Config, 'term_cols', term_cols)
+ monkeypatch.setattr(Config, 'show_range', False)
+ assert f"{'echo bonjour':{term_cols - 7}} [PASS]" == result_leak_pass.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [PASS]" == result_leak_pass_no_count.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_definitive.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_indirect.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_both.summarize(-1)
+ assert f"{'echo bonjour':{term_cols - 7}} [FAIL]" == result_leak_fail_timeout.summarize(-1)
+ monkeypatch.setattr(Config, 'show_range', True)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass_no_count.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_definitive.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_indirect.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_both.summarize(1)
+ assert f" 1: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_timeout.summarize(1)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [PASS]" == result_leak_pass_no_count.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_definitive.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_indirect.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_both.summarize(99)
+ assert f"99: {'echo bonjour':{term_cols - 11}} [FAIL]" == result_leak_fail_timeout.summarize(99)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [PASS]" == result_leak_pass.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [PASS]" == result_leak_pass_no_count.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_definitive.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_indirect.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_both.summarize(100)
+ assert f"100: {'echo bonjour':{term_cols - 12}} [FAIL]" == result_leak_fail_timeout.summarize(100)
def test_repr(self, result_leak_fail_indirect, result_leak_fail_definitive, result_leak_fail_both, result_leak_fail_timeout):
assert r"""
diff --git a/tests/test/test_test.py b/tests/test/test_test.py
index cad0e19..11255c0 100644
--- a/tests/test/test_test.py
+++ b/tests/test/test_test.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/03/02 18:48:57 by cacharle #+# #+# #
-# Updated: 2021/03/03 15:59:01 by cacharle ### ########.fr #
+# Updated: 2021/03/06 10:07:07 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -21,8 +21,6 @@ from minishell_test.test.result import LeakResult
from minishell_test.test.captured import CapturedCommand, CapturedTimeout
from minishell_test.test.test import Test, TestSetupException
-from tests.helpers import config_context
-
colors.disable()
Config.init([])
@@ -33,16 +31,16 @@ class TestTest:
def reset_config(self):
Config.init([])
- def test_init_timeout(self):
+ def test_init_timeout(self, monkeypatch):
assert Config.timeout_test == Test("")._timeout
assert Config.timeout_test == Test("", timeout=0)._timeout
assert 2 == Test("", timeout=2)._timeout
assert 100 == Test("", timeout=100)._timeout
- with config_context(check_leaks=True):
- assert Config.timeout_leaks == Test("", timeout=-10)._timeout
- assert Config.timeout_leaks == Test("", timeout=0)._timeout
- assert Config.timeout_leaks == Test("", timeout=1)._timeout
- assert Config.timeout_leaks == Test("", timeout=100)._timeout
+ monkeypatch.setattr(Config, 'check_leaks', True)
+ assert Config.timeout_leaks == Test("", timeout=-10)._timeout
+ assert Config.timeout_leaks == Test("", timeout=0)._timeout
+ assert Config.timeout_leaks == Test("", timeout=1)._timeout
+ assert Config.timeout_leaks == Test("", timeout=100)._timeout
def test_coerce_into_list(self):
assert [1] == Test._coerce_into_list(1)
@@ -51,7 +49,7 @@ class TestTest:
assert ["bonjour"] == Test._coerce_into_list(["bonjour"])
assert ["bonjour", "foo", "bar"] == Test._coerce_into_list(["bonjour", "foo", "bar"])
- def test_apply_hook(self):
+ def test_apply_hook(self, monkeypatch):
bonjour_to_foo = lambda x: x.replace("bonjour", "foo") # noqa
foo_to_aurevoir = lambda x: x.replace("foo", "aurevoir") # noqa
assert "foo" == Test._apply_hooks("bonjour", [bonjour_to_foo])
@@ -64,11 +62,11 @@ class TestTest:
assert -1 == Test._apply_hooks(0, [minus_one])
assert -5 == Test._apply_hooks(0, [minus_one, minus_one, minus_one, minus_one, minus_one])
assert 0 == Test._apply_hooks(0, [plus_one, minus_one, plus_one, minus_one, plus_one, minus_one])
- with config_context(check_leaks=True):
- assert "foo" == Test._apply_hooks("foo", [foo_to_aurevoir])
- assert "bonjour" == Test._apply_hooks("bonjour", [bonjour_to_foo, foo_to_aurevoir])
- assert 0 == Test._apply_hooks(0, [plus_one, plus_one, plus_one, plus_one, plus_one])
- assert 0 == Test._apply_hooks(0, [minus_one, minus_one, minus_one, minus_one, minus_one])
+ monkeypatch.setattr(Config, 'check_leaks', True)
+ assert "foo" == Test._apply_hooks("foo", [foo_to_aurevoir])
+ assert "bonjour" == Test._apply_hooks("bonjour", [bonjour_to_foo, foo_to_aurevoir])
+ assert 0 == Test._apply_hooks(0, [plus_one, plus_one, plus_one, plus_one, plus_one])
+ assert 0 == Test._apply_hooks(0, [minus_one, minus_one, minus_one, minus_one, minus_one])
def test_extended_cmd(self):
assert "" == Test("")._extended_cmd
@@ -79,11 +77,11 @@ class TestTest:
BIN_DIR = Path(__file__).parent / "bin"
- def test_run_echo(self):
- with config_context(minishell_exec_path=self.BIN_DIR / "minishell-echo"):
- r = Test("bonjour").run()
- assert r.actual == CapturedCommand("bonjour\n", 0, [])
- assert r.expected.status != 0
+ def test_run_echo(self, monkeypatch):
+ monkeypatch.setattr(Config, 'minishell_exec_path', self.BIN_DIR / "minishell-echo")
+ r = Test("bonjour").run()
+ assert r.actual == CapturedCommand("bonjour\n", 0, [])
+ assert r.expected.status != 0
@pytest.mark.parametrize(
"cmd",
@@ -110,10 +108,10 @@ class TestTest:
{"FOO": "foo"}
],
)
- def test_run_minishell_is_bash(self, cmd, setup, exports):
- with config_context(minishell_exec_path=Path("/bin/bash")):
- r = Test(cmd).run()
- assert r.passed
+ def test_run_minishell_is_bash(self, monkeypatch, cmd, setup, exports):
+ monkeypatch.setattr(Config, 'minishell_exec_path', Path("/bin/bash"))
+ r = Test(cmd).run()
+ assert r.passed
@pytest.mark.parametrize(
"setup",
@@ -132,32 +130,33 @@ class TestTest:
assert "yes" == e.value._cmd
assert f"Error: `{setup[0]}` setup command failed for `yes`\n\twith '{setup[1]}'" == e.value.__str__()
- def test_run_check_leaks(self):
- with config_context(check_leaks=True, valgrind_cmd=["/bin/bash"]):
- r = Test("echo bonjour").run()
- assert isinstance(r, LeakResult)
- assert "bonjour\n" == r._captured.output
- assert "echo bonjour" == r._cmd
+ def test_run_check_leaks(self, monkeypatch):
+ monkeypatch.setattr(Config, 'check_leaks', True)
+ monkeypatch.setattr(Config, 'valgrind_cmd', ["/bin/bash"])
+ r = Test("echo bonjour").run()
+ assert isinstance(r, LeakResult)
+ assert "bonjour\n" == r._captured.output
+ assert "echo bonjour" == r._cmd
@pytest.mark.parametrize("timeout", [0.05, 0.1, 0.15, 0.2, 0.3])
- def test_run_timeout(self, timeout):
- with config_context(minishell_exec_path=self.BIN_DIR / "minishell-timeout"):
- r = Test("echo bonjour", timeout=timeout).run()
- assert "bonjour\n" == r.expected.output
- assert 0 == r.expected.status
- assert isinstance(r.actual, CapturedTimeout)
+ def test_run_timeout(self, monkeypatch, timeout):
+ monkeypatch.setattr(Config, 'minishell_exec_path', self.BIN_DIR / "minishell-timeout")
+ r = Test("echo bonjour", timeout=timeout).run()
+ assert "bonjour\n" == r.expected.output
+ assert 0 == r.expected.status
+ assert isinstance(r.actual, CapturedTimeout)
@pytest.mark.parametrize("file", ["/dev/random", "/dev/urandom"])
- def test_run_decode_error(self, file):
- with config_context(minishell_exec_path="/bin/bash"):
- r = Test(f"/usr/bin/head -c 100 {file}").run()
- assert r.expected.output.startswith("UNICODE DECODE ERROR: ")
- assert r.actual.output.startswith("UNICODE DECODE ERROR: ")
- assert 0 == r.expected.status
- assert 0 == r.actual.status
-
- def test_run_files(self):
- with config_context(minishell_exec_path=self.BIN_DIR / "minishell-file"):
- r = Test("echo bonjour > bonjour", files=["bonjour", "aurevoir"]).run()
- assert CapturedCommand("", 0, ["bonjour\n", None]) == r.expected
- assert CapturedCommand("", 0, ["bonjour\n", "aurevoir\n"]) == r.actual
+ def test_run_decode_error(self, monkeypatch, file):
+ monkeypatch.setattr(Config, 'minishell_exec_path', Path("/bin/bash"))
+ r = Test(f"/usr/bin/head -c 100 {file}").run()
+ assert r.expected.output.startswith("UNICODE DECODE ERROR: ")
+ assert r.actual.output.startswith("UNICODE DECODE ERROR: ")
+ assert 0 == r.expected.status
+ assert 0 == r.actual.status
+
+ def test_run_files(self, monkeypatch):
+ monkeypatch.setattr(Config, 'minishell_exec_path', self.BIN_DIR / "minishell-file")
+ r = Test("echo bonjour > bonjour", files=["bonjour", "aurevoir"]).run()
+ assert CapturedCommand("", 0, ["bonjour\n", None]) == r.expected
+ assert CapturedCommand("", 0, ["bonjour\n", "aurevoir\n"]) == r.actual
diff --git a/tests/test_config.py b/tests/test_config.py
index b0a9142..deb9761 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/03/03 12:25:29 by cacharle #+# #+# #
-# Updated: 2021/03/03 16:27:07 by cacharle ### ########.fr #
+# Updated: 2021/03/06 10:11:30 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -169,26 +169,25 @@ class TestConfig:
Config.init(["--range", "1", "100"])
assert Config.show_range
- def test_init_valgrind_not_found(self):
- prev = distutils.spawn.find_executable
- distutils.spawn.find_executable = lambda _: None # noqa
+ def test_init_valgrind_not_found(self, monkeypatch):
+ monkeypatch.setattr(distutils.spawn, "find_executable", lambda _: None)
with pytest.raises(ConfigSetupException) as e:
Config.init(["--check-leaks"])
- distutils.spawn.find_executable = prev
assert "Could not find the valgrind command on your system" == e.value.__str__()
- def test_init_term_cols_too_low(self):
- prev = shutil.get_terminal_size
- shutil.get_terminal_size = lambda: os.terminal_size((39, 80)) # noqa
+ def test_init_term_cols_too_low(self, monkeypatch):
+ def mock_get_terminal_size(fallback=None):
+ # has to support fallback since pytest uses this function during the verbose test
+ if fallback is not None:
+ return os.terminal_size(fallback)
+ return os.terminal_size((39, 80))
+ monkeypatch.setattr(shutil, "get_terminal_size", mock_get_terminal_size)
with pytest.raises(ConfigSetupException) as e:
Config.init(["--check-leaks"])
- shutil.get_terminal_size = prev
assert "You're terminal isn't wide enough 40 cols minimum required" == e.value.__str__()
- def test_platform_not_supported(self):
- prev = sys.platform
- sys.platform = 'windows'
+ def test_platform_not_supported(self, monkeypatch):
+ monkeypatch.setattr(sys, "platform", "windows")
with pytest.raises(ConfigSetupException) as e:
Config.init(["--check-leaks"])
- sys.platform = prev
assert "Your platform (windows) is not supported, supported platforms are: linux, darwin" == e.value.__str__()
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 6c7cdb1..2df31d2 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/02/27 20:03:52 by cacharle #+# #+# #
-# Updated: 2021/03/03 09:17:22 by cacharle ### ########.fr #
+# Updated: 2021/03/06 09:56:09 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -26,8 +26,6 @@ from minishell_test.hooks import (
DISCARDED_TEXT,
)
-from tests.helpers import config_context
-
Config.init([])
@@ -65,7 +63,7 @@ _=/usr/bin/env\
""")
-def test_error_line0():
+def test_error_line0(monkeypatch):
assert "" == error_line0("")
assert "foo" == error_line0("foo")
assert "a\nb" == error_line0("a\nb")
@@ -73,8 +71,8 @@ def test_error_line0():
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")
- with config_context(check_error_messages=False):
- assert DISCARDED_TEXT == error_line0(Config.shell_reference_prefix + "-c: bonjour\nfoo\n")
+ monkeypatch.setattr(Config, 'check_error_messages', False)
+ assert DISCARDED_TEXT == error_line0(Config.shell_reference_prefix + "-c: bonjour\nfoo\n")
def test_discard():
@@ -82,7 +80,7 @@ def test_discard():
assert DISCARDED_TEXT == discard("foo")
-def test_export_singleton():
+def test_export_singleton(monkeypatch):
assert "" == export_singleton("declare -x IGOTNUMBERS42")
assert "" == export_singleton("declare -x IGOTUNDERSCORE__")
assert "" == export_singleton("declare -x I")
@@ -109,12 +107,12 @@ declare -x YSU_VERSION="1.7.3"
declare -x ZDOTDIR="/home/cacharle/.config/zsh"\
""")
- with config_context(shell_reference_args=["--posix"]):
- assert "" == export_singleton("export IGOTNUMBERS42")
- assert "" == export_singleton("export IGOTUNDERSCORE__")
- assert "" == export_singleton("export I")
- assert "" == export_singleton("export _")
- assert """\
+ monkeypatch.setattr(Config, 'shell_reference_args', ['--posix'])
+ 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"
@@ -157,32 +155,32 @@ def test_should_not_be():
assert DISCARDED_TEXT == should_not_be("bar")("bonjour")
-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("\\", "")("\\")
-
-
-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")
+def test_platform_status(monkeypatch):
+ monkeypatch.setattr(Config, 'platform', 'darwin')
+ assert 0 == platform_status(0, 1)(0)
+ assert 1 == platform_status(42, 42)(1)
+ monkeypatch.setattr(Config, 'platform', 'linux')
+ assert 0 == platform_status(0, 1)(1)
+ assert 42 == platform_status(0, 1)(42)
+ monkeypatch.setattr(Config, 'platform', 'foo')
+ assert 0 == platform_status(42, 42)(0)
+
+
+def test_linux_replace(monkeypatch):
+ monkeypatch.setattr(Config, '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("\\", "")("\\")
+ monkeypatch.setattr(Config, '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("\\", "")("\\")
+
+
+def test_linux_discard(monkeypatch):
+ monkeypatch.setattr(Config, 'platform', 'darwin')
+ assert "" == linux_discard("")
+ assert "foo" == linux_discard("foo")
+ monkeypatch.setattr(Config, 'platform', 'linux')
+ assert DISCARDED_TEXT == linux_discard("")
+ assert DISCARDED_TEXT == linux_discard("foo")
diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py
index 2536be7..bb2ea98 100644
--- a/tests/test_sandbox.py
+++ b/tests/test_sandbox.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/03/03 08:09:00 by cacharle #+# #+# #
-# Updated: 2021/03/03 09:15:42 by cacharle ### ########.fr #
+# Updated: 2021/03/06 09:58:17 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -14,7 +14,6 @@ import pytest
import shutil
from pathlib import Path
-from tests.helpers import config_context
from minishell_test import sandbox
from minishell_test.config import Config
@@ -31,55 +30,55 @@ def sandbox_dirs():
]]
-def test_create(tmpdir, sandbox_dirs):
+def test_create(tmpdir, monkeypatch, sandbox_dirs):
for sandbox_dir in sandbox_dirs:
sandbox_dir = Path(tmpdir / sandbox_dir)
- with config_context(sandbox_dir=sandbox_dir):
- assert not sandbox_dir.exists()
- sandbox.create()
- assert sandbox_dir.exists()
+ monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
+ assert not sandbox_dir.exists()
+ sandbox.create()
+ assert sandbox_dir.exists()
-def test_remove(tmpdir, sandbox_dirs):
+def test_remove(tmpdir, monkeypatch, sandbox_dirs):
for sandbox_dir in sandbox_dirs:
sandbox_dir = Path(tmpdir / sandbox_dir)
- with config_context(sandbox_dir=sandbox_dir):
- sandbox.create()
- assert sandbox_dir.exists()
- sandbox.remove()
- assert not sandbox_dir.exists()
+ monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
+ sandbox.create()
+ assert sandbox_dir.exists()
+ sandbox.remove()
+ assert not sandbox_dir.exists()
for sandbox_dir in sandbox_dirs:
sandbox_dir = Path(tmpdir / sandbox_dir)
- with config_context(sandbox_dir=sandbox_dir):
- sandbox.create()
- assert sandbox_dir.exists()
- sandbox_dir.chmod(000)
- sandbox.remove()
- assert not sandbox_dir.exists()
+ monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
+ sandbox.create()
+ assert sandbox_dir.exists()
+ sandbox_dir.chmod(000)
+ sandbox.remove()
+ assert not sandbox_dir.exists()
for sandbox_dir in sandbox_dirs:
sandbox_dir = Path(tmpdir / sandbox_dir)
- with config_context(sandbox_dir=sandbox_dir):
- sandbox.create()
- assert sandbox_dir.exists()
- shutil.rmtree(sandbox_dir)
- sandbox.remove()
- assert not sandbox_dir.exists()
+ monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
+ sandbox.create()
+ assert sandbox_dir.exists()
+ shutil.rmtree(sandbox_dir)
+ sandbox.remove()
+ assert not sandbox_dir.exists()
-def test_context(tmpdir, sandbox_dirs):
+def test_context(tmpdir, monkeypatch, sandbox_dirs):
+ for sandbox_dir in sandbox_dirs:
+ sandbox_dir = Path(tmpdir / sandbox_dir)
+ monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
+ assert not sandbox_dir.exists()
+ with sandbox.context():
+ assert sandbox_dir.exists()
+ assert not sandbox_dir.exists()
for sandbox_dir in sandbox_dirs:
sandbox_dir = Path(tmpdir / sandbox_dir)
- with config_context(sandbox_dir=sandbox_dir):
+ monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
+ with pytest.raises(RuntimeError):
assert not sandbox_dir.exists()
with sandbox.context():
assert sandbox_dir.exists()
+ raise RuntimeError
assert not sandbox_dir.exists()
- for sandbox_dir in sandbox_dirs:
- sandbox_dir = Path(tmpdir / sandbox_dir)
- with config_context(sandbox_dir=sandbox_dir):
- with pytest.raises(RuntimeError):
- assert not sandbox_dir.exists()
- with sandbox.context():
- assert sandbox_dir.exists()
- raise RuntimeError
- assert not sandbox_dir.exists()