304 lines
13 KiB
Swift
304 lines
13 KiB
Swift
import Foundation
|
|
import HelderCore
|
|
import HelderServices
|
|
import Testing
|
|
|
|
/// The tree and folder listings, through the walk (no ripgrep) and through ripgrep.
|
|
@Suite struct FileServiceTreeTests {
|
|
/// src/a.ts, README.md and a binary logo, plus node_modules and .git that must stay hidden.
|
|
private func fixture() throws -> TempDirectory {
|
|
let dir = try TempDirectory()
|
|
try dir.write("src/a.ts", "export const a = 1\n")
|
|
try dir.write("README.md", "# hi\n")
|
|
try dir.write("node_modules/pkg/index.js", "module.exports = 1\n")
|
|
try dir.write(".git/HEAD", "ref: refs/heads/main\n")
|
|
try dir.write("logo.bin", Data([0x00, 0x01, 0x02, 0x00, 0x99]))
|
|
return dir
|
|
}
|
|
|
|
private func walkService(_ dir: TempDirectory) -> FileService {
|
|
FileService(root: dir.root, search: NoRipgrepSearch())
|
|
}
|
|
|
|
private func ripgrepService(_ dir: TempDirectory) -> FileService {
|
|
let search = RipgrepSearchService(root: dir.root, config: FixedConfig(), locator: Ripgrep.locator, environment: [:])
|
|
return FileService(root: dir.root, search: search)
|
|
}
|
|
|
|
private func names(_ nodes: [FileNode]?) -> [String] {
|
|
(nodes ?? []).map(\.name)
|
|
}
|
|
|
|
@Test func theWalkListsFoldersBeforeFilesAndSkipsIgnoredFolders() async throws {
|
|
let dir = try fixture()
|
|
let tree = await walkService(dir).tree()
|
|
#expect(tree.name == dir.root.lastPathComponent)
|
|
#expect(tree.path == "")
|
|
#expect(names(tree.children) == ["src", "logo.bin", "README.md"])
|
|
#expect(tree.children?.first?.children == [FileNode(name: "a.ts", kind: .file, path: "src/a.ts")])
|
|
}
|
|
|
|
@Test func theWalkSortsNamesCaseInsensitively() async throws {
|
|
let dir = try TempDirectory()
|
|
for name in ["b.txt", "A.txt", "c.txt", "Zeta", "alpha"] {
|
|
if name.hasSuffix(".txt") { try dir.write(name, "") } else { try dir.mkdir(name) }
|
|
}
|
|
#expect(names(await walkService(dir).tree().children) == ["alpha", "Zeta", "A.txt", "b.txt", "c.txt"])
|
|
}
|
|
|
|
@Test func theWalkShowsEmptyFoldersAndDotfiles() async throws {
|
|
let dir = try fixture()
|
|
try dir.mkdir("empty")
|
|
try dir.write(".env", "APP_NAME=x\n")
|
|
let tree = await walkService(dir).tree()
|
|
#expect(names(tree.children) == ["empty", "src", ".env", "logo.bin", "README.md"])
|
|
#expect(tree.children?.first == FileNode(name: "empty", kind: .directory, path: "empty", children: []))
|
|
}
|
|
|
|
@Test func theWalkStopsAtTheDepthLimit() async throws {
|
|
let dir = try TempDirectory()
|
|
let deep = (0...FileLimits.maxWalkDepth + 2).map { "d\($0)" }.joined(separator: "/")
|
|
try dir.write(deep + "/leaf.txt", "x")
|
|
var node: FileNode? = await walkService(dir).tree()
|
|
var depth = 0
|
|
while let next = node?.children?.first {
|
|
node = next
|
|
depth += 1
|
|
}
|
|
#expect(depth == FileLimits.maxWalkDepth + 1)
|
|
}
|
|
|
|
@Test(.enabled(if: Ripgrep.isPresent))
|
|
func theRipgrepTreeAddsTheEmptyFoldersRipgrepCannotList() async throws {
|
|
let dir = try fixture()
|
|
try dir.mkdir("empty")
|
|
try dir.mkdir("src/nested/deep")
|
|
let tree = await ripgrepService(dir).tree()
|
|
#expect(names(tree.children) == ["empty", "src", "logo.bin", "README.md"])
|
|
let src = FileTree.find("src", in: tree)
|
|
#expect(names(src?.children) == ["nested", "a.ts"])
|
|
#expect(names(FileTree.find("src/nested", in: tree)?.children) == ["deep"])
|
|
}
|
|
|
|
@Test(.enabled(if: Ripgrep.isPresent))
|
|
func theRipgrepTreeShowsGitignoredFilesByDefault() async throws {
|
|
let repo = try TempGitRepo()
|
|
try repo.write(".gitignore", "secret.txt\n")
|
|
try repo.write("secret.txt", "shh")
|
|
let search = RipgrepSearchService(root: repo.root, config: FixedConfig(), locator: Ripgrep.locator, environment: [:])
|
|
let tree = await FileService(root: repo.root, search: search).tree()
|
|
#expect(names(tree.children) == [".gitignore", "secret.txt"])
|
|
}
|
|
|
|
@Test func childrenOfTheRootAndOfASubfolder() async throws {
|
|
let dir = try fixture()
|
|
let service = walkService(dir)
|
|
#expect(names(await service.children(of: "")) == ["src", "logo.bin", "README.md"])
|
|
#expect(await service.children(of: "src") == [FileNode(name: "a.ts", kind: .file, path: "src/a.ts")])
|
|
}
|
|
|
|
@Test func childrenAreReadFreshOnEveryCall() async throws {
|
|
let dir = try fixture()
|
|
let service = walkService(dir)
|
|
_ = await service.children(of: "src")
|
|
try dir.write("src/b.ts", "export const b = 2\n")
|
|
try FileManager.default.removeItem(at: dir.url("src/a.ts"))
|
|
try dir.mkdir("src/fresh")
|
|
#expect(await service.children(of: "src") == [
|
|
FileNode(name: "fresh", kind: .directory, path: "src/fresh", children: []),
|
|
FileNode(name: "b.ts", kind: .file, path: "src/b.ts"),
|
|
])
|
|
}
|
|
|
|
@Test func childrenOutsideTheProjectAreEmpty() async throws {
|
|
let dir = try fixture()
|
|
#expect(await walkService(dir).children(of: "../").isEmpty)
|
|
}
|
|
|
|
@Test(.enabled(if: Ripgrep.isPresent))
|
|
func ripgrepChildrenIncludeAFreshEmptyFolder() async throws {
|
|
let dir = try fixture()
|
|
try dir.mkdir("src/fresh")
|
|
let service = ripgrepService(dir)
|
|
#expect(await service.children(of: "src") == [
|
|
FileNode(name: "fresh", kind: .directory, path: "src/fresh", children: []),
|
|
FileNode(name: "a.ts", kind: .file, path: "src/a.ts"),
|
|
])
|
|
#expect(!names(await service.children(of: "")).contains("node_modules"))
|
|
}
|
|
|
|
@Test func emptyDirectoriesAreFoldersWithoutAnyFileBelow() async throws {
|
|
let dir = try fixture()
|
|
try dir.mkdir("empty")
|
|
try dir.mkdir("src/nested/deep")
|
|
try dir.mkdir("node_modules/also-empty")
|
|
let service = walkService(dir)
|
|
#expect(Set(await service.emptyDirectories()) == ["empty", "src/nested", "src/nested/deep"])
|
|
#expect(Set(await service.emptyDirectories(under: "src")) == ["src/nested", "src/nested/deep"])
|
|
}
|
|
|
|
@Test func aFolderHoldingOnlyAnIgnoredFolderCountsAsEmpty() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("app/node_modules/x.js", "x")
|
|
#expect(await walkService(dir).emptyDirectories() == ["app"])
|
|
}
|
|
}
|
|
|
|
/// Reading, writing, creating, renaming and deleting single entries.
|
|
@Suite struct FileServiceEntryTests {
|
|
private func service(_ dir: TempDirectory) -> FileService {
|
|
FileService(root: dir.root, search: NoRipgrepSearch())
|
|
}
|
|
|
|
@Test func writeThenReadRoundTrips() async throws {
|
|
let dir = try TempDirectory()
|
|
let files = service(dir)
|
|
try await files.write("note.txt", contents: "hello world\n")
|
|
#expect(try await files.read("note.txt") == "hello world\n")
|
|
}
|
|
|
|
@Test func aBinaryFileReadsAsEmpty() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("logo.bin", Data([0x89, 0x00, 0x01]))
|
|
#expect(try await service(dir).read("logo.bin") == "")
|
|
}
|
|
|
|
@Test func readTextTellsAnEmptyFileFromABinaryOne() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("empty.txt", "")
|
|
try dir.write("logo.bin", Data([0x89, 0x00, 0x01]))
|
|
try dir.write("note.txt", "hello\n")
|
|
let files = service(dir)
|
|
#expect(try await files.readText("empty.txt") == .text(""))
|
|
#expect(try await files.readText("logo.bin") == .binary)
|
|
#expect(try await files.readText("note.txt") == .text("hello\n"))
|
|
}
|
|
|
|
@Test func aNulByteAfterTheFirst8000BytesStillReadsAsText() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("long.txt", Data(repeating: 0x61, count: 8000) + Data([0x00]))
|
|
#expect(try await service(dir).readText("long.txt") == .text(String(repeating: "a", count: 8000) + "\0"))
|
|
}
|
|
|
|
@Test func readingOutsideTheProjectThrows() async throws {
|
|
let dir = try TempDirectory()
|
|
await #expect(throws: FileServiceError.outsideProject) { try await service(dir).read("../etc/passwd") }
|
|
}
|
|
|
|
@Test func createFileMakesAnEmptyFileAndItsFolders() async throws {
|
|
let dir = try TempDirectory()
|
|
try await service(dir).createFile("src/new/fresh.ts")
|
|
#expect(try dir.read("src/new/fresh.ts") == "")
|
|
}
|
|
|
|
@Test func createFileRefusesToOverwrite() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("keep.txt", "precious\n")
|
|
await #expect(throws: FileServiceError.alreadyExists("keep.txt")) { try await service(dir).createFile("keep.txt") }
|
|
#expect(try dir.read("keep.txt") == "precious\n")
|
|
}
|
|
|
|
@Test func createFileRefusesTheRootAndPathsOutsideIt() async throws {
|
|
let dir = try TempDirectory()
|
|
let files = service(dir)
|
|
await #expect(throws: FileServiceError.outsideProject) { try await files.createFile("../escape.txt") }
|
|
await #expect(throws: FileServiceError.outsideProject) { try await files.createFile("") }
|
|
#expect(!FileManager.default.fileExists(atPath: dir.root.deletingLastPathComponent().appendingPathComponent("escape.txt").path))
|
|
}
|
|
|
|
@Test func createDirectoryMakesNestedFolders() async throws {
|
|
let dir = try TempDirectory()
|
|
try await service(dir).createDirectory("a/b/c")
|
|
var isDirectory: ObjCBool = false
|
|
#expect(FileManager.default.fileExists(atPath: dir.url("a/b/c").path, isDirectory: &isDirectory) && isDirectory.boolValue)
|
|
}
|
|
|
|
@Test func createDirectoryRefusesATakenName() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.mkdir("taken")
|
|
try dir.write("file", "")
|
|
let files = service(dir)
|
|
await #expect(throws: FileServiceError.alreadyExists("taken")) { try await files.createDirectory("taken") }
|
|
await #expect(throws: FileServiceError.alreadyExists("file")) { try await files.createDirectory("file") }
|
|
await #expect(throws: FileServiceError.outsideProject) { try await files.createDirectory("../out") }
|
|
}
|
|
|
|
@Test func renameMovesAFileWithinItsFolder() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("src/old.ts", "x\n")
|
|
#expect(try await service(dir).rename("src/old.ts", to: "new.ts") == "src/new.ts")
|
|
#expect(try dir.read("src/new.ts") == "x\n")
|
|
#expect(!dir.exists("src/old.ts"))
|
|
}
|
|
|
|
@Test func renameMovesAFolderWithEverythingInside() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("old/a.ts", "a\n")
|
|
#expect(try await service(dir).rename("old", to: "new") == "new")
|
|
#expect(try dir.read("new/a.ts") == "a\n")
|
|
}
|
|
|
|
@Test func renameCanChangeOnlyTheCase() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("notes.md", "hi\n")
|
|
#expect(try await service(dir).rename("notes.md", to: "Notes.md") == "Notes.md")
|
|
#expect(try FileManager.default.contentsOfDirectory(atPath: dir.root.path) == ["Notes.md"])
|
|
}
|
|
|
|
@Test func renameToTheSameNameIsANoOp() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("a.txt", "a\n")
|
|
#expect(try await service(dir).rename("a.txt", to: " a.txt/ ") == "a.txt")
|
|
}
|
|
|
|
@Test func renameRefusesATakenName() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("a.txt", "a\n")
|
|
try dir.write("b.txt", "b\n")
|
|
await #expect(throws: FileServiceError.alreadyExists("b.txt")) { try await service(dir).rename("a.txt", to: "b.txt") }
|
|
#expect(try dir.read("b.txt") == "b\n")
|
|
#expect(try dir.read("a.txt") == "a\n")
|
|
}
|
|
|
|
@Test(arguments: ["sub/a.txt", "../a.txt", " ", "..", "."])
|
|
func renameRefusesAPathShapedOrEmptyName(_ name: String) async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.mkdir("sub")
|
|
try dir.write("a.txt", "a\n")
|
|
await #expect(throws: FileServiceError.invalidName(name)) { try await service(dir).rename("a.txt", to: name) }
|
|
#expect(try dir.read("a.txt") == "a\n")
|
|
}
|
|
|
|
@Test func renameRefusesTheRootAndEscapes() async throws {
|
|
let dir = try TempDirectory()
|
|
let files = service(dir)
|
|
await #expect(throws: FileServiceError.outsideProject) { _ = try await files.rename("", to: "x") }
|
|
await #expect(throws: FileServiceError.outsideProject) { _ = try await files.rename("../x", to: "y") }
|
|
}
|
|
|
|
@Test func renameOfAMissingEntryThrows() async throws {
|
|
let dir = try TempDirectory()
|
|
await #expect(throws: (any Error).self) { _ = try await service(dir).rename("missing.txt", to: "other.txt") }
|
|
}
|
|
|
|
@Test func deleteRemovesAFolderButNeverTheRoot() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("folder/a.txt", "a")
|
|
let files = service(dir)
|
|
try await files.delete("folder")
|
|
#expect(!dir.exists("folder"))
|
|
await #expect(throws: FileServiceError.outsideProject) { try await files.delete("") }
|
|
#expect(FileManager.default.fileExists(atPath: dir.root.path))
|
|
}
|
|
|
|
@Test func readImageReturnsBytesOnlyForImages() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("pic.png", Data([0x89, 0x50, 0x4E, 0x47]))
|
|
try dir.write("notes.txt", "text")
|
|
let files = service(dir)
|
|
#expect(await files.readImage("pic.png") == Data([0x89, 0x50, 0x4E, 0x47]))
|
|
#expect(await files.readImage("notes.txt") == nil)
|
|
#expect(await files.readImage("missing.png") == nil)
|
|
}
|
|
}
|