uber / ios-snapshot-test-case

Snapshot view unit tests for iOS
MIT License
1.79k stars 211 forks source link

Update to latest bazel, rules #189

Open alanzeino opened 1 year ago

alanzeino commented 1 year ago

Attempting to update the repo so that the build.sh script that is run in the GitHub Action succeeds.

alanzeino commented 1 year ago
$ bazelisk test //src/iOSSnapshotTestCaseTests:iOSSnapshotTestCaseTests --test_output=all

INFO: Analyzed target //src/iOSSnapshotTestCaseTests:iOSSnapshotTestCaseTests (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
ERROR: /Users/alanz/repositories/github/ios-snapshot-test-case/src/iOSSnapshotTestCaseTests/BUILD.bazel:3:13: Compiling src/iOSSnapshotTestCaseTests/FBSnapshotControllerTests.m failed: (Exit 1): wrapped_clang failed: error executing command (from target //src/iOSSnapshotTestCaseTests:iOSSnapshotTestCaseTestsLib) external/local_config_cc/wrapped_clang -target arm64-apple-ios11.0-simulator '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 ... (remaining 43 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
src/iOSSnapshotTestCaseTests/FBSnapshotControllerTests.m:12:9: fatal error: module 'iOSSnapshotTestCaseCore' not found
@import iOSSnapshotTestCaseCore;
 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Error in child process '/usr/bin/xcrun'. 1
Target //src/iOSSnapshotTestCaseTests:iOSSnapshotTestCaseTests failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.204s, Critical Path: 1.12s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully
//src/iOSSnapshotTestCaseTests:iOSSnapshotTestCaseTests         FAILED TO BUILD

Executed 0 out of 1 test: 1 fails to build.

@chiragramani I've never used Bazel outside of our Uber context — do you know why this target can't use this import? It may have worked in the past with our old rules and bazel version. I also found this:

        # With the flip of `--incompatible_objc_framework_cleanup`, the
        # `objc_library` implementation in Bazel no longer passes module
        # maps as inputs to the compile actions, so that `@import`
        # statements for user-provided framework no longer work in a
        # sandbox. This trap door allows users to continue using `@import`
        # statements for imported framework by adding module map to
        # header_imports so that they are included in Obj-C compilation but
        # they aren't processed in any way.
luispadron commented 8 months ago

@alanzeino Thanks for putting up this PR, we'd love the new rules being supported as well where I work. I found that the following diff on the bazel branch is what you need to get CI passing (and it removes the need for #if BAZEL):

diff --git a/BUILD.bazel b/BUILD.bazel
index 4d276f7..da15ca6 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -11,6 +11,9 @@ objc_library(
     defines = [
         "SWIFT_PACKAGE"
     ],
+    includes = [
+        "src/iOSSnapshotTestCaseCore/Public"
+    ],
     copts = [
         "-Wno-deprecated-declarations"
     ],
@@ -25,5 +28,6 @@ swift_library(
     deps = [
         ":iOSSnapshotTestCaseCore"
     ],
+    testonly = True,
     visibility = ["//visibility:public"]
 )
diff --git a/src/iOSSnapshotTestCaseTests/BUILD.bazel b/src/iOSSnapshotTestCaseTests/BUILD.bazel
index 0c63a59..6b59b7b 100644
--- a/src/iOSSnapshotTestCaseTests/BUILD.bazel
+++ b/src/iOSSnapshotTestCaseTests/BUILD.bazel
@@ -7,9 +7,6 @@ objc_library(
     deps = [
         "//:iOSSnapshotTestCaseCore"
     ],
-    defines = [
-        "BAZEL"
-    ],
     data = glob(["Resources/*.png"], allow_empty = False),
     sdk_frameworks = [
         "CoreGraphics",
diff --git a/src/iOSSnapshotTestCaseTests/FBSnapshotControllerTests.m b/src/iOSSnapshotTestCaseTests/FBSnapshotControllerTests.m
index 9331038..999a23b 100644
--- a/src/iOSSnapshotTestCaseTests/FBSnapshotControllerTests.m
+++ b/src/iOSSnapshotTestCaseTests/FBSnapshotControllerTests.m
@@ -8,14 +8,8 @@
  */

 #import <XCTest/XCTest.h>
-#if BAZEL
-@import iOSSnapshotTestCaseCore;
-@import UIKit;
-@import CoreGraphics;
-@import QuartzCore;
-#else
+
 #import "FBSnapshotTestCase.h"
-#endif

 @interface FBSnapshotControllerTests : XCTestCase