Skip to content

[Parseable output] Implement SignalledMessage #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions Sources/SwiftDriver/Driver/ToolExecutionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
//===----------------------------------------------------------------------===//
import TSCBasic

#if canImport(Darwin)
import Darwin.C
#elseif os(Windows)
import MSVCRT
import WinSDK
#elseif canImport(Glibc)
import Glibc
#else
#error("Missing libc or equivalent")
#endif

/// Delegate for printing execution information on the command-line.
public struct ToolExecutionDelegate: JobExecutorDelegate {
public enum Mode {
Expand Down Expand Up @@ -58,18 +69,20 @@ public struct ToolExecutionDelegate: JobExecutorDelegate {
}

case .parsableOutput:
let output = (try? result.utf8Output() + result.utf8stderrOutput()).flatMap { $0.isEmpty ? nil : $0 }
let message: ParsableMessage

switch result.exitStatus {
case .terminated(let code):
let output = (try? result.utf8Output() + result.utf8stderrOutput()) ?? ""
let finishedMessage = FinishedMessage(exitStatus: Int(code), pid: pid, output: output.isEmpty ? nil : output)
let message = ParsableMessage.finishedMessage(name: job.kind.rawValue, msg: finishedMessage)
emit(message)
let finishedMessage = FinishedMessage(exitStatus: Int(code), pid: pid, output: output)
message = ParsableMessage.finishedMessage(name: job.kind.rawValue, msg: finishedMessage)

case .signalled:
// FIXME: Implement this.
break
case .signalled(let signal):
let errorMessage = strsignal(signal).map { String(cString: $0) } ?? ""
let signalledMessage = SignalledMessage(pid: pid, output: output, errorMessage: errorMessage, signal: Int(signal))
message = ParsableMessage.signalledMessage(name: job.kind.rawValue, msg: signalledMessage)
}

emit(message)
}
}

Expand Down
35 changes: 32 additions & 3 deletions Sources/SwiftDriver/Execution/ParsableOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct ParsableMessage {
public enum Kind {
case began(BeganMessage)
case finished(FinishedMessage)
case signalled
case signalled(SignalledMessage)
case skipped
}

Expand All @@ -38,6 +38,13 @@ public struct ParsableMessage {
return ParsableMessage(name: name, kind: .finished(msg))
}

public static func signalledMessage(
name: String,
msg: SignalledMessage
) -> ParsableMessage {
return ParsableMessage(name: name, kind: .signalled(msg))
}

public func toJSON() throws -> Data {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
Expand Down Expand Up @@ -109,6 +116,27 @@ public struct FinishedMessage: Encodable {
}
}

public struct SignalledMessage: Encodable {
let pid: Int
let output: String?
let errorMessage: String
let signal: Int

public init(pid: Int, output: String?, errorMessage: String, signal: Int) {
self.pid = pid
self.output = output
self.errorMessage = errorMessage
self.signal = signal
}

private enum CodingKeys: String, CodingKey {
case pid
case output
case errorMessage = "error-message"
case signal
}
}

extension ParsableMessage: Encodable {
enum CodingKeys: CodingKey {
case name
Expand All @@ -126,8 +154,9 @@ extension ParsableMessage: Encodable {
case .finished(let msg):
try container.encode("finished", forKey: .kind)
try msg.encode(to: encoder)
case .signalled:
break
case .signalled(let msg):
try container.encode("signalled", forKey: .kind)
try msg.encode(to: encoder)
case .skipped:
break
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/SwiftDriverTests/ParsableMessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,22 @@ final class ParsableMessageTests: XCTestCase {
}
""")
}

func testSignalledMessage() throws {
let msg = SignalledMessage(pid: 2, output: "sig", errorMessage: "err", signal: 3)
let signalledMessage = ParsableMessage.signalledMessage(name: "compile", msg: msg)
let encoded = try signalledMessage.toJSON()
let string = String(data: encoded, encoding: .utf8)!

XCTAssertEqual(string, """
{
"error-message" : "err",
"kind" : "signalled",
"name" : "compile",
"output" : "sig",
"pid" : 2,
"signal" : 3
}
""")
}
}