This repository was archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_envvar.py
70 lines (49 loc) · 2.11 KB
/
test_envvar.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
from configmanager import Section
def test_envvar_attribute_enables_value_override_via_envvars(monkeypatch):
config = Section({
'uploads': {
'threads': 1
}
})
assert config.uploads.threads.envvar is None
assert config.uploads.threads.envvar_name is None
assert config.uploads.threads.value == 1
config.uploads.threads.envvar = True
assert config.uploads.threads.value == 1
monkeypatch.setenv('UPLOADS_THREADS', '23')
assert config.uploads.threads.value == 23
config.uploads.threads.envvar_name = 'OTHER_UPLOADS_THREADS'
assert config.uploads.threads.value == 1
monkeypatch.setenv('OTHER_UPLOADS_THREADS', '42')
assert config.uploads.threads.value == 42
config.uploads.threads.envvar = False
assert config.uploads.threads.value == 1
config.uploads.threads.envvar = 'UPLOADS_THREADS'
assert config.uploads.threads.value == 23
config.uploads.threads.envvar = 'OTHER_UPLOADS_THREADS'
assert config.uploads.threads.value == 42
config.uploads.threads.envvar = 'SOMETHING_NONEXISTENT'
assert config.uploads.threads.value == 1
def test_dynamic_override_of_envvar_name(monkeypatch):
config = Section({
'uploads': {
'threads': 1
}
})
assert config.uploads.threads.envvar_name is None
@config.item_attribute
def envvar_name(item=None, **kwargs):
return 'TEST_{}'.format('_'.join(item.get_path()).upper())
assert config.uploads.threads.envvar_name == 'TEST_UPLOADS_THREADS'
assert config.uploads.threads.value == 1
# This has no immediate effect because envvar is still None
monkeypatch.setenv('TEST_UPLOADS_THREADS', '23')
assert config.uploads.threads.value == 1
# This changes everything
config.uploads.threads.envvar = True
assert config.uploads.threads.value == 23
# But, if envvar is set to a name, envvar_name doesn't matter again
config.uploads.threads.envvar = 'OTHER_UPLOADS_THREADS'
assert config.uploads.threads.value == 1
monkeypatch.setenv('OTHER_UPLOADS_THREADS', '42')
assert config.uploads.threads.value == 42