blob: b0be35ca8a47a55ed737ccbf7cb54f6a97791687 [file] [log] [blame]
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Defines TestPackageExecutable to help run stand-alone executables."""
import logging
import os
import sys
import tempfile
from pylib import cmd_helper
from pylib import constants
from pylib import pexpect
from pylib.device import device_errors
from pylib.gtest.test_package import TestPackage
class TestPackageExecutable(TestPackage):
"""A helper class for running stand-alone executables."""
_TEST_RUNNER_RET_VAL_FILE = 'gtest_retval'
def __init__(self, suite_name):
suite_name: Name of the test suite (e.g. base_unittests).
TestPackage.__init__(self, suite_name)
self.suite_path = os.path.join(constants.GetOutDirectory(), suite_name)
self._symbols_dir = os.path.join(constants.GetOutDirectory(),
def GetGTestReturnCode(self, device):
ret = None
ret_code = 1 # Assume failure if we can't find it
ret_code_file = tempfile.NamedTemporaryFile()
if not device.PullFile(
constants.TEST_EXECUTABLE_DIR + '/' +
logging.critical('Unable to pull gtest ret val file %s',
raise ValueError
ret_code = file(
ret = int(ret_code)
except ValueError:
logging.critical('Error reading gtest ret val file %s [%s]',, ret_code)
ret = 1
return ret
def _AddNativeCoverageExports(device):
# export GCOV_PREFIX set the path for native coverage results
# export GCOV_PREFIX_STRIP indicates how many initial directory
# names to strip off the hardwired absolute paths.
# This value is calculated in and
# depends on where the tree is built.
# Ex: /usr/local/google/code/chrome will become
# /code/chrome if GCOV_PREFIX_STRIP=3
depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP']
export_string = ('export GCOV_PREFIX="%s/gcov"\n' %
export_string += 'export GCOV_PREFIX_STRIP=%s\n' % depth
return export_string
except KeyError:'NATIVE_COVERAGE_DEPTH_STRIP is not defined: '
'No native coverage.')
return ''
except device_errors.CommandFailedError:'No external storage found: No native coverage.')
return ''
def ClearApplicationState(self, device):
# We don't expect the executable to be running, so we don't attempt
# to retry on failure.
device.KillAll(self.suite_name, blocking=True, timeout=30, retries=0)
except device_errors.CommandFailedError:
# KillAll raises an exception if it can't find a process with the given
# name. We only care that there is no process with the given name, so
# we can safely eat the exception.
def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
tool_wrapper = self.tool.GetTestWrapper()
sh_script_file = tempfile.NamedTemporaryFile()
# We need to capture the exit status from the script since adb shell won't
# propagate to us.
sh_script_file.write('cd %s\n'
'%s %s/%s --gtest_filter=%s %s\n'
'echo $? > %s' %
tool_wrapper, constants.TEST_EXECUTABLE_DIR,
test_filter, test_arguments,
cmd_helper.RunCmd(['chmod', '+x',])
constants.TEST_EXECUTABLE_DIR + '/')])'Conents of the test runner script: ')
for line in open(' ' + line.rstrip())
def GetAllTests(self, device):
all_tests = device.RunShellCommand(
'%s %s/%s --gtest_list_tests' %
return self._ParseGTestListTests(all_tests)
def SpawnTestProcess(self, device):
args = ['adb', '-s', str(device), 'shell', 'sh',
constants.TEST_EXECUTABLE_DIR + '/']
return pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
def Install(self, device):
if self.tool.NeedsDebugInfo():
target_name = self.suite_path
target_name = self.suite_path + '_stripped'
if not os.path.isfile(target_name):
raise Exception('Did not find %s, build target %s' %
(target_name, self.suite_name + '_stripped'))
target_mtime = os.stat(target_name).st_mtime
source_mtime = os.stat(self.suite_path).st_mtime
if target_mtime < source_mtime:
raise Exception(
'stripped binary (%s, timestamp %d) older than '
'source binary (%s, timestamp %d), build target %s' %
(target_name, target_mtime, self.suite_path, source_mtime,
self.suite_name + '_stripped'))
test_binary = constants.TEST_EXECUTABLE_DIR + '/' + self.suite_name
device.PushChangedFiles([(target_name, test_binary)])