1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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__()
|