Search code examples
xcodemacossandbox

How to disable sandbox for Xcode UI tests


For my macOS desktop application's UI test target, I need to be able to run command line tools (specifically git) to verify some application actions, but I get an error that it cannot be run in the app sandbox.

I never explicitly enabled sandboxing for either my app or for the test target, so I'm having trouble figuring out how to turn it off. There is no entitlements file, and I can't find any target settings that seem to relate to sandboxing.

So is it possible to disable sandboxing for a UI test target? I know workarounds could include copying git into the bundle, or having the user explicitly select the tool, but neither of those seems really desirable.

Update: I tried copying git into the test bundle to run from there, but still got the same error.

Update 2: You can turn off sandboxing for the test bundle, but the test runner which Xcode builds automatically is still sandboxed (and unable to load an unsigned bundle), and AFAICT there is no way to change how the runner is built. Am I wrong?


Solution

  • The fix that worked for me was to copy git into the bundle, making sure that it's code signed during the build. Then it can be run from the sandboxed test runner.

    The catch is that the binary at /usr/bin/git is not the real git tool, it's a placeholder that forwards to xcrun which finds your active Xcode installation and runs the copy of git inside that app bundle, and you can't do any of that inside the sandbox. So you have to copy it directly out of Xcode.

    Like this:

    • Add git from Xcode by navigating to Xcode.app/Contents/Developer/usr/bin/git and setting the Location popup to "Relative to Developer Directory"
    • Add a Copy Files build phase to your UI Test target to copy git into the Executables directory (if you put it elsewhere it won't get code signed as an executable)
    • Make sure "Code Sign on Copy" is checked in that build phase (it's the default)
    • At runtime, find git using Bundle(identifier: YourTestBundleID).url(forAuxiliaryExecutable: "git")