Skip to content

Instantly share code, notes, and snippets.

@wilhuff
Last active December 5, 2019 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilhuff/94af78f34c79881b2f446da7e94904dc to your computer and use it in GitHub Desktop.
Save wilhuff/94af78f34c79881b2f446da7e94904dc to your computer and use it in GitHub Desktop.
XCTest bundle resources issue

This gist shows how resources aren't getting included in XCTest bundles

To reproduce:

  • Use a Mac that has Xcode installed
  • Clone this gist
  • Build and test with cmake:
    cmake -H. -Bbuild && (cd build; make && ctest --output-on-failure)
  • Observe test failure

The output I'm getting looks like this:

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mcg/src/testing/bundle-resources/build
[ 50%] Built target host_app
[100%] Built target xctest
Test project /Users/mcg/src/testing/bundle-resources/build
    Start 1: xctest
1/1 Test #1: xctest ...........................***Failed    0.17 sec
Test Suite 'All tests' started at 2019-12-05 10:52:20.900
Test Suite 'xctest.xctest' started at 2019-12-05 10:52:20.901
Test Suite 'FSTExampleTests' started at 2019-12-05 10:52:20.901
Test Case '-[FSTExampleTests testBundleLoading]' started.
/Users/mcg/src/testing/bundle-resources/test.mm:17: error: -[FSTExampleTests testBundleLoading] : ((error) == nil) failed: "Error Domain=NSCocoaErrorDomain Code=260 "The folder “Resources” doesn’t exist." UserInfo={NSFilePath=/Users/mcg/src/testing/bundle-resources/build/xctest.xctest/Contents/Resources, NSUserStringVariant=(
    Folder
), NSUnderlyingError=0x7ff099c17e80 {Error Domain=NSOSStatusErrorDomain Code=-43 "fnfErr: File not found"}}"
/Users/mcg/src/testing/bundle-resources/test.mm:18: error: -[FSTExampleTests testBundleLoading] : (([files count]) equal to (1)) failed: ("0") is not equal to ("1")
Test Case '-[FSTExampleTests testBundleLoading]' failed (0.096 seconds).
Test Suite 'FSTExampleTests' failed at 2019-12-05 10:52:20.996.
	 Executed 1 test, with 2 failures (0 unexpected) in 0.096 (0.096) seconds
Test Suite 'xctest.xctest' failed at 2019-12-05 10:52:20.997.
	 Executed 1 test, with 2 failures (0 unexpected) in 0.096 (0.096) seconds
Test Suite 'All tests' failed at 2019-12-05 10:52:20.997.
	 Executed 1 test, with 2 failures (0 unexpected) in 0.096 (0.096) seconds


0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.17 sec

The following tests FAILED:
	  1 - xctest (Failed)
Errors while running CTest

No resources are created:

$ find build/xctest.xctest -type f
build/xctest.xctest/Contents/MacOS/xctest
build/xctest.xctest/Contents/Info.plist

I would have expected to also be present

build/xctest.xctest/Contents/Resources/test.json
cmake_minimum_required(VERSION 3.5.1)
project(bundle-resources VERSION 0.0.1 LANGUAGES C CXX)
enable_testing()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(
objc_flags
-Wblock-capture-autoreleasing
-Wimplicit-atomic-properties
-Wnon-modular-include-in-framework-module
-fobjc-arc
-fmodules
-fno-autolink
)
find_package(XCTest REQUIRED)
add_executable(
host_app MACOSX_BUNDLE
host.m
)
target_compile_options(
host_app PRIVATE
${objc_flags}
)
target_link_libraries(
host_app PRIVATE
"-framework Foundation"
"-framework AppKit"
)
xctest_add_bundle(
xctest host_app
test.mm
)
set(resources test.json)
set_target_properties(
xctest PROPERTIES
RESOURCE "${resources}"
)
xctest_add_test(
xctest xctest
)
#import <Cocoa/Cocoa.h>
int main(int argc, const char* argv[]) {
return NSApplicationMain(argc, argv);
}
{
"test": true
}
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
NS_ASSUME_NONNULL_BEGIN
@interface FSTExampleTests : XCTestCase
@end
@implementation FSTExampleTests
- (void)testBundleLoading {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSFileManager *fs = [NSFileManager defaultManager];
NSError *error;
NSArray<NSString *> *files = [fs contentsOfDirectoryAtPath:[bundle resourcePath] error:&error];
XCTAssertNil(error);
XCTAssertEqual([files count], 1);
}
@end
NS_ASSUME_NONNULL_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment