Files
helder/Tests/HelderServicesTests/Files/FileServiceSafetyTests.swift
T

121 lines
6.1 KiB
Swift

import Foundation
import HelderCore
import HelderServices
import Testing
/// Saves that must not lose data, and paths that must not leave the project through a symlink.
@Suite struct FileServiceSafetyTests {
private func service(_ root: URL) -> FileService {
FileService(root: root, search: NoRipgrepSearch())
}
private func permissions(_ url: URL) throws -> Int {
try (FileManager.default.attributesOfItem(atPath: url.path)[.posixPermissions] as? NSNumber)?.intValue ?? -1
}
// MARK: Saving
@Test func aSaveKeepsTheFilePermissions() async throws {
let dir = try TempDirectory()
try dir.write("run.sh", "echo old\n")
try FileManager.default.setAttributes([.posixPermissions: 0o750], ofItemAtPath: dir.url("run.sh").path)
try await service(dir.root).write("run.sh", contents: "echo new\n")
#expect(try dir.read("run.sh") == "echo new\n")
#expect(try permissions(dir.url("run.sh")) == 0o750)
}
@Test func aSaveThroughASymlinkWritesTheTargetAndKeepsTheLink() async throws {
let dir = try TempDirectory()
try dir.write("real/config.php", "old\n")
try FileManager.default.createSymbolicLink(atPath: dir.url("config.php").path, withDestinationPath: "real/config.php")
try await service(dir.root).write("config.php", contents: "new\n")
#expect(try dir.read("real/config.php") == "new\n")
#expect(try FileManager.default.destinationOfSymbolicLink(atPath: dir.url("config.php").path) == "real/config.php")
}
@Test func aSaveReplacesTheFileInOneStepAndLeavesNoTempFileBehind() async throws {
let dir = try TempDirectory()
try dir.write("a.txt", "old\n")
let before = try FileManager.default.attributesOfItem(atPath: dir.url("a.txt").path)[.systemFileNumber] as? NSNumber
try await service(dir.root).write("a.txt", contents: "new\n")
let after = try FileManager.default.attributesOfItem(atPath: dir.url("a.txt").path)[.systemFileNumber] as? NSNumber
#expect(before != after)
#expect(try FileManager.default.contentsOfDirectory(atPath: dir.root.path) == ["a.txt"])
}
@Test func aSaveOfANewFileCreatesIt() async throws {
let dir = try TempDirectory()
try await service(dir.root).write("fresh.txt", contents: "x\n")
#expect(try dir.read("fresh.txt") == "x\n")
}
// MARK: Symlinks out of the project
/// A project whose `link` folder is a symlink to a folder outside it holding `outside.txt`.
private func linkedProject() throws -> (project: TempDirectory, outside: TempDirectory) {
let project = try TempDirectory()
let outside = try TempDirectory()
try outside.write("outside.txt", "keep\n")
try FileManager.default.createSymbolicLink(at: project.url("link"), withDestinationURL: outside.root)
return (project, outside)
}
@Test func deleteThroughASymlinkedFolderIsRefused() async throws {
let (project, outside) = try linkedProject()
await #expect(throws: FileServiceError.outsideProject) { try await service(project.root).delete("link/outside.txt") }
#expect(outside.exists("outside.txt"))
}
@Test func renameThroughASymlinkedFolderIsRefused() async throws {
let (project, outside) = try linkedProject()
await #expect(throws: FileServiceError.outsideProject) { _ = try await service(project.root).rename("link/outside.txt", to: "moved.txt") }
#expect(outside.exists("outside.txt"))
#expect(!outside.exists("moved.txt"))
}
@Test func creatingThroughASymlinkedFolderIsRefused() async throws {
let (project, outside) = try linkedProject()
let files = service(project.root)
await #expect(throws: FileServiceError.outsideProject) { try await files.createFile("link/new.txt") }
await #expect(throws: FileServiceError.outsideProject) { try await files.createDirectory("link/sub") }
await #expect(throws: FileServiceError.outsideProject) { try await files.write("link/new.txt", contents: "x") }
#expect(!outside.exists("new.txt"))
#expect(!outside.exists("sub"))
}
@Test func editingAnExistingFileThroughASymlinkIsAllowed() async throws {
let (project, outside) = try linkedProject()
try await service(project.root).write("link/outside.txt", contents: "edited\n")
#expect(try outside.read("outside.txt") == "edited\n")
}
@Test func deletingTheSymlinkItselfRemovesOnlyTheLink() async throws {
let (project, outside) = try linkedProject()
try await service(project.root).delete("link")
#expect(!project.exists("link"))
#expect(outside.exists("outside.txt"))
}
// MARK: Case-only renames
@Test func aCaseOnlyRenameOnACaseSensitiveVolumeNeverClobbersAnotherFile() async throws {
let volume = try CaseSensitiveVolume()
let root = volume.root.appendingPathComponent("project")
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
try Data("lower\n".utf8).write(to: root.appendingPathComponent("readme.md"))
try Data("upper\n".utf8).write(to: root.appendingPathComponent("README.md"))
await #expect(throws: FileServiceError.alreadyExists("README.md")) { _ = try await service(root).rename("readme.md", to: "README.md") }
#expect(try String(contentsOf: root.appendingPathComponent("README.md"), encoding: .utf8) == "upper\n")
#expect(try String(contentsOf: root.appendingPathComponent("readme.md"), encoding: .utf8) == "lower\n")
}
@Test func aCaseOnlyRenameOnACaseSensitiveVolumeStillWorksWhenTheNameIsFree() async throws {
let volume = try CaseSensitiveVolume()
let root = volume.root.appendingPathComponent("project")
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
try Data("x\n".utf8).write(to: root.appendingPathComponent("notes.md"))
#expect(try await service(root).rename("notes.md", to: "Notes.md") == "Notes.md")
#expect(try FileManager.default.contentsOfDirectory(atPath: root.path) == ["Notes.md"])
}
}