aboutsummaryrefslogtreecommitdiff
path: root/tests/test_config.py
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-03-03 17:25:46 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-03-03 17:25:46 +0100
commit422c56fbc5cd7493aaa96a341ebfff077f7a7de3 (patch)
tree199c9dbb084b36b6d85774869076484305dc449a /tests/test_config.py
parent820102f9ccdfdfe3ae78c22f803da4db87cb91ba (diff)
downloadminishell_test-422c56fbc5cd7493aaa96a341ebfff077f7a7de3.tar.gz
minishell_test-422c56fbc5cd7493aaa96a341ebfff077f7a7de3.tar.bz2
minishell_test-422c56fbc5cd7493aaa96a341ebfff077f7a7de3.zip
Added config tests
Diffstat (limited to 'tests/test_config.py')
-rw-r--r--tests/test_config.py194
1 files changed, 194 insertions, 0 deletions
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644
index 0000000..b0a9142
--- /dev/null
+++ b/tests/test_config.py
@@ -0,0 +1,194 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# test_config.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2021/03/03 12:25:29 by cacharle #+# #+# #
+# Updated: 2021/03/03 16:27:07 by cacharle ### ########.fr #
+# #
+# ############################################################################ #
+
+import os
+import sys
+import pytest
+from pathlib import Path
+import contextlib
+import distutils.spawn
+import shutil
+
+from minishell_test import config
+from minishell_test.config import (
+ Config,
+ ConfigSetupException,
+ ConfigParser,
+ ConfigParserException,
+)
+
+
+Config.init([])
+
+
+class TestConfigParser:
+ @pytest.fixture
+ def config(self):
+ c = ConfigParser()
+ c.read_dict({
+ "general": {
+ "somepath": "bonjour/je/suis",
+ "someargs": "bonjour je suis",
+ "someargs_spaces": "bonjour je suis",
+ "someargs_around": " bonjour je suis ",
+ "someargs_spaces_only": " ",
+ "someargs_none": "",
+ "somemultiline": "bonjour\nje\nsuis",
+ "somemultiline_around": "\n\n bonjour\nje\nsuis\n\n",
+ "somemultiline_crlf": "bonjour\r\nje\r\nsuis",
+ "somemultiline_sep_only": "\n\n \n\n",
+ "somemultiline_none": "",
+ }
+ })
+ return c
+
+ def test_getpath(self, config):
+ p = config.getpath("general", "somepath")
+ assert isinstance(p, Path)
+ assert Path("bonjour/je/suis").resolve() == p
+
+ def test_getargs(self, config):
+ assert ["bonjour", "je", "suis"] == config.getargs("general", "someargs")
+ assert ["bonjour", "je", "suis"] == config.getargs("general", "someargs_spaces")
+ assert ["bonjour", "je", "suis"] == config.getargs("general", "someargs_around")
+ assert [] == config.getargs("general", "someargs_spaces_only")
+ assert [] == config.getargs("general", "someargs_none")
+
+ def test_getmultiline(self, config):
+ assert ["bonjour", "je", "suis"] == config.getmultiline("general", "somemultiline")
+ assert ["bonjour", "je", "suis"] == config.getmultiline("general", "somemultiline_around")
+ assert ["bonjour", "je", "suis"] == config.getmultiline("general", "somemultiline_crlf")
+ assert [] == config.getmultiline("general", "somemultiline_sep_only")
+ assert [] == config.getmultiline("general", "somemultiline_none")
+
+
+class TestConfig:
+ # @pytest.fixture
+ # def default_config(self):
+ # c = ConfigParser()
+ # c.read(config.DATA_DIR / 'default.cfg')
+ # return c
+ #
+ # @pytest.mark.parametrize(
+ # "config_dict",
+ # [
+ # {"minishell_test": {"bonus": True}},
+ # {"minishell_test": {"bonus": True}},
+ # ]
+ # )
+ # def test_load_cfg_defaults(self, tmpdir, config_dict, default_config):
+ # c = ConfigParser()
+ # c.read_dict(config_dict)
+ # for suite in default_config:
+ # for key in default_config:
+
+
+
+ @pytest.mark.parametrize(
+ "file_content",
+ [
+ ("foo", "[{}]\nbonus = true"),
+ ("minishell_test ", "[{}]\nbonus = true"),
+ (" minishell_test", "[{}]\nbonus = true"),
+ (" minishell_test ", "[{}]\nbonus = true"),
+ ("minishell_tes", "[{}]\nbonus = true"),
+ ("inishell_test", "[{}]\nbonus = true"),
+ ("shell_", "[{}]\nbonus = true"),
+ ("she ll", "[{}]\nbonus = true"),
+ ("shell:reference ", "[{}]\nbonus = true"),
+ ("timout", "[{}]\nbonus = true"),
+ ]
+ )
+ def test_load_cfg_unknown_section(self, tmpdir, file_content):
+ with open(tmpdir / config.CONFIG_FILENAME, "w") as file:
+ file.write(file_content[1].format(file_content[0]))
+ with pytest.raises(ConfigParserException) as e:
+ Config.init(["--path", str(tmpdir)])
+ assert file_content[0] == e.value._section
+ assert e.value._key is None
+ assert f"Configuration parsing error: unknown section name: {file_content[0]}" == e.value.__str__()
+
+ @pytest.mark.parametrize(
+ "file_content",
+ [
+ ("minishell_test", "bonu", "[{}]\n{} = true"),
+ ("minishell_test", "exec name", "[{}]\n{} = hello"),
+ ("minishell_test", "log_pat", "[{}]\n{} = a/b/c"),
+ ("minishell_test", "ager_prog", "[{}]\n{} = a/b/c"),
+ ("shell", "available_command", "[{}]\n{} = a/b/c"),
+ ("shell:reference", "pa", "[{}]\n{} = /bin/sh"),
+ ]
+ )
+ def test_load_cfg_unknown_section_key(self, tmpdir, file_content):
+ with open(tmpdir / config.CONFIG_FILENAME, "w") as file:
+ file.write(file_content[2].format(file_content[0], file_content[1]))
+ with pytest.raises(ConfigParserException) as e:
+ Config.init(["--path", str(tmpdir)])
+ assert file_content[0] == e.value._section
+ assert file_content[1] == e.value._key
+ assert f"Configuration parsing error: unknown key name: {file_content[1]} in {file_content[0]}" == e.value.__str__()
+
+ def test_init_cache_dir_xdg_cache_home(self, monkeypatch):
+ monkeypatch.setenv("XDG_CACHE_HOME", "bonjour")
+ Config.init([])
+ assert Path("bonjour", "minishell_test") == Config.cache_dir
+
+ def test_init_cache_dir_xdg_cache_home_missing(self, monkeypatch):
+ monkeypatch.delenv("XDG_CACHE_HOME")
+ Config.init([])
+ assert Path("~", ".cache", "minishell_test").expanduser() == Config.cache_dir
+
+ def test_init_cache_dir_home(self, monkeypatch):
+ monkeypatch.delenv("XDG_CACHE_HOME")
+ monkeypatch.setenv("HOME", "bonjour")
+ Config.init([])
+ assert Path("bonjour", ".cache", "minishell_test") == Config.cache_dir
+
+ def test_init_cache_dir_home_missing(self, monkeypatch):
+ monkeypatch.delenv("XDG_CACHE_HOME")
+ monkeypatch.delenv("HOME")
+ Config.init([])
+ assert Path(".cache", "minishell_test") == Config.cache_dir
+
+ def test_init_show_range(self):
+ Config.init([])
+ assert not Config.show_range
+ Config.init(["--show-range"])
+ assert Config.show_range
+ Config.init(["--check-leaks"])
+ assert Config.show_range
+ 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
+ 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
+ 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'
+ 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__()