如何使用 Buck 为 Swift 应用程序构建 SBTUITestTunnel?

问题描述 投票:0回答:0

我正在尝试使用 BuckSBTUITestTunnel 库包含在我正在开发的几个 iOS 应用程序中。该库扩展了标准 Xcode UI 测试框架,以提供其他功能,例如监控网络调用。虽然 SBTUITestTunnel 主要是用 Objective-C 编写的,但我希望包含此库的应用程序是用 Swift 编写的,并使用 Buck 作为构建工具。我还希望能够在编写测试后使用 Buck 和 Xcode 来执行测试。

不幸的是,SBTUITestTunnel Installation/Setup/Usage 文档似乎没有提供任何使用 Buck 构建它们的指导。该存储库确实包含 CocoaPods 配置文件,我已尝试将其转换为 BUCK 文件。然而,经过多次尝试后,我一直无法使其正常工作。我什至尝试从提供的

SBTUITestTunnel 示例应用程序
的构建输出中获取预构建的 .framework,但仍然没有运气。

以下是我迄今为止在创建工作 BUCK 文件方面所做的尝试:

尝试 1 - 从源代码构建库:

代码:

apple_library(
  name = 'SBTUITestTunnelCommon',
  visibility = ['PUBLIC'],
  swift_version = '5',
  exported_headers = glob([
    'Pod/Common/**/*.h',
  ]),
  srcs = glob([
    'Pod/Common/**/*.swift',
    'Pod/Common/**/*.m',
  ]),
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
  ]
)

apple_library(
  name = 'SBTUITestTunnelClient',
  visibility = ['PUBLIC'],
  swift_version = '5',
  exported_headers = glob([
    'Pod/Client/**/*.h',
  ]),
  srcs = glob([
    'Pod/Client/**/*.swift',
    'Pod/Client/**/*.m',
  ]),
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
    '//Path/SBTUITestTunnel:SBTUITestTunnelCommon',
  ]
)

apple_library(
  name = 'SBTUITestTunnelServer',
  visibility = ['PUBLIC'],
  swift_version = '5',
  exported_headers = glob([
    'Pod/Server/**/*.h',
  ]),
  srcs = glob([
    'Pod/Server/**/*.swift',
    'Pod/Server/**/*.m',
  ]),
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
    '//Path/SBTUITestTunnel:SBTUITestTunnelCommon',
  ]
)

结果:

Buck 构建成功完成,但在 Buck 中运行测试并尝试在 Xcode 中构建都因类似以下错误而失败(当尝试从

@import SBTUITestTunnelCommon;
类中的文件运行
SBTUITestTunnelServer
时):

Module 'SBTUITestTunnelCommon' not found

尝试 2 - 从源代码构建为框架:

代码:

sbt_ui_test_tunnel_common_bundle_name = 'SBTUITestTunnelCommon'
sbt_ui_test_tunnel_common_binary_name = sbt_ui_test_tunnel_common_bundle_name + 'Binary'
sbt_ui_test_tunnel_common_module_name = sbt_ui_test_tunnel_common_bundle_name
sbt_ui_test_tunnel_common_product_name = sbt_ui_test_tunnel_common_bundle_name
# Copied `Info.plist` from the Example app to this Pod folder:
sbt_ui_test_tunnel_common_info_plist = 'Pod/Common/' + sbt_ui_test_tunnel_common_bundle_name + '-Info.plist'

sbt_ui_test_tunnel_client_bundle_name = 'SBTUITestTunnelClient'
sbt_ui_test_tunnel_client_binary_name = sbt_ui_test_tunnel_client_bundle_name + 'Binary'
sbt_ui_test_tunnel_client_module_name = sbt_ui_test_tunnel_client_bundle_name
sbt_ui_test_tunnel_client_product_name = sbt_ui_test_tunnel_client_bundle_name
# Copied `Info.plist` from the Example app to this Pod folder:
sbt_ui_test_tunnel_client_info_plist = 'Pod/Client/' + sbt_ui_test_tunnel_client_bundle_name + '-Info.plist'

sbt_ui_test_tunnel_server_bundle_name = 'SBTUITestTunnelServer'
sbt_ui_test_tunnel_server_binary_name = sbt_ui_test_tunnel_server_bundle_name + 'Binary'
sbt_ui_test_tunnel_server_module_name = sbt_ui_test_tunnel_server_bundle_name
sbt_ui_test_tunnel_server_product_name = sbt_ui_test_tunnel_server_bundle_name
# Copied `Info.plist` from the Example app to this Pod folder:
sbt_ui_test_tunnel_server_info_plist = 'Pod/Server/' + sbt_ui_test_tunnel_server_bundle_name + '-Info.plist'

