Coverage for tests/test_getTempFilePath.py : 29%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# # LSST Data Management System # Copyright 2016 LSST Corporation. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program 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 General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <http://www.lsstcorp.org/LegalNotices/>. #
""" This file contains tests for lsst.utils.tests.getTempFilePath.
The TestNameClashN classes are used to check that getTempFilePath does not use the same name across different test classes in the same file even if they have the same test methods. They are distinct classes with the same test method in an attempt to trigger a race condition whereby context managers use the same name and race to delete the file. The sleeps are there to ensure the race condition occurs in older versions of this package. This should not happen as of DM-13046."""
with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: # Path will have unique component so do not test full equality self.assertIn("test_getTempFilePath_testBasics", tmpFile) self.assertTrue(tmpFile.endswith(".txt")) f = open(tmpFile, "w") f.write("foo\n") f.close() self.assertFalse(os.path.exists(tmpFile))
"""Test getTempFile with multiple call depth""" funcName = "testMultipleCallDepth" self.runGetTempFile(funcName) self.runLevel2(funcName) self.runLevel3(funcName)
with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: # Path will have unique component so do not test full equality self.assertIn("test_getTempFilePath_%s" % (funcName,), tmpFile) self.assertTrue(tmpFile.endswith(".fits")) f = open(tmpFile, "w") f.write("foo\n") f.close() self.assertFalse(os.path.exists(tmpFile))
"""Call runGetTempFile""" self.runGetTempFile(funcName)
"""Call runLevel2, which calls runGetTempFile""" self.runLevel2(funcName)
"""Tests of the use of getTempFilePath in nested context managers."""
with lsst.utils.tests.getTempFilePath(".fits") as tmpFile1: with lsst.utils.tests.getTempFilePath(".fits") as tmpFile2: self.assertNotEqual(tmpFile1, tmpFile2) with open(tmpFile1, "w") as f1: f1.write("foo\n") with open(tmpFile2, "w") as f2: f2.write("foo\n") self.assertTrue(os.path.exists(tmpFile1)) self.assertFalse(os.path.exists(tmpFile2)) self.assertFalse(os.path.exists(tmpFile1))
"""Tests that we get files when we expect to get them and we get upset when we don't get them."""
with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: with open(tmpFile, "w") as f: f.write("foo\n") self.assertFalse(os.path.exists(tmpFile))
with self.assertRaises(RuntimeError): with lsst.utils.tests.getTempFilePath(".txt", expectOutput=True) as tmpFile: pass
with self.assertRaises(RuntimeError): with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: pass
with self.assertRaises(RuntimeError): with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile: with open(tmpFile, "w") as f: f.write("foo\n")
with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile: pass self.assertFalse(os.path.exists(tmpFile))
"""Create the temp file and pause before trying to delete it.""" with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: with open(tmpFile, "w") as f: f.write("foo\n") time.sleep(0.2) self.assertTrue(os.path.exists(tmpFile))
"""Pause a little before trying to create the temp file. The pause time is less than the time that TestNameClash1 is pausing.""" with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: time.sleep(0.1) with open(tmpFile, "w") as f: f.write("foo\n") self.assertTrue(os.path.exists(tmpFile))
"""Create temp file and remove it without pauses.""" with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: with open(tmpFile, "w") as f: f.write("foo\n") self.assertTrue(os.path.exists(tmpFile))
lsst.utils.tests.init()
setup_module(sys.modules[__name__]) unittest.main() |