/core/externals/google-toolbox-for-mac/UnitTesting/RunMacOSUnitTests.sh
Shell | 273 lines | 156 code | 35 blank | 82 comment | 27 complexity | 66a59cc324f1c90733c259a19fd6e777 MD5 | raw file
1#!/bin/bash 2# 3# RunMacOSUnitTests.sh 4# Copyright 2008 Google Inc. 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); you may not 7# use this file except in compliance with the License. You may obtain a copy 8# of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 15# License for the specific language governing permissions and limitations under 16# the License. 17# 18# Run the unit tests in this test bundle. 19# Set up some env variables to make things as likely to crash as possible. 20# See http://developer.apple.com/technotes/tn2004/tn2124.html for details. 21# 22 23set -o errexit 24set -o nounset 25# Uncomment the next line to trace execution. 26#set -o verbose 27 28# Controlling environment variables: 29# 30# GTM_DISABLE_ZOMBIES - 31# Set to a non-zero value to turn on zombie checks. You will probably 32# want to turn this off if you enable leaks. 33GTM_DISABLE_ZOMBIES=${GTM_DISABLE_ZOMBIES:=0} 34 35# GTM_ENABLE_LEAKS - 36# Set to a non-zero value to turn on the leaks check. You will probably want 37# to disable zombies, otherwise you will get a lot of false positives. 38GTM_ENABLE_LEAKS=${GTM_ENABLE_LEAKS:=0} 39 40# GTM_LEAKS_SYMBOLS_TO_IGNORE 41# List of comma separated symbols that leaks should ignore. Mainly to control 42# leaks in frameworks you don't have control over. 43# Search this file for GTM_LEAKS_SYMBOLS_TO_IGNORE to see examples. 44# Please feel free to add other symbols as you find them but make sure to 45# reference Radars or other bug systems so we can track them. 46GTM_LEAKS_SYMBOLS_TO_IGNORE=${GTM_LEAKS_SYMBOLS_TO_IGNORE:=""} 47 48# GTM_DO_NOT_REMOVE_GCOV_DATA 49# By default before starting the test, we remove any *.gcda files for the 50# current project build configuration so you won't get errors when a source 51# file has changed and the gcov data can't be merged. 52# We remove all the gcda files for the current configuration for the entire 53# project so that if you are building a test bundle to test another separate 54# bundle we make sure to clean up the files for the test bundle and the bundle 55# that you are testing. 56# If you DO NOT want this to occur, set GTM_DO_NOT_REMOVE_GCOV_DATA to a 57# non-zero value. 58GTM_DO_NOT_REMOVE_GCOV_DATA=${GTM_DO_NOT_REMOVE_GCOV_DATA:=0} 59 60# GTM_REMOVE_TARGET_GCOV_ONLY 61# By default all *.gcda files are removed form the project. Setting this to 62# 1 causes only the *.gcda files for the target to be removed. 63# If GTM_DO_NOT_REMOVE_GCOV_DATA is set, this has no effect. 64GTM_REMOVE_TARGET_GCOV_ONLY=${GTM_REMOVE_TARGET_GCOV_ONLY:=0} 65 66# GTM_ONE_TEST_AT_A_TIME 67# By default your tests run how ever parallel your projects/targets are 68# setup. Setting this to 1 will cause only one to run at a time, this is 69# useful if you are doing UI tests with the helper that controls the 70# colorsync profile, or any other machine wide state. 71GTM_ONE_TEST_AT_A_TIME=${GTM_ONE_TEST_AT_A_TIME:=0} 72 73ScriptDir=$(dirname "$(echo $0 | sed -e "s,^\([^/]\),$(pwd)/\1,")") 74ScriptName=$(basename "$0") 75ThisScript="${ScriptDir}/${ScriptName}" 76 77GTMXcodeNote() { 78 echo ${ThisScript}:${1}: note: GTM ${2} 79} 80 81# Helper that works like the linux flock util, so you can run something, but 82# have only one run at a time. 83MaybeFlock() { 84 if [ $GTM_ONE_TEST_AT_A_TIME -ne 0 ]; then 85 GTMXcodeNote ${LINENO} "Serializing test execution." 86 python -c "import fcntl, subprocess, sys 87file = open('$TMPDIR/GTM_one_test_at_a_time', 'a') 88fcntl.flock(file.fileno(), fcntl.LOCK_EX) 89sys.exit(subprocess.call(sys.argv[1:]))" "${@}" 90 else 91 GTMXcodeNote ${LINENO} "Allowing parallel test execution." 92 "${@}" 93 fi 94} 95 96# The workaround below is due to 97# Radar 6248062 otest won't run with MallocHistory enabled under rosetta 98# Basically we go through and check what architecture we are running on 99# and which architectures we can support 100AppendToSymbolsLeaksShouldIgnore() { 101 if [ "${GTM_LEAKS_SYMBOLS_TO_IGNORE}" = "" ]; then 102 GTM_LEAKS_SYMBOLS_TO_IGNORE="${1}" 103 else 104 GTM_LEAKS_SYMBOLS_TO_IGNORE="${GTM_LEAKS_SYMBOLS_TO_IGNORE}, ${1}" 105 fi 106} 107 108AppendToLeakTestArchs() { 109 if [ "${LEAK_TEST_ARCHS}" = "" ]; then 110 LEAK_TEST_ARCHS="${1}" 111 else 112 LEAK_TEST_ARCHS="${LEAK_TEST_ARCHS} ${1}" 113 fi 114} 115 116AppendToNoLeakTestArchs() { 117 if [ "${NO_LEAK_TEST_ARCHS}" = "" ]; then 118 NO_LEAK_TEST_ARCHS="${1}" 119 else 120 NO_LEAK_TEST_ARCHS="${NO_LEAK_TEST_ARCHS} ${1}" 121 fi 122} 123 124UpdateArchitecturesToTest() { 125 case "${NATIVE_ARCH_ACTUAL}" in 126 ppc) 127 if [ "${1}" = "ppc" ]; then 128 AppendToLeakTestArchs "${1}" 129 fi 130 ;; 131 132 ppc64) 133 if [ "${1}" = "ppc" -o "${1}" = "ppc64" ]; then 134 AppendToLeakTestArchs "${1}" 135 fi 136 ;; 137 138 i386) 139 if [ "${1}" = "i386" ]; then 140 AppendToLeakTestArchs "${1}" 141 elif [ "${1}" = "ppc" ]; then 142 AppendToNoLeakTestArchs "${1}" 143 fi 144 ;; 145 146 x86_64) 147 if [ "${1}" = "i386" -o "${1}" = "x86_64" ]; then 148 AppendToLeakTestArchs "${1}" 149 elif [ "${1}" = "ppc" -o "${1}" = "ppc64" ]; then 150 AppendToNoLeakTestArchs "${1}" 151 fi 152 ;; 153 154 *) 155 echo "RunMacOSUnitTests.sh Unknown native architecture: ${NATIVE_ARCH_ACTUAL}" 156 exit 1 157 ;; 158 esac 159} 160 161SetMemoryVariables() { 162 # Jack up some memory stress so we can catch more bugs. 163 164 # This is done via a helper so it can be invoked in two places at the 165 # last possible moment to avoid the variables causing tracing from other 166 # processes that are invoked along the way. 167 168 if [ $GTM_DISABLE_ZOMBIES -eq 0 ]; then 169 GTMXcodeNote ${LINENO} "Enabling zombies" 170 # CFZombieLevel disabled because it doesn't play well with the 171 # security framework 172 # export CFZombieLevel=3 173 export NSZombieEnabled=YES 174 fi 175 176 export MallocScribble=YES 177 export MallocPreScribble=YES 178 export MallocGuardEdges=YES 179 export NSAutoreleaseFreedObjectCheckEnabled=YES 180 181 # Turn on the mostly undocumented OBJC_DEBUG stuff. 182 export OBJC_DEBUG_FRAGILE_SUPERCLASSES=YES 183 export OBJC_DEBUG_UNLOAD=YES 184 # Turned off due to the amount of false positives from NS classes. 185 # export OBJC_DEBUG_FINALIZERS=YES 186 export OBJC_DEBUG_NIL_SYNC=YES 187} 188 189RunTests() { 190 if [ "${CURRENT_ARCH}" = "" ]; then 191 CURRENT_ARCH=`arch` 192 fi 193 194 if [ "${ONLY_ACTIVE_ARCH}" = "YES" ]; then 195 ARCHS="${CURRENT_ARCH}" 196 fi 197 198 if [ "${ARCHS}" = "" ]; then 199 ARCHS=`arch` 200 fi 201 202 if [ "${VALID_ARCHS}" = "" ]; then 203 VALID_ARCHS=`arch` 204 fi 205 206 if [ "${NATIVE_ARCH_ACTUAL}" = "" ]; then 207 NATIVE_ARCH_ACTUAL=`arch` 208 fi 209 210 LEAK_TEST_ARCHS="" 211 NO_LEAK_TEST_ARCHS="" 212 213 for TEST_ARCH in ${ARCHS}; do 214 for TEST_VALID_ARCH in ${VALID_ARCHS}; do 215 if [ "${TEST_VALID_ARCH}" = "${TEST_ARCH}" ]; then 216 UpdateArchitecturesToTest "${TEST_ARCH}" 217 fi 218 done 219 done 220 221 # These are symbols that leak on OS 10.5.5 222 # radar 6247293 NSCollatorElement leaks in +initialize. 223 AppendToSymbolsLeaksShouldIgnore "+[NSCollatorElement initialize]" 224 # radar 6247911 The first call to udat_open leaks only on x86_64 225 AppendToSymbolsLeaksShouldIgnore "icu::TimeZone::initDefault()" 226 # radar 6263983 +[IMService allServices] leaks 227 AppendToSymbolsLeaksShouldIgnore "-[IMServiceAgentImpl allServices]" 228 # radar 6264034 +[IKSFEffectDescription initialize] Leaks 229 AppendToSymbolsLeaksShouldIgnore "+[IKSFEffectDescription initialize]" 230 # radar 7598715 Leak when creating new NSColor using lab color space. 231 AppendToSymbolsLeaksShouldIgnore "CMSSetLabCLUT" 232 233 # Running leaks on architectures that support leaks. 234 export MallocStackLogging=YES 235 export GTM_LEAKS_SYMBOLS_TO_IGNORE="${GTM_LEAKS_SYMBOLS_TO_IGNORE}" 236 ARCHS="${LEAK_TEST_ARCHS}" 237 VALID_ARCHS="${LEAK_TEST_ARCHS}" 238 GTMXcodeNote ${LINENO} "Leak checking enabled for $ARCHS. Ignoring leaks from $GTM_LEAKS_SYMBOLS_TO_IGNORE." 239 SetMemoryVariables 240 MaybeFlock "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 241 242 # Running leaks on architectures that don't support leaks. 243 unset MallocStackLogging 244 unset GTM_ENABLE_LEAKS 245 ARCHS="${NO_LEAK_TEST_ARCHS}" 246 VALID_ARCHS="${NO_LEAK_TEST_ARCHS}" 247 GTMXcodeNote ${LINENO} "Leak checking disabled for $ARCHS due to no support for leaks on platform". 248 MaybeFlock "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 249} 250 251if [ ! $GTM_DO_NOT_REMOVE_GCOV_DATA ]; then 252 GTM_GCOV_CLEANUP_DIR="${CONFIGURATION_TEMP_DIR}" 253 if [ $GTM_REMOVE_TARGET_GCOV_ONLY ]; then 254 GTM_GCOV_CLEANUP_DIR="${OBJECT_FILE_DIR}-${CURRENT_VARIANT}" 255 fi 256 if [ "${GTM_GCOV_CLEANUP_DIR}" != "-" ]; then 257 if [ -d "${GTM_GCOV_CLEANUP_DIR}" ]; then 258 GTMXcodeNote ${LINENO} "Removing gcov data files from ${GTM_GCOV_CLEANUP_DIR}" 259 (cd "${GTM_GCOV_CLEANUP_DIR}" && \ 260 find . -type f -name "*.gcda" -print0 | xargs -0 rm -f ) 261 fi 262 fi 263fi 264 265# If leaks testing is enabled, we have to go through our convoluted path 266# to handle architectures that don't allow us to do leak testing. 267if [ $GTM_ENABLE_LEAKS -ne 0 ]; then 268 RunTests 269else 270 GTMXcodeNote ${LINENO} "Leak checking disabled." 271 SetMemoryVariables 272 MaybeFlock "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 273fi