Skip to content

Commit

Permalink
Merge pull request #213 from HeEAaD/skip_ActivityLogSectionAttachment…
Browse files Browse the repository at this point in the history
…_for_version_10

Skip parsing of section attachments for logs with version less than 11
  • Loading branch information
aleksandergrzyb authored Dec 11, 2024
2 parents 97e1375 + ba46699 commit 251e44b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Sources/XCLogParser/activityparser/ActivityParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class ActivityParser {
/// that into account
var isCommandLineLog = false

/// The version of the parsed `IDEActivityLog`.
/// Used to skip parsing of the `IDEActivityLogSectionAttachment` list on version less than 11.
var logVersion: Int8?

public init() {}

/// Parses the xcacticitylog argument into a `IDEActivityLog`
Expand All @@ -53,7 +57,9 @@ public class ActivityParser {

public func parseIDEActiviyLogFromTokens(_ tokens: [Token]) throws -> IDEActivityLog {
var iterator = tokens.makeIterator()
return IDEActivityLog(version: Int8(try parseAsInt(token: iterator.next())),
let logVersion = Int8(try parseAsInt(token: iterator.next()))
self.logVersion = logVersion
return IDEActivityLog(version: logVersion,
mainSection: try parseLogSection(iterator: &iterator))
}

Expand Down Expand Up @@ -458,6 +464,14 @@ public class ActivityParser {

private func parseIDEActivityLogSectionAttachments(iterator: inout IndexingIterator<[Token]>)
throws -> [IDEActivityLogSectionAttachment] {
guard let logVersion else {
throw XCLogParserError.parseError("Log version not parsed before parsing " +
"array of IDEActivityLogSectionAttachment")
}
/// The list of IDEActivityLogSectionAttachment was introduced with version 11
guard logVersion >= 11 else {
return []
}
guard let listToken = iterator.next() else {
throw XCLogParserError.parseError("Unexpected EOF parsing array of IDEActivityLogSectionAttachment")
}
Expand Down
60 changes: 60 additions & 0 deletions Tests/XCLogParserTests/ActivityParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ class ActivityParserTests: XCTestCase {
return startTokens + logMessageTokens + endTokens
}()

lazy var IDEActivityLogSectionTokensWithoutAttachments: [Token] = {
let startTokens = [Token.int(2),
Token.string("com.apple.dt.IDE.BuildLogSection"),
Token.string("Prepare build"),
Token.string("Prepare build"),
Token.double(575479851.278759),
Token.double(575479851.778325),
Token.null,
Token.string("note: Using legacy build system"),
Token.list(1),
Token.className("IDEActivityLogMessage"),
Token.classNameRef("IDEActivityLogMessage"),
]
let logMessageTokens = IDEActivityLogMessageTokens
let endTokens = [Token.int(1),
Token.int(0),
Token.int(1),
Token.string("subtitle"),
Token.null,
Token.string("commandDetailDesc"),
Token.string("501796C4-6BE4-4F80-9F9D-3269617ECC17"),
Token.string("localizedResultString"),
Token.string("xcbuildSignature"),
Token.int(0)
]
return startTokens + logMessageTokens + endTokens
}()

let IDEConsoleItemTokens: [Token] = [
Token.className("IDEConsoleItem"),
Token.classNameRef("IDEConsoleItem"),
Expand Down Expand Up @@ -288,6 +316,7 @@ class ActivityParserTests: XCTestCase {
}

func testParseIDEActivityLogSection() throws {
parser.logVersion = 11
let tokens = IDEActivityLogSectionTokens
var iterator = tokens.makeIterator()
let logSection = try parser.parseIDEActivityLogSection(iterator: &iterator)
Expand All @@ -310,15 +339,46 @@ class ActivityParserTests: XCTestCase {
XCTAssertEqual("501796C4-6BE4-4F80-9F9D-3269617ECC17", logSection.uniqueIdentifier)
XCTAssertEqual("localizedResultString", logSection.localizedResultString)
XCTAssertEqual("xcbuildSignature", logSection.xcbuildSignature)
XCTAssertEqual(1, logSection.attachments.count)
XCTAssertEqual(0, logSection.unknown)
}

func testParseIDEActivityLogSection_version10() throws {
parser.logVersion = 10
let tokens = IDEActivityLogSectionTokensWithoutAttachments
var iterator = tokens.makeIterator()
let logSection = try parser.parseIDEActivityLogSection(iterator: &iterator)
XCTAssertEqual(2, logSection.sectionType)
XCTAssertEqual("com.apple.dt.IDE.BuildLogSection", logSection.domainType)
XCTAssertEqual("Prepare build", logSection.title)
XCTAssertEqual("Prepare build", logSection.signature)
XCTAssertEqual(575479851.278759, logSection.timeStartedRecording)
XCTAssertEqual(575479851.778325, logSection.timeStoppedRecording)
XCTAssertEqual(0, logSection.subSections.count)
XCTAssertEqual("note: Using legacy build system", logSection.text)
XCTAssertEqual(1, logSection.messages.count)
XCTAssertTrue(logSection.wasCancelled)
XCTAssertFalse(logSection.isQuiet)
XCTAssertTrue(logSection.wasFetchedFromCache)
XCTAssertEqual("subtitle", logSection.subtitle)
XCTAssertEqual("", logSection.location.documentURLString)
XCTAssertEqual(0, logSection.location.timestamp)
XCTAssertEqual("commandDetailDesc", logSection.commandDetailDesc)
XCTAssertEqual("501796C4-6BE4-4F80-9F9D-3269617ECC17", logSection.uniqueIdentifier)
XCTAssertEqual("localizedResultString", logSection.localizedResultString)
XCTAssertEqual("xcbuildSignature", logSection.xcbuildSignature)
XCTAssertEqual(0, logSection.attachments.count)
XCTAssertEqual(0, logSection.unknown)
}

func testParseActivityLog() throws {
let activityLog = try parser.parseIDEActiviyLogFromTokens(IDEActivityLogTokens)
XCTAssertEqual(10, activityLog.version)
XCTAssertEqual(10, parser.logVersion)
}

func testParseDBGConsoleLog() throws {
parser.logVersion = 11
let tokens = DBGConsoleLogTokens
var iterator = tokens.makeIterator()
let DBGConsoleLog = try parser.parseDBGConsoleLog(iterator: &iterator)
Expand Down

0 comments on commit 251e44b

Please sign in to comment.