Files
VCCPlusOSS/platforms/macos/Tests/CMOCBuildHistoryTests.swift
T

117 lines
8.3 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
final class CMOCBuildHistoryTests: XCTestCase {
private var root: URL!
private var builds: URL { root.appendingPathComponent("Builds") }
private var project: URL { root.appendingPathComponent("Project") }
private var trash: URL { root.appendingPathComponent("Trash") }
override func setUpWithError() throws {
root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOC-History-" + UUID().uuidString)
for url in [builds, project, trash] { try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) }
}
override func tearDownWithError() throws { try? FileManager.default.removeItem(at: root) }
private func history() -> CMOCBuildHistory {
let destination = trash
return CMOCBuildHistory(root: builds) { url in
try FileManager.default.moveItem(at: url, to: destination.appendingPathComponent(url.lastPathComponent))
}
}
private func artifact(_ name: String = "main.bin") throws -> CMOCBuildArtifact {
let id = UUID(), directory = builds.appendingPathComponent(id.uuidString)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
let source = Data("int main(void) { return 0; }".utf8), binary = Data([0, 1, 2, 3])
try source.write(to: directory.appendingPathComponent("main.c"))
try binary.write(to: directory.appendingPathComponent(name))
return CMOCBuildArtifact(id: id, directory: directory, source: String(decoding: source, as: UTF8.self),
sourceData: source, sourceSHA256: CMOCProject.sha256(source), binaryData: binary, buildLog: "",
diskRelativePath: name.hasSuffix(".dsk") ? name : nil)
}
func testCompletedHistoryPersistsAndProtectsLiveSessions() throws {
let artifact = try artifact(), owner = history(), other = history()
try owner.record(artifact, project: project)
let entries = try owner.entries(project: project)
XCTAssertEqual(entries.count, 1)
XCTAssertEqual(entries[0].id, artifact.id)
XCTAssertEqual(entries[0].byteCount, Int64(artifact.sourceData.count + artifact.binaryData.count))
XCTAssertNil(entries[0].protectionReason)
XCTAssertNotNil(try other.entries(project: project)[0].protectionReason)
XCTAssertTrue(try other.remove(ids: [artifact.id], project: project, protectedIDs: []).removedIDs.isEmpty)
XCTAssertTrue(try owner.remove(ids: [artifact.id], project: project, protectedIDs: [artifact.id]).removedIDs.isEmpty)
XCTAssertTrue(FileManager.default.fileExists(atPath: artifact.directory.path))
XCTAssertTrue(try other.entries(project: root.appendingPathComponent("OtherProject")).isEmpty)
}
func testEligibleCleanupMovesOnlyCompletedSnapshotAndPreservesRunDisk() throws {
let artifact = try artifact("hello.dsk")
var owner: CMOCBuildHistory? = history()
try owner!.record(artifact, project: project)
let run = root.appendingPathComponent("Development Runs/Run-1/hello.dsk")
try FileManager.default.createDirectory(at: run.deletingLastPathComponent(), withIntermediateDirectories: true)
try Data([9, 8, 7]).write(to: run)
try Data("Finder metadata".utf8).write(to: artifact.directory.appendingPathComponent(".DS_Store"))
owner = nil // Simulate quitting the app; an abandoned flock is released.
let reopened = history()
XCTAssertNil(try reopened.entries(project: project)[0].protectionReason)
let removed = try reopened.remove(ids: [artifact.id], project: project, protectedIDs: [])
XCTAssertEqual(removed.removedIDs, [artifact.id]); XCTAssertTrue(removed.retained.isEmpty)
XCTAssertFalse(FileManager.default.fileExists(atPath: artifact.directory.path))
XCTAssertTrue(FileManager.default.fileExists(atPath: trash.appendingPathComponent(artifact.id.uuidString + "/hello.dsk").path))
XCTAssertEqual(try Data(contentsOf: run), Data([9, 8, 7]))
}
func testChangedMediaAddedHiddenFilesAndMissingFilesAreRetained() throws {
let owner = history()
let changed = try artifact("hello.dsk"), added = try artifact(), missing = try artifact()
for artifact in [changed, added, missing] { try owner.record(artifact, project: project) }
try Data([4, 5, 6]).write(to: changed.directory.appendingPathComponent("hello.dsk"))
try Data("keep".utf8).write(to: added.directory.appendingPathComponent(".user-notes"))
try FileManager.default.removeItem(at: missing.sourceURL)
XCTAssertTrue(try owner.entries(project: project).allSatisfy { $0.protectionReason != nil })
let result = try owner.remove(ids: [changed.id, added.id, missing.id], project: project, protectedIDs: [])
XCTAssertTrue(result.removedIDs.isEmpty); XCTAssertEqual(result.retained.count, 3)
for artifact in [changed, added, missing] { XCTAssertTrue(FileManager.default.fileExists(atPath: artifact.directory.path)) }
}
func testUnrecognizedLinkedAndForeignDirectoriesAreNeverCleaned() throws {
let owner = history(), unknown = try artifact(), foreign = try artifact(), linked = UUID()
try owner.record(foreign, project: root.appendingPathComponent("Other"))
try FileManager.default.createSymbolicLink(at: builds.appendingPathComponent(linked.uuidString), withDestinationURL: project)
XCTAssertTrue(try owner.entries(project: project).isEmpty)
let result = try owner.remove(ids: [unknown.id, foreign.id, linked], project: project, protectedIDs: [])
XCTAssertTrue(result.removedIDs.isEmpty); XCTAssertEqual(result.retained.count, 3)
XCTAssertTrue(FileManager.default.fileExists(atPath: project.path))
XCTAssertTrue(FileManager.default.fileExists(atPath: unknown.directory.path))
}
func testReservedNamesAndReplacedLeaseFailClosed() throws {
let owner = history(), collision = try artifact()
try Data("recipe output".utf8).write(to: collision.directory.appendingPathComponent(CMOCBuildHistory.manifestName))
XCTAssertThrowsError(try owner.record(collision, project: project))
XCTAssertEqual(try String(contentsOf: collision.directory.appendingPathComponent(CMOCBuildHistory.manifestName)), "recipe output")
let artifact = try artifact()
try owner.record(artifact, project: project)
let lease = artifact.directory.appendingPathComponent(CMOCBuildHistory.leaseName)
try FileManager.default.removeItem(at: lease)
try Data().write(to: lease)
XCTAssertNotNil(try owner.entries(project: project).first { $0.id == artifact.id }?.protectionReason)
XCTAssertTrue(try owner.remove(ids: [artifact.id], project: project, protectedIDs: []).removedIDs.isEmpty)
}
func testServiceRecordsSuccessfulBuildsButNotFailedAttempts() async throws {
let project = try CMOCProject.create(at: root.appendingPathComponent("Build Project"))
let toolchain = root.appendingPathComponent("Tools"), bin = toolchain.appendingPathComponent("bin")
try FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true)
for name in ["make", "cmoc", "cpp", "lwasm", "lwlink"] {
let script = name == "make" ? "for ext in bin o s lst map link; do printf fixture > main.$ext; done" : "exit 0"
let url = bin.appendingPathComponent(name)
try Data(("#!/bin/sh\n" + script + "\n").utf8).write(to: url)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: url.path)
}
let service = CMOCBuildService(toolchain: toolchain, buildRoot: builds)
let first = try await service.build(project: project, source: project.readSource())
let second = try await service.build(project: project, overlays: [:])
XCTAssertEqual(Set(try service.history(project: project.directory).map(\.id)), [first.id, second.id])
try Data("#!/bin/sh\nexit 2\n".utf8).write(to: bin.appendingPathComponent("make"))
do { _ = try await service.build(project: project, overlays: [:]); XCTFail("Expected recipe failure") }
catch { XCTAssertTrue(error.localizedDescription.contains("exit 2")) }
XCTAssertEqual(Set(try service.history(project: project.directory).map(\.id)), [first.id, second.id])
}
}