1
|
|
|
# |
2
|
|
|
# Copyright (C) 2018 Satoru SATOH <ssato @ redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
# pylint: disable=missing-docstring, invalid-name |
6
|
|
|
import os.path |
7
|
|
|
import unittest |
8
|
|
|
|
9
|
|
|
import anyconfig.backend.ini |
10
|
|
|
import anyconfig.backend.json |
11
|
|
|
import anyconfig.compat |
12
|
|
|
import anyconfig.ioinfo as TT |
13
|
|
|
import anyconfig.utils |
14
|
|
|
|
15
|
|
|
from anyconfig.backends import ( |
16
|
|
|
_PARSERS_BY_EXT as CPS_BY_EXT, |
17
|
|
|
_PARSERS_BY_TYPE as CPS_BY_TYPE |
18
|
|
|
) |
19
|
|
|
from anyconfig.globals import ( |
20
|
|
|
IOI_PATH_STR, IOI_PATH_OBJ, IOI_STREAM, |
21
|
|
|
UnknownParserTypeError, UnknownFileTypeError |
22
|
|
|
) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
IPATH_0 = os.path.join(os.path.dirname(__file__), "00-cnf.json") |
26
|
|
|
IPATH_0_FULL = anyconfig.utils.normpath(IPATH_0) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class Test_30_inspect_io_obj(unittest.TestCase): |
30
|
|
|
|
31
|
|
|
def test_10_path_str(self): |
32
|
|
|
self.assertEqual(TT.inspect_io_obj(IPATH_0), |
33
|
|
|
(IOI_PATH_STR, IPATH_0_FULL, open)) |
34
|
|
|
|
35
|
|
|
def test_20_stream(self): |
36
|
|
|
self.assertEqual(TT.inspect_io_obj(open(IPATH_0)), |
37
|
|
|
(IOI_STREAM, IPATH_0_FULL, |
38
|
|
|
anyconfig.utils.noop)) |
39
|
|
|
|
40
|
|
|
def test_30_path_obj(self): |
41
|
|
|
if anyconfig.compat.pathlib is None: |
42
|
|
|
return |
43
|
|
|
|
44
|
|
|
ipo = anyconfig.compat.pathlib.Path(IPATH_0) |
45
|
|
|
self.assertEqual(TT.inspect_io_obj(ipo), |
46
|
|
|
(IOI_PATH_OBJ, IPATH_0_FULL, ipo.open)) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
class Test_50_find_processor(unittest.TestCase): |
50
|
|
|
cpss = (CPS_BY_EXT, CPS_BY_TYPE) |
51
|
|
|
(ipath, ipath_full) = (IPATH_0, IPATH_0_FULL) |
52
|
|
|
|
53
|
|
|
def __init__(self, *args, **kwargs): |
54
|
|
|
super(Test_50_find_processor, self).__init__(*args, **kwargs) |
55
|
|
|
self.fun = TT.find_processor |
56
|
|
|
|
57
|
|
|
def __checks_helper(self, psr, pcls): |
58
|
|
|
self.assertTrue(isinstance(psr, pcls)) |
59
|
|
|
|
60
|
|
|
def test_10__ng_cases(self): |
61
|
|
|
with self.assertRaises(ValueError): |
62
|
|
|
self.fun(None, CPS_BY_EXT, CPS_BY_TYPE) |
63
|
|
|
|
64
|
|
|
with self.assertRaises(UnknownParserTypeError): |
65
|
|
|
self.fun(None, CPS_BY_EXT, CPS_BY_TYPE, |
66
|
|
|
forced_type="type_not_exist") |
67
|
|
|
with self.assertRaises(UnknownFileTypeError): |
68
|
|
|
self.fun("cnf.unknown_ext", *self.cpss) |
69
|
|
|
|
70
|
|
|
def test_20__forced_type(self): |
71
|
|
|
res = self.fun(None, CPS_BY_EXT, CPS_BY_TYPE, forced_type="ini") |
72
|
|
|
self.__checks_helper(res, anyconfig.backend.ini.Parser) |
73
|
|
|
|
74
|
|
|
def test_30__by_fileext(self): |
75
|
|
|
res = self.fun(self.ipath, *self.cpss) |
76
|
|
|
self.__checks_helper(res, anyconfig.backend.json.Parser) |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class Test_60_make(Test_50_find_processor): |
80
|
|
|
|
81
|
|
|
def __init__(self, *args, **kwargs): |
82
|
|
|
super(Test_60_make, self).__init__(*args, **kwargs) |
83
|
|
|
self.fun = TT.make |
84
|
|
|
|
85
|
|
|
def __checks_helper(self, inp, *args): |
86
|
|
|
self.assertEqual(inp.src, args[0]) |
87
|
|
|
self.assertEqual(inp.path, args[1]) |
88
|
|
|
self.assertEqual(inp.type, args[2]) |
89
|
|
|
self.assertTrue(isinstance(inp.processor, args[3])) |
90
|
|
|
self.assertEqual(inp.opener, args[4]) |
91
|
|
|
|
92
|
|
|
def test_20__forced_type(self): |
93
|
|
|
res = self.fun(None, CPS_BY_EXT, CPS_BY_TYPE, forced_type="ini") |
94
|
|
|
self.__checks_helper(res, None, None, None, |
95
|
|
|
anyconfig.backend.ini.Parser, |
96
|
|
|
anyconfig.utils.noop) |
97
|
|
|
|
98
|
|
|
def test_30__by_fileext(self): |
99
|
|
|
res = self.fun(self.ipath, *self.cpss) |
100
|
|
|
self.__checks_helper(res, self.ipath, self.ipath_full, IOI_PATH_STR, |
101
|
|
|
anyconfig.backend.json.Parser, open) |
102
|
|
|
|
103
|
|
|
def test_40__pathlib(self): |
104
|
|
|
ipath = self.ipath |
105
|
|
|
if anyconfig.compat.pathlib is not None: |
106
|
|
|
# Replace w/ pathlib.Path object. |
107
|
|
|
ipath = anyconfig.compat.pathlib.Path(ipath) |
108
|
|
|
itype = IOI_PATH_OBJ |
109
|
|
|
opener = ipath.open |
110
|
|
|
else: |
111
|
|
|
itype = IOI_PATH_STR |
112
|
|
|
opener = open |
113
|
|
|
|
114
|
|
|
res = self.fun(ipath, *self.cpss) |
115
|
|
|
self.__checks_helper(res, ipath, self.ipath_full, itype, |
116
|
|
|
anyconfig.backend.json.Parser, opener) |
117
|
|
|
|
118
|
|
|
def test_50__stream(self): |
119
|
|
|
ifo = open(self.ipath) |
120
|
|
|
res = self.fun(ifo, *self.cpss) |
121
|
|
|
self.__checks_helper(res, ifo, self.ipath_full, IOI_STREAM, |
122
|
|
|
anyconfig.backend.json.Parser, |
123
|
|
|
anyconfig.utils.noop) |
124
|
|
|
|
125
|
|
|
# vim:sw=4:ts=4:et: |
126
|
|
|
|