Skip to content

Commit

Permalink
Close streams properly after use
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chelombitko committed Aug 3, 2024
1 parent 1c5b089 commit 09fdc2e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class HtmlSummaryReporter(
* - suites/suiteId.json
* - suites/deviceId/testId.json
*/
@Suppress("LongMethod")
@Suppress("CyclomaticComplexMethod", "LongMethod")
override fun generate(executionReport: ExecutionReport) {
val summary = executionReport.summary
if (summary.pools.isEmpty()) return
Expand All @@ -57,13 +57,17 @@ class HtmlSummaryReporter(
val formattedDate = SimpleDateFormat("HH:mm:ss z, MMM d yyyy").apply { timeZone = TimeZone.getTimeZone("UTC") }.format(Date())

val appJs = File(outputDir, "app.min.js")
inputStreamFromResources("html-report/app.min.js").copyTo(appJs.outputStream())
inputStreamFromResources("html-report/app.min.js").use { input ->
appJs.outputStream().use { input.copyTo(it) }
}

val appCss = File(outputDir, "app.min.css")
inputStreamFromResources("html-report/app.min.css").copyTo(appCss.outputStream())
inputStreamFromResources("html-report/app.min.css").use { input ->
appCss.outputStream().use { input.copyTo(it) }
}

// index.html is a page that can render all kinds of inner pages: Index, Suite, Test.
val indexHtml = inputStreamFromResources("html-report/index.html").reader().readText()
val indexHtml = inputStreamFromResources("html-report/index.html").reader().use { it.readText() }

val indexHtmlFile = File(outputDir, "index.html")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ internal class TimelineReporter(
val indexHtmlFile = File(timelineDir, "index.html")

val chartCss = File(timelineDir, "chart.css")
inputStreamFromResources("timeline/chart.css").copyTo(chartCss.outputStream())
inputStreamFromResources("timeline/chart.css").use { input ->
chartCss.outputStream().use { input.copyTo(it) }
}

val chartJs = File(timelineDir, "chart.js")
inputStreamFromResources("timeline/chart.js").copyTo(chartJs.outputStream())
inputStreamFromResources("timeline/chart.js").use { input ->
chartJs.outputStream().use { input.copyTo(it) }
}

val json = gson.toJson(provider.generate(executionReport))
val index = inputStreamFromResources("timeline/index.html")
val indexText = index.reader().readText()
indexHtmlFile.writeText(indexText.replace("\${dataset}", json))
inputStreamFromResources("timeline/index.html").use { input ->
val indexText = input.reader().readText()
indexHtmlFile.writeText(indexText.replace("\${dataset}", json))
}
}

private fun inputStreamFromResources(path: String): InputStream =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package com.malinskiy.marathon.android

import com.android.SdkConstants
import com.shazam.axmlparser.AXMLParser
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.zip.ZipFile

class ApkParser {
@Suppress(
"ComplexMethod",
"ThrowsCount",
"TooGenericExceptionThrown",
"NestedBlockDepth"
)
fun parseInstrumentationInfo(apk: File): InstrumentationInfo {
var apkInputStream: InputStream? = null
try {
val zip = ZipFile(apk)
val entry = zip.getEntry("AndroidManifest.xml")
apkInputStream = zip.getInputStream(entry)
return ZipFile(apk).use { zip ->
val androidManifest = zip.getEntry(SdkConstants.ANDROID_MANIFEST_XML)
zip.getInputStream(androidManifest).use { parseAndroidManifest(it) }
}
}

val parser = AXMLParser(apkInputStream)
@Suppress("CyclomaticComplexMethod", "NestedBlockDepth")
private fun parseAndroidManifest(inputStream: InputStream): InstrumentationInfo {
try {
val parser = AXMLParser(inputStream)
var eventType = parser.type

var appPackage: String? = null
Expand Down Expand Up @@ -60,9 +58,7 @@ class ApkParser {

return InstrumentationInfo(appPackage, testPackage, testRunnerClass)
} catch (e: IOException) {
throw RuntimeException("Unable to parse test app AndroidManifest.xml.", e)
} finally {
apkInputStream?.close()
throw IOException("Unable to parse test app AndroidManifest.xml.", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.malinskiy.marathon.android.executor.listeners.screenshot
// Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

import java.awt.image.RenderedImage
import java.io.Closeable
import java.io.File
import java.io.IOException
import javax.imageio.IIOException
Expand Down Expand Up @@ -42,7 +43,7 @@ constructor(
imageType: Int,
timeBetweenFramesMS: Int,
loopContinuously: Boolean
) {
) : Closeable {
private var gifWriter: ImageWriter
private var imageWriteParam: ImageWriteParam
private var imageMetaData: IIOMetadata
Expand Down Expand Up @@ -125,7 +126,7 @@ constructor(
* stream, just finishes off the GIF.
*/
@Throws(IOException::class)
fun close() {
override fun close() {
gifWriter.endWriteSequence()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ class ScreenCapturer(
FileType.SCREENSHOT,
AttachmentType.SCREENSHOT
)
val outputStream = FileImageOutputStream(attachment.file)
val writer = GifSequenceWriter(outputStream, TYPE_INT_ARGB, DELAY, true)
var targetOrientation = UNDEFINED
while (isActive) {
val capturingTimeMillis = measureTimeMillis {
getScreenshot(targetOrientation)?.let {
if (targetOrientation == UNDEFINED) {
// remember the target orientation
targetOrientation = it.getOrientation()
FileImageOutputStream(attachment.file).use { outputStream ->
GifSequenceWriter(outputStream, TYPE_INT_ARGB, DELAY, true).use { writer ->
var targetOrientation = UNDEFINED
while (isActive) {
val capturingTimeMillis = measureTimeMillis {
getScreenshot(targetOrientation)?.let {
if (targetOrientation == UNDEFINED) {
// remember the target orientation
targetOrientation = it.getOrientation()
}
writer.writeToSequence(it)
}
}
writer.writeToSequence(it)
val sleepTimeMillis = when {
(DELAY - capturingTimeMillis) < 0 -> 0
else -> DELAY - capturingTimeMillis
}
delay(sleepTimeMillis)
}
}
val sleepTimeMillis = when {
(DELAY - capturingTimeMillis) < 0 -> 0
else -> DELAY - capturingTimeMillis
}
delay(sleepTimeMillis)
}
writer.close()
outputStream.close()
}

private fun getScreenshot(targetOrientation: Int): RenderedImage? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.malinskiy.marathon.report.logs.Log
import com.malinskiy.marathon.report.logs.LogEvent
import com.malinskiy.marathon.report.logs.LogTest
import kotlinx.coroutines.CompletableDeferred
import java.io.Closeable
import java.io.File
import java.io.Writer
import java.time.ZoneId
Expand Down Expand Up @@ -50,7 +51,7 @@ class BatchLogSaver {
class Event(val event: LogEvent) : SaveEntry()
}

private class LogSaver {
private class LogSaver : Closeable {

private val logFile: File = createTempFile()
.also {
Expand All @@ -72,7 +73,7 @@ class BatchLogSaver {
}
}

fun close() {
override fun close() {
try {
fileWriter.close()
} finally {
Expand Down

0 comments on commit 09fdc2e

Please sign in to comment.