Skip to content

Commit ee85d0e

Browse files
vlmaciidgh
authored andcommitted
lifted the validation API
1 parent 7567ce2 commit ee85d0e

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

Sources/Basic/Path.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ public struct AbsolutePath {
9898
self.init(absPath, RelativePath(relStr))
9999
}
100100

101+
/// Convenience initializer that verifies that the path is absolute.
102+
public init(validating path: String) throws {
103+
switch path.first {
104+
case "/":
105+
self.init(path)
106+
case "~":
107+
throw PathValidationError.startsWithTilde(path)
108+
default:
109+
throw PathValidationError.invalidAbsolutePath(path)
110+
}
111+
}
112+
101113
/// Directory component. An absolute path always has a non-empty directory
102114
/// component (the directory component of the root path is the root itself).
103115
public var dirname: String {
@@ -503,17 +515,6 @@ extension AbsolutePath {
503515
return self.components.starts(with: other.components)
504516
}
505517

506-
/// Checks the path and throws a `PathValidationError` if the path is invalid.
507-
public static func validate(pathString path: String) throws {
508-
switch path.first {
509-
case "/":
510-
return
511-
case "~":
512-
throw PathValidationError.startsWithTilde(path)
513-
default:
514-
throw PathValidationError.invalidAbsolutePath(path)
515-
}
516-
}
517518
}
518519

519520
// FIXME: We should consider whether to merge the two `normalize()` functions.

Sources/Workspace/ManagedDependency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ extension ManagedDependency.State: JSONMappable, JSONSerializable {
143143
self = try .checkout(json.get("checkoutState"))
144144
case "edited":
145145
let path: String? = json.get("path")
146-
self = .edited(path.map(AbsolutePath.init))
146+
self = .edited(path.map({AbsolutePath($0)}))
147147
default:
148148
throw JSON.MapError.custom(key: nil, message: "Invalid state \(name)")
149149
}

Tests/BasicTests/PathTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ class PathTests: XCTestCase {
273273
}
274274

275275
func testAbsolutePathValidation() {
276-
XCTAssertNoThrow(try AbsolutePath.validate(pathString: "/a/b/c/d"))
276+
XCTAssertNoThrow(try AbsolutePath(validating: "/a/b/c/d"))
277277

278-
XCTAssertThrowsError(try AbsolutePath.validate(pathString: "~/a/b/d")) { error in
278+
XCTAssertThrowsError(try AbsolutePath(validating: "~/a/b/d")) { error in
279279
XCTAssertEqual("\(error)", "invalid absolute path '~/a/b/d'; absolute path must begin with /")
280280
}
281281

282-
XCTAssertThrowsError(try AbsolutePath.validate(pathString: "a/b/d")) { error in
282+
XCTAssertThrowsError(try AbsolutePath(validating: "a/b/d")) { error in
283283
XCTAssertEqual("\(error)", "invalid absolute path 'a/b/d'")
284284
}
285285
}

Tests/BasicTests/miscTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class miscTests: XCTestCase {
5050
func testEnvSearchPaths() throws {
5151
let cwd = AbsolutePath("/dummy")
5252
let paths = getEnvSearchPaths(pathString: "something:.:abc/../.build/debug:/usr/bin:/bin/", currentWorkingDirectory: cwd)
53-
XCTAssertEqual(paths, ["/dummy/something", "/dummy", "/dummy/.build/debug", "/usr/bin", "/bin"].map(AbsolutePath.init))
53+
XCTAssertEqual(paths, ["/dummy/something", "/dummy", "/dummy/.build/debug", "/usr/bin", "/bin"].map({AbsolutePath($0)}))
5454
}
5555

5656
func testEmptyEnvSearchPaths() throws {

0 commit comments

Comments
 (0)