-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfigutils_tests.py
More file actions
60 lines (52 loc) · 1.55 KB
/
Copy pathconfigutils_tests.py
File metadata and controls
60 lines (52 loc) · 1.55 KB
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
#!/usr/bin/python
#
# Self tests for cs.configutils.
# - Cameron Simpson <cs@cskk.id.au>
#
from copy import deepcopy
from os.path import basename, dirname, join as joinpath
import sys
from shutil import copy, rmtree
from tempfile import mkdtemp
from time import sleep
import unittest
from cs.configutils import ConfigWatcher, ConfigSectionWatcher
testdatadir = joinpath(dirname(__file__), 'testdata', 'cs.configutils')
test_config_file = joinpath(testdatadir, 'test.ini')
class TestConfigUtils(unittest.TestCase):
''' Test `cs.configutils`.
'''
def setUp(self):
self.tmpdir = mkdtemp()
self.tmpfile = joinpath(self.tmpdir, basename(test_config_file))
copy(test_config_file, self.tmpfile)
self.state0 = {
'clause1': {
'clause1_value1': '1'
},
'clause2': {
'clause2_value1': '2'
},
}
def tearDown(self):
rmtree(self.tmpdir)
def testWatcher(self):
expected1 = self.state0
expected2 = deepcopy(expected1)
expected2['clause2']['clause2_value2'] = '3'
cfg = ConfigWatcher(self.tmpfile)
state1 = cfg.as_dict()
self.assertEqual(state1, expected1)
with open(self.tmpfile, "a") as fp:
fp.write("clause2_value2 = 3\n")
# too early to repoll, get cached content
state2 = cfg.as_dict()
self.assertEqual(state2, expected1)
# later - should reload new content
sleep(2)
state3 = cfg.as_dict()
self.assertEqual(state3, expected2)
def selftest(argv):
unittest.main(__name__, None, argv)
if __name__ == '__main__':
selftest(sys.argv)