forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPathTest.py
More file actions
122 lines (90 loc) · 4.03 KB
/
Copy pathPathTest.py
File metadata and controls
122 lines (90 loc) · 4.03 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
#
# 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: Apr 29, 2016
:author: Hubert Degaudenzi
'''
import unittest
import subprocess
from ElementsKernel.Temporary import TempDir
from ElementsKernel.Path import joinPath, multiPathAppend, getLocationsFromEnv
from ElementsKernel.Path import getLocations
from ElementsKernel.Path import which
from ElementsKernel.Path import getTargetPath
from ElementsKernel.Path import removeDuplicates
class PathTest(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
self._tmpdir_1 = TempDir(suffix="tempdir")
self._tmpdir_2 = TempDir(suffix="tempdir")
def tearDown(self):
unittest.TestCase.tearDown(self)
del self._tmpdir_1
del self._tmpdir_2
def testJoinPath(self):
path_list = ["/toto", "titi", "./tutu"]
self.assertEqual(joinPath(path_list), "/toto:titi:./tutu")
path_list2 = ["", "/toto", "titi", "./tutu"]
self.assertEqual(joinPath(path_list2), ":/toto:titi:./tutu")
path_list3 = ["/toto", "titi", "./tutu", ""]
self.assertEqual(joinPath(path_list3), "/toto:titi:./tutu:")
def testMultiPathAppend(self):
locations = ["loc1", "/loc2", "./loc3"]
suffixes = ["bin", "scripts"]
ref_paths = ["loc1/bin", "loc1/scripts",
"/loc2/bin", "/loc2/scripts",
"./loc3/bin", "./loc3/scripts"]
self.assertEqual(multiPathAppend(locations, suffixes), ref_paths)
def testGetLoctions(self):
# by construction the PATH variable is never empty
self.assertNotEqual(getLocations(), [])
def testGetLocationsFromEnv(self):
self.assertEqual(getLocationsFromEnv("NonExistingEnvVar"), [])
self.assertNotEqual(getLocationsFromEnv("PATH"), [])
tmp_list = getLocationsFromEnv("PATH", exist_only=True)
self.assertNotEqual(len(tmp_list), 0)
def testWhich(self):
sys_ls = subprocess.check_output(["which", "ls"]).strip()
self.assertEqual(sys_ls, which(sys_ls))
def testGetTargetPath(self):
file_name = "toto"
target_dir = "/blab/blo"
self.assertEqual(getTargetPath(file_name, target_dir), "/blab/blo/toto")
file_name = "tata/toto"
target_dir = "/blab/blo"
self.assertEqual(getTargetPath(file_name, target_dir), "/blab/blo/toto")
file_name = "tata/toto"
target_dir = "/blab/blo"
self.assertEqual(getTargetPath(file_name, target_dir, use_stem=True), "/blab/blo/tata/toto")
file_name = "tata/toto"
target_dir = "/blab/blo"
target_name = "tutu"
self.assertEqual(getTargetPath(file_name, target_dir, target_name), "/blab/blo/tutu")
file_name = "tata/toto"
target_dir = "/blab/blo"
target_name = "foo/tutu"
self.assertEqual(getTargetPath(file_name, target_dir, target_name), "/blab/blo/foo/tutu")
def testRemoveDuplicates(self):
locations = ["/usr/bin", "/usr/local/bin",
"/usr/bin", "/opt/bin", "/opt/local/bin",
"/usr/bin", "/usr/local/bin"]
unique_locations = ["/usr/bin", "/usr/local/bin",
"/opt/bin", "/opt/local/bin"]
self.assertEqual(removeDuplicates(locations), unique_locations)
if __name__ == "__main__":
unittest.main()