71 lines
3.1 KiB
Swift
71 lines
3.1 KiB
Swift
import HelderCore
|
|
import Testing
|
|
|
|
@Suite struct FileNodeTests {
|
|
private func names(_ node: FileNode?) -> [String] {
|
|
(node?.children ?? []).map(\.name)
|
|
}
|
|
|
|
@Test("folders come before files, each sorted by name")
|
|
func foldersFirst() {
|
|
let tree = FileTree.build(rootName: "proj", paths: ["README.md", "src/a.ts", "logo.bin", "app/x.php"])
|
|
#expect(tree.name == "proj")
|
|
#expect(tree.path == "")
|
|
#expect(names(tree) == ["app", "src", "logo.bin", "README.md"])
|
|
}
|
|
|
|
@Test("sorting ignores case, like localeCompare")
|
|
func caseInsensitiveSort() {
|
|
let tree = FileTree.build(rootName: "p", paths: ["b.txt", "A.txt", "a.md", "C.txt"])
|
|
#expect(names(tree) == ["a.md", "A.txt", "b.txt", "C.txt"])
|
|
}
|
|
|
|
@Test("nested paths build nested folders with full relative paths")
|
|
func nesting() {
|
|
let tree = FileTree.build(rootName: "p", paths: ["a/b/c.txt", "a/d.txt"])
|
|
let b = FileTree.find("a/b", in: tree)
|
|
#expect(b?.kind == .directory)
|
|
#expect(names(b) == ["c.txt"])
|
|
#expect(FileTree.find("a/b/c.txt", in: tree)?.kind == .file)
|
|
#expect(FileTree.find("a/b/c.txt", in: tree)?.children == nil)
|
|
}
|
|
|
|
@Test("empty folders that rg cannot list are forced into the tree")
|
|
func emptyFolders() {
|
|
let tree = FileTree.build(rootName: "p", paths: ["src/a.ts"], emptyDirs: ["empty", "src/nested/deep"])
|
|
#expect(names(tree) == ["empty", "src"])
|
|
#expect(FileTree.find("empty", in: tree)?.children == [])
|
|
#expect(names(FileTree.find("src/nested", in: tree)) == ["deep"])
|
|
}
|
|
|
|
@Test("duplicate and malformed paths do not duplicate nodes")
|
|
func duplicates() {
|
|
let tree = FileTree.build(rootName: "p", paths: ["a.txt", "a.txt", "", "/b.txt", "c//d.txt"], emptyDirs: ["", "c"])
|
|
#expect(names(tree) == ["c", "a.txt", "b.txt"])
|
|
#expect(names(FileTree.find("c", in: tree)) == ["d.txt"])
|
|
}
|
|
|
|
@Test("an empty listing is an empty root")
|
|
func emptyListing() {
|
|
#expect(FileTree.build(rootName: "p", paths: []) == FileNode(name: "p", kind: .directory, path: "", children: []))
|
|
}
|
|
|
|
@Test("visible rows honour expanded folders and hidden files")
|
|
func visibleRows() {
|
|
let tree = FileTree.build(rootName: "p", paths: [".env", "src/a.ts", "src/.hidden", "b.txt"])
|
|
#expect(FileTree.visibleRows(tree, expanded: [], showHidden: false).map(\.path) == ["src", "b.txt"])
|
|
let open = FileTree.visibleRows(tree, expanded: ["src"], showHidden: true)
|
|
#expect(open.map(\.path) == ["src", "src/.hidden", "src/a.ts", ".env", "b.txt"])
|
|
#expect(open.map(\.depth) == [0, 1, 1, 0, 0])
|
|
}
|
|
|
|
@Test("replacing a folder's children leaves the rest of the tree alone")
|
|
func replacingChildren() {
|
|
let tree = FileTree.build(rootName: "p", paths: ["src/a.ts", "lib/b.ts"])
|
|
let fresh = FileNode(name: "z.ts", kind: .file, path: "src/z.ts")
|
|
let updated = FileTree.replacingChildren(of: "src", with: [fresh], in: tree)
|
|
#expect(names(FileTree.find("src", in: updated)) == ["z.ts"])
|
|
#expect(names(FileTree.find("lib", in: updated)) == ["b.ts"])
|
|
}
|
|
}
|