Issue with @testable import test
remirobert opened this issue · 11 comments
I am currently having an issue, in my tests, I am using @testable import to access the API code I want to test.
Here my BUCK:
buck test :chatApp
apple_asset_catalog(
name = 'Assets',
app_icon = 'AppIcon',
dirs = ['app/Assets.xcassets'],
)
apple_resource(
name = 'appResources',
files = glob(['app/*.png', 'app/**/*.storyboard']),
)
apple_bundle(
name = 'app',
binary = ':appBinary',
extension = 'app',
info_plist = 'app/Info.plist',
tests = [':appTests']
)
apple_binary(
name = 'appBinary',
deps = [':appResources', ':Assets'],
srcs = glob([
'app/**/*.swift'
]),
frameworks = [
'$SDKROOT/System/Library/Frameworks/UIKit.framework',
'$SDKROOT/System/Library/Frameworks/Foundation.framework',
]
)
apple_package(
name = 'appPackage',
bundle = ':app'
)
apple_test(
name = 'appTests',
test_host_app = ':app',
run_test_separately = True,
info_plist = 'appTests/Info.plist',
srcs = glob([
'appTests/**/*.swift'
]),
frameworks = [
'$SDKROOT/System/Library/Frameworks/Foundation.framework',
'$SDKROOT/System/Library/Frameworks/UIKit.framework',
'$PLATFORM_DIR/Developer/Library/Frameworks/XCTest.framework',
]
)
I am having a runtime crash.
Is it possible to have the @testable import?
If not what is the other alternative to test my code?
Here the error :
Building... 0.4 sec
[2017-12-18 16:21:26.111][error][command:null][tid:465][com.facebook.buck.cli.Main] Uncaught exception at top level
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Optional.<init>(Optional.java:96)
at java.util.Optional.of(Optional.java:108)
at com.facebook.buck.apple.AppleTestDescription.createTestHostInfo(AppleTestDescription.java:496)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:233)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:84)
at com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer.transform(DefaultTargetNodeToBuildRuleTransformer.java:88)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.lambda$requireRule$0(SingleThreadedBuildRuleResolver.java:111)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.computeIfAbsent(SingleThreadedBuildRuleResolver.java:84)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.requireRule(SingleThreadedBuildRuleResolver.java:107)
at com.facebook.buck.swift.SwiftLibraryDescription.createCompanionBuildRule(SwiftLibraryDescription.java:391)
at com.facebook.buck.apple.AppleLibraryDescription.lambda$requireSingleArchUnstrippedBuildRule$8(AppleLibraryDescription.java:587)
at java.util.Optional.flatMap(Optional.java:241)
at com.facebook.buck.apple.AppleLibraryDescription.requireSingleArchUnstrippedBuildRule(AppleLibraryDescription.java:585)
at com.facebook.buck.apple.AppleLibraryDescription.requireUnstrippedBuildRule(AppleLibraryDescription.java:546)
at com.facebook.buck.apple.AppleLibraryDescription.createLibraryBuildRule(AppleLibraryDescription.java:441)
at com.facebook.buck.apple.AppleTestDescription.createTestLibraryRule(AppleTestDescription.java:411)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:251)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:84)
at com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer.transform(DefaultTargetNodeToBuildRuleTransformer.java:88)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.lambda$requireRule$0(SingleThreadedBuildRuleResolver.java:111)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.computeIfAbsent(SingleThreadedBuildRuleResolver.java:84)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.requireRule(SingleThreadedBuildRuleResolver.java:107)
at com.facebook.buck.rules.ActionGraphCache$2.visit(ActionGraphCache.java:330)
at com.facebook.buck.rules.ActionGraphCache$2.visit(ActionGraphCache.java:327)
at com.facebook.buck.graph.AbstractBottomUpTraversal.traverse(AbstractBottomUpTraversal.java:36)
at com.facebook.buck.rules.ActionGraphCache.createActionGraphSerially(ActionGraphCache.java:332)
at com.facebook.buck.rules.ActionGraphCache.createActionGraph(ActionGraphCache.java:264)
at com.facebook.buck.rules.ActionGraphCache.getActionGraph(ActionGraphCache.java:167)
at com.facebook.buck.rules.ActionGraphCache.getActionGraph(ActionGraphCache.java:68)
at com.facebook.buck.cli.TestCommand.runWithoutHelp(TestCommand.java:549)
at com.facebook.buck.cli.AbstractCommand.run(AbstractCommand.java:232)
at com.facebook.buck.cli.AbstractContainerCommand.run(AbstractContainerCommand.java:79)
at com.facebook.buck.cli.BuckCommand.run(BuckCommand.java:82)
at com.facebook.buck.cli.Main.runMainWithExitCode(Main.java:1101)
at com.facebook.buck.cli.Main.runMainThenExit(Main.java:395)
at com.facebook.buck.cli.Main.nailMain(Main.java:1855)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.martiansoftware.nailgun.NGSession.run(NGSession.java:329)
@remirobert are you using the sample repo or your own repo? I didn't see anything related to @testable here.
My own.
On this repo, there is no test target.
So if you want to test your app, how will you do?
Buck itself provide the guideline about how to config apple_test
build rule: https://buckbuild.com/rule/apple_test.html
We are able to use @testable in our codebase, which is doing almost the same thing as what we do in apple_library
.
In our migration, we did encounter some cases that @testable fail, but that's mostly due to there is swift module in the module which being tested.
But without looking into your code / config, I can't tell what's exactly broken.
I have read the documentation, and tried different settings, but at this end I always got that runtime crash.
I created a sample project with the configuration I am using, and reproducing the crash with a test target : https://github.com/remirobert/test-fail-testable-import
Thanks for your help.
you need to add deps = [':app']
into the apple_test
build rule so that you can use the app
module.
Also, your crash is due to use are setting test_host_app = ':app'
, I think that's probably not the correct way, I remove this line and the test is able to build
Yes without the test_host_app = ':app'
it's building fine.
But I can't use the @testable import app
in my tests.
My understanding is that test_host_app
allows you to do that.
I found an Objc example working with the test_host_app
: https://github.com/bhamiltoncx/buck-ios-sample
Nope, it’s not related. You need to put “deps” into the test build rule to make it build.
Closing this since the problem should have been resolved
@zayhero, Still doesn't build even adding deps = [':app'],
into BUCK:
apple_test(
name = 'appTests',
deps = [':app'],
test_host_app = ':app',
run_test_separately = True,
info_plist = 'appTests/Info.plist',
srcs = glob([
'appTests/**/*.swift'
]),
frameworks = [
'$SDKROOT/System/Library/Frameworks/Foundation.framework',
'$SDKROOT/System/Library/Frameworks/UIKit.framework',
'$PLATFORM_DIR/Developer/Library/Frameworks/XCTest.framework',
]
)
@remirobert, it builds because there are no swift sources in srcs
section added.
I'm debugging buck and see it tries to build a SwiftLibrary
that has no output path and this is the root cause of the NPE.