forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuxiliaryTest.py
More file actions
129 lines (106 loc) · 5.79 KB
/
Copy pathAuxiliaryTest.py
File metadata and controls
129 lines (106 loc) · 5.79 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
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
#
# Copyright (C) 2012-2020 Euclid Science Ground Segment
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
:data: Apr 29, 2016
:author: Hubert Degaudenzi
"""
import os
import unittest
from ElementsKernel.Temporary import TempDir, TempEnv
from ElementsKernel.Auxiliary import configure, getAuxiliaryPath, getPath
class AuxiliaryTest(unittest.TestCase):
""" TestCast for the Auxiliary feature """
class TestFile(object):
def __init__(self, path, content=""):
self.setPath(path)
self.setContent(content)
def setPath(self, path):
self._path = path
def path(self):
return self._path
def setContent(self, content):
self._content = content
if self._content:
parent_path = os.path.dirname(self._path)
if not os.path.exists(parent_path):
os.makedirs(parent_path)
with open(self._path, "w") as f:
f.write(self._content)
def content(self):
return self._content
def setUp(self):
unittest.TestCase.setUp(self)
self._tmpdir = TempDir(suffix="aux_tempdir")
self._tmpenv = TempEnv()
self._exiting_dir = os.path.join(self._tmpdir.path(), "ThisDir")
os.mkdir(self._exiting_dir)
self._tmpenv["ELEMENTS_AUX_PATH"] = self._tmpdir.path()
self._test_files = []
self._test_files.append(AuxiliaryTest.TestFile(os.path.join(self._tmpdir.path(),
"file1"),
"That content no replacement"))
self._test_files.append(AuxiliaryTest.TestFile(os.path.join(self._tmpdir.path(),
"tata", "file1"),
"This content no replacement"))
self._test_files.append(AuxiliaryTest.TestFile(os.path.join(self._tmpdir.path(),
"tata", "tutu", "file1"),
"This content no replacement"))
self._test_files.append(AuxiliaryTest.TestFile(os.path.join(self._tmpdir.path(),
"file2"),
"That content %(bla)s %(blu)s"))
self._test_files.append(AuxiliaryTest.TestFile(os.path.join(self._tmpdir.path(),
"tata", "file2"),
"This content %(bla)s %(blu)s"))
self._test_files.append(AuxiliaryTest.TestFile(os.path.join(self._tmpdir.path(),
"tata", "tutu", "file2"),
"This content %(bla)s %(blu)s"))
def tearDown(self):
unittest.TestCase.tearDown(self)
del self._tmpenv
del self._tmpdir
def testAuxPathEnv(self):
self.assertEqual(self._tmpenv["ELEMENTS_AUX_PATH"], self._tmpdir.path())
self.assertEqual(getAuxiliaryPath("file1"), os.path.join(self._tmpdir.path(), "file1"))
self.assertEqual(getAuxiliaryPath("tata/file1"), os.path.join(self._tmpdir.path(), "tata", "file1"))
self.assertEqual(getAuxiliaryPath("tata/tutu/file1"),
os.path.join(self._tmpdir.path(), "tata", "tutu", "file1"))
self.assertEqual(getAuxiliaryPath("file2"),
os.path.join(self._tmpdir.path(), "file2"))
self.assertEqual(getAuxiliaryPath("tata/file2"), os.path.join(self._tmpdir.path(), "tata", "file2"))
self.assertEqual(getAuxiliaryPath("tata/tutu/file2"),
os.path.join(self._tmpdir.path(), "tata", "tutu", "file2"))
def testAuxShortPathEnv(self):
self.assertEqual(self._tmpenv["ELEMENTS_AUX_PATH"], self._tmpdir.path())
self.assertEqual(getPath("file1"), os.path.join(self._tmpdir.path(), "file1"))
self.assertEqual(getPath("tata/file1"), os.path.join(self._tmpdir.path(), "tata", "file1"))
self.assertEqual(getPath("tata/tutu/file1"),
os.path.join(self._tmpdir.path(), "tata", "tutu", "file1"))
self.assertEqual(getPath("file2"),
os.path.join(self._tmpdir.path(), "file2"))
self.assertEqual(getPath("tata/file2"), os.path.join(self._tmpdir.path(), "tata", "file2"))
self.assertEqual(getPath("tata/tutu/file2"),
os.path.join(self._tmpdir.path(), "tata", "tutu", "file2"))
def testConfigure(self):
configure("file1", self._exiting_dir)
self.assertTrue(os.path.exists(os.path.join(self._exiting_dir, "file1")))
configure("file2", self._exiting_dir, configuration={"bla":"foo", "blu":"bar"})
target_file = os.path.join(self._exiting_dir, "file2")
self.assertTrue(os.path.exists(target_file))
self.assertEqual(open(target_file).read(), "That content foo bar")
if __name__ == "__main__":
unittest.main()