Are custom swift compiler flags from target not applied to test target/code itself?

Dear fellow developers,


I use custom compiler flags to distinguish if my app is build for test or for production:


#if PROD
  hostname = "foo"
#else
  hostname = "bar"
#endif


I added the relevant compiler flag to the target, e.g. under "Swift Compiler - Custom Flags" I added "-D" "PROD".


This works when running the app.


The Problem: Compiler Flags seem to ignored for test code

I created a TestTarget and associated with the target above. When I create a unit test I wrote the following in my test code

#if PROD
  XCTAssertEqual(sut.hostname, "foo")
#else
  XCTAssertEqual(sut.hostname, "bar")
#endif


The test case will always validate for "bar", i.e. the compiler ran into the else-case. The Swift Compiler Flags for the target are ignored, when compiling the test class itself - so it seems.


The test target configuration does not show fields for changing the Swift Compiler Flags (in contrast to the settings Apple LLVM 7.1 - Preprocessing).


Am I missing something? Am I supposed to add the Swift Compiler Flags as custom fields to my test target?


Kind regards,

Alexander

Replies

I'm also using the -D style Swift Compiler - Custom Flags

But with Xcode 9 I'm getting the following build errors


CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler

.....

....

....

<unknown>:0: error: conditional compilation flags must be valid Swift identifiers (rather than '-D')

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1



I had to drop the -D part


so Custom Flag -D ALPHA became just ALPHA

Since the beginning, my Swift flags have always been in the form:


-DSomeFlag


i.e. no space between 'D' and the flag name. This has worked in the various Xcode versions supporting swift including Xcode 9.

With XCode 9 install yesterday


-DSomeFlag No Space and -D SomeFlag with Space


Produces the Error

<unknown>:0: error: conditional compilation flags must be valid Swift identifiers (rather than '-DSomeFlag')

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1



The only "fix" I have found is just SomeFlag and when executed

#if SomeFlag triggers as expected