-
Notifications
You must be signed in to change notification settings - Fork 19
/
build_and_run_unit_tests.sh
executable file
·88 lines (75 loc) · 3.33 KB
/
build_and_run_unit_tests.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Script to compile and run unit tests from the command line
# The scheme and target name of the main app
MAIN_APP_TARGET="$1"
# The scheme and target name of the unit tests
UNIT_TEST_TARGET="$2"
# The path to libXcodeTest.a, if not in current directory
PATH_TO_XCODE_TEST_LIB="$3"
# Output variable defaults to current directory of not specified
LINK_TO_XCODE_TEST_LIB=""
if [[ "${PATH_TO_XCODE_TEST_LIB}" != "" ]]; then
XCODE_TEST_ABS_LIB_PATH="${PWD}/${PATH_TO_XCODE_TEST_LIB}"
LINK_TO_XCODE_TEST_LIB="-lXcodeTest -L \"${XCODE_TEST_ABS_LIB_PATH}\""
else
CURRENT_PATH="${PWD}"
LINK_TO_XCODE_TEST_LIB="-lXcodeTest -L\"${CURRENT_PATH}\""
fi
# Calculate the variables to feed into the build
OUTPUT_DIR="/tmp/xcodetest/${MAIN_APP_TARGET}"
XCODE_TEST_PATH="${OUTPUT_DIR}/${UNIT_TEST_TARGET}.octest/${UNIT_TEST_TARGET}"
XCODE_TEST_LDFLAGS="-ObjC -framework SenTestingKit ${LINK_TO_XCODE_TEST_LIB} -F \"$\(SDKROOT\)/Developer/Library/Frameworks\""
# More reliable if the simulator is not already running
osascript -e 'tell app "iPhone Simulator" to quit'
# Build the unit tests bundle, so it can be fed into waxsim
echo "========================="
echo "Building unit test bundle"
echo "========================="
echo "xcodebuild -sdk iphonesimulator -scheme ${UNIT_TEST_TARGET} build CONFIGURATION_BUILD_DIR=\"${OUTPUT_DIR}\""
echo "========================="
xcodebuild -sdk iphonesimulator -scheme "${UNIT_TEST_TARGET}" build CONFIGURATION_BUILD_DIR="${OUTPUT_DIR}"
if [[ $? != 0 ]]; then
echo "Failed to build unit tests!"
exit $?
fi
# Build the main app, with libXcodeTest.a linked in
echo "==========================="
echo "Building app with xcodetest"
echo "==========================="
echo "xcodebuild -sdk iphonesimulator -scheme ${MAIN_APP_TARGET} build CONFIGURATION_BUILD_DIR=\"${OUTPUT_DIR}\" XCODE_TEST_LDFLAGS=\"${XCODE_TEST_LDFLAGS}\""
echo "==========================="
xcodebuild -sdk iphonesimulator -scheme "${MAIN_APP_TARGET}" build CONFIGURATION_BUILD_DIR="${OUTPUT_DIR}" XCODE_TEST_LDFLAGS="${XCODE_TEST_LDFLAGS}"
if [[ $? != 0 ]]; then
echo "Failed to build app!"
exit $?
fi
# Check that waxsim is installed, used to run the app in the simulator
which waxsim
if [[ $? != 0 ]]; then
echo "Could not find 'waxsim', make sure it is installed and try again"
exit $?
fi
# Warn users that it wont run the tests unless you tweak the linker settings
echo "================="
echo "If tests do not run, make sure you have included XCODE_TEST_LDFLAGS in your linker flags:"
echo " In xcconfigs: OTHER_LDFLAGS = \$(inherited) \$(XCODE_TEST_LDFLAGS)"
echo " In Xcode: set Other Linker Flags to include \$(XCODE_TEST_LDFLAGS)"
echo "================="
# Run the app in the simulator, will automatically load and run unit tests
OUT_FILE="${OUTPUT_DIR}/waxsim.out"
XCODE_TEST_PATH="${XCODE_TEST_PATH}" waxsim "${OUTPUT_DIR}/${MAIN_APP_TARGET}.app" -SenTest All > "${OUT_FILE}" 2>&1
cat "${OUT_FILE}"
osascript -e 'tell app "iPhone Simulator" to quit'
# if there was a failure, show what waxsim was hiding and crucially return with a non-zero exit code
grep -q ": error:" "$OUT_FILE"
success=`exec grep -c ": error:" $OUT_FILE`
if [[ $success != 0 ]]; then
echo "================="
echo "Unit Tests Failed"
echo "================="
exit 1
else
echo "================="
echo "Unit Tests Passed"
echo "================="
fi