Skip to content

Commit 25fc6ea

Browse files
committed
[TSCBasic] Add RelativePath.appending(components:) method
1 parent da50ef4 commit 25fc6ea

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Diff for: Sources/TSCBasic/Path.swift

+24
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,30 @@ public struct RelativePath: Hashable {
266266
public var components: [String] {
267267
return _impl.components
268268
}
269+
270+
/// Returns the relative path with the given relative path applied.
271+
public func appending(_ subpath: RelativePath) -> RelativePath {
272+
return RelativePath(_impl.appending(relativePath: subpath._impl))
273+
}
274+
275+
/// Returns the relative path with an additional literal component appended.
276+
///
277+
/// This method accepts pseudo-path like '.' or '..', but should not contain "/".
278+
public func appending(component: String) -> RelativePath {
279+
return RelativePath(_impl.appending(component: component))
280+
}
281+
282+
/// Returns the relative path with additional literal components appended.
283+
///
284+
/// This method should only be used in cases where the input is guaranteed
285+
/// to be a valid path component (i.e., it cannot be empty, contain a path
286+
/// separator, or be a pseudo-path like '.' or '..').
287+
public func appending(components names: String...) -> RelativePath {
288+
// FIXME: This doesn't seem a particularly efficient way to do this.
289+
return names.reduce(self, { path, name in
290+
path.appending(component: name)
291+
})
292+
}
269293
}
270294

271295
extension AbsolutePath: Codable {

Diff for: Tests/TSCBasicTests/PathTests.swift

+3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ class PathTests: XCTestCase {
214214
XCTAssertEqual(AbsolutePath("/").appending(components: "..").pathString, "/")
215215
XCTAssertEqual(AbsolutePath("/").appending(components: ".").pathString, "/")
216216
XCTAssertEqual(AbsolutePath("/").appending(components: "..", "a").pathString, "/a")
217+
218+
XCTAssertEqual(RelativePath("hello").appending(components: "a", "b", "c", "..").pathString, "hello/a/b")
219+
XCTAssertEqual(RelativePath("hello").appending(RelativePath("a/b/../c/d")).pathString, "hello/a/c/d")
217220
}
218221

219222
func testPathComponents() {

0 commit comments

Comments
 (0)