diff --git a/Sources/DatadogSDKTesting/Coverage/DDCoverageHelper.swift b/Sources/DatadogSDKTesting/Coverage/DDCoverageHelper.swift index cffa575a..360eb769 100644 --- a/Sources/DatadogSDKTesting/Coverage/DDCoverageHelper.swift +++ b/Sources/DatadogSDKTesting/Coverage/DDCoverageHelper.swift @@ -35,8 +35,8 @@ class DDCoverageHelper { Log.debug("DDCoverageHelper location: \(path.url.path)") initialCoverageSaved = false coverageWorkQueue = OperationQueue() - coverageWorkQueue.qualityOfService = .background - coverageWorkQueue.maxConcurrentOperationCount = (ProcessInfo.processInfo.activeProcessorCount - 1) + coverageWorkQueue.qualityOfService = .utility + coverageWorkQueue.maxConcurrentOperationCount = max(ProcessInfo.processInfo.activeProcessorCount - 1, 1) } func clearCounters() { diff --git a/Sources/DatadogSDKTesting/Utils/GitInfo.swift b/Sources/DatadogSDKTesting/Utils/GitInfo.swift index 35a8736a..2ce06964 100644 --- a/Sources/DatadogSDKTesting/Utils/GitInfo.swift +++ b/Sources/DatadogSDKTesting/Utils/GitInfo.swift @@ -22,27 +22,21 @@ struct GitInfo { init(gitFolder: URL) throws { workspacePath = gitFolder.deletingLastPathComponent().path let headPath = gitFolder.appendingPathComponent("HEAD") - var mergePath: String? - let head = try String(contentsOf: headPath) + let head = try String(contentsOf: headPath).trimmingCharacters(in: .whitespacesAndNewlines) if head.hasPrefix("ref:") { - mergePath = head.trimmingCharacters(in: .whitespacesAndNewlines) - mergePath!.removeFirst(4) - mergePath = mergePath!.trimmingCharacters(in: .whitespacesAndNewlines) + var mergePath = head + mergePath.removeFirst(4) + mergePath = mergePath.trimmingCharacters(in: .whitespacesAndNewlines) self.branch = mergePath - let refData = try String(contentsOf: gitFolder.appendingPathComponent(mergePath!)) + let refData = try String(contentsOf: gitFolder.appendingPathComponent(mergePath)) commit = refData.trimmingCharacters(in: .whitespacesAndNewlines) } else { let fetchHeadPath = gitFolder.appendingPathComponent("FETCH_HEAD") - if let fetchHead = try? String(contentsOf: fetchHeadPath), - let first = fetchHead.firstIndex(of: "'"), - let last = fetchHead[fetchHead.index(after: first)...].firstIndex(of: "'") - { - let auxBranch = fetchHead[fetchHead.index(after: first) ... fetchHead.index(before: last)] - if !auxBranch.isEmpty { - self.branch = String(auxBranch) - } + if let records = FetchHeadRecord.from(file: fetchHeadPath)? + .filter({$0.commit == head && $0.type == "branch"}), records.count > 0 { + self.branch = records.first!.ref } - commit = head.trimmingCharacters(in: .whitespacesAndNewlines) + commit = head } let configPath = gitFolder.appendingPathComponent("config") @@ -413,3 +407,35 @@ struct CommitInfo { fullMessage = message } } + +extension GitInfo { + struct FetchHeadRecord { + let commit: String + let modifier: String? + let type: String? + let ref: String + let info: String + + init?(line: String) { + let parts = line.components(separatedBy: "\t") + guard parts.count == 3 else { return nil } + let refInfo = parts[2] + guard let refStart = refInfo.firstIndex(of: "'"), + let refEnd = refInfo[refInfo.index(after: refStart)...].firstIndex(of: "'") else { return nil } + commit = parts[0] + modifier = parts[1].count > 0 ? parts[1] : nil + type = refStart > refInfo.startIndex ? + refInfo[.. [Self]? { + (try? String(contentsOf: url)).map { Self.from(contents: $0) } + } + + static func from(contents string: String) -> [Self] { + string.components(separatedBy: .newlines).compactMap { .init(line: $0) } + } + } +} diff --git a/Tests/DatadogSDKTesting/Utils/GitInfoTests.swift b/Tests/DatadogSDKTesting/Utils/GitInfoTests.swift index 468c6c86..bf1c600b 100644 --- a/Tests/DatadogSDKTesting/Utils/GitInfoTests.swift +++ b/Tests/DatadogSDKTesting/Utils/GitInfoTests.swift @@ -88,4 +88,45 @@ class GitInfoTests: XCTestCase { XCTAssertEqual(gitInfo.committerEmail, "nacho.bonafontearruga@datadoghq.com") XCTAssertEqual(gitInfo.committerDate, "2021-05-06T08:03:32Z") } + + func testFetchHeadParsing() { + let git = fixturesURL.appendingPathComponent("git") + let records1 = GitInfo.FetchHeadRecord.from(file: git.appendingPathComponent("fetch_head_1")) + XCTAssertNotNil(records1) + XCTAssertEqual(records1?.count, 2) + + XCTAssertEqual(records1?[0].commit, "284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef") + XCTAssertNil(records1?[0].modifier) + XCTAssertNil(records1?[0].type) + XCTAssertEqual(records1?[0].ref, "refs/pipelines/42233408") + XCTAssertEqual(records1?[0].info, " of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios") + + XCTAssertEqual(records1?[1].commit, "284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef") + XCTAssertNil(records1?[1].modifier) + XCTAssertEqual(records1?[1].type, "branch") + XCTAssertEqual(records1?[1].ref, "ncreated/chore/ci-test-viz-debug-branch-name") + XCTAssertEqual(records1?[1].info, " of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios") + + let records2 = GitInfo.FetchHeadRecord.from(file: git.appendingPathComponent("fetch_head_2")) + XCTAssertNotNil(records2) + XCTAssertEqual(records2?.count, 3) + + XCTAssertEqual(records2?[0].commit, "2803d13cb33b06e2deb75428f19f59158468e370") + XCTAssertEqual(records2?[0].modifier, "not-for-merge") + XCTAssertEqual(records2?[0].type, "branch") + XCTAssertEqual(records2?[0].ref, "main") + XCTAssertEqual(records2?[0].info, " of github.com:DataDog/dd-sdk-swift-testing") + + XCTAssertEqual(records2?[1].commit, "836a91d263748a7ac857d2427f6f19fbc7cdb547") + XCTAssertEqual(records2?[1].modifier, "not-for-merge") + XCTAssertEqual(records2?[1].type, "branch") + XCTAssertEqual(records2?[1].ref, "update-binary") + XCTAssertEqual(records2?[1].info, " of github.com:DataDog/dd-sdk-swift-testing") + + XCTAssertEqual(records2?[2].commit, "5cba509698f41d8869253d5cc7966a5ef2d46f0a") + XCTAssertEqual(records2?[2].modifier, "not-for-merge") + XCTAssertEqual(records2?[2].type, "branch") + XCTAssertEqual(records2?[2].ref, "yehor-popovych/manual-api-refactoring") + XCTAssertEqual(records2?[2].info, " of github.com:DataDog/dd-sdk-swift-testing") + } } diff --git a/Tests/fixtures/git/fetch_head_1 b/Tests/fixtures/git/fetch_head_1 new file mode 100644 index 00000000..a021a1a7 --- /dev/null +++ b/Tests/fixtures/git/fetch_head_1 @@ -0,0 +1,2 @@ +284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef 'refs/pipelines/42233408' of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios +284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef branch 'ncreated/chore/ci-test-viz-debug-branch-name' of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios diff --git a/Tests/fixtures/git/fetch_head_2 b/Tests/fixtures/git/fetch_head_2 new file mode 100644 index 00000000..7eb650e3 --- /dev/null +++ b/Tests/fixtures/git/fetch_head_2 @@ -0,0 +1,3 @@ +2803d13cb33b06e2deb75428f19f59158468e370 not-for-merge branch 'main' of github.com:DataDog/dd-sdk-swift-testing +836a91d263748a7ac857d2427f6f19fbc7cdb547 not-for-merge branch 'update-binary' of github.com:DataDog/dd-sdk-swift-testing +5cba509698f41d8869253d5cc7966a5ef2d46f0a not-for-merge branch 'yehor-popovych/manual-api-refactoring' of github.com:DataDog/dd-sdk-swift-testing