apple_binary(
  name = sbt_ui_test_tunnel_common_binary_name,
  visibility = ['PUBLIC'],
  module_name = sbt_ui_test_tunnel_common_module_name,
  swift_version = '5',
  linker_flags = [
    '-ObjC', # Have tried with and without this
  ],
  link_style = 'shared',
  exported_headers = glob([
    'Pod/Common/**/*.h',
  ]),
  srcs = glob([
    'Pod/Common/**/*.swift',
    'Pod/Common/**/*.m',
  ]),
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
  ]
)

apple_bundle(
  name = sbt_ui_test_tunnel_common_bundle_name,
  visibility = ['PUBLIC'],
  product_name = sbt_ui_test_tunnel_common_product_name,
  binary = ':' + sbt_ui_test_tunnel_common_binary_name,
  extension = 'framework',
  xcode_product_type = 'com.apple.product-type.framework',
  info_plist = sbt_ui_test_tunnel_common_info_plist,
)

apple_binary(
  name = sbt_ui_test_tunnel_client_binary_name,
  visibility = ['PUBLIC'],
  module_name = sbt_ui_test_tunnel_client_module_name,
  swift_version = '5',
  linker_flags = [
    '-ObjC', # Have tried with and without this
  ],
  link_style = 'shared',
  exported_headers = glob([
    'Pod/Client/**/*.h',
  ]),
  srcs = glob([
    'Pod/Client/**/*.swift',
    'Pod/Client/**/*.m',
  ]),
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
    '//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_binary_name,
  ]
)

apple_bundle(
  name = sbt_ui_test_tunnel_client_bundle_name,
  visibility = ['PUBLIC'],
  product_name = sbt_ui_test_tunnel_client_product_name,
  binary = ':' + sbt_ui_test_tunnel_client_binary_name,
  extension = 'framework',
  xcode_product_type = 'com.apple.product-type.framework',
  info_plist = sbt_ui_test_tunnel_client_info_plist,
)

apple_binary(
  name = sbt_ui_test_tunnel_server_binary_name,
  visibility = ['PUBLIC'],
  module_name = sbt_ui_test_tunnel_server_module_name,
  swift_version = '5',
enter code here
  linker_flags = [
    '-ObjC', # Have tried with and without this
  ],
  link_style = 'shared',
  exported_headers = glob([
    'Pod/Server/**/*.h',
  ]),
  srcs = glob([
    'Pod/Server/**/*.swift',
    'Pod/Server/**/*.m',
  ]),
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
    '//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_binary_name,
  ]
)

apple_bundle(
  name = sbt_ui_test_tunnel_server_bundle_name,
  visibility = ['PUBLIC'],
  product_name = sbt_ui_test_tunnel_server_product_name,
  binary = ':' + sbt_ui_test_tunnel_server_binary_name,
  extension = 'framework',
  xcode_product_type = 'com.apple.product-type.framework',
  info_plist = sbt_ui_test_tunnel_server_info_plist,
)

结果:

Buck 构建失败并显示以下错误消息:

Command failed with exit code 1.
stderr: ld: warning: -sdk_version and -platform_version are not compatible, ignoring - 
sdk_version
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

尝试 3 - 构建为
prebuilt_apple_framework
s:

代码:

sbt_ui_test_tunnel_common_framework_name = 'SBTUITestTunnelCommon'
# Copied .framework file from build output of the Example app to this Frameworks folder:
sbt_ui_test_tunnel_common_framework_path = 'Frameworks/' + sbt_ui_test_tunnel_common_framework_name + '/' + sbt_ui_test_tunnel_common_framework_name + '.framework'

sbt_ui_test_tunnel_client_framework_name = 'SBTUITestTunnelClient'
# Copied .framework file from build output of the Example app to this Frameworks folder:
sbt_ui_test_tunnel_client_framework_path = 'Frameworks/' + sbt_ui_test_tunnel_common_framework_name + '/' + sbt_ui_test_tunnel_common_framework_name + '.framework'

sbt_ui_test_tunnel_server_framework_name = 'SBTUITestTunnelServer'
# Copied .framework file from build output of the Example app to this Frameworks folder:
sbt_ui_test_tunnel_server_framework_path = 'Frameworks/' + sbt_ui_test_tunnel_common_framework_name + '/' + sbt_ui_test_tunnel_common_framework_name + '.framework'

