fix(tests): unwrap url.path the same way as lastPathComponent

contentsOfFile: fixed the documented NSURL/URL bridging bug
(swiftlang/swift-corelibs-foundation#3707) in the previous commit,
confirmed by the Linux relay's error moving off String(contentsOf:)
entirely -- but url.path has the identical Darwin String vs Linux
String? split lastPathComponent already had. Same fix, same reason.

Verified via a full clean macOS build and the specific test passing,
plus swiftformat/swiftlint --strict clean.
This commit is contained in:
Mihaela Mihaljevic 2026-07-18 23:56:16 +02:00
parent 7fcfdea68c
commit 93b90adfab

View File

@ -19,25 +19,22 @@ struct TaggedNodeCorpusRoundTripTests {
))
var accepted = 0
var rejected = 0
// `URL.lastPathComponent` is `String` on Darwin's Foundation but `String?` on this
// toolchain's swift-corelibs-foundation. Every attempt to derive a `[URL]` THROUGH a
// `map`/`sorted` chain touching `.lastPathComponent` -- an inline cast, a named helper, a
// tuple-and-keypath precompute -- came back from the Linux relay with `url` re-inferred as
// `NSURL` somewhere downstream, breaking `String(contentsOf:encoding:)`. Sorting the plain
// `Int` INDICES instead keeps every `.sorted(by:)`/`.map` in this file entirely divorced
// from any URL-typed value; `urls[index]` below is the one and only place a `URL` from the
// original, never-transformed array is touched, so its type can never drift.
// Two independent Darwin/Linux Foundation divergences on this toolchain, both worked
// around the same way (an explicit `as String?` coercion so both platforms produce a
// uniform Optional before `?? ""`, since one side's non-optional String cannot itself
// take `??`): (1) `URL.lastPathComponent` is `String` on Darwin, `String?` here, used for
// the sort key and the diagnostic labels below. (2) `String(contentsOf: url, encoding:)`
// itself hits a documented Linux-only NSURL/URL bridging bug in
// swift-corelibs-foundation (swiftlang/swift-corelibs-foundation#3707), independent of
// how `url` is obtained -- `contentsOfFile:` (a `String`-path overload with no such
// bridging ambiguity) sidesteps it, which needs `URL.path`'s own String/String? split
// handled the same way as `lastPathComponent`.
let names = urls.map { ($0.lastPathComponent as String?) ?? "" }
let order = names.indices.sorted { names[$0] < names[$1] }
for index in order {
let url = urls[index]
// `String(contentsOf: url, encoding:)` itself hit the same NSURL/URL confusion on
// this toolchain regardless of how `url` was obtained (a bare `for url in urls` with
// no sort at all reproduces it too). `contentsOfFile:` takes a `String` path -- a
// type this toolchain's Foundation does not have two conflicting URL/NSURL overloads
// for -- sidestepping the ambiguity at its actual point of failure instead of
// upstream of it.
let source = try String(contentsOfFile: url.path, encoding: .utf8)
let path = (url.path as String?) ?? ""
let source = try String(contentsOfFile: path, encoding: .utf8)
let parsed: PureYAML.Tagged.Node
do {
parsed = try PureYAML.parseTagged(source)