-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load sentry-debug-meta.properties (#2734)
- Loading branch information
Showing
14 changed files
with
456 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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 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
50 changes: 50 additions & 0 deletions
50
...d-core/src/main/java/io/sentry/android/core/internal/debugmeta/AssetsDebugMetaLoader.java
This file contains 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,50 @@ | ||
package io.sentry.android.core.internal.debugmeta; | ||
|
||
import static io.sentry.util.DebugMetaPropertiesApplier.DEBUG_META_PROPERTIES_FILENAME; | ||
|
||
import android.content.Context; | ||
import android.content.res.AssetManager; | ||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
import io.sentry.internal.debugmeta.IDebugMetaLoader; | ||
import java.io.BufferedInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Internal | ||
public final class AssetsDebugMetaLoader implements IDebugMetaLoader { | ||
private final @NotNull Context context; | ||
private final @NotNull ILogger logger; | ||
|
||
public AssetsDebugMetaLoader(final @NotNull Context context, final @NotNull ILogger logger) { | ||
this.context = context; | ||
this.logger = logger; | ||
} | ||
|
||
@Override | ||
public @Nullable Properties loadDebugMeta() { | ||
final AssetManager assets = context.getAssets(); | ||
// one may have thousands of asset files and looking up this list might slow down the SDK init. | ||
// quite a bit, for this reason, we try to open the file directly and take care of errors | ||
// like FileNotFoundException | ||
try (final InputStream is = | ||
new BufferedInputStream(assets.open(DEBUG_META_PROPERTIES_FILENAME))) { | ||
final Properties properties = new Properties(); | ||
properties.load(is); | ||
return properties; | ||
} catch (FileNotFoundException e) { | ||
logger.log(SentryLevel.INFO, e, "%s file was not found.", DEBUG_META_PROPERTIES_FILENAME); | ||
} catch (IOException e) { | ||
logger.log(SentryLevel.ERROR, "Error getting Proguard UUIDs.", e); | ||
} catch (RuntimeException e) { | ||
logger.log(SentryLevel.ERROR, e, "%s file is malformed.", DEBUG_META_PROPERTIES_FILENAME); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains 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
96 changes: 96 additions & 0 deletions
96
...core/src/test/java/io/sentry/android/core/internal/debugmeta/AssetsDebugMetaLoaderTest.kt
This file contains 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,96 @@ | ||
package io.sentry.android.core.internal.debugmeta | ||
|
||
import android.content.Context | ||
import android.content.res.AssetManager | ||
import io.sentry.ILogger | ||
import io.sentry.SentryOptions | ||
import io.sentry.util.DebugMetaPropertiesApplier | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import java.io.FileNotFoundException | ||
import java.nio.charset.Charset | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
class AssetsDebugMetaLoaderTest { | ||
|
||
class Fixture { | ||
val context = mock<Context>() | ||
val assets = mock<AssetManager>() | ||
val logger = mock<ILogger>() | ||
|
||
fun getSut( | ||
fileName: String = "sentry-debug-meta.properties", | ||
content: String? = null, | ||
throws: Boolean = false | ||
): AssetsDebugMetaLoader { | ||
if (content != null) { | ||
whenever(assets.open(fileName)).thenReturn( | ||
content.byteInputStream(Charset.defaultCharset()) | ||
) | ||
} | ||
if (throws) { | ||
whenever(assets.open(fileName)).thenThrow(FileNotFoundException()) | ||
} | ||
whenever(context.assets).thenReturn(assets) | ||
return AssetsDebugMetaLoader(context, logger) | ||
} | ||
} | ||
|
||
private val fixture = Fixture() | ||
|
||
@Test | ||
fun `reads from assets into properties and can be applied to options`() { | ||
val sut = fixture.getSut( | ||
content = | ||
""" | ||
#Generated by sentry-maven-plugin | ||
#Wed May 17 15:33:34 CEST 2023 | ||
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b | ||
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68 | ||
io.sentry.build-tool=maven | ||
""".trimIndent() | ||
) | ||
|
||
val options = SentryOptions() | ||
|
||
assertNotNull(sut.loadDebugMeta()) { | ||
DebugMetaPropertiesApplier.applyToOptions(options, it) | ||
} | ||
|
||
assertEquals(options.bundleIds, setOf("88ba82db-cd26-4c09-8b31-21461d286b68")) | ||
assertEquals(options.proguardUuid, "34077988-a0e5-4839-9618-7400e1616d1b") | ||
} | ||
|
||
@Test | ||
fun `reads multiple bundle IDs from assets into properties and can be applied to options`() { | ||
val sut = fixture.getSut( | ||
content = | ||
""" | ||
#Generated by sentry-maven-plugin | ||
#Wed May 17 15:33:34 CEST 2023 | ||
io.sentry.ProguardUuids=34077988-a0e5-4839-9618-7400e1616d1b | ||
io.sentry.bundle-ids=88ba82db-cd26-4c09-8b31-21461d286b68,8d11a44a-facd-46c1-a49b-87d256227101 | ||
io.sentry.build-tool=maven | ||
""".trimIndent() | ||
) | ||
|
||
val options = SentryOptions() | ||
|
||
assertNotNull(sut.loadDebugMeta()) { | ||
DebugMetaPropertiesApplier.applyToOptions(options, it) | ||
} | ||
|
||
assertEquals(options.bundleIds, setOf("88ba82db-cd26-4c09-8b31-21461d286b68", "8d11a44a-facd-46c1-a49b-87d256227101")) | ||
assertEquals(options.proguardUuid, "34077988-a0e5-4839-9618-7400e1616d1b") | ||
} | ||
|
||
@Test | ||
fun `when file does not exist, swallows exception and returns null`() { | ||
val sut = fixture.getSut(throws = true) | ||
|
||
assertNull(sut.loadDebugMeta()) | ||
} | ||
} |
This file contains 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 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.