Download this file
| 1 | import Foundation |
| 2 | import ImageIO |
| 3 | import Testing |
| 4 | @testable import EquipoShareCore |
| 5 | |
| 6 | @Test func filePreparationBoundsAndScopedMetadata() throws { |
| 7 | let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 8 | try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) |
| 9 | defer { try? FileManager.default.removeItem(at: directory) } |
| 10 | let input = directory.appendingPathComponent("hello.txt") |
| 11 | try Data("hello".utf8).write(to: input) |
| 12 | let access = ScopedAccess() |
| 13 | let prepared = try AttachmentFile.prepareFile(input, directory: directory, access: access) |
| 14 | #expect(try Data(contentsOf: prepared.url) == Data("hello".utf8)) |
| 15 | #expect(!access.started) |
| 16 | #expect(throws: AttachmentFailure.type) { try AttachmentFile.prepareFile(directory, directory: directory) } |
| 17 | #expect(throws: AttachmentFailure.tooLarge) { try AttachmentFile.stage(input, directory: directory, maximumBytes: 4, access: access) } |
| 18 | } |
| 19 | private final class ScopedAccess: AttachmentFileAccess, @unchecked Sendable { |
| 20 | var started = false |
| 21 | func start(_ url: URL) -> Bool { started = true; return true } |
| 22 | func stop(_ url: URL) { started = false } |
| 23 | func isRegularFile(_ url: URL) throws -> Bool { |
| 24 | #expect(started) |
| 25 | if !started { throw AttachmentFailure.type } |
| 26 | return try url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile == true |
| 27 | } |
| 28 | } |
| 29 | |