From 861758b47dd41f48026fdf346d0d9c842bd5ae4c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 7 Aug 2025 14:02:07 +0200 Subject: [PATCH] Only change the log folder on enterprise build --- .../impl/reporter/DefaultBugReporter.kt | 23 ++++--- .../impl/reporter/DefaultBugReporterTest.kt | 67 ++++++++++++++++++- 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporter.kt b/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporter.kt index 60b4274689..028b671eb6 100755 --- a/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporter.kt +++ b/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporter.kt @@ -93,13 +93,16 @@ class DefaultBugReporter @Inject constructor( private var currentLogDirectory: File = baseLogDirectory init { - val logSubfolder = runBlocking { - sessionStore.getLatestSession() - }?.userId?.substringAfter(":") - setCurrentLogDirectory(logSubfolder) - matrixAuthenticationService.listenToNewMatrixClients { - // When a new Matrix client is created, we update the tracing configuration to write to files - setLogDirectorySubfolder(it.userIdServerName()) + if (buildMeta.isEnterpriseBuild) { + val logSubfolder = runBlocking { + sessionStore.getLatestSession() + }?.userId?.substringAfter(":") + setCurrentLogDirectory(logSubfolder) + matrixAuthenticationService.listenToNewMatrixClients { + // When a new Matrix client is created, we update the tracing configuration to write + // the files in a dedicated subfolders. + setLogDirectorySubfolder(it.userIdServerName()) + } } } @@ -312,8 +315,10 @@ class DefaultBugReporter @Inject constructor( } override fun setLogDirectorySubfolder(subfolderName: String?) { - setCurrentLogDirectory(subfolderName) - tracingService.updateWriteToFilesConfiguration(createWriteToFilesConfiguration()) + if (buildMeta.isEnterpriseBuild) { + setCurrentLogDirectory(subfolderName) + tracingService.updateWriteToFilesConfiguration(createWriteToFilesConfiguration()) + } } private fun setCurrentLogDirectory(subfolderName: String?) { diff --git a/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt b/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt index 05fcbd26df..c7c424bfae 100755 --- a/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt +++ b/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt @@ -13,6 +13,7 @@ import io.element.android.features.rageshake.api.reporter.BugReporterListener import io.element.android.features.rageshake.impl.crash.CrashDataStore import io.element.android.features.rageshake.impl.crash.FakeCrashDataStore import io.element.android.features.rageshake.impl.screenshot.FakeScreenshotHolder +import io.element.android.libraries.core.meta.BuildMeta import io.element.android.libraries.matrix.api.MatrixClientProvider import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService import io.element.android.libraries.matrix.api.tracing.TracingService @@ -305,6 +306,7 @@ class DefaultBugReporterTest { @Test fun `the log directory is initialized using the last session store data`() = runTest { val sut = createDefaultBugReporter( + buildMeta = aBuildMeta(isEnterpriseBuild = true), sessionStore = InMemorySessionStore().apply { storeData(aSessionData(sessionId = "@alice:domain.com")) } @@ -312,6 +314,16 @@ class DefaultBugReporterTest { assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs/domain.com") } + @Test + fun `foss build - the log directory is initialized to the root log directory`() = runTest { + val sut = createDefaultBugReporter( + sessionStore = InMemorySessionStore().apply { + storeData(aSessionData(sessionId = "@alice:domain.com")) + } + ) + assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs") + } + @Test fun `when the log directory is updated, the tracing service is invoked`() = runTest { var param: WriteToFilesConfiguration? = null @@ -319,9 +331,10 @@ class DefaultBugReporterTest { param = it } val sut = createDefaultBugReporter( + buildMeta = aBuildMeta(isEnterpriseBuild = true), tracingService = FakeTracingService( updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult, - ) + ), ) sut.setLogDirectorySubfolder("my.sub.folder") updateWriteToFilesConfigurationResult.assertions().isCalledOnce() @@ -333,6 +346,18 @@ class DefaultBugReporterTest { assertThat((param as WriteToFilesConfiguration.Enabled).filenameSuffix).isEqualTo("log") } + @Test + fun `foss build - when the log directory is updated, the tracing service is not invoked`() = runTest { + val updateWriteToFilesConfigurationResult = lambdaRecorder {} + val sut = createDefaultBugReporter( + tracingService = FakeTracingService( + updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult, + ) + ) + sut.setLogDirectorySubfolder("my.sub.folder") + updateWriteToFilesConfigurationResult.assertions().isNeverCalled() + } + @Test fun `when the log directory is reset, the tracing service is invoked`() = runTest { var param: WriteToFilesConfiguration? = null @@ -340,9 +365,10 @@ class DefaultBugReporterTest { param = it } val sut = createDefaultBugReporter( + buildMeta = aBuildMeta(isEnterpriseBuild = true), tracingService = FakeTracingService( updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult, - ) + ), ) sut.setLogDirectorySubfolder(null) updateWriteToFilesConfigurationResult.assertions().isCalledOnce() @@ -354,6 +380,18 @@ class DefaultBugReporterTest { assertThat((param as WriteToFilesConfiguration.Enabled).filenameSuffix).isEqualTo("log") } + @Test + fun `foss build - when the log directory is reset, the tracing service is not invoked`() = runTest { + val updateWriteToFilesConfigurationResult = lambdaRecorder {} + val sut = createDefaultBugReporter( + tracingService = FakeTracingService( + updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult, + ) + ) + sut.setLogDirectorySubfolder(null) + updateWriteToFilesConfigurationResult.assertions().isNeverCalled() + } + @Test fun `when a new MatrixClient is created the logs folder is updated`() = runTest { var param: WriteToFilesConfiguration? = null @@ -368,6 +406,7 @@ class DefaultBugReporterTest { ) } val sut = createDefaultBugReporter( + buildMeta = aBuildMeta(isEnterpriseBuild = true), matrixAuthenticationService = matrixAuthenticationService, tracingService = FakeTracingService( updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult, @@ -385,7 +424,30 @@ class DefaultBugReporterTest { assertThat((param as WriteToFilesConfiguration.Enabled).filenameSuffix).isEqualTo("log") } + @Test + fun `foss build - when a new MatrixClient is created the logs folder is not updated`() = runTest { + val updateWriteToFilesConfigurationResult = lambdaRecorder {} + val matrixAuthenticationService = FakeMatrixAuthenticationService().apply { + givenMatrixClient( + FakeMatrixClient( + userIdServerNameLambda = { "domain.foo.org" }, + ) + ) + } + val sut = createDefaultBugReporter( + matrixAuthenticationService = matrixAuthenticationService, + tracingService = FakeTracingService( + updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult, + ) + ) + assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs") + matrixAuthenticationService.login("alice", "password") + assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs") + updateWriteToFilesConfigurationResult.assertions().isNeverCalled() + } + private fun TestScope.createDefaultBugReporter( + buildMeta: BuildMeta = aBuildMeta(), sessionStore: SessionStore = InMemorySessionStore(), matrixClientProvider: MatrixClientProvider = FakeMatrixClientProvider(), crashDataStore: CrashDataStore = FakeCrashDataStore(), @@ -393,7 +455,6 @@ class DefaultBugReporterTest { tracingService: TracingService = FakeTracingService(), matrixAuthenticationService: MatrixAuthenticationService = FakeMatrixAuthenticationService(), ): DefaultBugReporter { - val buildMeta = aBuildMeta() return DefaultBugReporter( context = RuntimeEnvironment.getApplication(), screenshotHolder = FakeScreenshotHolder(),