1 from __future__
import absolute_import
8 class TestFormat(object):
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
:
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
:
28 testSuite
, path_in_suite
+ (filename
,), localConfig
38 class OneCommandPerFileTest(TestFormat
):
39 # FIXME: Refactor into generic test for running some command on a directory
42 def __init__(self
, command
, dir, recursive
=False, pattern
=".*", useTempInput
=False):
43 if isinstance(command
, str):
44 self
.command
= [command
]
46 self
.command
= list(command
)
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
):
57 dir = testSuite
.getSourcePath(path_in_suite
)
59 for dirname
, subdirs
, filenames
in os
.walk(dir):
60 if not self
.recursive
:
64 d
for d
in subdirs
if (d
!= ".svn" and d
not in localConfig
.excludes
)
67 for filename
in filenames
:
69 filename
.startswith(".")
70 or not self
.pattern
.match(filename
)
71 or filename
in localConfig
.excludes
75 path
= os
.path
.join(dirname
, filename
)
76 suffix
= path
[len(dir) :]
77 if suffix
.startswith(os
.sep
):
80 testSuite
, path_in_suite
+ tuple(suffix
.split(os
.sep
)), localConfig
83 test
.source_path
= path
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
98 tmp
= tempfile
.NamedTemporaryFile(suffix
=".cpp")
99 self
.createTempInput(tmp
, test
)
102 elif hasattr(test
, "source_path"):
103 cmd
.append(test
.source_path
)
105 cmd
.append(test
.getSourcePath())
107 out
, err
, exitCode
= lit
.util
.executeCommand(cmd
)
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())
134 return lit
.Test
.PASS
, ""
136 return lit
.Test
.FAIL
, out
+ err