Skip to content

Commit

Permalink
Add Bazel build support for swift-snapshot-testing
Browse files Browse the repository at this point in the history
Refactors the Bazel support to better match the Package.swift structure:

- Makes the following targets available: `AccessibilitySnapshotCore`, `AccessibilitySnapshotCore_ObjC`, `FBSnapshotTestCase_Accessibility`, `FBSnapshotTestCase_Accessibility_ObjC`, `AccessibilitySnapshot`.
- The module `AccessibilitySnapshot` uses `swift-snapshot-testing` and the `FBSnapshotTestCase_Accessibility*` modules use `iOSSnapshotTestCase`.
- Adds custom BUILD file for `swift-snapshot-testing` to allow it to build with Bazel. I opted for this vs. registry because it's a simple BUILD files.
  • Loading branch information
luispadron committed Jan 23, 2024
1 parent adb65ef commit f234c83
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 102 deletions.
101 changes: 0 additions & 101 deletions BUILD.bazel

This file was deleted.

11 changes: 10 additions & 1 deletion Bazel/non_bzlmod_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# -- Non-bzlmod versions

IOS_SNAPSHOT_TEST_CASE_VERSION = "57b023c8bb3df361e2fae01532cd066ec0b65d2e"
IOS_SNAPSHOT_TEST_CASE_VERSION = "57b023c8bb3df361e2fae01532cd066ec0b65d2e" # NOTE: this is 8.0.0 with some Bazel build fixes
SWIFT_SNAPSHOT_TESTING_VERSION = "1.8.2"

# -- Module extension

Expand All @@ -20,6 +21,14 @@ def non_bzlmod_repositories():
patch_args = ["-p1"],
)

http_archive(
name = "swift_snapshot_testing",
url = "https://github.com/pointfreeco/swift-snapshot-testing/archive/refs/tags/%s.tar.gz" % SWIFT_SNAPSHOT_TESTING_VERSION,
strip_prefix = "swift-snapshot-testing-%s" % SWIFT_SNAPSHOT_TESTING_VERSION,
sha256 = "f924de0b1e326b108120593e802cd0b6577edf7fbb8a87c6841a428722d3b14d",
build_file = "//Bazel:swift_snapshot_testing.BUILD.bazel",
)

def _non_bzlmod_deps_impl(_):
non_bzlmod_repositories()

