Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Karumi/Shot
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrovgs committed May 15, 2022
2 parents e8a8f89 + 957c19e commit 4f609da
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 59 deletions.
22 changes: 14 additions & 8 deletions core/src/main/scala/com/karumi/shot/Shot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class Shot(
consoleReporter: ConsoleReporter,
envVars: EnvVars
) {
def configureAdbPath(adbPath: Folder): Unit = {
Adb.adbBinaryPath = adbPath
}

def downloadScreenshots(appId: AppId, shotFolder: ShotFolder, orchestrated: Boolean): Unit = {
console.show("⬇️ Pulling screenshots from your connected devices!")
pullScreenshots(appId, shotFolder, orchestrated)
Expand Down Expand Up @@ -123,7 +119,6 @@ class Shot(
} else {
console.showSuccess("✅ Yeah!!! Your tests are passing.")
}
removeProjectTemporalScreenshotsFolder(shotFolder)
reporter.generateVerificationReport(
appId,
comparison,
Expand All @@ -134,6 +129,7 @@ class Shot(
"🤓 You can review the execution report here: " + shotFolder
.verificationReportFolder() + "index.html"
)
removeProjectTemporalScreenshotsFolder(shotFolder)
comparison
}
}
Expand Down Expand Up @@ -297,9 +293,11 @@ class Shot(
}

private def removeProjectTemporalScreenshotsFolder(shotFolder: ShotFolder): Unit = {
FileUtils.deleteDirectory(new File(shotFolder.pulledScreenshotsFolder()))
FileUtils.deleteDirectory(new File(shotFolder.pulledComposeScreenshotsFolder()))
FileUtils.deleteDirectory(new File(shotFolder.pulledComposeOrchestratedScreenshotsFolder()))
// Fix for https://github.com/pedrovgs/Shot/issues/53
// Avoid crash when directory can't be deleted
safeDeleteDirectory(new File(shotFolder.pulledScreenshotsFolder()))
safeDeleteDirectory(new File(shotFolder.pulledComposeScreenshotsFolder()))
safeDeleteDirectory(new File(shotFolder.pulledComposeOrchestratedScreenshotsFolder()))
}

private def extractPicturesFromBundle(screenshotsFolder: String): Unit = {
Expand All @@ -308,4 +306,12 @@ class Shot(
TinyZip.unzip(bundleFile, screenshotsFolder)
}
}

private def safeDeleteDirectory(file: File): Unit = {
try {
FileUtils.deleteDirectory(file)
} catch {
case e: Throwable => println(Console.YELLOW + s"Failed to delete directory: $e")
}
}
}
9 changes: 5 additions & 4 deletions core/src/main/scala/com/karumi/shot/android/Adb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import com.karumi.shot.domain.model.{AppId, Folder}
import scala.sys.process._

object Adb {
var adbBinaryPath: String = ""
// To be able to support API 29+ with scoped storage we need to change
// the base url where the app saves our screenshots inside the device.
// This value is computed in runtime in shot-android AndroidStorageInfo.
private val baseStoragePath = "/storage/emulated/0/Download"
}

