summaryrefslogtreecommitdiff
path: root/Tests/BubombCoreTests/SequenceExecutorTests.swift
blob: 63af9399144038a3d457736bbfcb59fcf5800b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#if canImport(XCTest)
import XCTest
#endif
import Foundation
import BubombCore

final class SequenceExecutorTests: XCTestCase {
    func testSuccessfulSequentialExecution() async throws {
        let step1 = SequenceStep(
            name: "Step 1",
            action: .runCommand(command: "echo 'first'", workingDirectory: nil, timeoutSeconds: 5)
        )
        let step2 = SequenceStep(
            name: "Step 2",
            action: .runCommand(command: "echo 'second'", workingDirectory: nil, timeoutSeconds: 5)
        )

        let config = SequenceConfig(steps: [step1, step2], stopOnError: true)
        let executor = SequenceExecutor()

        let tracker = StepTracker()
        let result = try await executor.execute(config: config) { step, total, name in
            tracker.record(step: step, total: total, name: name)
        }

        XCTAssertEqual(result.exitCode, 0)
        XCTAssertTrue(result.stdout.contains("first"))
        XCTAssertTrue(result.stdout.contains("second"))
        let calls = tracker.getCalls()
        XCTAssertEqual(calls.count, 2)
        XCTAssertEqual(calls[0].0, 1)
        XCTAssertEqual(calls[1].0, 2)
    }

    func testSequenceStopsOnErrorWhenConfigured() async {
        let step1 = SequenceStep(
            name: "Failing Step",
            action: .runCommand(command: "exit 12", workingDirectory: nil, timeoutSeconds: 5)
        )
        let step2 = SequenceStep(
            name: "Should Not Run",
            action: .runCommand(command: "echo 'unreachable'", workingDirectory: nil, timeoutSeconds: 5)
        )

        let config = SequenceConfig(steps: [step1, step2], stopOnError: true)
        let executor = SequenceExecutor()

        do {
            _ = try await executor.execute(config: config)
            XCTFail("Should have thrown stepFailed error")
        } catch let error as ExecutionError {
            if case .stepFailed(let index, let name, _) = error {
                XCTAssertEqual(index, 0)
                XCTAssertEqual(name, "Failing Step")
            } else {
                XCTFail("Unexpected error: \(error)")
            }
        } catch {
            XCTFail("Unexpected non-ExecutionError: \(error)")
        }
    }

    func testSequenceContinuesOnErrorWhenConfigured() async throws {
        let step1 = SequenceStep(
            name: "Ignored Failure",
            action: .runCommand(command: "exit 5", workingDirectory: nil, timeoutSeconds: 5)
        )
        let step2 = SequenceStep(
            name: "Subsequent Step",
            action: .runCommand(command: "echo 'recovered'", workingDirectory: nil, timeoutSeconds: 5)
        )

        let config = SequenceConfig(steps: [step1, step2], stopOnError: false)
        let executor = SequenceExecutor()

        let result = try await executor.execute(config: config)
        XCTAssertEqual(result.exitCode, 0)
        XCTAssertTrue(result.stdout.contains("recovered"))
        XCTAssertTrue(result.stderr.contains("Step 1 failed"))
    }
}

private final class StepTracker: @unchecked Sendable {
    private var calls: [(Int, Int, String)] = []
    private let lock = NSLock()

    func record(step: Int, total: Int, name: String) {
        lock.lock()
        calls.append((step, total, name))
        lock.unlock()
    }

    func getCalls() -> [(Int, Int, String)] {
        lock.lock()
        defer { lock.unlock() }
        return calls
    }
}