mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
357 lines
11 KiB
Swift
357 lines
11 KiB
Swift
/*
|
|
This source file is part of the Swift System open source project
|
|
|
|
Copyright (c) 2020 Apple Inc. and the Swift System project authors
|
|
Licensed under Apache License v2.0 with Runtime Library Exception
|
|
|
|
See https://swift.org/LICENSE.txt for license information
|
|
*/
|
|
|
|
import XCTest
|
|
|
|
#if SYSTEM_PACKAGE
|
|
@testable import SystemPackage
|
|
#else
|
|
@testable import System
|
|
#endif
|
|
|
|
@available(System 0.0.2, *)
|
|
struct TestPathComponents: TestCase {
|
|
var path: FilePath
|
|
var expectedRoot: FilePath.Root?
|
|
var expectedComponents: [FilePath.Component]
|
|
|
|
var pathComponents: Array<FilePath.Component> { Array(path.components) }
|
|
|
|
let file: StaticString
|
|
let line: UInt
|
|
|
|
func failureMessage(_ reason: String?) -> String {
|
|
"""
|
|
|
|
Fail \(reason ?? "")
|
|
path: \(path)
|
|
components: \(pathComponents))
|
|
expected: \(expectedComponents)
|
|
"""
|
|
}
|
|
|
|
init<C: Collection>(
|
|
_ path: FilePath,
|
|
root: FilePath.Root?,
|
|
_ components: C,
|
|
file: StaticString = #file, line: UInt = #line
|
|
) where C.Element == FilePath.Component {
|
|
self.path = path
|
|
self.expectedRoot = root
|
|
self.expectedComponents = Array(components)
|
|
self.file = file
|
|
self.line = line
|
|
}
|
|
|
|
func testComponents() {
|
|
expectEqual(expectedRoot, path.root)
|
|
expectEqualSequence(
|
|
expectedComponents, Array(path.components), "testComponents()")
|
|
}
|
|
|
|
func testBidi() {
|
|
expectEqualSequence(
|
|
expectedComponents.reversed(), path.components.reversed(), "reversed()")
|
|
expectEqualSequence(
|
|
path.components, path.components.reversed().reversed(),
|
|
"reversed().reversed()")
|
|
for i in 0 ..< path.components.count {
|
|
expectEqualSequence(
|
|
expectedComponents.dropLast(i), path.components.dropLast(i), "dropLast")
|
|
expectEqualSequence(
|
|
expectedComponents.suffix(i), path.components.suffix(i), "suffix")
|
|
}
|
|
}
|
|
|
|
func testRRC() {
|
|
// Show that FilePathComponents's replaceSubrange behaves like Array.
|
|
let pool: [FilePath.Component] = ["x", "..", ".", "longer-name"]
|
|
let replacements: [[FilePath.Component]] = [[]] + (1 ... 3).flatMap { len in
|
|
pool.indices.map { offset in
|
|
(0 ..< len).map { pool[(offset + $0) % pool.count] }
|
|
}
|
|
}
|
|
|
|
let count = expectedComponents.count
|
|
for lower in 0 ... count {
|
|
for upper in lower ... count {
|
|
for replacement in replacements {
|
|
var model = expectedComponents
|
|
model.replaceSubrange(lower ..< upper, with: replacement)
|
|
|
|
var p = path
|
|
let indices = Array(p.components.indices) + [p.components.endIndex]
|
|
p.components.replaceSubrange(
|
|
indices[lower] ..< indices[upper], with: replacement
|
|
)
|
|
|
|
let expected = FilePath(root: path.root, model)
|
|
let message = """
|
|
replaceSubrange(\(lower)..<\(upper), with: \(replacement)) \
|
|
returned \(p), expected \(expected)
|
|
"""
|
|
expectEqual(expected, p, message + " [path]")
|
|
expectEqual(path.root, p.root, message + " [root]")
|
|
expectEqualSequence(model, p.components, message + " [components]")
|
|
expectEqual(model.last, p.lastComponent, message + " [lastComponent]")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func testModify() {
|
|
if path.root == nil {
|
|
let rootedPath = FilePath(root: "/", path.components)
|
|
expectNotEqual(rootedPath, path)
|
|
var pathCopy = path
|
|
expectEqual(path, pathCopy)
|
|
pathCopy.components = rootedPath.components
|
|
expectNil(pathCopy.root, "components.set doesn't assign root")
|
|
expectEqual(path, pathCopy)
|
|
} else {
|
|
let rootlessPath = FilePath(root: nil, path.components)
|
|
var pathCopy = path
|
|
expectEqual(path, pathCopy)
|
|
pathCopy.components = rootlessPath.components
|
|
expectNotNil(pathCopy.root, "components.set preserves root")
|
|
expectEqual(path, pathCopy)
|
|
}
|
|
}
|
|
|
|
func runAllTests() {
|
|
testComponents()
|
|
testBidi()
|
|
testRRC()
|
|
testModify()
|
|
}
|
|
}
|
|
|
|
@available(System 0.0.2, *)
|
|
final class FilePathComponentsTest: XCTestCase {
|
|
func testAdHocRRC() throws {
|
|
var path: FilePath = "/usr/local/bin"
|
|
|
|
func expect(
|
|
_ s: String,
|
|
_ file: StaticString = #filePath,
|
|
_ line: UInt = #line
|
|
) {
|
|
if path == FilePath(s) { return }
|
|
|
|
defer { print("expected: \(s), actual: \(path)") }
|
|
XCTAssert(false, file: file, line: line)
|
|
}
|
|
|
|
// Run `body`, restoring `path` afterwards
|
|
func restoreAfter(
|
|
body: () throws -> ()
|
|
) rethrows {
|
|
let copy = path
|
|
defer { path = copy }
|
|
try body()
|
|
}
|
|
|
|
// Interior removal keeps path prefix intact, if there is one
|
|
try restoreAfter {
|
|
path = "prefix//middle1/middle2////suffix"
|
|
let suffix = try XCTUnwrap(path.components.indices.last)
|
|
path.components.removeSubrange(..<suffix)
|
|
expect("suffix")
|
|
}
|
|
|
|
try restoreAfter {
|
|
path = "prefix//middle1/middle2////suffix"
|
|
let firstMiddle = path.components.index(
|
|
path.components.startIndex, offsetBy: 1)
|
|
let suffix = try XCTUnwrap(path.components.indices.last)
|
|
path.components.removeSubrange(firstMiddle ..< suffix)
|
|
expect("prefix/suffix")
|
|
}
|
|
|
|
restoreAfter {
|
|
path.components.removeFirst()
|
|
expect("/local/bin")
|
|
path.components.removeFirst()
|
|
expect("/bin")
|
|
}
|
|
|
|
restoreAfter {
|
|
path.components.insert(
|
|
contentsOf: ["bar", "baz"], at: path.components.startIndex)
|
|
expect("/bar/baz/usr/local/bin")
|
|
}
|
|
|
|
restoreAfter {
|
|
path.components.insert("start", at: path.components.startIndex)
|
|
expect("/start/usr/local/bin")
|
|
path.components.insert("prefix", at: path.components.startIndex)
|
|
expect("/prefix/start/usr/local/bin")
|
|
path.components.removeSubrange(
|
|
..<path.components.index(path.components.startIndex, offsetBy: 4))
|
|
expect("/bin")
|
|
path.components.removeFirst()
|
|
expect("/")
|
|
path.components.append(contentsOf: ["usr", "local", "bin"])
|
|
expect("/usr/local/bin")
|
|
path.components.removeLast(2)
|
|
expect("/usr")
|
|
}
|
|
|
|
restoreAfter {
|
|
path.root = nil
|
|
expect("usr/local/bin")
|
|
path.components.removeAll()
|
|
expect("")
|
|
path.components.insert("foo", at: path.components.startIndex)
|
|
expect("foo")
|
|
|
|
path.components.removeAll()
|
|
path.components.insert(
|
|
contentsOf: ["bar", "baz"], at: path.components.startIndex)
|
|
expect("bar/baz")
|
|
}
|
|
|
|
restoreAfter {
|
|
path.components.append("tail")
|
|
expect("/usr/local/bin/tail")
|
|
path.components.append(contentsOf: ["tail2", "tail3", "tail4"])
|
|
expect("/usr/local/bin/tail/tail2/tail3/tail4")
|
|
}
|
|
|
|
// Insertion into the middle adds trailing separator
|
|
try restoreAfter {
|
|
path.components.remove(
|
|
at: path.components.index(after: path.components.startIndex))
|
|
expect("/usr/bin")
|
|
path.components.insert(
|
|
"middle", at: try XCTUnwrap(path.components.indices.last))
|
|
expect("/usr/middle/bin")
|
|
path.components.insert(
|
|
contentsOf: ["middle2", "middle3"],
|
|
at: try XCTUnwrap(path.components.indices.last))
|
|
expect("/usr/middle/middle2/middle3/bin")
|
|
|
|
let slice = path.components.dropFirst().dropLast()
|
|
let range = slice.startIndex ..< slice.endIndex
|
|
path.components.replaceSubrange(
|
|
range, with: ["newMiddle", "newMiddle2", "newMiddle3", "newMiddle4/"])
|
|
expect("/usr/newMiddle/newMiddle2/newMiddle3/newMiddle4/bin")
|
|
}
|
|
|
|
// Replacing components at the end of a path must not append a separator.
|
|
try restoreAfter {
|
|
let last = try XCTUnwrap(path.components.indices.last)
|
|
path.components.replaceSubrange(last..., with: ["sbin"])
|
|
expect("/usr/local/sbin")
|
|
XCTAssertEqual(path.lastComponent, "sbin")
|
|
XCTAssertEqual(path.components.last, "sbin")
|
|
|
|
path.components.replaceSubrange(
|
|
path.components.index(after: path.components.startIndex)...,
|
|
with: ["a", "b", "c"])
|
|
expect("/usr/a/b/c")
|
|
|
|
path.components.append("d")
|
|
expect("/usr/a/b/c/d")
|
|
XCTAssertEqual(path.components.count, 5)
|
|
|
|
path.components.replaceSubrange(
|
|
path.components.startIndex..., with: ["x", "y"])
|
|
expect("/x/y")
|
|
}
|
|
try restoreAfter {
|
|
path = "a/b"
|
|
path.components.replaceSubrange(
|
|
try XCTUnwrap(path.components.indices.last)..., with: ["x"])
|
|
expect("a/x")
|
|
XCTAssertEqual(path.lastComponent, "x")
|
|
}
|
|
|
|
restoreAfter {
|
|
path.components.removeLast(3)
|
|
expect("/")
|
|
path.components.append(contentsOf: ["bar", "baz"])
|
|
expect("/bar/baz")
|
|
}
|
|
|
|
// Empty insertions / removals
|
|
try restoreAfter {
|
|
path.components.replaceSubrange(..<path.components.startIndex, with: [])
|
|
expect("/usr/local/bin")
|
|
path.components.replaceSubrange(path.components.endIndex..., with: [])
|
|
expect("/usr/local/bin")
|
|
path.components.insert(contentsOf: [], at: path.components.startIndex)
|
|
expect("/usr/local/bin")
|
|
path.components.insert(
|
|
contentsOf: [], at: try XCTUnwrap(path.components.indices.last))
|
|
expect("/usr/local/bin")
|
|
}
|
|
}
|
|
|
|
func testCases() {
|
|
var testPaths: Array<TestPathComponents> = [
|
|
TestPathComponents("", root: nil, []),
|
|
TestPathComponents("/", root: "/", []),
|
|
TestPathComponents("foo", root: nil, ["foo"]),
|
|
TestPathComponents("foo/", root: nil, ["foo"]),
|
|
TestPathComponents("/foo", root: "/", ["foo"]),
|
|
TestPathComponents("foo/bar", root: nil, ["foo", "bar"]),
|
|
TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]),
|
|
TestPathComponents("/foo/bar", root: "/", ["foo", "bar"]),
|
|
TestPathComponents("/foo///bar", root: "/", ["foo", "bar"]),
|
|
TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]),
|
|
TestPathComponents("foo///bar/baz/", root: nil, ["foo", "bar", "baz"]),
|
|
TestPathComponents("./", root: nil, ["."]),
|
|
TestPathComponents("./..", root: nil, [".", ".."]),
|
|
TestPathComponents("/./..//", root: "/", [".", ".."]),
|
|
]
|
|
#if !os(Windows)
|
|
testPaths.append(contentsOf:[
|
|
TestPathComponents("///foo//", root: "/", ["foo"]),
|
|
TestPathComponents("//foo///bar/baz/", root: "/", ["foo", "bar", "baz"])
|
|
])
|
|
#else
|
|
// On Windows, these are UNC paths
|
|
testPaths.append(contentsOf:[
|
|
TestPathComponents("///foo//", root: "///foo//", []),
|
|
TestPathComponents("//foo///bar/baz/", root: "//foo//", ["bar", "baz"])
|
|
])
|
|
#endif
|
|
testPaths.forEach {
|
|
$0.runAllTests()
|
|
}
|
|
}
|
|
|
|
func testSeparatorNormalization() {
|
|
var paths: Array<FilePath> = [
|
|
"/a/b",
|
|
"/a/b/",
|
|
"/a//b/",
|
|
"/a/b//",
|
|
"/a/b////",
|
|
"/a////b/",
|
|
]
|
|
#if !os(Windows)
|
|
paths.append("//a/b")
|
|
paths.append("///a/b")
|
|
paths.append("///a////b")
|
|
paths.append("///a////b///")
|
|
#endif
|
|
|
|
for path in paths {
|
|
var path = path
|
|
path._normalizeSeparators()
|
|
XCTAssertEqual(path, "/a/b")
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: Test hashValue and equatable for equal components, i.e. make
|
|
// sure indices are not part of the hash.
|