Skip to content

Commit

Permalink
Custom Allure directory (#61)
Browse files Browse the repository at this point in the history
* Customise allure results directory

* 2.3.4
  • Loading branch information
alex-tiurin authored Mar 13, 2024
1 parent 44f5788 commit d37e965
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kotlin.code.style=official
android.useAndroidX=true
GROUP=com.atiurin
POM_ARTIFACT_ID=ultron
VERSION_NAME=2.3.3
VERSION_NAME=2.3.4

POM_NAME=ultron

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.atiurin.sampleapp.tests

import android.os.Environment
import androidx.test.platform.app.InstrumentationRegistry
import com.atiurin.ultron.testlifecycle.rulesequence.RuleSequence
import com.atiurin.sampleapp.data.repositories.CURRENT_USER
Expand Down Expand Up @@ -37,6 +38,7 @@ abstract class BaseTest {
UltronConfig.applyRecommended()
UltronAllureConfig.applyRecommended()
UltronComposeConfig.applyRecommended()
UltronAllureConfig.setAllureResultsDirectory()
UltronComposeConfig.addListener(ScreenshotAttachListener())
UltronComposeConfig.addListener(WindowHierarchyAttachListener())
UltronComposeConfig.addListener(DetailedOperationAllureListener())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.atiurin.ultron.allure.attachment

import androidx.test.platform.app.InstrumentationRegistry
import io.qameta.allure.kotlin.util.PropertiesUtils
import java.io.File

object AllureDirectoryUtil {

fun getResultsDirectoryName(): String = PropertiesUtils.resultsDirectoryPath

/**
* From Allure source code
* see [https://github.com/allure-framework/allure-kotlin/blob/master/allure-kotlin-android/src/main/kotlin/io/qameta/allure/android/AllureAndroidLifecycle.kt]
*/
fun getOriginalResultsDirectory(): File {
return File(InstrumentationRegistry.getInstrumentation().targetContext.filesDir, getResultsDirectoryName())
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.atiurin.ultron.allure.config

import androidx.test.platform.app.InstrumentationRegistry
import com.atiurin.ultron.allure.attachment.AllureDirectoryUtil
import com.atiurin.ultron.allure.attachment.AttachUtil
import java.io.File

data class AllureConfigParams(
var addScreenshotPolicy: MutableSet<AllureAttachStrategy> = mutableSetOf(
AllureAttachStrategy.TEST_FAILURE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.atiurin.ultron.allure.config

import android.os.Environment
import androidx.test.platform.app.InstrumentationRegistry
import com.atiurin.ultron.allure.attachment.AllureDirectoryUtil
import com.atiurin.ultron.allure.attachment.AttachUtil
import com.atiurin.ultron.allure.condition.AllureConditionExecutorWrapper
import com.atiurin.ultron.allure.condition.AllureConditionsExecutor
import com.atiurin.ultron.allure.getRunInformer
Expand All @@ -9,12 +12,16 @@ import com.atiurin.ultron.allure.listeners.ScreenshotAttachListener
import com.atiurin.ultron.allure.listeners.WindowHierarchyAttachListener
import com.atiurin.ultron.allure.runner.LogcatAttachRunListener
import com.atiurin.ultron.allure.runner.ScreenshotAttachRunListener
import com.atiurin.ultron.allure.runner.UltronAllureResultsTransferListener
import com.atiurin.ultron.allure.runner.UltronLogAttachRunListener
import com.atiurin.ultron.allure.runner.UltronLogCleanerRunListener
import com.atiurin.ultron.allure.runner.WindowHierarchyAttachRunListener
import com.atiurin.ultron.core.config.UltronConfig
import com.atiurin.ultron.extensions.simpleClassName
import com.atiurin.ultron.listeners.AbstractListener
import com.atiurin.ultron.log.UltronLog
import com.atiurin.ultron.runner.UltronRunListener
import java.io.File

object UltronAllureConfig {
var params: AllureConfigParams = AllureConfigParams()
Expand All @@ -27,6 +34,41 @@ object UltronAllureConfig {
UltronConfig.Conditions.conditionExecutorWrapper = AllureConditionExecutorWrapper()
}

/**
* Sets the directory where Allure results will be stored.
* Original results artifacts are saved in [AllureDirectoryUtil.getOriginalResultsDirectory]
*
* The results will be copied from [AllureDirectoryUtil.getOriginalResultsDirectory]
* to [targetDirectory] once test run is finished.
*
* @param targetDirectory The directory where the Allure results will be stored.
*
* @see AllureDirectoryUtil.getOriginalResultsDirectory
*/
fun setAllureResultsDirectory(targetDirectory: File) {
removeRunListener(UltronLogCleanerRunListener::class.java)
addRunListener(
UltronAllureResultsTransferListener(
sourceDir = AllureDirectoryUtil.getOriginalResultsDirectory(),
targetDir = targetDirectory
)
)
addRunListener(UltronLogCleanerRunListener())
}

/**
* Sets the directory where Allure results will be stored with default public directory.
*
* @param publicDirectory The public directory where the Allure results will be stored.
* Default is [Environment.DIRECTORY_DOWNLOADS].
*
* So, the final path by default will be like '/sdcard/Download/allure-result'
*/
fun setAllureResultsDirectory(publicDirectory: String = Environment.DIRECTORY_DOWNLOADS) {
val targetDir = Environment.getExternalStoragePublicDirectory(publicDirectory).resolve(AllureDirectoryUtil.getResultsDirectoryName())
setAllureResultsDirectory(targetDir)
}

private fun modify() {
if (params.detailedAllureReport) {
UltronConfig.addGlobalListener(DetailedOperationAllureListener())
Expand All @@ -43,10 +85,10 @@ object UltronAllureConfig {
setAllureConditionsExecutorWrapper()
setAllureConditionExecutor()
}
if (!params.attachUltronLog){
if (!params.attachUltronLog) {
removeRunListener(UltronLogAttachRunListener::class.java)
}
if (!params.attachLogcat){
if (!params.attachLogcat) {
removeRunListener(LogcatAttachRunListener::class.java)
}
UltronLog.info("UltronAllureConfig applied with params $params")
Expand All @@ -63,10 +105,12 @@ object UltronAllureConfig {
}

fun addRunListener(listener: UltronRunListener) {
UltronLog.info("Add ${listener::class.simpleName} run listener")
InstrumentationRegistry.getInstrumentation().getRunInformer().addListener(listener)
}

fun <T: AbstractListener> removeRunListener(listenerClass: Class<T>) {
fun <T : AbstractListener> removeRunListener(listenerClass: Class<T>) {
UltronLog.info("Remove ${listenerClass.simpleClassName()} run listener")
InstrumentationRegistry.getInstrumentation().getRunInformer().removeListener(listenerClass)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.atiurin.ultron.allure.runner

import com.atiurin.ultron.extensions.createDirectoryIfNotExists
import com.atiurin.ultron.log.UltronLog
import com.atiurin.ultron.runner.UltronRunListener
import org.junit.runner.Result
import java.io.File
import kotlin.system.measureTimeMillis

/**
* Transfer files from original Allure results directory [sourceDir]
* to custom directory [targetDir] provided by user [UltronAllureConfig.setAllureResultsDirectory]
*/
class UltronAllureResultsTransferListener(private val sourceDir: File, private val targetDir: File) : UltronRunListener() {
override fun testRunFinished(result: Result) {
UltronLog.info("Copy Allure results from '${sourceDir.absolutePath}' to '${targetDir.absolutePath}'")
targetDir.createDirectoryIfNotExists()
var isSuccessfullyCopied = true
val time = measureTimeMillis {
sourceDir.copyRecursively(targetDir, true, onError = { file, ioException ->
UltronLog.error("""
|Unable to copy Allure results file '${file.absolutePath}' to '${targetDir.absolutePath}'.
|Got exception : '${ioException.message}'.
|Source directory '${sourceDir.absolutePath}' won't be deleted.
""".trimMargin()
)
isSuccessfullyCopied = false
OnErrorAction.SKIP
})
if (isSuccessfullyCopied) sourceDir.deleteRecursively()
}
UltronLog.info("Allure results files transfer time = $time ms")
}
}

8 changes: 8 additions & 0 deletions ultron/src/main/java/com/atiurin/ultron/extensions/FileExt.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.atiurin.ultron.extensions

import com.atiurin.ultron.log.UltronLog
import java.io.File
import java.io.PrintWriter

Expand All @@ -8,4 +9,11 @@ fun File.clearContent() {
print("")
close()
}
}

fun File.createDirectoryIfNotExists() {
if (!exists()) {
val result = mkdirs()
if (!result) UltronLog.error("Unable to create directory '${this.absolutePath}'")
}
}

0 comments on commit d37e965

Please sign in to comment.