+from __future__ import absolute_import
import os, signal, subprocess, sys
-import StringIO
-
-import ShUtil
-import Test
-import Util
-
+import re
import platform
import tempfile
-import re
+import lit.ShUtil as ShUtil
+import lit.Test as Test
+import lit.util
class InternalShellError(Exception):
def __init__(self, command, message):
# Use temporary files to replace /dev/null on Windows.
kAvoidDevNull = kIsWindows
-def executeCommand(command, cwd=None, env=None):
- # Close extra file handles on UNIX (on Windows this cannot be done while
- # also redirecting input).
- close_fds = not kIsWindows
-
- p = subprocess.Popen(command, cwd=cwd,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- env=env, close_fds=close_fds)
- out,err = p.communicate()
- exitCode = p.wait()
-
- # Detect Ctrl-C in subprocess.
- if exitCode == -signal.SIGINT:
- raise KeyboardInterrupt
-
- return out, err, exitCode
-
def executeShCmd(cmd, cfg, cwd, results):
if isinstance(cmd, ShUtil.Seq):
if cmd.op == ';':
return executeShCmd(cmd.rhs, cfg, cwd, results)
if cmd.op == '&':
- raise NotImplementedError,"unsupported test command: '&'"
+ raise InternalShellError(cmd,"unsupported shell operator: '&'")
if cmd.op == '||':
res = executeShCmd(cmd.lhs, cfg, cwd, results)
if res != 0:
res = executeShCmd(cmd.rhs, cfg, cwd, results)
return res
+
if cmd.op == '&&':
res = executeShCmd(cmd.lhs, cfg, cwd, results)
if res is None:
res = executeShCmd(cmd.rhs, cfg, cwd, results)
return res
- raise ValueError,'Unknown shell command: %r' % cmd.op
+ raise ValueError('Unknown shell command: %r' % cmd.op)
assert isinstance(cmd, ShUtil.Pipeline)
procs = []
elif r[0] == ('<',):
redirects[0] = [r[1], 'r', None]
else:
- raise NotImplementedError,"Unsupported redirect: %r" % (r,)
+ raise InternalShellError(j,"Unsupported redirect: %r" % (r,))
# Map from the final redirections to something subprocess can handle.
final_redirects = []
result = input
elif r == (1,):
if index == 0:
- raise NotImplementedError,"Unsupported redirect for stdin"
+ raise InternalShellError(j,"Unsupported redirect for stdin")
elif index == 1:
result = subprocess.PIPE
else:
result = subprocess.STDOUT
elif r == (2,):
if index != 2:
- raise NotImplementedError,"Unsupported redirect on stdout"
+ raise InternalShellError(j,"Unsupported redirect on stdout")
result = subprocess.PIPE
else:
if r[2] is None:
# Resolve the executable path ourselves.
args = list(j.args)
- args[0] = Util.which(args[0], cfg.environment['PATH'])
+ args[0] = lit.util.which(args[0], cfg.environment['PATH'])
if not args[0]:
raise InternalShellError(j, '%r: command not found' % j.args[0])
results.append((cmd.commands[i], out, err, res))
if cmd.pipe_err:
# Python treats the exit code as a signed char.
- if res < 0:
+ if exitCode is None:
+ exitCode = res
+ elif res < 0:
exitCode = min(exitCode, res)
else:
exitCode = max(exitCode, res)
cmds = []
for ln in commands:
try:
- cmds.append(ShUtil.ShParser(ln, litConfig.isWindows).parse())
+ cmds.append(ShUtil.ShParser(ln, litConfig.isWindows,
+ test.config.pipefail).parse())
except:
return (Test.FAIL, "shell parser error on: %r" % ln)
results = []
try:
exitCode = executeShCmd(cmd, test.config, cwd, results)
- except InternalShellError,e:
+ except InternalShellError:
+ e = sys.exc_info()[1]
exitCode = 127
results.append((e.command, '', e.message, exitCode))
script += '.bat'
# Write script file
- f = open(script,'w')
+ mode = 'w'
+ if litConfig.isWindows and not isWin32CMDEXE:
+ mode += 'b' # Avoid CRLFs when writing bash scripts.
+ f = open(script, mode)
if isWin32CMDEXE:
f.write('\nif %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands))
else:
+ if test.config.pipefail:
+ f.write('set -o pipefail;')
f.write('{ ' + '; } &&\n{ '.join(commands) + '; }')
f.write('\n')
f.close()
# run on clang with no real loss.
command = litConfig.valgrindArgs + command
- return executeCommand(command, cwd=cwd, env=test.config.environment)
+ return lit.util.executeCommand(command, cwd=cwd,
+ env=test.config.environment)
def isExpectedFail(test, xfails):
# Check if any of the xfails match an available feature or the target.
return False
+def parseIntegratedTestScriptCommands(sourcepath):
+ """
+ parseIntegratedTestScriptCommands(source_path) -> commands
+
+ Parse the commands in an integrated test script file into a list of
+ (line_number, command_type, line).
+ """
+ line_number = 0
+ for ln in open(sourcepath):
+ line_number += 1
+ if 'RUN:' in ln:
+ yield (line_number, 'RUN', ln[ln.index('RUN:')+4:])
+ elif 'XFAIL:' in ln:
+ yield (line_number, 'XFAIL', ln[ln.index('XFAIL:') + 6:])
+ elif 'REQUIRES:' in ln:
+ yield (line_number, 'REQUIRES', ln[ln.index('REQUIRES:') + 9:])
+ elif 'END.' in ln:
+ yield (line_number, 'END', ln[ln.index('END.') + 4:])
+
def parseIntegratedTestScript(test, normalize_slashes=False,
extra_substitutions=[]):
"""parseIntegratedTestScript - Scan an LLVM/Clang style integrated test
execdir,execbase = os.path.split(execpath)
tmpDir = os.path.join(execdir, 'Output')
tmpBase = os.path.join(tmpDir, execbase)
- if test.index is not None:
- tmpBase += '_%d' % test.index
# Normalize slashes, if requested.
if normalize_slashes:
('%T', tmpDir),
('#_MARKER_#', '%')])
+ # "%/[STpst]" should be normalized.
+ substitutions.extend([
+ ('%/s', sourcepath.replace('\\', '/')),
+ ('%/S', sourcedir.replace('\\', '/')),
+ ('%/p', sourcedir.replace('\\', '/')),
+ ('%/t', tmpBase.replace('\\', '/') + '.tmp'),
+ ('%/T', tmpDir.replace('\\', '/')),
+ ])
+
# Collect the test lines from the script.
script = []
xfails = []
requires = []
- line_number = 0
- for ln in open(sourcepath):
- line_number += 1
- if 'RUN:' in ln:
- # Isolate the command to run.
- index = ln.index('RUN:')
- ln = ln[index+4:]
-
+ for line_number, command_type, ln in \
+ parseIntegratedTestScriptCommands(sourcepath):
+ if command_type == 'RUN':
# Trim trailing whitespace.
ln = ln.rstrip()
script[-1] = script[-1][:-1] + ln
else:
script.append(ln)
- elif 'XFAIL:' in ln:
- items = ln[ln.index('XFAIL:') + 6:].split(',')
- xfails.extend([s.strip() for s in items])
- elif 'REQUIRES:' in ln:
- items = ln[ln.index('REQUIRES:') + 9:].split(',')
- requires.extend([s.strip() for s in items])
- elif 'END.' in ln:
- # Check for END. lines.
- if ln[ln.index('END.'):].strip() == 'END.':
+ elif command_type == 'XFAIL':
+ xfails.extend([s.strip() for s in ln.split(',')])
+ elif command_type == 'REQUIRES':
+ requires.extend([s.strip() for s in ln.split(',')])
+ elif command_type == 'END':
+ # END commands are only honored if the rest of the line is empty.
+ if not ln.strip():
break
+ else:
+ raise ValueError("unknown script command type: %r" % (
+ command_type,))
# Apply substitutions to the script. Allow full regular
# expression syntax. Replace each matching occurrence of regular
# Strip the trailing newline and any extra whitespace.
return ln.strip()
- script = map(processLine, script)
+ script = [processLine(ln)
+ for ln in script]
# Verify the script contains a run line.
if not script:
return script,isXFail,tmpBase,execdir
def formatTestOutput(status, out, err, exitCode, script):
- output = StringIO.StringIO()
- print >>output, "Script:"
- print >>output, "--"
- print >>output, '\n'.join(script)
- print >>output, "--"
- print >>output, "Exit Code: %r" % exitCode,
- print >>output
+ output = """\
+Script:
+--
+%s
+--
+Exit Code: %d
+
+""" % ('\n'.join(script), exitCode)
+
+ # Append the stdout, if present.
if out:
- print >>output, "Command Output (stdout):"
- print >>output, "--"
- output.write(out)
- print >>output, "--"
+ output += """\
+Command Output (stdout):
+--
+%s
+--
+""" % (out,)
+
+ # Append the stderr, if present.
if err:
- print >>output, "Command Output (stderr):"
- print >>output, "--"
- output.write(err)
- print >>output, "--"
- return (status, output.getvalue())
+ output += """\
+Command Output (stderr):
+--
+%s
+--
+""" % (err,)
+ return (status, output)
def executeShTest(test, litConfig, useExternalSh,
extra_substitutions=[]):
return (Test.PASS, '')
# Create the output directory if it does not already exist.
- Util.mkdir_p(os.path.dirname(tmpBase))
+ lit.util.mkdir_p(os.path.dirname(tmpBase))
if useExternalSh:
res = executeScript(test, litConfig, tmpBase, script, execdir)