Integrate API review

Fix bug in _modify and add better testing. Change lexicallyNormal to
lexicallyNormalized() to match "ed" rule.

Fix up some comments and remove some dead code

Co-authored-by: Kyle Macomber <kmacomber@apple.com>
This commit is contained in:
Michael IlsemanandKyle Macomber committed 2021-02-04 16:34:20 -07:00
1 parent 05e3d04b0c
commit d24083e1be
7 files changed
+70 -210

No files matched your search

@@ -43,7 +43,7 @@ extension FilePath {
public var components: ComponentView {
__consuming get { ComponentView(self) }
_modify {
// RRC's empty init means that we cann't guarantee that the yielded
// RRC's empty init means that we can't guarantee that the yielded
// view will restore our root. So copy it out first.
//
// TODO(perf): Small-form root (especially on Unix). Have Root
@@ -54,13 +54,7 @@ extension FilePath {
self = FilePath()
defer {
self = comp._path
if !rootStr.isEmpty {
if let r = self.root {
// Roots can be forgotten but never altered
assert(r._slice.elementsEqual(rootStr))
}
// TODO: conditional on it having changed?
if root?._slice.elementsEqual(rootStr) != true {
self.root = Root(rootStr)
}
}
@@ -105,7 +99,6 @@ extension FilePath.ComponentView: BidirectionalCollection {
extension FilePath.ComponentView: RangeReplaceableCollection {
public init() {
// FIXME: but what about the root?
self.init(FilePath())
}
@@ -199,8 +199,6 @@ extension FilePath: _PlatformStringable {
}
// TODO(root): Re-think these initializers. We want them to be conv
extension FilePath.Component {
// The index of the `.` denoting an extension
internal func _extensionIndex() -> SystemString.Index? {
+1 -23
View File
@@ -158,8 +158,6 @@ extension FilePath {
if writeIdx != relStart {
let priorComponent = _parseComponent(priorTo: writeIdx)
// FIXME: it's not up until writeIdx because separator,
// parseComponent should give the component...
if !_isParentDirectory(priorComponent) {
writeIdx = priorComponent.lowerBound
readIdx = nextStart
@@ -182,8 +180,6 @@ extension FilePath {
assert(readIdx == _storage.endIndex && readIdx >= writeIdx)
if readIdx != writeIdx {
// TODO: Why was it everything after writeIdx?
// storage.removeSubrange(storage.index(after: writeIdx)...)
_storage.removeSubrange(writeIdx...)
_removeTrailingSeparator()
}
@@ -275,24 +271,6 @@ extension FilePath.ComponentView {
internal var _relativeStart: SystemString.Index {
_path._relativeStart
}
internal func parseComponentStart(
endingAt i: SystemString.Index
) -> SystemString.Index {
if i == _relativeStart, i != _path._storage.startIndex {
return _path._storage.startIndex
}
var i = i
if i != _path._storage.endIndex {
assert(isSeparator(_path._storage[i]))
i = _path._storage.index(before: i)
}
var slice = _path._storage[..<i]
while let c = slice.last, !isSeparator(c) {
slice.removeLast()
}
return slice.endIndex
}
}
extension SystemString {
@@ -370,7 +348,7 @@ extension FilePath {
}
// Perform an append, inseting a separator if needed.
// Note tha this will not check whether `content` is a root
// Note that this will not check whether `content` is a root
internal mutating func _append(unchecked content: Slice<SystemString>) {
assert(FilePath(SystemString(content)).root == nil)
if content.isEmpty { return }
+12 -14
View File
@@ -305,13 +305,13 @@ extension FilePath {
/// replace the extension.
///
/// Examples:
/// * `/tmp/foo.txt => txt`
/// * `/Appliations/Foo.app/ => app`
/// * `/Appliations/Foo.app/bar.txt => txt`
/// * `/tmp/foo.tar.gz => gz`
/// * `/tmp/.hidden => nil`
/// * `/tmp/.hidden. => ""`
/// * `/tmp/.. => nil`
/// * `/tmp/foo.txt => txt`
/// * `/Applications/Foo.app/ => app`
/// * `/Applications/Foo.app/bar.txt => txt`
/// * `/tmp/foo.tar.gz => gz`
/// * `/tmp/.hidden => nil`
/// * `/tmp/.hidden. => ""`
/// * `/tmp/.. => nil`
///
/// Example:
///
@@ -391,12 +391,10 @@ extension FilePath {
/// Returns a copy of `self` in lexical-normal form, that is `.` and `..`
/// components have been collapsed lexically (i.e. without following
/// symlinks). See `lexicallyNormalize`
public var lexicallyNormal: FilePath {
__consuming get {
var copy = self
copy.lexicallyNormalize()
return copy
}
public __consuming func lexicallyNormalized() -> FilePath {
var copy = self
copy.lexicallyNormalize()
return copy
}
/// Create a new `FilePath` by resolving `subpath` relative to `self`,
@@ -428,7 +426,7 @@ extension FilePath {
public __consuming func lexicallyResolving(
_ subpath: __owned FilePath
) -> FilePath? {
let subpath = subpath.removingRoot().lexicallyNormal
let subpath = subpath.removingRoot().lexicallyNormalized()
guard !subpath.isEmpty else { return self }
guard subpath.components.first?.kind != .parentDirectory else {
return nil
@@ -13,104 +13,6 @@ import SystemPackage
@testable
import SystemPackage
// Helper to organize some ad-hoc testing
//
// TODO: Currently a class so we can overwrite file/line, but that can be
// re-evaluated when we have source loc stacks.
private final class AdHocComponentsTest: TestCase {
// TODO (source-loc stack): Push fil/line from init onto stack
// Record the top-most file/line info (`expect` overwrites these values)
//
// TODO: When we have source loc stacks, push the location from the init,
// and `expect` will be push/pops
var file: StaticString
var line: UInt
var path: FilePath
var body: (AdHocComponentsTest) -> ()
init(
_ path: FilePath,
_ file: StaticString = #file,
_ line: UInt = #line,
_ body: @escaping (AdHocComponentsTest) -> ()
) {
self.file = file
self.line = line
self.path = path
self.body = body
}
func runAllTests() {
body(self)
}
}
private func adhocComponentsTest(
_ path: FilePath,
_ file: StaticString = #file,
_ line: UInt = #line,
_ body: @escaping (AdHocComponentsTest) -> ()
) {
let test = AdHocComponentsTest(path, file, line, body)
test.runAllTests()
}
extension AdHocComponentsTest {
// Temporarily re-bind file/line
func withSourceLoc(
_ newFile: StaticString,
_ newLine: UInt,
_ body: () -> ()
) {
let (origFile, origLine) = (self.file, self.line)
(self.file, self.line) = (newFile, newLine)
defer { (self.file, self.line) = (origFile, origLine) }
body()
}
// Customize error report by adding our path and components to output
func failureMessage(_ reason: String?) -> String {
"""
Fail
path: \(path)
components: \(Array(path.components))
\(reason ?? "")
"""
}
func expect(
_ expected: FilePath,
file: StaticString = #file, line: UInt = #line
) {
withSourceLoc(file, line) {
expectEqual(expected, path, "expected: \(expected)")
}
}
func expectRelative(
file: StaticString = #file, line: UInt = #line
) {
withSourceLoc(file, line) {
expectTrue(path.isRelative, "expected relative")
}
}
func expectAbsolute(
file: StaticString = #file, line: UInt = #line
) {
withSourceLoc(file, line) {
expectTrue(path.isAbsolute, "expected absolute")
}
}
// TODO: Do we want to overload others like expectEqual[Sequence]?
}
// @available(9999....)
struct TestPathComponents: TestCase {
var path: FilePath
@@ -152,34 +54,47 @@ struct TestPathComponents: TestCase {
}
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() {
// TODO: Convert tests into mutation tests
// // What generalized tests can we do, given how initial "/" is special?
// // E.g. absolute path inserted into itself can have only one root
//
// do {
// var path = self.path
// if path.isAbsolute {
// path.components.removeFirst()
// }
// expectTrue(path.isRelative)
//
// let componentsArray = Array(path.components)
// path.components.append(contentsOf: componentsArray)
// expectEqualSequence(componentsArray + componentsArray, path.components)
//
// // TODO: Other generalized tests which work on relative paths
// }
// TODO: programmatic tests showing parity with Array<Component>
}
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()
}
}
@@ -187,34 +102,6 @@ struct TestPathComponents: TestCase {
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
final class FilePathComponentsTest: XCTestCase {
let 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//", root: "/", ["foo"]),
TestPathComponents("/foo///bar", root: "/", ["foo", "bar"]),
TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]),
TestPathComponents("foo///bar/baz/", root: nil, ["foo", "bar", "baz"]),
TestPathComponents("//foo///bar/baz/", root: "/", ["foo", "bar", "baz"]),
TestPathComponents("./", root: nil, ["."]),
TestPathComponents("./..", root: nil, [".", ".."]),
TestPathComponents("/./..//", root: "/", [".", ".."]),
]
// TODO: generalize to a driver protocol that will inherit from XCTest, expose allTestCases
// based on an associated type, and provide the testCasees func, assuming XCTest supports
// that.
func testCases() {
testPaths.forEach { $0.runAllTests() }
}
// TODO: Convert these kinds of test cases into mutation API test cases.
func testAdHocRRC() {
var path: FilePath = "/usr/local/bin"
@@ -343,21 +230,26 @@ final class FilePathComponentsTest: XCTestCase {
}
}
func testConcatenation() {
// TODO: convert tests into mutation tests
// for lhsTest in testPaths {
// let lhs = lhsTest.path
// for rhsTest in testPaths {
// let rhs = rhsTest.path
// adhocComponentsTest(lhs + rhs) { concatpath in
// // (lhs + rhs).components == (lhs.components + rhs.compontents)
// concatpath.expectEqualSequence(lhs.components + rhs.components, concatpath.components)
//
// // TODO: More tests around non-normalized separators
// }
// }
// }
func testCases() {
let 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//", root: "/", ["foo"]),
TestPathComponents("/foo///bar", root: "/", ["foo", "bar"]),
TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]),
TestPathComponents("foo///bar/baz/", root: nil, ["foo", "bar", "baz"]),
TestPathComponents("//foo///bar/baz/", root: "/", ["foo", "bar", "baz"]),
TestPathComponents("./", root: nil, ["."]),
TestPathComponents("./..", root: nil, [".", ".."]),
TestPathComponents("/./..//", root: "/", [".", ".."]),
]
testPaths.forEach { $0.runAllTests() }
}
func testSeparatorNormalization() {
@@ -50,7 +50,8 @@ extension FilePath {
guard !other.isEmpty else { return true }
guard !isEmpty else { return false }
let (selfLex, otherLex) = (self.lexicallyNormal, other.lexicallyNormal)
let (selfLex, otherLex) =
(self.lexicallyNormalized(), other.lexicallyNormalized())
if otherLex.isAbsolute { return selfLex.starts(with: otherLex) }
// FIXME: Windows semantics with relative roots?
@@ -243,7 +243,7 @@ extension SyntaxTestCase {
copy.lexicallyNormalize()
expectEqual(copy == path, path.isLexicallyNormal, "isLexicallyNormal")
expectEqual(lexicallyNormalized, copy.description, "lexically normalized")
expectEqual(copy, path.lexicallyNormal, "lexicallyNormal")
expectEqual(copy, path.lexicallyNormalized(), "lexicallyNormal")
expectEqual(absolute, path.isAbsolute, "absolute")
expectEqual(!absolute, path.isRelative, "!absolute")