How to add / open package file for test purpose

I have a static library associated with the application. The library code opens a file that is in a package that is in a set of applications, opening is performed as:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"]; 

This works fine.

However, I want to add some unit test code to the library, and therefore I have a logical test object for it. Since the file is in the bundle for the application, and not in the package for the static library, I copied the Config.plist file and added it to the target test code using Copy Bundle Resources . However, when I execute the test code, the file cannot be found. Why is this?

In that the above is confusing, here is a brief description of the structure of the workspace.

 Workspace contains: Application Project with application target, which contains (X) Config.plist (a) Library project which contains: Library target, which contains: the code opening the file in the bundle (b) Test library target, which contains: (Y) A Copy of the Config.plist (c) 

So, if I build X, then when b starts, it will find a. But when I build Y, when it works, b cannot find c.

+6
source share
3 answers

I found that if I changed [Bundle mainBundle] to [NSBundle bundleForClass: [self class]], then it works in both cases

+10
source

You can create the target application program in the project library, copy this file to this target, and then create the target audience of the application (within the same project) where you will test your library code.

0
source

The problem is that the static library has no package resources. After compilation, it will consist only of compiled code files and headers. Therefore, the slist library plist resource is not required for copying.

So, you need another process to copy a set of libraries so that it falls into the assembly directory.

The most efficient way I've found is to do this with a script in the script run phase before the Link Binary With Libraries phase. I am attaching a screenshot of one of my projects where I had to do something similar. You must be able to achieve what you need by adapting the file name in the first line of the script. All environment variables are predefined as standard, so I do not think that additional configuration is required, except for this script.

Did not check if environment variables work with anything other than the default xCode 4 build locations.

Script to copy resource from static library to Parent Project build directory

0
source

Source: https://habr.com/ru/post/944008/


All Articles