[lit] Move formats into their own subpackage.
[oota-llvm.git] / utils / lit / lit / formats / base.py
1 from __future__ import absolute_import
2 import os
3 import sys
4
5 import lit.Test
6 import lit.TestRunner
7 import lit.util
8
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):
17                 continue
18
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,),
24                                         localConfig)
25
26 ###
27
28 import re
29 import tempfile
30
31 class OneCommandPerFileTest:
32     # FIXME: Refactor into generic test for running some command on a directory
33     # of inputs.
34
35     def __init__(self, command, dir, recursive=False,
36                  pattern=".*", useTempInput=False):
37         if isinstance(command, str):
38             self.command = [command]
39         else:
40             self.command = list(command)
41         if dir is not None:
42             dir = str(dir)
43         self.dir = dir
44         self.recursive = bool(recursive)
45         self.pattern = re.compile(pattern)
46         self.useTempInput = useTempInput
47
48     def getTestsInDirectory(self, testSuite, path_in_suite,
49                             litConfig, localConfig):
50         dir = self.dir
51         if dir is None:
52             dir = testSuite.getSourcePath(path_in_suite)
53
54         for dirname,subdirs,filenames in os.walk(dir):
55             if not self.recursive:
56                 subdirs[:] = []
57
58             subdirs[:] = [d for d in subdirs
59                           if (d != '.svn' and
60                               d not in localConfig.excludes)]
61
62             for filename in filenames:
63                 if (filename.startswith('.') or
64                     not self.pattern.match(filename) or
65                     filename in localConfig.excludes):
66                     continue
67
68                 path = os.path.join(dirname,filename)
69                 suffix = path[len(dir):]
70                 if suffix.startswith(os.sep):
71                     suffix = suffix[1:]
72                 test = lit.Test.Test(
73                     testSuite, path_in_suite + tuple(suffix.split(os.sep)),
74                     localConfig)
75                 # FIXME: Hack?
76                 test.source_path = path
77                 yield test
78
79     def createTempInput(self, tmp, test):
80         abstract
81
82     def execute(self, test, litConfig):
83         if test.config.unsupported:
84             return (lit.Test.UNSUPPORTED, 'Test is unsupported')
85
86         cmd = list(self.command)
87
88         # If using temp input, create a temporary file and hand it to the
89         # subclass.
90         if self.useTempInput:
91             tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
92             self.createTempInput(tmp, test)
93             tmp.flush()
94             cmd.append(tmp.name)
95         elif hasattr(test, 'source_path'):
96             cmd.append(test.source_path)
97         else:
98             cmd.append(test.getSourcePath())
99
100         out, err, exitCode = lit.TestRunner.executeCommand(cmd)
101
102         diags = out + err
103         if not exitCode and not diags.strip():
104             return lit.Test.PASS,''
105
106         # Try to include some useful information.
107         report = """Command: %s\n""" % ' '.join(["'%s'" % a
108                                                  for a in cmd])
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
113
114         return lit.Test.FAIL, report