Skip to content

Commit 6ce150a

Browse files
liamrosenfeldcmyr
authored andcommitted
Directory Checker Test + Clearer CLI Errors
1 parent 0b7b880 commit 6ce150a

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

Sources/XiCLICore/CLIHelper.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,18 @@ import ArgumentParser
1818
struct CLIHelper {
1919
static func resolvePath(from input: String) throws -> String {
2020
let fileManager = FileManager.default
21-
var filePath: URL!
2221

23-
// Small helper function used to determine if path is not a folder.
24-
func pathIsNotDirectory(_ path: String) -> Bool {
25-
var isDirectory = ObjCBool(false)
26-
if fileManager.fileExists(atPath: path, isDirectory: &isDirectory) {
27-
return !isDirectory.boolValue
28-
} else {
29-
return true
30-
}
31-
}
32-
3322
let pathString = canonicalPath(input)
3423

3524
guard pathIsNotDirectory(pathString) else {
36-
throw ValidationError("The path entered is to a directory")
25+
throw ValidationError("\(pathString) is a directory")
3726
}
3827

3928
if !fileManager.fileExists(atPath: pathString) {
4029
let createSuccess = fileManager.createFile(atPath: pathString, contents: nil, attributes: nil)
4130

4231
guard createSuccess else {
43-
throw RuntimeError("Could not create a file")
32+
throw RuntimeError("Could not create \(pathString)")
4433
}
4534
}
4635

@@ -77,9 +66,20 @@ struct CLIHelper {
7766
}
7867
}
7968
}
80-
69+
70+
71+
// MARK: - Helper Functions
8172
static func canonicalPath(_ path: String) -> String {
8273
return URL(fileURLWithPath: path).standardizedFileURL.resolvingSymlinksInPath().path
8374
}
75+
76+
static func pathIsNotDirectory(_ path: String) -> Bool {
77+
var isDirectory = ObjCBool(false)
78+
if FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) {
79+
return !isDirectory.boolValue
80+
} else {
81+
return true
82+
}
83+
}
8484
}
8585

Tests/XiCLICoreTests/CliHelperTests.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CLIHelperTests: XCTestCase {
3030
}
3131

3232
func testResolveAbsolutePath() {
33-
let fromPath = fileInTempDir("testResolvePath")
33+
let fromPath = fileInTempDir("test.txt")
3434
FileManager
3535
.default
3636
.createFile(atPath: fromPath, contents: "This is a tester file".data(using: .utf8), attributes: nil)
@@ -65,6 +65,12 @@ class CLIHelperTests: XCTestCase {
6565
func testFileOpen() {
6666
XCTAssertNoThrow(try CLIHelper.openFile(at: "test.txt"))
6767
}
68+
69+
func testDirectoryChecker() {
70+
let path = FileManager.default.temporaryDirectory.path
71+
let notDir = CLIHelper.pathIsNotDirectory(path)
72+
XCTAssert(!notDir, "directory was not detected")
73+
}
6874

6975
func testObserver() {
7076
let group = DispatchGroup()

0 commit comments

Comments
 (0)