Files
swift-nio/Tests/NIOFileSystemTests/FilePathTests.swift
T
aryan-25andGeorge Barnett 96fbe40aae Add a new file path type, NIOFilePath, backed by SystemPackage's FilePath type (#3322)
Motivation:

Two separate types for representing file paths currently exist:
[`SystemPackage`'s
`FilePath`](https://swiftpackageindex.com/apple/swift-system/1.6.0/documentation/systempackage/filepath)
type and [`System`'s
`FilePath`](https://developer.apple.com/documentation/system/filepath)
type.

`NIOFileSystem` currently uses `SystemPackage`'s `FilePath`. However,
the lack of an API for converting between the two representations means
that users whose application also uses `System.FilePath` may find
interacting with `NIOFileSystem` difficult.

Modifications:

- Added `NIOFilePath`, and its subcomponents `NIOFilePath.Root`,
`NIOFilePath.Component`, and `NIOFilePath.ComponentView`.
- These types mirror the API of `SystemPackage`'s `FilePath` and are
internally backed by it.
- `NIOFilePath` can be initialized from either an instance of
`SystemPackage.FilePath` or `System.FilePath`.

Result:

Reduces the friction of interacting with `NIOFileSystem` by providing a
bridge between `SystemPackage.FilePath` and `System.FilePath`.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2025-07-30 15:13:50 +01:00

40 lines
1.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import SystemPackage
import XCTest
@_spi(Testing) @testable import _NIOFileSystem
#if canImport(System)
import System
#endif
final class FilePathTests: XCTestCase {
/// Tests that the conversion of a SystemPackage.FilePath instance to a NIOFilePath instance (and vice-versa) results in the same underlying object.
func testConversion() {
// Create a NIOFilePath and SystemPackage.FilePath instance, both pointing to "/foo/bar/../baz"
let filePathNIO = NIOFilePath("/foo/bar/../baz")
let filePathSystemPackage = SystemPackage.FilePath("/foo/bar/../baz")
// Convert the NIOFilePath instance to a SystemPackage.FilePath one, and check if the result matches the
// directly initialized SystemPackage.FilePath instance
XCTAssertEqual(SystemPackage.FilePath(filePathNIO), filePathSystemPackage)
// Now do the opposite: convert the SystemPackage.FilePath instance to a NIOFilePath, and check if the result
// matches the directly initialized NIOFilePath instance
let systemPackageFilePathConvertedToNIO = NIOFilePath(filePathSystemPackage)
XCTAssertEqual(systemPackageFilePathConvertedToNIO, filePathNIO)
}
}