Skip to content

Commit

Permalink
Refines CLI arguments and updates integration tests (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubiratansoares authored Oct 25, 2023
1 parent 5f5cd35 commit 95fd53a
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion e2e/baselines/duckduckgo-5.175.1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ features = [
"android.hardware.microphone",
"android.hardware.screen.portrait"
]
trustedComponents = [ ]
trustedPackages = [ ]
activities = [
"com.duckduckgo.app.SelectedTextSearchActivity",
"com.duckduckgo.app.WidgetThemeConfiguration",
Expand Down
2 changes: 1 addition & 1 deletion e2e/baselines/firefoxfocus-118.2.0.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ permissions = [
"org.mozilla.focus.permission.RECEIVE_DOWNLOAD_BROADCAST"
]
features = [ ]
trustedComponents = [ ]
trustedPackages = [ ]
activities = [
"androidx.compose.ui.tooling.PreviewActivity",
"com.google.android.gms.common.api.GoogleApiActivity",
Expand Down
2 changes: 1 addition & 1 deletion e2e/baselines/woocommerce-15.7.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ features = [
"android.hardware.location",
"android.hardware.wifi"
]
trustedComponents = [
trustedPackages = [
"com.woocommerce.*",
"org.wordpress.*"
]
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ e2e() {

echo -e "• Comparing against $baseline"

arw compare -t ".tmp/$artifact" -b "$baseline" >/dev/null
arw compare -a ".tmp/$artifact" -b "$baseline" >/dev/null

echo -e "✔ No issues found"
}
Expand Down
14 changes: 7 additions & 7 deletions scripts/acceptance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ test_usage() {
}

test_invalid_inputs() {
comparison=$("$arw" overview -t "$fixtures/missing.apk" -b "$toml" || true)
comparison=$("$arw" overview -a "$fixtures/missing.apk" -b "$toml" || true)
echo "$comparison" | grep "missing.apk does not exist" >/dev/null
}

test_overview() {

echo "✔ Testing artifact overview"

"$arw" overview --target="$fixtures/app-debug.apk"
"$arw" overview --archive="$fixtures/app-debug.apk"

overview=$("$arw" overview --target="$fixtures/app-debug.apk" --json)
overview=$("$arw" overview --archive="$fixtures/app-debug.apk" --json)
(( $(echo "$overview" | jq '.min_sdk') == 28 ))
(( $(echo "$overview" | jq '.target_sdk') == 33 ))
}

test_generate_baseline_complete() {
echo "✔ Testing baseline generation (complete)"

"$arw" generate --target="$fixtures/app-debug.apk"
"$arw" generate -a "$fixtures/app-debug.apk"

local complete_toml="io.dotanuki.norris.android.debug.toml"

Expand All @@ -45,7 +45,7 @@ test_generate_baseline_complete() {
test_generate_baseline_compact() {
echo "✔ Testing baseline generation (compact)"

"$arw" generate --target="$fixtures/app-debug.apk" --ignore="io.dotanuki"
"$arw" generate --archive="$fixtures/app-debug.apk" --trust="io.dotanuki"

local compact_toml="io.dotanuki.norris.android.debug.toml"

Expand All @@ -60,10 +60,10 @@ test_compare_baseline_with_artifact() {
echo
echo "✔ Testing comparison between baseline and artifacts"

"$arw" generate --target="$fixtures/app-release.aab" --ignore="io.dotanuki"
"$arw" generate --archive="$fixtures/app-release.aab" --trust="io.dotanuki"

local toml="io.dotanuki.norris.android.toml"
comparison=$("$arw" compare -t "$fixtures/app-release-changed.apk" -b "$toml" || true)
comparison=$("$arw" compare -a "$fixtures/app-release-changed.apk" -b "$toml" || true)
echo "$comparison"
echo "$comparison" | grep "Your baseline file does not match the supplied artifact" >/dev/null

Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/io/dotanuki/arw/core/toml/WatchdogConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class WatchdogConfig(
val applicationId: String,
val permissions: Set<String> = emptySet(),
val features: Set<String> = emptySet(),
val trustedComponents: Set<String> = emptySet(),
val trustedPackages: Set<String> = emptySet(),
val activities: Set<String> = emptySet(),
val services: Set<String> = emptySet(),
val receivers: Set<String> = emptySet(),
Expand All @@ -27,7 +27,7 @@ data class WatchdogConfig(
permissions,
features,
aggregateComponents().toSet(),
trustedComponents
trustedPackages
)

private fun aggregateComponents() =
Expand All @@ -37,12 +37,10 @@ data class WatchdogConfig(
providers.map { AndroidComponent(it, PROVIDER) }

companion object {
fun from(analysed: AnalysedArtifact, packages: String? = null) = with(analysed) {
val packagesToIgnore = packages?.split(";") ?: emptyList()

fun from(analysed: AnalysedArtifact, packagesToIgnore: List<String>) = with(analysed) {
WatchdogConfig(
applicationId,
trustedComponents = packagesToIgnore.map { "$it.*" }.toSortedSet(),
trustedPackages = packagesToIgnore.toSortedSet(),
permissions = androidPermissions,
features = androidFeatures,
activities = androidComponents.declaredNames(ACTIVITY, packagesToIgnore),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import io.dotanuki.arw.core.toml.WatchdogConfig

context (BaselineContext)
class GenerateCommand : CliktCommand(
help = "arw generate -t/--target <path/to/target>",
help = "arw generate -a/--archive <path/to/archive> -t/--trust <packages>",
name = "generate"
) {

private val target: String by option("-t", "--target").required()
private val ignored: String? by option("-i", "--ignore")
private val pathToArchive: String by option("-a", "--archive").required()
private val trustedPackages: String? by option("-t", "--trust")
private val debugMode by option("--stacktrace").flag(default = false)

override fun run() {
Expand All @@ -28,10 +28,9 @@ class GenerateCommand : CliktCommand(

context (ErrorAware)
private fun extractBaseline() {
val analysed = AndroidArtifactAnalyser.analyse(ValidatedFile(target))
val analysed = AndroidArtifactAnalyser.analyse(ValidatedFile(pathToArchive))
val baseline = WatchdogConfig.from(analysed, ValidatedPackages(trustedPackages))
val outputFile = "${analysed.applicationId}.toml"

val baseline = WatchdogConfig.from(analysed, ignored)
BaselineWriter.write(baseline, outputFile)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.dotanuki.arw.features.baseline

import io.dotanuki.arw.core.errors.ErrorAware

object ValidatedPackages {

context (ErrorAware)
operator fun invoke(packages: String?): List<String> =
packages
?.split(",")
?.map { if (!it.endsWith(".*")) "$it.*" else it }
?: emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import io.dotanuki.arw.core.toml.ValidatedTOML

context (CompareContext)
class CompareCommand : CliktCommand(
help = "arw compare -t/--target <path/to/target> -b/--baseline <path/to/baseline>",
help = "arw compare -a/--archive <path/to/archive> -b/--baseline <path/to/baseline>",
name = "compare"
) {

private val target: String by option("-t", "--target").required()
private val baseline: String by option("-b", "--baseline").required()
private val pathToArchive: String by option("-a", "--archive").required()
private val pathToBaseline: String by option("-b", "--baseline").required()
private val debugMode by option("--stacktrace").flag(default = false)

override fun run() {
Expand All @@ -28,8 +28,8 @@ class CompareCommand : CliktCommand(

context (ErrorAware)
private fun performComparison() {
val current = AndroidArtifactAnalyser.analyse(ValidatedFile(target))
val reference = ValidatedTOML(ValidatedFile(baseline))
val current = AndroidArtifactAnalyser.analyse(ValidatedFile(pathToArchive))
val reference = ValidatedTOML(ValidatedFile(pathToBaseline))
val comparison = ArtifactsComparator.compare(current, reference.asBaseline())
ComparisonReporter.reportChanges(comparison)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import io.dotanuki.arw.core.filesystem.ValidatedFile

context (OverviewContext)
class OverviewCommand : CliktCommand(
help = "arw overview [--console | --json] ",
help = "arw overview -a/--archive <path/to/archive> [--console | --json] ",
name = "overview"
) {

private val switches = listOf("--json" to "json", "--console" to "console").toTypedArray()

private val format: String by option().switch(*switches).default("console")
private val target: String by option("-t", "--target").required()
private val pathToArchive: String by option("-a", "--archive").required()
private val debugMode by option("--stacktrace").flag(default = false)

override fun run() {
Expand All @@ -34,7 +34,7 @@ class OverviewCommand : CliktCommand(

context (ErrorAware)
private fun extractOverview() {
val analysed = AndroidArtifactAnalyser.analyse(ValidatedFile(target))
val analysed = AndroidArtifactAnalyser.analyse(ValidatedFile(pathToArchive))

val overview = with(analysed) {
ArtifactOverview(
Expand Down

0 comments on commit 95fd53a

Please sign in to comment.