Publish VCC Plus OSS source distribution for 3.6.2 build 60
This commit is contained in:
commit
c652a5276d
1192 files changed
+272368
No files matched your search
@@ -0,0 +1,235 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import XCTest
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Library source opening owns no guest execution and must review current drafts.
|
||||
/// All projects, settings, recovery and tool paths are authored private fixtures.
|
||||
final class CMOCImportedProjectTests: XCTestCase {
|
||||
private struct Deadline: Error {}
|
||||
private struct Fixture {
|
||||
let root: URL
|
||||
let project: CMOCProject
|
||||
let imported: CMOCProject
|
||||
let model: CMOCDevelopmentModel
|
||||
let machine: MachineModel
|
||||
let controller: VCCMachineController
|
||||
let defaults: UserDefaults
|
||||
var marker: URL { imported.directory.appendingPathComponent("MAKE_WAS_RUN") }
|
||||
var recoveryRoot: URL { root.appendingPathComponent("Recovery") }
|
||||
}
|
||||
@MainActor func testImportedProjectSelectsOnlyADeclaredBuildConfiguration() async throws {
|
||||
try await withFixture { fixture in
|
||||
let identity = fixture.model.assistantProjectID
|
||||
do {
|
||||
_ = try await fixture.model.openImportedProject(at: fixture.imported.directory,
|
||||
expectedProjectID: identity, preferredConfiguration: "Missing", reviewUnsaved: { XCTFail("Invalid configuration must fail before review"); return true })
|
||||
XCTFail("Missing configuration accepted")
|
||||
} catch { }
|
||||
XCTAssertEqual(fixture.model.project?.directory, fixture.project.directory)
|
||||
let opened = try await fixture.model.openImportedProject(at: fixture.imported.directory,
|
||||
expectedProjectID: identity, preferredConfiguration: "Release", reviewUnsaved: { true })
|
||||
XCTAssertTrue(opened); XCTAssertEqual(fixture.model.configurationName, "Release")
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path))
|
||||
}
|
||||
}
|
||||
@MainActor private func settle(_ fixture: Fixture) async throws {
|
||||
for _ in 0..<500 {
|
||||
if !fixture.machine.busy && !fixture.model.building { return }
|
||||
try await Task.sleep(nanoseconds: 10_000_000)
|
||||
}
|
||||
XCTFail("Isolated imported-project fixture did not settle"); throw Deadline()
|
||||
}
|
||||
@MainActor private func step(_ operation: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot {
|
||||
await withCheckedContinuation { done in operation { done.resume(returning: $0) } }
|
||||
}
|
||||
@MainActor private func snapshot(_ controller: VCCMachineController) async -> VCCDebuggerSnapshot {
|
||||
await withCheckedContinuation { done in
|
||||
controller.debuggerSnapshot(address: 0x8000, followPC: false, memoryAddress: 0x2000) { done.resume(returning: $0) }
|
||||
}
|
||||
}
|
||||
@MainActor private func flush(_ model: CMOCDevelopmentModel) async {
|
||||
await withCheckedContinuation { done in model.flushDraftRecovery { done.resume() } }
|
||||
}
|
||||
@MainActor private func withFixture(_ body: (Fixture) async throws -> Void) async throws {
|
||||
let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCImported-" + UUID().uuidString)
|
||||
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
||||
let suite = "CMOCImported." + UUID().uuidString, defaults = UserDefaults(suiteName: suite)!
|
||||
defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: root) }
|
||||
let project = try CMOCProject.create(at: root.appendingPathComponent("Existing Project"))
|
||||
let imported = try CMOCProject.create(at: root.appendingPathComponent("Imported Project"))
|
||||
try Data("#define PERSONAL_VALUE 1\n".utf8).write(to: project.directory.appendingPathComponent("personal.h"))
|
||||
try Data("int imported_value;\nint main(void) { return 0; }\n".utf8).write(to: imported.sourceURL)
|
||||
// Both Make parsing and executing a target would leave this marker.
|
||||
try Data("IMPORT_SENTINEL := $(shell touch MAKE_WAS_RUN)\nbuild:\n\ttouch MAKE_WAS_RUN\n".utf8)
|
||||
.write(to: imported.makefileURL)
|
||||
let controller = VCCMachineController()
|
||||
let machine = MachineModel(diskStore: .init(url: root.appendingPathComponent("Disks.json")), controller: controller,
|
||||
displayDefaults: defaults, inputStore: .init(url: root.appendingPathComponent("Input.json")))
|
||||
let model = CMOCDevelopmentModel(machine: machine, toolchain: root.appendingPathComponent("No External Tools"),
|
||||
defaults: defaults, buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery"))
|
||||
let fixture = Fixture(root: root, project: project, imported: imported, model: model,
|
||||
machine: machine, controller: controller, defaults: defaults)
|
||||
do {
|
||||
try await settle(fixture)
|
||||
try model.adopt(project)
|
||||
try await body(fixture)
|
||||
model.cancelBuild(); try await settle(fixture)
|
||||
await flush(model)
|
||||
await withCheckedContinuation { done in machine.shutdown { done.resume() } }
|
||||
} catch {
|
||||
model.cancelBuild()
|
||||
try? await settle(fixture)
|
||||
await flush(model)
|
||||
await withCheckedContinuation { done in machine.shutdown { done.resume() } }
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func testCancelPreservesCurrentDraftsNativeUndoAndRecovery() async throws {
|
||||
try await withFixture { fixture in
|
||||
let model = fixture.model, original = model.source
|
||||
model.openFile("personal.h"); model.source = "#define PERSONAL_VALUE 42\n"; model.openFile("main.c")
|
||||
let view = CSourceEditor(text: Binding(get: { model.source }, set: { model.source = $0 }), currentLine: nil)
|
||||
let coordinator = view.makeCoordinator()
|
||||
let editor = coordinator.makeEditor(file: "main.c", text: original)
|
||||
coordinator.activeFile = "main.c"
|
||||
model.assistantEditor = editor; model.assistantEditorPool = coordinator; model.assistantEditorFile = "main.c"
|
||||
let undo = try XCTUnwrap(editor.undoManager)
|
||||
undo.groupsByEvent = false; undo.beginUndoGrouping()
|
||||
editor.insertText("\n/* personal unsaved change */\n", replacementRange: NSRange(location: original.utf16.count, length: 0))
|
||||
editor.breakUndoCoalescing(); undo.endUndoGrouping()
|
||||
XCTAssertEqual(model.source, editor.string)
|
||||
let dirty = model.source, identity = model.assistantProjectID
|
||||
let documentTexts = model.documents.map(\.text)
|
||||
var reviews = 0
|
||||
let opened = try await model.openImportedProject(at: fixture.imported.directory, expectedProjectID: identity) {
|
||||
reviews += 1; return false
|
||||
}
|
||||
XCTAssertFalse(opened); XCTAssertEqual(reviews, 1)
|
||||
XCTAssertEqual(model.project?.directory, fixture.project.directory)
|
||||
XCTAssertEqual(model.assistantProjectID, identity)
|
||||
XCTAssertEqual(model.documents.map(\.text), documentTexts)
|
||||
XCTAssertEqual(model.source, dirty)
|
||||
XCTAssertIdentical(model.assistantEditorPool, coordinator)
|
||||
XCTAssertIdentical(editor.undoManager, undo); XCTAssertTrue(undo.canUndo)
|
||||
XCTAssertEqual(try fixture.project.readSource(), original, "Cancel must not save over personal source")
|
||||
await flush(model)
|
||||
let recovery = CMOCWorkspaceRecovery(root: fixture.recoveryRoot)
|
||||
let records = try recovery.scan(projectDirectory: fixture.project.directory).records
|
||||
XCTAssertTrue(records.flatMap(\.drafts).contains(where: { $0.path == "main.c" && $0.text == dirty }))
|
||||
XCTAssertTrue(records.flatMap(\.drafts).contains(where: { $0.path == "personal.h" && $0.text.contains("42") }))
|
||||
undo.undo()
|
||||
XCTAssertEqual(editor.string, original)
|
||||
XCTAssertEqual(model.source, original, "The pre-import typing history remains usable")
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path))
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func testStaleProjectIdentityRejectsBeforeReview() async throws {
|
||||
try await withFixture { fixture in
|
||||
let identity = fixture.model.assistantProjectID, before = fixture.model.source
|
||||
var reviewed = false
|
||||
do {
|
||||
_ = try await fixture.model.openImportedProject(at: fixture.imported.directory, expectedProjectID: UUID()) {
|
||||
reviewed = true; return true
|
||||
}
|
||||
XCTFail("A stale downloader must not replace the current C project")
|
||||
} catch { XCTAssertTrue(error.localizedDescription.contains("changed during")) }
|
||||
XCTAssertFalse(reviewed)
|
||||
XCTAssertEqual(fixture.model.assistantProjectID, identity)
|
||||
XCTAssertEqual(fixture.model.source, before)
|
||||
XCTAssertEqual(fixture.model.project?.directory, fixture.project.directory)
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path))
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func testProjectChangedDuringReviewCannotBeOverwrittenByImportedResult() async throws {
|
||||
try await withFixture { fixture in
|
||||
let other = try CMOCProject.create(at: fixture.root.appendingPathComponent("Other Project"))
|
||||
var changed = false
|
||||
do {
|
||||
_ = try await fixture.model.openImportedProject(at: fixture.imported.directory,
|
||||
expectedProjectID: fixture.model.assistantProjectID) {
|
||||
do { try fixture.model.adopt(other); changed = true } catch { XCTFail(error.localizedDescription) }
|
||||
return true
|
||||
}
|
||||
XCTFail("A review that changes projects must invalidate the import completion")
|
||||
} catch { XCTAssertTrue(error.localizedDescription.contains("changed during")) }
|
||||
XCTAssertTrue(changed)
|
||||
XCTAssertEqual(fixture.model.project?.directory, other.directory)
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path))
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func testBusyBuildAfterReviewPreventsImportedProjectSwitch() async throws {
|
||||
try await withFixture { fixture in
|
||||
let identity = fixture.model.assistantProjectID
|
||||
do {
|
||||
_ = try await fixture.model.openImportedProject(at: fixture.imported.directory, expectedProjectID: identity) {
|
||||
fixture.model.build()
|
||||
XCTAssertTrue(fixture.model.building)
|
||||
return true
|
||||
}
|
||||
XCTFail("A build started during review must block the workspace switch")
|
||||
} catch { XCTAssertTrue(error.localizedDescription.contains("current C operation")) }
|
||||
fixture.model.cancelBuild()
|
||||
XCTAssertEqual(fixture.model.project?.directory, fixture.project.directory)
|
||||
XCTAssertEqual(fixture.model.assistantProjectID, identity)
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path))
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func testMalformedImportFailsBeforeReviewAndKeepsDirtyProject() async throws {
|
||||
try await withFixture { fixture in
|
||||
fixture.model.source += "\n/* keep this draft */\n"
|
||||
let dirty = fixture.model.source, identity = fixture.model.assistantProjectID
|
||||
try Data("{bad configuration".utf8).write(to: fixture.imported.directory.appendingPathComponent(CMOCProject.configurationFilename))
|
||||
var reviewed = false
|
||||
do {
|
||||
_ = try await fixture.model.openImportedProject(at: fixture.imported.directory, expectedProjectID: identity) {
|
||||
reviewed = true; return true
|
||||
}
|
||||
XCTFail("Malformed source project must fail")
|
||||
} catch { }
|
||||
XCTAssertFalse(reviewed)
|
||||
XCTAssertEqual(fixture.model.project?.directory, fixture.project.directory)
|
||||
XCTAssertEqual(fixture.model.source, dirty)
|
||||
XCTAssertEqual(fixture.model.assistantProjectID, identity)
|
||||
XCTAssertTrue(fixture.model.documents.contains(where: \.dirty))
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path))
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func testSuccessfulSourceOpenDoesNotExecuteMakeOrChangePausedGuest() async throws {
|
||||
try await withFixture { fixture in
|
||||
var rom = Data(repeating: 0x12, count: 32768)
|
||||
// LDA #$5A; STA $2000; BRA self. This is an authored CoCo ROM fixture.
|
||||
for (index, byte) in [UInt8(0x86), 0x5a, 0xb7, 0x20, 0, 0x20, 0xfe].enumerated() { rom[index] = byte }
|
||||
rom[32766] = 0x80; rom[32767] = 0
|
||||
let loaded = await step { fixture.controller.loadROM(rom, use6309: false, completion: $0) }
|
||||
XCTAssertNil(loaded.errorMessage)
|
||||
_ = await step(fixture.controller.resume)
|
||||
try await Task.sleep(nanoseconds: 40_000_000)
|
||||
_ = await step(fixture.controller.pause)
|
||||
let before = await snapshot(fixture.controller)
|
||||
XCTAssertEqual(before.machine.state, .paused)
|
||||
XCTAssertGreaterThan(before.machine.cycles, 0)
|
||||
XCTAssertEqual(before.logicalMemory[0x2000], 0x5a)
|
||||
let opened = try await fixture.model.openImportedProject(at: fixture.imported.directory,
|
||||
expectedProjectID: fixture.model.assistantProjectID, reviewUnsaved: { true })
|
||||
XCTAssertTrue(opened)
|
||||
XCTAssertEqual(fixture.model.project?.directory, fixture.imported.directory)
|
||||
XCTAssertTrue(fixture.model.source.contains("imported_value"))
|
||||
XCTAssertFalse(fixture.model.building); XCTAssertNil(fixture.model.artifact)
|
||||
XCTAssertNil(fixture.model.launchedBuildID)
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fixture.marker.path), "Opening must not even evaluate Make's shell expansion")
|
||||
let after = await snapshot(fixture.controller)
|
||||
XCTAssertEqual(after.machine.generation, before.machine.generation)
|
||||
XCTAssertEqual(after.machine.state, .paused)
|
||||
XCTAssertEqual(after.machine.cycles, before.machine.cycles)
|
||||
XCTAssertEqual(after.registers, before.registers)
|
||||
XCTAssertEqual(after.logicalMemory, before.logicalMemory)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user