[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / llvm / utils / lit / lit / formats / base.py
blob0f8e984b2ab48e8fcce739f993e6719d9c244eb7
1 from __future__ import absolute_import
2 import os
4 import lit.Test
5 import lit.util
8 class TestFormat(object):
9 pass
12 ###
15 class FileBasedTest(TestFormat):
16 def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
17 source_path = testSuite.getSourcePath(path_in_suite)
18 for filename in os.listdir(source_path):
19 # Ignore dot files and excluded tests.
20 if filename.startswith(".") or filename in localConfig.excludes:
21 continue
23 filepath = os.path.join(source_path, filename)
24 if not os.path.isdir(filepath):
25 base, ext = os.path.splitext(filename)
26 if ext in localConfig.suffixes:
27 yield lit.Test.Test(
28 testSuite, path_in_suite + (filename,), localConfig
32 ###
34 import re
35 import tempfile
38 class OneCommandPerFileTest(TestFormat):
39 # FIXME: Refactor into generic test for running some command on a directory
40 # of inputs.
42 def __init__(self, command, dir, recursive=False, pattern=".*", useTempInput=False):
43 if isinstance(command, str):
44 self.command = [command]
45 else:
46 self.command = list(command)
47 if dir is not None:
48 dir = str(dir)
49 self.dir = dir
50 self.recursive = bool(recursive)
51 self.pattern = re.compile(pattern)
52 self.useTempInput = useTempInput
54 def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
55 dir = self.dir
56 if dir is None:
57 dir = testSuite.getSourcePath(path_in_suite)
59 for dirname, subdirs, filenames in os.walk(dir):
60 if not self.recursive:
61 subdirs[:] = []
63 subdirs[:] = [
64 d for d in subdirs if (d != ".svn" and d not in localConfig.excludes)
67 for filename in filenames:
68 if (
69 filename.startswith(".")
70 or not self.pattern.match(filename)
71 or filename in localConfig.excludes
73 continue
75 path = os.path.join(dirname, filename)
76 suffix = path[len(dir) :]
77 if suffix.startswith(os.sep):
78 suffix = suffix[1:]
79 test = lit.Test.Test(
80 testSuite, path_in_suite + tuple(suffix.split(os.sep)), localConfig
82 # FIXME: Hack?
83 test.source_path = path
84 yield test
86 def createTempInput(self, tmp, test):
87 raise NotImplementedError("This is an abstract method.")
89 def execute(self, test, litConfig):
90 if test.config.unsupported:
91 return (lit.Test.UNSUPPORTED, "Test is unsupported")
93 cmd = list(self.command)
95 # If using temp input, create a temporary file and hand it to the
96 # subclass.
97 if self.useTempInput:
98 tmp = tempfile.NamedTemporaryFile(suffix=".cpp")
99 self.createTempInput(tmp, test)
100 tmp.flush()
101 cmd.append(tmp.name)
102 elif hasattr(test, "source_path"):
103 cmd.append(test.source_path)
104 else:
105 cmd.append(test.getSourcePath())
107 out, err, exitCode = lit.util.executeCommand(cmd)
109 diags = out + err
110 if not exitCode and not diags.strip():
111 return lit.Test.PASS, ""
113 # Try to include some useful information.
114 report = """Command: %s\n""" % " ".join(["'%s'" % a for a in cmd])
115 if self.useTempInput:
116 report += """Temporary File: %s\n""" % tmp.name
117 report += "--\n%s--\n" "" % open(tmp.name).read()
118 report += """Output:\n--\n%s--""" % diags
120 return lit.Test.FAIL, report
125 # Check exit code of a simple executable with no input
126 class ExecutableTest(FileBasedTest):
127 def execute(self, test, litConfig):
128 if test.config.unsupported:
129 return lit.Test.UNSUPPORTED
131 out, err, exitCode = lit.util.executeCommand(test.getSourcePath())
133 if not exitCode:
134 return lit.Test.PASS, ""
136 return lit.Test.FAIL, out + err