Skip to content

Commit

Permalink
Avoid failing Obj-C tests when xcpretty is not installed (#24173)
Browse files Browse the repository at this point in the history
Summary:
Fixes an issue where `scripts/objc-test-ios.sh` would fail if `xcpretty` is not installed. As this tool is not bundled with macOS, and it's not explicitly called out in our docs on testing, the script should not fail if it's not present.

In a related change, JUnit reports are written to a more sensible location when the script is run outside of Circle CI.

[iOS] [Fixed] - Fixed test script failure when xcpretty is not present
Pull Request resolved: #24173

Differential Revision: D14726101

Pulled By: hramos

fbshipit-source-id: 9f3081a75a4a262f55aef8498241fe7d1e04b931
  • Loading branch information
hramos authored and facebook-github-bot committed Apr 3, 2019
1 parent 81b87e4 commit 8443487
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 53 deletions.
3 changes: 2 additions & 1 deletion scripts/objc-test-ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# also run the RNTester integration test (needs JS and packager):
# ./objc-test-ios.sh test

set -ex
set -e

SCRIPTS=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT=$(dirname "$SCRIPTS")
Expand All @@ -25,6 +25,7 @@ export TEST_NAME="iOS"
export SCHEME="RNTester"
export SDK="iphonesimulator"
export DESTINATION="platform=iOS Simulator,name=${IOS_DEVICE},OS=${IOS_TARGET_OS}"
export USE_MODERN_BUILD_SYSTEM="NO"

# If there's a "test" argument, pass it to the test script.
./scripts/objc-test.sh $1
1 change: 1 addition & 0 deletions scripts/objc-test-tvos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export TEST_NAME="tvOS"
export SCHEME="RNTester-tvOS"
export SDK="appletvsimulator"
export DESTINATION="platform=tvOS Simulator,name=${TVOS_DEVICE},OS=${IOS_TARGET_OS}"
export USE_MODERN_BUILD_SYSTEM="NO"

# If there's a "test" argument, pass it to the test script.
./scripts/objc-test.sh $1
130 changes: 78 additions & 52 deletions scripts/objc-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
# also run the RNTester integration test (needs JS and packager).
# ./objc-test.sh test

set -ex

SCRIPTS=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT=$(dirname "$SCRIPTS")

cd "$ROOT"

# Create cleanup handler
function cleanup {
cleanup() {
EXIT=$?
set +e

Expand All @@ -34,14 +30,13 @@ function cleanup {
# kill whatever is occupying port 5555 (web socket server)
lsof -i tcp:5555 | awk 'NR!=1 {print $2}' | xargs kill
}
trap cleanup EXIT

# Wait for the package to start
function waitForPackager {
waitForPackager() {
local -i max_attempts=60
local -i attempt_num=1

until $(curl -s http://localhost:8081/status | grep "packager-status:running" -q); do
until curl -s http://localhost:8081/status | grep "packager-status:running" -q; do
if (( attempt_num == max_attempts )); then
echo "Packager did not respond in time. No more attempts left."
exit 1
Expand All @@ -55,47 +50,78 @@ function waitForPackager {
echo "Packager is ready!"
}

# If first argument is "test", actually start the packager and run tests.
# Otherwise, just build RNTester for tvOS and exit

if [ "$1" = "test" ]; then

# Start the packager
yarn start --max-workers=1 || echo "Can't start packager automatically" &
# Start the WebSocket test server
open "./IntegrationTests/launchWebSocketServer.command" || echo "Can't start web socket server automatically"

waitForPackager

# Preload the RNTesterApp bundle for better performance in integration tests
curl 'http://localhost:8081/RNTester/js/RNTesterApp.ios.bundle?platform=ios&dev=true' -o temp.bundle
rm temp.bundle
curl 'http://localhost:8081/RNTester/js/RNTesterApp.ios.bundle?platform=ios&dev=true&minify=false' -o temp.bundle
rm temp.bundle
curl 'http://localhost:8081/IntegrationTests/IntegrationTestsApp.bundle?platform=ios&dev=true' -o temp.bundle
rm temp.bundle
curl 'http://localhost:8081/IntegrationTests/RCTRootViewIntegrationTestApp.bundle?platform=ios&dev=true' -o temp.bundle
rm temp.bundle

# Run tests
xcodebuild \
-project "RNTester/RNTester.xcodeproj" \
-scheme "$SCHEME" \
-sdk "$SDK" \
-destination "$DESTINATION" \
-UseModernBuildSystem=NO \
build test \
| xcpretty --report junit --output "$HOME/react-native/reports/junit/$TEST_NAME/results.xml" \
&& exit "${PIPESTATUS[0]}"

else

# Don't run tests. No need to pass -destination to xcodebuild.
xcodebuild \
-project "RNTester/RNTester.xcodeproj" \
-scheme "$SCHEME" \
-sdk "$SDK" \
-UseModernBuildSystem=NO \
build

fi
runTests() {
xcodebuild \
-project "RNTester/RNTester.xcodeproj" \
-scheme "$SCHEME" \
-sdk "$SDK" \
-destination "$DESTINATION" \
-UseModernBuildSystem="$USE_MODERN_BUILD_SYSTEM" \
build test
}

buildProject() {
xcodebuild \
-project "RNTester/RNTester.xcodeproj" \
-scheme "$SCHEME" \
-sdk "$SDK" \
-UseModernBuildSystem="$USE_MODERN_BUILD_SYSTEM" \
build
}

xcprettyFormat() {
if [ "$CI" ]; then
# Circle CI expects JUnit reports to be available here
REPORTS_DIR="$HOME/react-native/reports"
else
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)

# Write reports to the react-native root dir
REPORTS_DIR="$THIS_DIR/../build/reports"
fi

xcpretty --report junit --output "$REPORTS_DIR/junit/$TEST_NAME/results.xml"
}

preloadBundles() {
# Preload the RNTesterApp bundle for better performance in integration tests
curl -s 'http://localhost:8081/RNTester/js/RNTesterApp.ios.bundle?platform=ios&dev=true' -o /dev/null
curl -s 'http://localhost:8081/RNTester/js/RNTesterApp.ios.bundle?platform=ios&dev=true&minify=false' -o /dev/null
curl -s 'http://localhost:8081/IntegrationTests/IntegrationTestsApp.bundle?platform=ios&dev=true' -o /dev/null
curl -s 'http://localhost:8081/IntegrationTests/RCTRootViewIntegrationTestApp.bundle?platform=ios&dev=true' -o /dev/null
}

main() {
cd "$ROOT" || exit

# If first argument is "test", actually start the packager and run tests.
# Otherwise, just build RNTester and exit
if [ "$1" = "test" ]; then

# Start the packager
yarn start --max-workers=1 || echo "Can't start packager automatically" &
# Start the WebSocket test server
open "./IntegrationTests/launchWebSocketServer.command" || echo "Can't start web socket server automatically"

waitForPackager
preloadBundles

# Build and run tests.
if [ -x "$(command -v xcpretty)" ]; then
runTests | xcprettyFormat && exit "${PIPESTATUS[0]}"
else
echo 'Warning: xcpretty is not installed. Install xcpretty to generate JUnit reports.'
runTests
fi
else
# Build without running tests.
if [ -x "$(command -v xcpretty)" ]; then
buildProject | xcprettyFormat && exit "${PIPESTATUS[0]}"
else
buildProject
fi
fi
}

trap cleanup EXIT
main "$@"

0 comments on commit 8443487

Please sign in to comment.