1 from __future__
import division, absolute_import
2 """Utilities to aid unit tests
11 from RO.StringUtil
import strFromException
12 from twistedActor
import testUtils
14 from .tccLib
import tai
16 _MaxEarthAgeSec = 3600
17 _ModeRWRWR = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH
18 _DaysPerSec = 1.0 / (24.0 * 3600.0)
21 """!Return path to unit tests
23 return os.path.join(eups.productDir(
"tcc"),
"tests")
26 """!Set TCC environment variables appropriately for running unit tests
29 os.environ[
"TCC_DATA_DIR"] = os.path.join(testDir,
"data")
32 """!Create a new earth prediction file, so the TCC can be started
34 The file is: <getTestDir()>/data/earthpred.dat and any existing copy is overwritten.
36 Note that this intentionally does not use os.environ["TCC_DATA_DIR"], for fear of
37 accidentally overwriting the real earth orientation prediction file.
41 earthPath = os.path.join(testDir,
"data",
"earthpred.dat")
43 mtime = os.path.getmtime(earthPath)
44 if time.time() - mtime < _MaxEarthAgeSec:
50 startMJD = int(currTAI * _DaysPerSec) - 1
51 print "Creating new %r" % (earthPath,)
53 with tempfile.NamedTemporaryFile(mode=
"w", dir=testDir, delete=
False)
as tempEarthFile:
54 tempEarthPath = tempEarthFile.name
55 print "Created temporary earthpred file %r" % (tempEarthPath,)
56 tempEarthFile.write(
"""! Earth Orientation Predictions (in TCC format)
58 ! UTC UTC-TAI UT1-TAI dX dY
59 ! days sec sec arcsec arcsec
62 utcDays = startMJD + i - 1
63 tempEarthFile.write(
"%d -35 -34.8 0.1 0.3\n" % (utcDays,))
64 except Exception
as e:
65 print "Failed to create %s: %s; trying to remove temp file %r" % \
66 (earthPath, strFromException(e), tempEarthPath)
67 os.remove(tempEarthPath)
69 os.chmod(tempEarthPath, _ModeRWRWR)
73 mtime = os.path.getmtime(earthPath)
74 if time.time() - mtime < _MaxEarthAgeSec:
75 print "Another thread already replaced %r; removing temp file %r" % (earthPath, tempEarthPath)
76 os.remove(tempEarthPath)
80 shutil.move(tempEarthPath, earthPath)
81 print "Updated %r" % (earthPath,)
84 """!Prepare for a unit test to run that starts an actor
86 @param[in] filePath path of file being tested (e.g. __file__), or None:
87 - If supplied must be in subdir tests of your package (NOT deeper in the hierarchy)
88 in order to determine where the log file should go.
89 - If None, no log file is created.
91 testUtils.startLogging(filePath)
def setEnviron
Set TCC environment variables appropriately for running unit tests.
def getTestDir
Return path to unit tests.
def init
Prepare for a unit test to run that starts an actor.
def makeEarthPred
Create a new earth prediction file, so the TCC can be started.