|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Testing |
| 14 | +import SWBTestSupport |
| 15 | +import SWBUtil |
| 16 | + |
| 17 | +@Suite(.requireHostOS(.windows)) |
| 18 | +fileprivate struct PathWindowsTests { |
| 19 | + @Test func testCanonicalPathRepresentation_deviceFiles() throws { |
| 20 | + #expect(try "NUL".canonicalPathRepresentation == "\\\\.\\NUL") |
| 21 | + #expect(try Path("NUL").canonicalPathRepresentation == "\\\\.\\NUL") |
| 22 | + |
| 23 | + #expect(try "\\\\.\\NUL".canonicalPathRepresentation == "\\\\.\\NUL") |
| 24 | + |
| 25 | + // System.FilePath appends a trailing slash to fully qualified device file names |
| 26 | + withKnownIssue { () throws -> () in |
| 27 | + #expect(try Path("\\\\.\\NUL").canonicalPathRepresentation == "\\\\.\\NUL") |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test func testCanonicalPathRepresentation_driveLetters() throws { |
| 32 | + #expect(try Path("C:/").canonicalPathRepresentation == "C:\\") |
| 33 | + #expect(try Path("c:/").canonicalPathRepresentation == "c:\\") |
| 34 | + |
| 35 | + #expect(try Path("\\\\?\\C:/").canonicalPathRepresentation == "C:\\") |
| 36 | + #expect(try Path("\\\\?\\c:/").canonicalPathRepresentation == "c:\\") |
| 37 | + } |
| 38 | + |
| 39 | + @Test func testCanonicalPathRepresentation_absolute() throws { |
| 40 | + #expect(try Path("C:" + String(repeating: "/foo/bar/baz", count: 21)).canonicalPathRepresentation == "C:" + String(repeating: "\\foo\\bar\\baz", count: 21)) |
| 41 | + #expect(try Path("C:" + String(repeating: "/foo/bar/baz", count: 22)).canonicalPathRepresentation == "\\\\?\\C:" + String(repeating: "\\foo\\bar\\baz", count: 22)) |
| 42 | + } |
| 43 | + |
| 44 | + @Test func testCanonicalPathRepresentation_relative() throws { |
| 45 | + let root = Path.root.str.dropLast() |
| 46 | + #expect(try Path(String(repeating: "/foo/bar/baz", count: 21)).canonicalPathRepresentation == root + String(repeating: "\\foo\\bar\\baz", count: 21)) |
| 47 | + #expect(try Path(String(repeating: "/foo/bar/baz", count: 22)).canonicalPathRepresentation == "\\\\?\\" + root + String(repeating: "\\foo\\bar\\baz", count: 22)) |
| 48 | + } |
| 49 | + |
| 50 | + @Test func testCanonicalPathRepresentation_driveRelative() throws { |
| 51 | + let current = Path.currentDirectory |
| 52 | + |
| 53 | + // Ensure the output path will be < 260 characters so we can assert it's not prefixed with \\?\ |
| 54 | + let chunks = (260 - current.str.count) / "foo/bar/baz/".count |
| 55 | + #expect(current.str.count < 248 && chunks > 0, "The current directory is too long for this test.") |
| 56 | + |
| 57 | + #expect(try Path(current.str.prefix(2) + String(repeating: "foo/bar/baz/", count: chunks)).canonicalPathRepresentation == current.join(String(repeating: "\\foo\\bar\\baz", count: chunks)).str) |
| 58 | + #expect(try Path(current.str.prefix(2) + String(repeating: "foo/bar/baz/", count: 22)).canonicalPathRepresentation == "\\\\?\\" + current.join(String(repeating: "\\foo\\bar\\baz", count: 22)).str) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fileprivate extension String { |
| 63 | + var canonicalPathRepresentation: String { |
| 64 | + get throws { |
| 65 | + #if os(Windows) |
| 66 | + return try withCString(encodedAs: UTF16.self) { platformPath in |
| 67 | + return try platformPath.withCanonicalPathRepresentation { canonicalPath in |
| 68 | + return String(decodingCString: canonicalPath, as: UTF16.self) |
| 69 | + } |
| 70 | + } |
| 71 | + #else |
| 72 | + return self |
| 73 | + #endif |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fileprivate extension Path { |
| 79 | + var canonicalPathRepresentation: String { |
| 80 | + get throws { |
| 81 | + #if os(Windows) |
| 82 | + return try withPlatformString { platformPath in |
| 83 | + return try platformPath.withCanonicalPathRepresentation { canonicalPath in |
| 84 | + return String(decodingCString: canonicalPath, as: UTF16.self) |
| 85 | + } |
| 86 | + } |
| 87 | + #else |
| 88 | + return str |
| 89 | + #endif |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments