This one is a late addition to the old Shellscripting series, and it's less about learning a new feature and more about a pattern that bit me in the test script of one of my side projects.
A lot of iOS projects end up with some kind of test-ios.sh wrapper around xcodebuild, because nobody wants to type the destination, the derived data path, the result bundle path and a dozen flags every time they want to run a single test class. Mine looked roughly like this:
BUILD_CMD="xcodebuild test" BUILD_CMD="$BUILD_CMD -scheme $SCHEME" BUILD_CMD="$BUILD_CMD -destination '$DESTINATION'" BUILD_CMD="$BUILD_CMD -resultBundlePath '$RESULT_BUNDLE_PATH'" for test in "${ONLY_TESTING[@]}"; do BUILD_CMD="$BUILD_CMD -only-testing:$test" done eval "$BUILD_CMD" 2>&1 | tee "$LOG" We build the command up as one long string, piece by piece, with some of the pieces wrapped in single quotes because the destination has spaces and commas in it, and at the end we hand the whole thing to eval, which parses the string again as shell code and runs it. It worked fine for whole targets (MainTests), for test classes (MainTests/LibraryTests) and for XCTest methods (MainTests/LibraryTests/testLoading).
Swift Testing ids end in ()
Then the project started moving tests to Swift Testing, and a Swift Testing test's identifier is the type name followed by the function name with its parentheses, MainTests/SomeSuite/someTest() (or the argument labels, checkThing(input:expected:), for a parameterized one). So we run:
./scripts/test-ios.sh --only-testing "MainTests/SomeSuite/someTest()" and the shell we typed that into is happy, because the quotes keep the parentheses as part of one argument, and the script stores it in the ONLY_TESTING array as one value. The problem is the next step, where the value gets glued into BUILD_CMD and eval reads it again, this time with no quotes around it. To eval, someTest() is the start of a function definition in the wrong place, which you can see with a tiny stand-in for xcodebuild that prints each argument it receives between angle brackets:
show() { printf '<%s> ' "$@"; echo; } id='MainTests/SomeSuite/someTest()' CMD="show test -only-testing:$id" eval "$CMD" # eval: line 1: syntax error near unexpected token `(' In the real script that meant xcodebuild never started at all. There was no build output, no result bundle, and the only thing the wrapper could say was that the result bundle was missing and the tests failed, which makes it very easy to blame xcodebuild for it (more on that at the end).
Quoting each piece is only half a fix
The first thing that comes to mind is to quote the id the same way the script already quoted -destination:
CMD="show test '-only-testing:$id'" eval "$CMD" # <test> <-only-testing:MainTests/SomeSuite/someTest()> and that does make the parentheses work. But eval still runs whatever is in the string as code, so the quotes only protect us as long as the value itself doesn't contain a single quote. An id with a quote and a command substitution in it closes our quotes for us and gets executed:
hostile="MainTests/SomeSuite/'\$(echo PWNED >&2)'()" CMD="show test '-only-testing:$hostile'" eval "$CMD" # PWNED # <test> <-only-testing:MainTests/SomeSuite/()> Nobody is going to attack their own test runner, but the same thing happens with a path that has an apostrophe in it, and a test runner that runs pieces of its arguments as code is not something I want to keep around. The other workaround I had written down for myself was to put quotes inside the argument (--only-testing "'MainTests/SomeSuite/someTest()'") so that they survive until eval. Nobody had measured that one, and the repo's docs said it "selects nothing". When I did run it through the fixed script, the quotes reached xcodebuild as part of the name, and it failed the run with exit 70, complaining that there is no test target called 'MainTests.
Use an array
Bash already has a way to keep a list of arguments where each one stays exactly what it is, an array, and running "${ARRAY[@]}" expands to one word per element without parsing any of them again:
BUILD_CMD=(xcodebuild test) BUILD_CMD+=(-scheme "$SCHEME") BUILD_CMD+=(-destination "$DESTINATION") BUILD_CMD+=(-resultBundlePath "$RESULT_BUNDLE_PATH") for test in "${ONLY_TESTING[@]}"; do BUILD_CMD+=("-only-testing:$test") done "${BUILD_CMD[@]}" 2>&1 | tee "$LOG" The single quotes around the destination are gone, because there is no second round of parsing for them to survive, and every value goes to xcodebuild as one argument whatever characters it holds. With the show stand-in, both the plain id and the hostile one arrive untouched, and nothing gets executed:
ARGS=(show test "-only-testing:$id" "-only-testing:$hostile") "${ARGS[@]}" # <test> <-only-testing:MainTests/SomeSuite/someTest()> <-only-testing:MainTests/SomeSuite/'$(echo PWNED >&2)'()> If the script has a --verbose mode that prints the command, printf '%q ' renders the array as a line you can copy and paste back into a shell, and it's better to print it with printf '%s' than with echo -e, which would read the backslashes %q adds as escapes:
printf -v display '%q ' "${BUILD_CMD[@]}" printf '%s\n' "${display% }" The same goes for any function that runs the command for you (to capture its output or its exit status). Pass the array as arguments and run "$@" inside the function, rather than having the function read a global BUILD_CMD, because then nothing depends on which part of the script set that global last.
Gotcha: a ${BUILD_CMD[@]} without the double quotes splits every element on spaces again, which brings back most of the problem. The quotes around "${BUILD_CMD[@]}" are the whole point.
Rule of thumb: when a script builds a command for another program, build it as an array and run it as "${CMD[@]}".
Testing it with the right input
The test I wrote first fed the runner MainTests/SomeSuite/$(touch marker)() against a stub xcodebuild that records its argv, and checked that the marker file never appeared. It passed with the array, and it failed with the old eval runner, so I thought it was done. In review it turned out that it didn't catch the half-fix from above, because under a plain eval the () is a syntax error before the $(...) ever runs, and with each id single-quoted there is no quote in that input to break out with. The input that catches both is the one with the quotes inside it, MainTests/SomeSuite/'$(touch marker)'(), which a plain eval runner and a quote-each-piece runner both execute. The fixture now carries both ids, compares the whole recorded argv instead of just the -only-testing entries (checking only those let 11 out of 12 broken variations of the other arguments through), and rejects any eval left in the runner, backtick form included.
What the old comment got wrong
Elsewhere in the same repo, the script that prints a "re-run the failures" command for a failed run had a comment saying that xcodebuild "rejects a parenthesised -only-testing argument outright", which was why it only ever suggested re-running whole suites. Someone (most likely me, or an agent working for me) saw xcodebuild fail on a () id and blamed xcodebuild, when the failure was this eval. Measured through the fixed runner, Suite/someTest() selects exactly one test, both for an XCTest method and for a plain Swift Testing function. The other half of that comment did hold, tho. Strip the parentheses from a Swift Testing id and xcodebuild runs zero tests without complaining, which is a good reason to keep a guard that fails a run when nothing was executed.
That's it for this one. The other parts of the series are here, here and here, if you need a refresher on the basics.
