LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | Private Attributes | Static Private Attributes | List of all members
lsst.sconsUtils.tests.Control Class Reference

A class to control unit tests. More...

Inheritance diagram for lsst.sconsUtils.tests.Control:

Public Member Functions

def __init__
 Create an object to run tests. More...
 
def args
 
def ignore
 
def messages
 
def run
 

Public Attributes

 runExamples
 

Private Attributes

 _env
 
 _tmpDir
 
 _cwd
 
 _verbose
 
 _info
 
 _args
 

Static Private Attributes

string _IGNORE = "IGNORE"
 
string _EXPECT_FAILURE = "EXPECT_FAILURE"
 

Detailed Description

A class to control unit tests.

This class is unchanged from previous versions of sconsUtils, but it will now generally be called via scripts.BasicSConscript.tests().

Definition at line 17 of file tests.py.

Constructor & Destructor Documentation

def lsst.sconsUtils.tests.Control.__init__ (   self,
  env,
  ignoreList = None,
  expectedFailures = None,
  args = None,
  tmpDir = ".tests",
  verbose = False 
)

Create an object to run tests.

Parameters
envAn SCons Environment (almost always lsst.sconsUtils.env).
ignoreListA list of tests that should NOT be run — useful in conjunction with glob patterns. If a file is listed as "@fileName", the @ is stripped and we don't bother to check if fileName exists (useful for machine-generated files).
expectedFaluresA dictionary; the keys are tests that are known to fail; the values are strings to print.
argsA dictionary with testnames as keys, and argument strings as values. As scons always runs from the top-level directory, tests has to fiddle with paths. If an argument is a file this is done automatically; if it's e.g. just a basename then you have to tell tests that it's really (part of a) filename by prefixing the name by "file:".
tmpDirThe location of the test outputs.
verboseHow chatty you want the test code to be.
1  tests = lsst.tests.Control(
2  env,
3  args={
4  "MaskIO_1" : "data/871034p_1_MI_msk.fits",
5  "MaskedImage_1" : "file:data/871034p_1_MI foo",
6  },
7  ignoreList=["Measure_1"],
8  expectedFailures={"BBox_1": "Problem with single-pixel BBox"}
9 )

Definition at line 52 of file tests.py.

52 
53  tmpDir=".tests", verbose=False):
54  if 'PYTHONPATH' in os.environ:
55  env.AppendENVPath('PYTHONPATH', os.environ['PYTHONPATH'])
56 
57  self._env = env
58 
59  self._tmpDir = tmpDir
60  self._cwd = os.path.abspath(os.path.curdir)
61 
62  self._verbose = verbose
63 
64  self._info = {} # information about processing targets
65  if ignoreList:
66  for f in ignoreList:
67  if re.search(r"^@", f): # @dfilename => don't complain if filename doesn't exist
68  f = f[1:]
69  else:
70  if not os.path.exists(f):
71  state.log.warn("You're ignoring a non-existent file, %s" % f)
72  self._info[f] = (self._IGNORE, None)
73 
74  if expectedFailures:
75  for f in expectedFailures:
76  self._info[f] = (self._EXPECT_FAILURE, expectedFailures[f])
77 
78  if args:
79  self._args = args # arguments for tests
80  else:
81  self._args = {}
82 
83  self.runExamples = True # should I run the examples?
84  try:
85  self.runExamples = (os.stat(self._tmpDir).st_mode & 0o700) != 0 # file is user read/write/executable
86  except OSError:
87  pass
88 
89  if not self.runExamples:
90  print("Not running examples; \"chmod 755 %s\" to run them again" % self._tmpDir,
91  file=sys.stderr)

Member Function Documentation

def lsst.sconsUtils.tests.Control.args (   self,
  test 
)

Definition at line 92 of file tests.py.

92 
93  def args(self, test):
94  try:
95  return self._args[test]
96  except KeyError:
97  return ""
def lsst.sconsUtils.tests.Control.ignore (   self,
  test 
)

Definition at line 98 of file tests.py.

