forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTempTest.py
More file actions
executable file
·162 lines (123 loc) · 5.02 KB
/
Copy pathTempTest.py
File metadata and controls
executable file
·162 lines (123 loc) · 5.02 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#
# 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 os
from shutil import rmtree
from ElementsKernel.Temporary import TempDir, TempFile
from ElementsKernel.Temporary import TempEnv
import unittest
WORKDIR_VAR = "TMP_WORKSPACE"
class TestCase(unittest.TestCase):
""" test case for the temporary directory class """
def setUp(self):
unittest.TestCase.setUp(self)
self.samplebasename = "toto"
self.tmpdir = TempDir(suffix="tempdir", prefix=self.samplebasename)
self.m_top_dir = TempDir()
self.m_env = TempEnv()
self.m_env[WORKDIR_VAR] = os.path.join(self.m_top_dir.path(), "work")
def tearDown(self):
unittest.TestCase.tearDown(self)
del self.tmpdir
del self.m_top_dir
def testName(self):
name1 = str(self.tmpdir)
name2 = self.tmpdir.getName()
# the next line should print twice the same thing
self.assertEqual(name1, name2)
def testDestruction(self):
mydir = TempDir(suffix="tempdir", prefix="mydir")
mydirname = mydir.getName()
self.assertTrue(os.path.exists(mydirname))
self.assertTrue(os.path.isdir(mydirname))
del mydir
# the temporary directory should have been removed
if self.m_env.get("KEEPTEMPDIR", None):
self.assertTrue(os.path.exists(mydirname))
else:
self.assertFalse(os.path.exists(mydirname))
def testContext(self):
with TempDir() as td:
td_path = td.getName()
self.assertTrue(os.path.exists(td_path))
if self.m_env.get("KEEPTEMPDIR", None):
self.assertTrue(os.path.exists(td_path))
else:
self.assertFalse(os.path.exists(td_path))
def testFileDestruction(self):
myfile = TempFile()
myfilename = myfile.getName()
self.assertTrue(os.path.exists(myfilename))
self.assertFalse(os.path.isdir(myfilename))
self.assertTrue(os.path.isfile(myfilename))
del myfile
# the temporary directory should have been removed
if self.m_env.get("KEEPTEMPDIR", None):
self.assertTrue(os.path.exists(myfilename))
else:
self.assertFalse(os.path.exists(myfilename))
def testFileContext(self):
with TempFile() as tf:
tf_path = tf.getName()
self.assertTrue(os.path.exists(tf_path))
if self.m_env.get("KEEPTEMPDIR", None):
self.assertTrue(os.path.exists(tf_path))
else:
self.assertFalse(os.path.exists(tf_path))
def testPath(self):
with TempDir() as td:
td_path = td.path()
self.assertTrue(os.path.exists(td_path))
def testEnvWith(self):
os.environ["TESTENVEK"] = "wuk"
with TempEnv() as e:
e["TESTENVEK"] = "bla"
e["TESTENVEK2"] = "bla"
self.assertEqual(os.environ.get("TESTENVEK"), "bla")
# The environment is restored
self.assertEqual(os.environ.get("TESTENVEK"), "wuk")
with TempEnv() as e:
e["TESTENVEK2"] = "bla"
self.assertEqual(os.environ.get("TESTENVEK2"), "bla")
self.assertEqual(os.environ.get("TESTENVEK2", None), None)
def testKeepTmpDir(self):
os.environ["KEEPTEMPDIR"] = "1"
td_path = ""
with TempDir() as td:
td_path = td.path()
self.assertTrue(os.path.exists(td_path))
# force deletion
del td
self.assertTrue(os.path.exists(td_path))
rmtree(td_path)
self.assertFalse(os.path.exists(td_path))
del os.environ["KEEPTEMPDIR"]
def testTempEnv2(self):
self.assertEqual(self.m_env[WORKDIR_VAR], os.path.join(self.m_top_dir.path(), "work"))
with TempEnv() as local:
self.assertEqual(self.m_env[WORKDIR_VAR], os.path.join(self.m_top_dir.path(), "work"))
self.assertEqual(local[WORKDIR_VAR], os.path.join(self.m_top_dir.path(), "work"))
local[WORKDIR_VAR] = "that_work"
self.assertEqual(self.m_env[WORKDIR_VAR], "that_work")
self.assertEqual(local[WORKDIR_VAR], "that_work")
self.assertEqual(self.m_env[WORKDIR_VAR], os.path.join(self.m_top_dir.path(), "work"))
if __name__ == '__main__':
unittest.main()