Expand Down
14 changes: 14 additions & 0 deletions Bazel/swift_snapshot_testing.BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Defines build targets for pointfreeco/swift-snapshot-testing.
"""

load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")

swift_library(
name = "SnapshotTesting",
srcs = glob(["Sources/SnapshotTesting/**/*.swift"]),
deps = [],
tags = ["manual"],
visibility = ["//visibility:public"],
testonly = True,
)
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ non_bzlmod_deps = use_extension("//Bazel:non_bzlmod_deps.bzl", "non_bzlmod_deps"
use_repo(
non_bzlmod_deps,
"ios_snapshot_test_case",
"swift_snapshot_testing",
)

apple_cc_configure = use_extension("@apple_support//crosstool:setup.bzl", "apple_cc_configure_extension")
Expand Down
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,71 @@ github "cashapp/AccessibilitySnapshot"
```
</details>

<details>
<summary><h3>Bazel</h3></summary>

Add the following to your `MODULE.bazel` file:

```starlark
bazel_dep(
name = "accessibility_snapshot",
version = "x.x.x",
)
```

Use the provided targets in the `BUILD.bazel` files. There is a `*_library` target for each target suffixed with `.lib` that can be used as a dependency. Without the suffix you will get an `ios_framework` target (dynamic).

```starlark

swift_library(
name = "MyLibrary",
...,
deps = [
"//Sources/AccessibilitySnapshot/Core:AccessibilitySnapshotCore.lib",
],
)
```

To use [iOSSnapshotTestCase](https://github.com/uber/ios-snapshot-test-case) you can add a dependency on the `//Sources/AccessibilitySnapshot/iOSSnapshotTestCase:AccessibilitySnapshot_iOSSnapshotTestCase` targets.

For example:

```starlark
swift_test(
name = "MyLibraryTests",
...,
deps = [
"//Sources/AccessibilitySnapshot/Core:AccessibilitySnapshotCore.lib",
"//Sources/AccessibilitySnapshot/iOSSnapshotTestCase:AccessibilitySnapshot_iOSSnapshotTestCase",
],
)
```

```swift
import AccessibilitySnapshot
```

To use [SnapshotTesting](https://github.com/pointfreeco/swift-snapshot-testing) you can add a dependency on the `//Sources/AccessibilitySnapshot/SnapshotTesting:AccessibilitySnapshot_SnapshotTesting` targets.

For example:

```starlark
swift_test(
name = "MyLibraryTests",
...,
deps = [
"//Sources/AccessibilitySnapshot/Core:AccessibilitySnapshotCore.lib",
"//Sources/AccessibilitySnapshot/SnapshotTesting:AccessibilitySnapshot_SnapshotTesting",
],
)
```

```swift
import AccessibilitySnapshot
```

</details>

## Usage

AccessibilitySnapshot builds on top of existing snapshot frameworks to add support for snapshotting your app's accessibility. By default it uses the [SnapshotTesting](https://github.com/pointfreeco/swift-snapshot-testing) framework for snapshotting, but can be switched over to [iOSSnapshotTestCase](https://github.com/uber/ios-snapshot-test-case) as well.
Expand Down
73 changes: 73 additions & 0 deletions Sources/AccessibilitySnapshot/Core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
load(
"@build_bazel_rules_apple//apple:ios.bzl",
"ios_framework",
)
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"swift_library",
)

# -- AccessibilitySnapshotCore

swift_library(
name = "AccessibilitySnapshotCore.lib",
srcs = glob(
["Swift/**/*.swift"],
allow_empty = False,
),
data = glob(
["Swift/Assets/**/*"],
allow_empty = True,
),
defines = ["BAZEL_PACKAGE"],
module_name = "AccessibilitySnapshotCore",
tags = ["manual"],
visibility = ["//visibility:public"],
deps = [":AccessibilitySnapshotCore_ObjC.lib"],
)

ios_framework(
name = "AccessibilitySnapshotCore",
bundle_id = "com.squareup.AccessibilitySnapshotCore",
families = [
"iphone",
"ipad",
],
infoplists = ["Info.plist"],
minimum_os_version = "13.0",
visibility = ["//visibility:public"],
deps = [":AccessibilitySnapshotCore.lib"],
)

# -- AccessibilitySnapshotCore_ObjC

objc_library(
name = "AccessibilitySnapshotCore_ObjC.lib",
srcs = glob(
["ObjC/**/*.m"],
allow_empty = False,
),
hdrs = glob(
["ObjC/include/**/*.h"],
allow_empty = False,
),
defines = ["BAZEL_PACKAGE"],
includes = ["ObjC/include"],
module_name = "AccessibilitySnapshotCore_ObjC",
tags = ["manual"],
visibility = ["//visibility:public"],
deps = [],
)

ios_framework(
name = "AccessibilitySnapshotCore_ObjC",
bundle_id = "com.squareup.AccessibilitySnapshotCore_ObjC",
families = [
"iphone",
"ipad",
],
infoplists = ["Info.plist"],
minimum_os_version = "13.0",
visibility = ["//visibility:public"],
deps = [":AccessibilitySnapshotCore_ObjC.lib"],
)
File renamed without changes.
37 changes: 37 additions & 0 deletions Sources/AccessibilitySnapshot/SnapshotTesting/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load(
"@build_bazel_rules_apple//apple:ios.bzl",
"ios_framework",
)
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"swift_library",
)

swift_library(
name = "AccessibilitySnapshot_SnapshotTesting.lib",
testonly = True,
srcs = glob(["**/*.swift"]),
defines = ["BAZEL_PACKAGE"],
module_name = "AccessibilitySnapshot",
tags = ["manual"],
visibility = ["//visibility:public"],
deps = [
"//Sources/AccessibilitySnapshot/Core:AccessibilitySnapshotCore.lib",
"//Sources/AccessibilitySnapshot/Core:AccessibilitySnapshotCore_ObjC.lib",
"@swift_snapshot_testing//:SnapshotTesting",
],
)

ios_framework(
name = "AccessibilitySnapshot_SnapshotTesting",
testonly = True,
bundle_id = "com.squareup.AccessibilitySnapshot",
families = [
"iphone",
"ipad",
],
infoplists = ["Info.plist"],
minimum_os_version = "13.0",
visibility = ["//visibility:public"],
deps = [":AccessibilitySnapshot_SnapshotTesting.lib"],
)
24 changes: 24 additions & 0 deletions Sources/AccessibilitySnapshot/SnapshotTesting/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
Loading

0 comments on commit f234c83

Please sign in to comment.