98 
99  def ignore(self, test):
100  if \
101  not re.search(r"\.py$", test) and \
102  len(self._env.Glob(test)) == 0: # we don't know how to build it
103  return True
104 
105  ignoreFile = test in self._info and self._info[test][0] == self._IGNORE
106 
107  if self._verbose and ignoreFile:
108  print("Skipping", test, file=sys.stderr)
109 
110  return ignoreFile
def lsst.sconsUtils.tests.Control.messages (   self,
  test 
)
Return the messages to be used in case of success/failure; the logicals
(note that they are strings) tell whether the test is expected to pass

Definition at line 111 of file tests.py.

112  def messages(self, test):
113  """Return the messages to be used in case of success/failure; the logicals
114  (note that they are strings) tell whether the test is expected to pass"""
115 
116  if test in self._info and self._info[test][0] == self._EXPECT_FAILURE:
117  msg = self._info[test][1]
118  return "false", "Passed, but should have failed: %s" % msg, \
119  "true", "Failed as expected: %s" % msg
120  else:
121  return "true", "passed", \
122  "false", "failed"
def lsst.sconsUtils.tests.Control.run (   self,
  fileGlob 
)

Definition at line 123 of file tests.py.

124  def run(self, fileGlob):
125  if not isinstance(fileGlob, basestring): # env.Glob() returns an scons Node
126  fileGlob = str(fileGlob)
127  targets = []
128  if not self.runExamples:
129  return targets
130  for f in glob.glob(fileGlob):
131  interpreter = "" # interpreter to run test, if needed
132 
133  if re.search(r"\.cc", f): # look for executable
134  f = os.path.splitext(f)[0]
135  else:
136  interpreter = "python"
137 
138  if self.ignore(f):
139  continue
140 
141  target = os.path.join(self._tmpDir, f)
142 
143  args = []
144  for a in self.args(f).split(" "):
145  # if a is a file, make it an absolute name as scons runs from the root directory
146  filePrefix = "file:"
147  if re.search(r"^" + filePrefix, a): # they explicitly said that this was a file
148  a = os.path.join(self._cwd, a[len(filePrefix):])
149  else:
150  try: # see if it's a file
151  os.stat(a)
152  a = os.path.join(self._cwd, a)
153  except OSError:
154  pass
155 
156  args += [a]
157 
158  (should_pass, passedMsg, should_fail, failedMsg) = self.messages(f)
159 
160  # The TRAVIS environment variable is set to allow us to disable
161  # the matplotlib font cache. See ticket DM-3856.
162  # TODO: Work out better way of solving matplotlib issue in build.
163  expandedArgs = " ".join(args)
164  result = self._env.Command(target, f, """
165  @rm -f ${TARGET}.failed;
166  @printf "%%s" 'running ${SOURCES}... ';
167  @echo $SOURCES %s > $TARGET; echo >> $TARGET;
168  @if TRAVIS=1 %s $SOURCES %s >> $TARGET 2>&1; then \
169  if ! %s; then mv $TARGET ${TARGET}.failed; fi; \
170  echo "%s"; \
171  else \
172  if ! %s; then mv $TARGET ${TARGET}.failed; fi; \
173  echo "%s"; \
174  fi;
175  """ % (expandedArgs, interpreter, expandedArgs, should_pass, passedMsg, should_fail, failedMsg))
176 
177  targets.extend(result)
178 
179  self._env.Alias(os.path.basename(target), target)
180 
181  self._env.Clean(target, self._tmpDir)
182 
183  return targets

Member Data Documentation

lsst.sconsUtils.tests.Control._args
private

Definition at line 78 of file tests.py.

lsst.sconsUtils.tests.Control._cwd
private

Definition at line 59 of file tests.py.

lsst.sconsUtils.tests.Control._env
private

Definition at line 56 of file tests.py.

string lsst.sconsUtils.tests.Control._EXPECT_FAILURE = "EXPECT_FAILURE"
staticprivate

Definition at line 19 of file tests.py.

string lsst.sconsUtils.tests.Control._IGNORE = "IGNORE"
staticprivate

Definition at line 18 of file tests.py.

lsst.sconsUtils.tests.Control._info
private

Definition at line 63 of file tests.py.

lsst.sconsUtils.tests.Control._tmpDir
private

Definition at line 58 of file tests.py.

lsst.sconsUtils.tests.Control._verbose
private

Definition at line 61 of file tests.py.

lsst.sconsUtils.tests.Control.runExamples

Definition at line 82 of file tests.py.


The documentation for this class was generated from the following file: