-
Notifications
You must be signed in to change notification settings - Fork 220
feat: Dart coverage collection that survives BrowserStack Espresso runs #3066
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
Open
fylyppo
wants to merge
1
commit into
master
Choose a base branch
from
feat/bs-coverage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/patrol/android/src/main/kotlin/pl/leancode/patrol/BrowserStackCoverage.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package pl.leancode.patrol | ||
|
|
||
| import android.content.Context | ||
| import java.io.DataOutputStream | ||
| import java.io.File | ||
| import java.io.FileOutputStream | ||
|
|
||
| /** | ||
| * Bridges Dart coverage (gathered on-device by patrol's Dart side) into the | ||
| * JaCoCo `.ec` file that BrowserStack's coverage pipeline picks up. | ||
| * | ||
| * Strategy: patrol's Dart side writes one or more LCOV-formatted files into | ||
| * `<filesDir>/patrol_coverage/`. After the JaCoCo agent finishes its own dump, | ||
| * we read those files, base64-chunk the bytes to fit the 65535-byte cap on | ||
| * JaCoCo UTF strings, and append a series of `SessionInfo` blocks to the | ||
| * existing `coverage.ec`. The header is left untouched (JaCoCo wrote it). | ||
| * | ||
| * Each appended block uses the id format: | ||
| * `PATROL_DART_COV:<sequence>:<total>:<base64_chunk>` | ||
| * which `patrol bs pull-coverage` reassembles on the host side. | ||
| */ | ||
| internal object BrowserStackCoverage { | ||
| private const val TAG = "BrowserStackCoverage" | ||
|
|
||
| // JaCoCo binary format constants (see ExecutionDataWriter). | ||
| private const val BLOCK_SESSIONINFO: Byte = 0x10 | ||
| // DataOutputStream.writeUTF uses a 2-byte length prefix → max 65535 bytes | ||
| // for the modified-UTF-8 encoding. Base64 expands ~4/3, and we add a | ||
| // ~30-byte id prefix. Chunk raw payload to keep encoded length comfortably | ||
| // below the limit: 48_000 raw bytes → ~64_000 b64 chars + prefix. | ||
| private const val MAX_CHUNK_BYTES = 48_000 | ||
|
|
||
| const val ID_PREFIX = "PATROL_DART_COV:" | ||
|
|
||
| /** | ||
| * Reads any LCOV files in `<filesDir>/patrol_coverage/`, encodes them, and | ||
| * appends JaCoCo session blocks to [coverageFile]. Safe to call even when | ||
| * no Dart coverage was produced. | ||
| */ | ||
| fun appendDartCoverage(context: Context, coverageFile: File) { | ||
| val t0 = android.os.SystemClock.elapsedRealtime() | ||
| val sourceDir = File(context.filesDir, "patrol_coverage") | ||
| if (!sourceDir.exists() || !sourceDir.isDirectory) { | ||
| Logger.i("$TAG: no patrol_coverage dir at ${sourceDir.absolutePath}, skipping") | ||
| return | ||
| } | ||
|
|
||
| val lcovFiles = sourceDir.listFiles { f -> f.isFile && f.length() > 0 } | ||
| ?.sortedBy { it.name } | ||
| ?: emptyList() | ||
| if (lcovFiles.isEmpty()) { | ||
| Logger.i("$TAG: no Dart coverage files to merge, skipping") | ||
| return | ||
| } | ||
| Logger.i("$TAG: t+${android.os.SystemClock.elapsedRealtime() - t0}ms list ${lcovFiles.size} file(s)") | ||
|
|
||
| if (!coverageFile.exists() || coverageFile.length() == 0L) { | ||
| // Without a JaCoCo header the file is unreadable. Patrol can't write | ||
| // a header itself (would require duplicating JaCoCo's magic + version). | ||
| // In practice this only happens when testCoverageEnabled is off — log | ||
| // and bail loudly so the misconfig is obvious. | ||
| Logger.e( | ||
| "$TAG: ${coverageFile.absolutePath} is missing or empty. Is testCoverageEnabled=true on the app under test?", | ||
| null, | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| val merged = StringBuilder() | ||
| for (file in lcovFiles) { | ||
| merged.append(file.readText()) | ||
| if (!merged.endsWith("\n")) merged.append('\n') | ||
| } | ||
| Logger.i("$TAG: t+${android.os.SystemClock.elapsedRealtime() - t0}ms read ${merged.length} chars") | ||
|
|
||
| val payload = merged.toString().toByteArray(Charsets.UTF_8) | ||
| val chunks = chunkBytes(payload, MAX_CHUNK_BYTES) | ||
| Logger.i("$TAG: t+${android.os.SystemClock.elapsedRealtime() - t0}ms chunked ${chunks.size} (${payload.size} bytes)") | ||
|
|
||
| FileOutputStream(coverageFile, /* append = */ true).use { fos -> | ||
| DataOutputStream(fos).use { out -> | ||
| val now = System.currentTimeMillis() | ||
| chunks.forEachIndexed { index, chunk -> | ||
| val b64 = android.util.Base64.encodeToString( | ||
| chunk, | ||
| android.util.Base64.NO_WRAP, | ||
| ) | ||
| val id = "$ID_PREFIX${index + 1}:${chunks.size}:$b64" | ||
| out.writeByte(BLOCK_SESSIONINFO.toInt()) | ||
| out.writeUTF(id) | ||
| out.writeLong(now) | ||
| out.writeLong(now) | ||
| } | ||
| out.flush() | ||
| fos.fd.sync() | ||
| } | ||
| } | ||
| Logger.i("$TAG: t+${android.os.SystemClock.elapsedRealtime() - t0}ms append complete, file now ${coverageFile.length()} bytes") | ||
| } | ||
|
|
||
| private fun chunkBytes(bytes: ByteArray, chunkSize: Int): List<ByteArray> { | ||
| if (bytes.isEmpty()) return emptyList() | ||
| val out = ArrayList<ByteArray>((bytes.size + chunkSize - 1) / chunkSize) | ||
| var offset = 0 | ||
| while (offset < bytes.size) { | ||
| val end = minOf(offset + chunkSize, bytes.size) | ||
| out += bytes.copyOfRange(offset, end) | ||
| offset = end | ||
| } | ||
| return out | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getDataDir()method was introduced in API level 24. If this runner is executed on older devices (e.g., API 21, which is common for Flutter apps), this call will result in aNoSuchMethodError. Consider adding a version check and providing a fallback for older API levels.