class Adb {
class Adb(
adbPath: String
) {

private final val CR_ASCII_DECIMAL = 13
private val logger = ProcessLogger(
Expand Down Expand Up @@ -94,10 +95,10 @@ class Adb {
}

private def executeAdbCommand(command: String): Int =
s"${Adb.adbBinaryPath} $command" ! logger
s"${adbPath} $command" ! logger

private def executeAdbCommandWithResult(command: String): String =
s"${Adb.adbBinaryPath} $command" !! logger
s"${adbPath} $command" !! logger

private def isCarriageReturnASCII(device: String): Boolean =
device.charAt(0) == CR_ASCII_DECIMAL
Expand Down
8 changes: 0 additions & 8 deletions core/src/test/scala/com/karumi/shot/ShotSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ class ShotSpec
)
}

it should "configure adb path" in {
val anyAdbPath = "/Library/androidsdk/bin/adb"

shot.configureAdbPath(anyAdbPath)

Adb.adbBinaryPath shouldBe anyAdbPath
}

it should "should delegate screenshots cleaning to Adb using the specified ANDROID_SERIAL env var" in {
val appId: AppId = AppIdMother.anyAppId
val device1: String = "emulator-5554"
Expand Down
33 changes: 9 additions & 24 deletions shot/src/main/scala/com/karumi/shot/ShotPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@ package com.karumi.shot
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.{AppExtension, LibraryExtension}
import com.android.builder.model.{BuildType, ProductFlavor}
import com.karumi.shot.android.Adb
import com.karumi.shot.base64.Base64Encoder
import com.karumi.shot.domain.Config
import com.karumi.shot.exceptions.ShotException
import com.karumi.shot.reports.{ConsoleReporter, ExecutionReporter}
import com.karumi.shot.screenshots.{
ScreenshotsComparator,
ScreenshotsDiffGenerator,
ScreenshotsSaver
}
import com.karumi.shot.system.EnvVars
import com.karumi.shot.tasks.{
DownloadScreenshotsTask,
ExecuteScreenshotTests,
Expand All @@ -29,33 +20,23 @@ import scala.util.Try
class ShotPlugin extends Plugin[Project] {

private val console = new Console
private lazy val shot: Shot =
new Shot(
new Adb,
new Files,
new ScreenshotsComparator,
new ScreenshotsDiffGenerator(new Base64Encoder),
new ScreenshotsSaver,
console,
new ExecutionReporter,
new ConsoleReporter(console),
new EnvVars()
)

override def apply(project: Project): Unit = {
addExtensions(project)
addAndroidTestDependency(project)
project.afterEvaluate { project =>
{
configureAdb(project)
addTasks(project)
}
}
}

private def configureAdb(project: Project): Unit = {
val adbPath = AdbPathExtractor.extractPath(project)
shot.configureAdbPath(adbPath)
}

private def findAdbPath(project: Project): String = {
AdbPathExtractor.extractPath(project)
}

private def addTasks(project: Project): Unit = {
Expand Down Expand Up @@ -170,7 +151,7 @@ class ShotPlugin extends Plugin[Project] {
RemoveScreenshotsTask.name(flavor, buildType, beforeExecution = true),
classOf[RemoveScreenshotsTask]
)

val adbPath = findAdbPath(project)
removeScreenshotsAfterExecution.configure { task =>
task.setDescription(RemoveScreenshotsTask.description(flavor, buildType))
task.flavor = flavor
Expand All @@ -187,6 +168,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}
removeScreenshotsBeforeExecution.configure { task =>
task.setDescription(RemoveScreenshotsTask.description(flavor, buildType))
Expand All @@ -204,6 +186,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}

val downloadScreenshots = tasks
Expand All @@ -224,6 +207,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}
val executeScreenshot = tasks
.register(ExecuteScreenshotTests.name(flavor, buildType), classOf[ExecuteScreenshotTests])
Expand All @@ -243,6 +227,7 @@ class ShotPlugin extends Plugin[Project] {
task.recordScreenshots = project.hasProperty("record")
task.printBase64 = project.hasProperty("printBase64")
task.projectName = project.getName
task.adbPath = adbPath
}

if (runInstrumentation(project, extension)) {
Expand Down
32 changes: 17 additions & 15 deletions shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ import com.karumi.shot.screenshots.{
import com.karumi.shot.system.EnvVars
import com.karumi.shot.ui.Console
import com.karumi.shot.{Files, Shot, ShotExtension}
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.{Input, TaskAction}
import org.gradle.api.{DefaultTask, GradleException}

import java.io.File

abstract class ShotTask extends DefaultTask {
var appId: String = _
var flavor: Option[String] = _
var buildTypeName: String = _
var orchestrated: Boolean = false
var projectPath: String = _
var buildPath: String = _
var shotExtension: ShotExtension = _
var directorySuffix: Option[String] = _
var recordScreenshots: Boolean = _
var printBase64: Boolean = _
var projectName: String = _
private val console = new Console
protected val shot: Shot =
@Input var appId: String = _
@Input var flavor: Option[String] = _
@Input var buildTypeName: String = _
@Input var orchestrated: Boolean = false
@Input var projectPath: String = _
@Input var buildPath: String = _
@Input var shotExtension: ShotExtension = _
@Input var directorySuffix: Option[String] = _
@Input var recordScreenshots: Boolean = _
@Input var printBase64: Boolean = _
@Input var projectName: String = _
@Input var adbPath: String = _
protected def shot: Shot = {
val console = new Console
new Shot(
new Adb,
new Adb(adbPath),
new Files,
new ScreenshotsComparator,
new ScreenshotsDiffGenerator(new Base64Encoder),
Expand All @@ -43,6 +44,7 @@ abstract class ShotTask extends DefaultTask {
new ConsoleReporter(console),
new EnvVars()
)
}

protected def shotFolder: ShotFolder = {
ShotFolder(
Expand Down

0 comments on commit 4f609da

Please sign in to comment.