Skip to content

Commit

Permalink
[lit] Improve logging with file redirection.
Browse files Browse the repository at this point in the history
 - This will cause lit to automatically include the first 1K of data in
   redirected output files when a command fails (previously if the command
   failed, but the main point of the test was, say, a `FileCheck` later on, then
   the log wasn't helpful in showing why the command failed).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272021 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
ddunbar committed Jun 7, 2016
1 parent 54dfe5f commit 89f76ce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
36 changes: 32 additions & 4 deletions utils/lit/lit/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ def _kill(self):
class ShellCommandResult(object):
"""Captures the result of an individual command."""

def __init__(self, command, stdout, stderr, exitCode, timeoutReached):
def __init__(self, command, stdout, stderr, exitCode, timeoutReached,
outputFiles = []):
self.command = command
self.stdout = stdout
self.stderr = stderr
self.exitCode = exitCode
self.timeoutReached = timeoutReached
self.outputFiles = list(outputFiles)

def executeShCmd(cmd, shenv, results, timeout=0):
"""
Expand Down Expand Up @@ -268,7 +270,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
# FIXME: Actually, this is probably an instance of PR6753.
if r[1] == 'a':
r[2].seek(0, 2)
opened_files.append(r[2])
opened_files.append(tuple(r) + (redir_filename,))
result = r[2]
final_redirects.append(result)

Expand Down Expand Up @@ -342,7 +344,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
# need to release any handles we may have on the temporary files (important
# on Win32, for example). Since we have already spawned the subprocess, our
# handles have already been transferred so we do not need them anymore.
for f in opened_files:
for (name, mode, f, path) in opened_files:
f.close()

# FIXME: There is probably still deadlock potential here. Yawn.
Expand Down Expand Up @@ -393,8 +395,21 @@ def to_string(bytes):
except:
err = str(err)

# Gather the redirected output files.
output_files = []
for (name, mode, f, path) in sorted(opened_files):
if mode in ('w', 'a'):
try:
with open(path) as f:
data = f.read()
except:
data = None
if data != None:
output_files.append((name, path, data))

results.append(ShellCommandResult(
cmd.commands[i], out, err, res, timeoutHelper.timeoutReached()))
cmd.commands[i], out, err, res, timeoutHelper.timeoutReached(),
output_files))
if cmd.pipe_err:
# Python treats the exit code as a signed char.
if exitCode is None:
Expand Down Expand Up @@ -455,6 +470,19 @@ def executeScriptInternal(test, litConfig, tmpBase, commands, cwd):
continue

# Otherwise, something failed or was printed, show it.

# Add the command output, if redirected.
for (name, path, data) in result.outputFiles:
if data.strip():
out += "# redirected output from %r:\n" % (name,)
data = to_string(data.decode('utf-8'))
if len(data) > 1024:
out += data[:1024] + "\n...\n"
out += "note: data was truncated\n"
else:
out += data
out += "\n"

if result.stdout.strip():
out += '# command output:\n%s\n' % (result.stdout,)
if result.stderr.strip():
Expand Down
2 changes: 1 addition & 1 deletion utils/lit/tests/Inputs/shtest-output-printing/basic.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# RUN: true
# RUN: echo hi
# RUN: false
# RUN: wc missing-file &> %t.out
8 changes: 5 additions & 3 deletions utils/lit/tests/shtest-output-printing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Check the various features of the ShTest format.
#
# RUN: not %{lit} -j 1 -v %{inputs}/shtest-output-printing > %t.out
# RUN: FileCheck < %t.out %s
# RUN: FileCheck --input-file %t.out %s
#
# END.

Expand All @@ -21,6 +21,8 @@
# CHECK-NEXT: # command output:
# CHECK-NEXT: hi
#
# CHECK: $ "false"
# CHECK-NEXT: note: command had no output on stdout or stderr
# CHECK: $ "wc" "missing-file"
# CHECK-NEXT: # redirected output from '{{.*}}/basic.txt.tmp.out':
# CHECK-NEXT: missing-file{{.*}} No such file or directory
# CHECK: note: command had no output on stdout or stderr
# CHECK-NEXT: error: command failed with exit status: 1
2 changes: 1 addition & 1 deletion utils/lit/tests/shtest-shell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Check the internal shell handling component of the ShTest format.
#
# RUN: not %{lit} -j 1 -v %{inputs}/shtest-shell > %t.out
# RUN: FileCheck < %t.out %s
# RUN: FileCheck --input-file %t.out %s
#
# END.

Expand Down

0 comments on commit 89f76ce

Please sign in to comment.