forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBoostrapTest.py
More file actions
95 lines (71 loc) · 2.75 KB
/
Copy pathBoostrapTest.py
File metadata and controls
95 lines (71 loc) · 2.75 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
#
# 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
#
'''
:date: July 5th, 2016
:author: Hubert Degaudenzi
'''
import unittest
import os, sys
from importlib import reload
from ElementsKernel.Temporary import TempEnv
from ElementsKernel import Logging
LOGGER = Logging.getLogger(__name__)
def _updateSysPath(extra_list):
""" function to update the sys.path with the extra_list
The list has to be placed after the PYTHONPATH entries.
"""
if extra_list:
os_env_str = os.environ.get("PYTHONPATH", None)
if os_env_str:
sys_path_str = os.pathsep.join(sys.path)
found_idx = sys_path_str.find(os_env_str)
size_of_env = len(os_env_str)
new_sys_path_str = sys_path_str[:found_idx + size_of_env]
new_sys_path_str += os.pathsep + os.pathsep.join(extra_list)
new_sys_path_str += sys_path_str[found_idx + size_of_env:]
sys.path = new_sys_path_str.split(os.pathsep)
else:
sys.path = extra_list + sys.path
class BoostrapCase(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
self._py_path_name = "PYTHONPATH"
self._extra_list = ["/tmp/toto", "/tmp/tata"]
self._pyth_path_list = ["/tmp/foo", "/tmp/bar"]
def tearDown(self):
unittest.TestCase.tearDown(self)
def testNoPythonPathUpdate(self):
# create temporary environment
e = TempEnv()
# ensure that there is no PYTHONPATH
# in the environment.
if self._py_path_name in e:
del e[self._py_path_name]
# ensure that sys.path is updated with the
# new environment
try:
import site
reload(site)
except ImportError as e:
LOGGER.error("Cannot reload the %s module", e.name)
orig_sys_path = sys.path
_updateSysPath(self._extra_list)
new_sys_path = sys.path
self.assertEqual(self._extra_list + orig_sys_path, new_sys_path)
if __name__ == '__main__':
unittest.main()