Coverage for tests/test_getTempFilePath.py: 20%
91 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-14 01:59 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-14 01:59 -0800
1# This file is part of utils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import os.path
13import sys
14import time
15import unittest
17import lsst.utils.tests
19"""
20This file contains tests for lsst.utils.tests.getTempFilePath.
22The TestNameClashN classes are used to check that getTempFilePath
23does not use the same name across different test classes in the same
24file even if they have the same test methods. They are distinct classes
25with the same test method in an attempt to trigger a race condition
26whereby context managers use the same name and race to delete the file.
27The sleeps are there to ensure the race condition occurs in older versions
28of this package. This should not happen as of DM-13046."""
31class GetTempFilePathTestCase(unittest.TestCase):
32 def testBasics(self):
33 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
34 # Path will have unique component so do not test full equality
35 self.assertIn("test_getTempFilePath_testBasics", tmpFile)
36 self.assertTrue(tmpFile.endswith(".txt"))
37 f = open(tmpFile, "w")
38 f.write("foo\n")
39 f.close()
40 self.assertFalse(os.path.exists(tmpFile))
42 def testMultipleCallDepth(self):
43 """Test getTempFile with multiple call depth"""
44 funcName = "testMultipleCallDepth"
45 self.runGetTempFile(funcName)
46 self.runLevel2(funcName)
47 self.runLevel3(funcName)
49 def runGetTempFile(self, funcName):
50 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
51 # Path will have unique component so do not test full equality
52 self.assertIn("test_getTempFilePath_%s" % (funcName,), tmpFile)
53 self.assertTrue(tmpFile.endswith(".fits"))
54 f = open(tmpFile, "w")
55 f.write("foo\n")
56 f.close()
57 self.assertFalse(os.path.exists(tmpFile))
59 def runLevel2(self, funcName):
60 """Call runGetTempFile"""
61 self.runGetTempFile(funcName)
63 def runLevel3(self, funcName):
64 """Call runLevel2, which calls runGetTempFile"""
65 self.runLevel2(funcName)
68class TestNested(unittest.TestCase):
69 """Tests of the use of getTempFilePath in nested context managers."""
71 def testNested(self):
72 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile1:
73 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile2:
74 self.assertNotEqual(tmpFile1, tmpFile2)
75 with open(tmpFile1, "w") as f1:
76 f1.write("foo\n")
77 with open(tmpFile2, "w") as f2:
78 f2.write("foo\n")
79 self.assertTrue(os.path.exists(tmpFile1))
80 self.assertFalse(os.path.exists(tmpFile2))
81 self.assertFalse(os.path.exists(tmpFile1))
84class TestExpected(unittest.TestCase):
85 """Tests that we get files when we expect to get them and we get upset
86 when we don't get them."""
88 def testOutputExpected(self):
89 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
90 with open(tmpFile, "w") as f:
91 f.write("foo\n")
92 self.assertFalse(os.path.exists(tmpFile))
94 with self.assertRaises(RuntimeError):
95 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=True) as tmpFile:
96 pass
98 with self.assertRaises(RuntimeError):
99 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
100 pass
102 def testOutputUnexpected(self):
103 with self.assertRaises(RuntimeError):
104 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile:
105 with open(tmpFile, "w") as f:
106 f.write("foo\n")
108 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile:
109 pass
110 self.assertFalse(os.path.exists(tmpFile))
113class TestNameClash1(unittest.TestCase):
114 def testClash(self):
115 """Create the temp file and pause before trying to delete it."""
116 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
117 with open(tmpFile, "w") as f:
118 f.write("foo\n")
119 time.sleep(0.2)
120 self.assertTrue(os.path.exists(tmpFile))
123class TestNameClash2(unittest.TestCase):
124 def testClash(self):
125 """Pause a little before trying to create the temp file. The pause
126 time is less than the time that TestNameClash1 is pausing."""
127 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
128 time.sleep(0.1)
129 with open(tmpFile, "w") as f:
130 f.write("foo\n")
131 self.assertTrue(os.path.exists(tmpFile))
134class TestNameClash3(unittest.TestCase):
135 def testClash(self):
136 """Create temp file and remove it without pauses."""
137 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
138 with open(tmpFile, "w") as f:
139 f.write("foo\n")
140 self.assertTrue(os.path.exists(tmpFile))
143class TestMemory(lsst.utils.tests.MemoryTestCase):
144 pass
147def setup_module(module):
148 lsst.utils.tests.init()
151if __name__ == "__main__": 151 ↛ 152line 151 didn't jump to line 152, because the condition on line 151 was never true
152 setup_module(sys.modules[__name__])
153 unittest.main()