aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-03-03 09:19:20 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-03-03 09:19:20 +0100
commit716265929bf861d340c1e71e2f24359875520d3a (patch)
treed6e536e42de09d7a908c559aa5b51d36936a4e5b
parenta90ac1b352bd5f7c2c677f051365401531229976 (diff)
downloadminishell_test-716265929bf861d340c1e71e2f24359875520d3a.tar.gz
minishell_test-716265929bf861d340c1e71e2f24359875520d3a.tar.bz2
minishell_test-716265929bf861d340c1e71e2f24359875520d3a.zip
Added sandbox and colors tests
-rw-r--r--minishell_test/colors.py8
-rw-r--r--minishell_test/sandbox.py10
-rw-r--r--tests/test_colors.py67
-rw-r--r--tests/test_hooks.py4
-rw-r--r--tests/test_sandbox.py85
5 files changed, 167 insertions, 7 deletions
diff --git a/minishell_test/colors.py b/minishell_test/colors.py
index 7b6cf45..f0c97fb 100644
--- a/minishell_test/colors.py
+++ b/minishell_test/colors.py
@@ -23,6 +23,14 @@ def disable() -> None:
_COLORS["close"] = ""
+def enable() -> None:
+ _COLORS["red"] = _DEFAULTS["red"]
+ _COLORS["green"] = _DEFAULTS["green"]
+ _COLORS["blue"] = _DEFAULTS["blue"]
+ _COLORS["bold"] = _DEFAULTS["bold"]
+ _COLORS["close"] = _DEFAULTS["close"]
+
+
def green(s: str) -> str:
return _COLORS["green"] + s + _COLORS["close"]
diff --git a/minishell_test/sandbox.py b/minishell_test/sandbox.py
index d579847..6fafc36 100644
--- a/minishell_test/sandbox.py
+++ b/minishell_test/sandbox.py
@@ -6,23 +6,21 @@
# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/09/11 13:48:07 by charles #+# #+# #
-# Updated: 2021/02/27 12:32:17 by cacharle ### ########.fr #
+# Updated: 2021/03/03 09:15:11 by cacharle ### ########.fr #
# #
# ############################################################################ #
import shutil
import subprocess
from contextlib import contextmanager
+from pathlib import Path
from minishell_test.config import Config
def create():
"""Create a new sandbox directory"""
- try:
- Config.sandbox_dir.mkdir(parents=True, exist_ok=True)
- except OSError:
- pass
+ Config.sandbox_dir.mkdir(parents=True, exist_ok=True)
def remove():
@@ -32,7 +30,7 @@ def remove():
try:
shutil.rmtree(Config.sandbox_dir)
except PermissionError:
- subprocess.run(["chmod", "777", *Config.sandbox_dir.glob("*")], check=True)
+ subprocess.run(["chmod", "-R", "777", Config.sandbox_dir], check=True)
shutil.rmtree(Config.sandbox_dir)
except FileNotFoundError:
pass
diff --git a/tests/test_colors.py b/tests/test_colors.py
new file mode 100644
index 0000000..2edcf27
--- /dev/null
+++ b/tests/test_colors.py
@@ -0,0 +1,67 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# test_colors.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2021/03/03 07:56:43 by cacharle #+# #+# #
+# Updated: 2021/03/03 08:07:46 by cacharle ### ########.fr #
+# #
+# ############################################################################ #
+
+import pytest
+
+from minishell_test import colors
+
+
+
+
+@pytest.fixture
+def texts():
+ return [
+ "",
+ "foo",
+ "aslkdfj;asdkflaskdjfalskdjflasdf",
+ "\n\n\n\n",
+ "\t\t\t\t",
+ ]
+
+
+def test_green(texts):
+ colors.enable()
+ for text in texts:
+ assert colors._DEFAULTS["green"] + text + colors._DEFAULTS["close"] == colors.green(text)
+
+
+def test_red(texts):
+ colors.enable()
+ for text in texts:
+ assert colors._DEFAULTS["red"] + text + colors._DEFAULTS["close"] == colors.red(text)
+
+
+def test_blue(texts):
+ colors.enable()
+ for text in texts:
+ assert colors._DEFAULTS["blue"] + text + colors._DEFAULTS["close"] == colors.blue(text)
+
+
+def test_bold(texts):
+ colors.enable()
+ for text in texts:
+ assert colors._DEFAULTS["bold"] + text + colors._DEFAULTS["close"] == colors.bold(text)
+
+def test_toggling(texts):
+ colors.disable()
+ for text in texts:
+ assert text == colors.green(text)
+ assert text == colors.red(text)
+ assert text == colors.blue(text)
+ assert text == colors.bold(text)
+ colors.enable()
+ for text in texts:
+ assert text != colors.green(text)
+ assert text != colors.red(text)
+ assert text != colors.blue(text)
+ assert text != colors.bold(text)
+ colors.disable()
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 2593b6e..6c7cdb1 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/02 17:45:15 by cacharle ### ########.fr #
+# Updated: 2021/03/03 09:17:22 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -73,6 +73,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")
def test_discard():
diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py
new file mode 100644
index 0000000..2536be7
--- /dev/null
+++ b/tests/test_sandbox.py
@@ -0,0 +1,85 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# test_sandbox.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2021/03/03 08:09:00 by cacharle #+# #+# #
+# Updated: 2021/03/03 09:15:42 by cacharle ### ########.fr #
+# #
+# ############################################################################ #
+
+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
+
+Config.init([])
+
+
+@pytest.fixture
+def sandbox_dirs():
+ return [Path(s) for s in [
+ "foo",
+ "asdfasdfas;dlkfjas;dkfjas;lkdfj",
+ "z/y",
+ "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",
+ ]]
+
+
+def test_create(tmpdir, 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()
+
+
+def test_remove(tmpdir, 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()
+ 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()
+ 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()
+
+
+def test_context(tmpdir, 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()
+ 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):
+ with pytest.raises(RuntimeError):
+ assert not sandbox_dir.exists()
+ with sandbox.context():
+ assert sandbox_dir.exists()
+ raise RuntimeError
+ assert not sandbox_dir.exists()