-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtests.py
143 lines (107 loc) · 3.75 KB
/
tests.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
import glob
import os
import shutil
from unittest.mock import patch
import pytest
import settings
settings_dir = "fortest/server1"
default_config = {"config": "default"}
dev_config = {"config": "dev"}
prod_config = {"config": "prod"}
site_config = {"config": "site"}
repo_dir = "/tmp/settings-repo"
git_settings_subdir = repo_dir + "/myapp1"
def setup_module():
cmds = [
"mkdir -p %s" % git_settings_subdir,
"git init %s" % repo_dir,
'echo "PROD = True" > %s/prod_settings.py' % git_settings_subdir,
'echo "PROD = False" > %s/dev_settings.py' % git_settings_subdir,
]
for cmd in cmds:
ret = os.system(cmd)
if ret != 0:
raise Exception("failed: %s" % cmd)
def create_config_lines(config):
lines = []
for kv in config.items():
lines.append('%s = "%s"' % kv)
return lines
def create_config_file(path, config):
open(path, 'w').writelines(create_config_lines(config))
def test_no_settings_dir():
settings_file = "settings/default_settings.py"
try:
assert settings.get("config") is None, settings.get("config")
create_config_file(settings_file, default_config)
settings.reload()
assert settings.get("config") == "default", settings.get("config")
finally:
os.remove(settings_file)
@patch.dict(os.environ, {"SETTINGS_DIR": settings_dir, "APP_MODE": "dev"}, clear=True)
def test_rc():
os.makedirs(settings_dir)
open(os.path.join(settings_dir, "__init__.py"), "w").close()
open(os.path.join(settings_dir, "../", "__init__.py"), "w").close()
config_path = os.path.join(settings_dir, "default_settings.py")
create_config_file(config_path, default_config)
settings.reload()
assert settings.config == "default"
config_path = os.path.join(settings_dir, "dev_settings.py")
create_config_file(config_path, dev_config)
settings.reload()
assert settings.config == "dev"
config_path = os.path.join(settings_dir, "prod_settings.py")
create_config_file(config_path, prod_config)
settings.reload()
assert settings.config == "dev"
config_path = os.path.join(settings_dir, "site_settings.py")
create_config_file(config_path, site_config)
settings.reload()
assert settings.config == "site"
def test_backward_compatibility():
from converge import settings
def test_env_vars():
config = {"SETTINGS_DIR": "settings"}
os.environ["SETTINGS_DIR"] = "settings/site1"
settings.parse_osenv(config)
assert config["SETTINGS_DIR"] == os.environ["SETTINGS_DIR"]
os.environ["SETTINGS_DIR"] = "settings/site2"
settings.parse_osenv(config)
assert config["SETTINGS_DIR"] == os.environ["SETTINGS_DIR"]
@patch.dict(
os.environ,
{
"SETTINGS_DIR": settings_dir,
"APP_MODE": "prod",
"GIT_SETTINGS_REPO": repo_dir,
"GIT_SETTINGS_SUBDIR": git_settings_subdir,
"PATH": os.environ["PATH"],
},
clear=True,
)
def test_git_settings():
settings.reload()
assert settings.PROD is True
def test_rc_file_deprecated():
convergerc = ".convergerc"
open(convergerc, "w").write("")
try:
with pytest.raises(Exception):
settings.reload()
finally:
os.remove(convergerc)
def test_ensure_settings_dir():
shutil.rmtree(settings_dir)
with pytest.raises(Exception, match="no such directory"):
settings.reload()
def teardown_module():
py_path = "default_settings.py"
pyc_path = py_path + "c"
for path in (py_path, pyc_path):
if os.path.exists(path):
os.remove(path)
if glob.glob(os.path.join(settings_dir, "__init__.py")): # playing safe
shutil.rmtree(settings_dir)
if repo_dir.startswith("/tmp"): # playing safe
shutil.rmtree(repo_dir)