prebuilt_apple_framework(
  name = sbt_ui_test_tunnel_common_framework_name,
  framework = sbt_ui_test_tunnel_common_framework_path,
  preferred_linkage = 'stat',
  visibility = [
    'PUBLIC'
  ],
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
  ],
)

prebuilt_apple_framework(
  name = sbt_ui_test_tunnel_client_framework_name,
  framework = sbt_ui_test_tunnel_client_framework_path,
  preferred_linkage = 'shared',
  visibility = [
    'PUBLIC'
  ],
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
    '//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_framework_name,
  ],
)

prebuilt_apple_framework(
  name = sbt_ui_test_tunnel_server_framework_name,
  framework = sbt_ui_test_tunnel_server_framework_path,
  preferred_linkage = 'shared',
  visibility = [
    'PUBLIC'
  ],
  deps = [
    '//Path/GCDWebServer:GCDWebServer',
    '//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_framework_name,
  ],
)

结果:

Buck 构建失败并显示以下错误消息:

Command failed with exit code 1.
stderr: /Users/me/path/BridgingHeader.h:12:9: error: 'SBTUITestTunnelServer/SBTUITestTunnelServer.h' file not found
#import <SBTUITestTunnelServer/SBTUITestTunnelServer.h>
        ^
<unknown>:0: error: failed to import bridging header 'path/BridgingHeader.h'

补充说明:

  • 我已遵循文档中的所有步骤,包括设置

    DEBUG
    /
    DEBUG=1
    设置并在
    BridgingHeaders.h
    文件中添加必要的依赖项,如下所示:

    • 在要测试的库的桥接头文件中:

      #import <SBTUITestTunnelCommon/SBTUITestTunnelCommon.h>
      #import <SBTUITestTunnelServer/SBTUITestTunnelServer.h>
      
    • 在具有测试的库的桥接头文件中:

      #import <SBTUITestTunnelClient/SBTUITunneledApplication.h>
      #import <SBTUITestTunnelClient/XCTestCase+AppExtension.h>
      #import <SBTUITestTunnelCommon/SBTUITestTunnelCommon.h> // Have tried with and without this line
      
  • 我将 SBTUITestTunnel 库/框架作为依赖项包含在我的主应用程序的 BUCK 文件中,如下所示:

        apple_library(
          ...
          bridging_header = 'BridgingHeader.h',
          deps = [
            '//Path/SBTUITestTunnel:SBTUITestTunnelCommon',
            '//Path/SBTUITestTunnel:SBTUITestTunnelServer',
            '//Path/GCDWebServer:GCDWebServer',
          ]
        )
    
        apple_test(
          ...
          bridging_header = 'UITests/BridgingHeader.h',
          deps = [
            '//Path/SBTUITestTunnel:SBTUITestTunnelClient',
          ]
        )
    
  • SBTUITestTunnel 依赖于另一个库,GCDWebServer,它也不提供 BUCK 文件。然而,我已经能够使用这个来工作:

    apple_library(
      name = 'GCDWebServer',
      visibility = ['PUBLIC'],
      swift_version = '5',
      exported_headers = glob([
        'GCDWebServer/**/*.h',
      ]),
      srcs = glob([
        'GCDWebServer/**/*.swift',
        'GCDWebServer/**/*.m',
      ])
    )
    
    apple_library(
      name = 'GCDWebDAVServer',
      visibility = ['PUBLIC'],
      swift_version = '5',
      exported_headers = glob([
        'GCDWebDAVServer/**/*.h',
      ]),
      srcs = glob([
        'GCDWebDAVServer/*.swift',
        'GCDWebDAVServer/*.m',
      ]),
      deps = [
        '//Path/GCDWebServer:GCDWebServer',
      ]
    )
    
    apple_library(
      name = 'GCDWebUploader',
      visibility = ['PUBLIC'],
      swift_version = '5',
      exported_headers = glob([
        'GCDWebUploader/**/*.h',
      ]),
      srcs = glob([
        'GCDWebUploader/**/*.swift',
        'GCDWebUploader/**/*.m',
      ]),
      deps = [
        '//Path/GCDWebServer:GCDWebServer',
      ]
    )
    

知道我做错了什么吗?或者至少可以就这些选项中哪一个最有可能是最佳路径提供一些建议?

ios swift xcode-ui-testing buck
© www.soinside.com 2019 - 2024. All rights reserved.