1 from __future__ import absolute_import
9 class FileBasedTest(object):
10 def getTestsInDirectory(self, testSuite, path_in_suite,
11 litConfig, localConfig):
12 source_path = testSuite.getSourcePath(path_in_suite)
13 for filename in os.listdir(source_path):
14 # Ignore dot files and excluded tests.
15 if (filename.startswith('.') or
16 filename in localConfig.excludes):
19 filepath = os.path.join(source_path, filename)
20 if not os.path.isdir(filepath):
21 base,ext = os.path.splitext(filename)
22 if ext in localConfig.suffixes:
23 yield lit.Test.Test(testSuite, path_in_suite + (filename,),
31 class OneCommandPerFileTest:
32 # FIXME: Refactor into generic test for running some command on a directory
35 def __init__(self, command, dir, recursive=False,
36 pattern=".*", useTempInput=False):
37 if isinstance(command, str):
38 self.command = [command]
40 self.command = list(command)
44 self.recursive = bool(recursive)
45 self.pattern = re.compile(pattern)
46 self.useTempInput = useTempInput
48 def getTestsInDirectory(self, testSuite, path_in_suite,
49 litConfig, localConfig):
52 dir = testSuite.getSourcePath(path_in_suite)
54 for dirname,subdirs,filenames in os.walk(dir):
55 if not self.recursive:
58 subdirs[:] = [d for d in subdirs
60 d not in localConfig.excludes)]
62 for filename in filenames:
63 if (filename.startswith('.') or
64 not self.pattern.match(filename) or
65 filename in localConfig.excludes):
68 path = os.path.join(dirname,filename)
69 suffix = path[len(dir):]
70 if suffix.startswith(os.sep):
73 testSuite, path_in_suite + tuple(suffix.split(os.sep)),
76 test.source_path = path
79 def createTempInput(self, tmp, test):
82 def execute(self, test, litConfig):
83 if test.config.unsupported:
84 return (lit.Test.UNSUPPORTED, 'Test is unsupported')
86 cmd = list(self.command)
88 # If using temp input, create a temporary file and hand it to the
91 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
92 self.createTempInput(tmp, test)
95 elif hasattr(test, 'source_path'):
96 cmd.append(test.source_path)
98 cmd.append(test.getSourcePath())
100 out, err, exitCode = lit.TestRunner.executeCommand(cmd)
103 if not exitCode and not diags.strip():
104 return lit.Test.PASS,''
106 # Try to include some useful information.
107 report = """Command: %s\n""" % ' '.join(["'%s'" % a
109 if self.useTempInput:
110 report += """Temporary File: %s\n""" % tmp.name
111 report += "--\n%s--\n""" % open(tmp.name).read()
112 report += """Output:\n--\n%s--""" % diags
114 return lit.Test.FAIL, report