aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/shell/bazel/apple/bazel_objc_test.sh
blob: 65cdd02043e5c1493557bfb1a15694f44abb97f7 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
#
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Load the test setup defined in the parent directory
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../../integration_test_setup.sh" \
  || { echo "integration_test_setup.sh not found!" >&2; exit 1; }

if [ "${PLATFORM}" != "darwin" ]; then
  echo "This test suite requires running on OS X" >&2
  exit 0
fi

function make_app() {
  rm -rf ios
  mkdir -p ios

  cat >ios/app.m <<EOF
#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  int retVal = UIApplicationMain(argc, argv, nil, nil);
  [pool release];
  return retVal;
}
EOF

  cat >ios/App-Info.plist <<EOF
<plist version="1.0">
<dict>
        <key>CFBundleExecutable</key>
        <string>app</string>
        <key>CFBundleName</key>
        <string>app</string>
        <key>CFBundleDisplayName</key>
        <string>app</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleIdentifier</key>
        <string>com.google.app</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
        <string>1.0</string>
        <key>LSRequiresIPhoneOS</key>
        <true/>
</dict>
</plist>
EOF

  cat >ios/PassTest-Info.plist <<EOF
<plist version="1.0">
<dict>
        <key>CFBundleExecutable</key>
        <string>PassingXcTest</string>
</dict>
</plist>
EOF

  cat >ios/passtest.m <<EOF
#import <XCTest/XCTest.h>

@interface PassingXcTest : XCTestCase

@end

@implementation PassingXcTest

- (void)testPass {
  XCTAssertEqual(1, 1, @"should pass");
}

@end
EOF

  cat >ios/BUILD <<EOF
objc_binary(name = "bin",
            non_arc_srcs = ['app.m'])
ios_application(name = "app",
                binary = ':bin',
                infoplist = 'App-Info.plist')
ios_test(name = 'PassingXcTest',
         srcs = ['passtest.m'],
         infoplist = "PassTest-Info.plist",
         xctest = True,
         xctest_app = ':app')
EOF
}

function test_build_app() {
  setup_objc_test_support
  make_app

  bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
      //ios:app >$TEST_log 2>&1 || fail "should pass"
  ls bazel-bin/ios/app.ipa || fail "should generate app.ipa"
}

function test_ios_test() {
  setup_objc_test_support
  make_app

  bazel build --test_output=all --ios_sdk_version=$IOS_SDK_VERSION \
      --ios_minimum_os=8.0 \
      //ios:PassingXcTest >$TEST_log 2>&1 || fail "should pass"
  ls bazel-bin/ios/PassingXcTest.ipa \
      || fail "should generate PassingXcTest.ipa"
}

function test_valid_ios_sdk_version() {
  setup_objc_test_support
  make_app

  bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
      //ios:app >$TEST_log 2>&1 || fail "should pass"
  ls bazel-bin/ios/app.ipa || fail "should generate app.ipa"
}

# Bazel caches mappings for ios sdk locations for local host execution.
# Verify that multiple invocations (with primed cache) work.
function test_xcrun_cache() {
  setup_objc_test_support
  make_app

  ! ls bazel-out/__xcruncache || fail "clean build should not have cache file"
  bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
      //ios:bin >$TEST_log 2>&1 || fail "should pass"
  ls bazel-out/__xcruncache || fail "xcrun cache should be present"
  bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
      //ios:app >$TEST_log 2>&1 || fail "should pass"
  ls bazel-bin/ios/app.ipa || fail "should generate app.ipa"
  ls bazel-out/__xcruncache || fail "xcrun cache should be present"

  bazel clean
  ! ls bazel-bin/__xcruncache || fail "xcrun cache should be removed on clean"
}

function test_invalid_ios_sdk_version() {
  setup_objc_test_support
  make_app

  ! bazel build --verbose_failures --ios_sdk_version=2.34 \
      //ios:app >$TEST_log 2>&1 || fail "should fail"
  expect_log "SDK \"iphonesimulator2.34\" cannot be located."
}

function test_xcodelocator_embedded_tool() {
  rm -rf ios
  mkdir -p ios

  cat >ios/BUILD <<EOF
genrule(
    name = "invoke_tool",
    srcs = ["@bazel_tools//tools/osx:xcode-locator"],
    outs = ["tool_output"],
    cmd = "\$< > \$@",
    tags = ["requires-darwin"],
)
EOF

  bazel build --verbose_failures //ios:invoke_tool >$TEST_log 2>&1 \
      || fail "should be able to resolve xcode-locator"
}

# Verifies contents of .a files do not contain timestamps -- if they did, the
# results would not be hermetic.
function test_archive_timestamps() {
  setup_objc_test_support

  mkdir -p objclib
  cat > objclib/BUILD <<EOF
objc_library(
    name = "objclib",
    srcs = ["mysrc.m"],
)
EOF

  cat > objclib/mysrc.m <<EOF
int aFunction() {
  return 0;
}
EOF

  bazel build --verbose_failures --apple_crosstool_transition \
      --ios_sdk_version=$IOS_SDK_VERSION //objclib:objclib >"$TEST_log" 2>&1 \
      || fail "Should build objc_library"

  # Based on timezones, ar -tv may show the timestamp of the contents as either
  # Dec 31 1969 or Jan 1 1970 -- either is fine.
  # We would use 'date' here, but the format is slightly different (Jan 1 vs.
  # Jan 01).
  ar -tv bazel-out/ios_x86_64-fastbuild/bin/objclib/libobjclib.a \
      | grep "mysrc" | grep "Dec 31" | grep "1969" \
      || ar -tv bazel-out/ios_x86_64-fastbuild/bin/objclib/libobjclib.a \
      | grep "mysrc" | grep "Jan  1" | grep "1970" || \
      fail "Timestamp of contents of archive file should be zero"
}

run_suite "objc/ios test suite"