-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
test_config.py
150 lines (126 loc) · 5.5 KB
/
test_config.py
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
import os
import sys
from pathlib import Path
from unittest import mock
import pytest
from praw.config import Config
from praw.exceptions import ClientException
class TestConfig:
@staticmethod
def _assert_config_read(environment, mock_config):
mock_instance = mock_config.return_value
Config.CONFIG = None # Force config file reload
prev_environment = {environment: None}
for env_name in ["APPDATA", "HOME", "XDG_CONFIG_HOME"]:
if env_name in os.environ:
prev_environment[env_name] = os.environ[env_name]
del os.environ[env_name]
os.environ[environment] = "/MOCK"
module_dir = Path(sys.modules["praw"].__file__).parent
environ_path = (
Path("/MOCK") / (".config" if environment == "HOME" else "") / "praw.ini"
)
locations = [
str(module_dir / "praw.ini"),
str(environ_path),
"praw.ini",
]
try:
Config._load_config()
mock_instance.read.assert_called_with(locations)
finally:
Config.CONFIG = None # Force config file reload
for env_name in prev_environment:
if prev_environment[env_name] is None:
del os.environ[env_name]
else:
os.environ[env_name] = prev_environment[env_name]
def test_check_for_updates__false(self):
for value in [False, "False", "other"]:
config = Config("DEFAULT", check_for_updates=value)
assert config.check_for_updates is False
def test_check_for_updates__true(self):
for value in [True, "1", "true", "YES", "on"]:
config = Config("DEFAULT", check_for_updates=value)
assert config.check_for_updates is True
def test_custom__extra_values_set(self):
config = Config("DEFAULT", user1="foo", user2="bar")
assert config.custom == {"user1": "foo", "user2": "bar"}
def test_custom__no_extra_values_set(self):
config = Config("DEFAULT")
assert config.custom == {}
@mock.patch("configparser.ConfigParser")
def test_load_ini_from_appdata(self, mock_config):
self._assert_config_read("APPDATA", mock_config)
@mock.patch("configparser.ConfigParser")
def test_load_ini_from_home(self, mock_config):
self._assert_config_read("HOME", mock_config)
@mock.patch("configparser.ConfigParser")
def test_load_ini_from_xdg_config_home(self, mock_config):
self._assert_config_read("XDG_CONFIG_HOME", mock_config)
@mock.patch("configparser.ConfigParser")
def test_load_ini_with_no_config_directory(self, mock_config):
mock_instance = mock_config.return_value
Config.CONFIG = None # Force config file reload
prev_environment = {}
for key in ["APPDATA", "HOME", "XDG_CONFIG_HOME"]:
if key in os.environ:
prev_environment[key] = os.environ[key]
del os.environ[key]
module_dir = os.path.dirname(sys.modules["praw"].__file__)
locations = [os.path.join(module_dir, "praw.ini"), "praw.ini"]
try:
Config._load_config()
mock_instance.read.assert_called_with(locations)
finally:
Config.CONFIG = None # Force config file reload
for key, value in prev_environment.items():
os.environ[key] = value
def test_short_url(self):
config = Config("DEFAULT")
assert config.short_url == "https://redd.it"
def test_short_url_not_defined(self):
config = Config("DEFAULT", short_url=None)
with pytest.raises(ClientException) as excinfo:
config.short_url
assert str(excinfo.value) == "No short domain specified."
def test_unset_value_has_useful_string_representation(self):
config = Config("DEFAULT", password=Config.CONFIG_NOT_SET)
assert str(config.password) == "NotSet"
class TestConfigInterpolation:
def test_basic_interpolation(self):
Config.CONFIG = None # Force config file reload
with mock.patch.dict(
"os.environ",
{
"APPDATA": os.path.dirname(__file__),
"XDG_CONFIG_HOME": os.path.dirname(__file__),
},
):
config = Config("INTERPOLATION", config_interpolation="basic")
assert config.custom["basic_interpolation"] == config.reddit_url
assert config.custom["extended_interpolation"] == "${reddit_url}"
def test_extended_interpolation(self):
Config.CONFIG = None # Force config file reload
with mock.patch.dict(
"os.environ",
{
"APPDATA": os.path.dirname(__file__),
"XDG_CONFIG_HOME": os.path.dirname(__file__),
},
):
config = Config("INTERPOLATION", config_interpolation="extended")
assert config.custom["basic_interpolation"] == "%(reddit_url)s"
assert config.custom["extended_interpolation"] == config.reddit_url
def test_no_interpolation(self):
Config.CONFIG = None # Force config file reload
with mock.patch.dict(
"os.environ",
{
"APPDATA": os.path.dirname(__file__),
"XDG_CONFIG_HOME": os.path.dirname(__file__),
},
):
config = Config("INTERPOLATION")
assert config.custom["basic_interpolation"] == "%(reddit_url)s"
assert config.custom["extended_interpolation"] == "${reddit_url}"