Skip to content

Commit 2a67ad4

Browse files
committed
Only change the log folder on enterprise build
1 parent 2d801a0 commit 2a67ad4

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporter.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ class DefaultBugReporter @Inject constructor(
9393
private var currentLogDirectory: File = baseLogDirectory
9494

9595
init {
96-
val logSubfolder = runBlocking {
97-
sessionStore.getLatestSession()
98-
}?.userId?.substringAfter(":")
99-
setCurrentLogDirectory(logSubfolder)
100-
matrixAuthenticationService.listenToNewMatrixClients {
101-
// When a new Matrix client is created, we update the tracing configuration to write to files
102-
setLogDirectorySubfolder(it.userIdServerName())
96+
if (buildMeta.isEnterpriseBuild) {
97+
val logSubfolder = runBlocking {
98+
sessionStore.getLatestSession()
99+
}?.userId?.substringAfter(":")
100+
setCurrentLogDirectory(logSubfolder)
101+
matrixAuthenticationService.listenToNewMatrixClients {
102+
// When a new Matrix client is created, we update the tracing configuration to write
103+
// the files in a dedicated subfolders.
104+
setLogDirectorySubfolder(it.userIdServerName())
105+
}
103106
}
104107
}
105108

@@ -312,8 +315,10 @@ class DefaultBugReporter @Inject constructor(
312315
}
313316

314317
override fun setLogDirectorySubfolder(subfolderName: String?) {
315-
setCurrentLogDirectory(subfolderName)
316-
tracingService.updateWriteToFilesConfiguration(createWriteToFilesConfiguration())
318+
if (buildMeta.isEnterpriseBuild) {
319+
setCurrentLogDirectory(subfolderName)
320+
tracingService.updateWriteToFilesConfiguration(createWriteToFilesConfiguration())
321+
}
317322
}
318323

319324
private fun setCurrentLogDirectory(subfolderName: String?) {

features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import io.element.android.features.rageshake.api.reporter.BugReporterListener
1313
import io.element.android.features.rageshake.impl.crash.CrashDataStore
1414
import io.element.android.features.rageshake.impl.crash.FakeCrashDataStore
1515
import io.element.android.features.rageshake.impl.screenshot.FakeScreenshotHolder
16+
import io.element.android.libraries.core.meta.BuildMeta
1617
import io.element.android.libraries.matrix.api.MatrixClientProvider
1718
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
1819
import io.element.android.libraries.matrix.api.tracing.TracingService
@@ -305,23 +306,35 @@ class DefaultBugReporterTest {
305306
@Test
306307
fun `the log directory is initialized using the last session store data`() = runTest {
307308
val sut = createDefaultBugReporter(
309+
buildMeta = aBuildMeta(isEnterpriseBuild = true),
308310
sessionStore = InMemorySessionStore().apply {
309311
storeData(aSessionData(sessionId = "@alice:domain.com"))
310312
}
311313
)
312314
assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs/domain.com")
313315
}
314316

317+
@Test
318+
fun `foss build - the log directory is initialized to the root log directory`() = runTest {
319+
val sut = createDefaultBugReporter(
320+
sessionStore = InMemorySessionStore().apply {
321+
storeData(aSessionData(sessionId = "@alice:domain.com"))
322+
}
323+
)
324+
assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs")
325+
}
326+
315327
@Test
316328
fun `when the log directory is updated, the tracing service is invoked`() = runTest {
317329
var param: WriteToFilesConfiguration? = null
318330
val updateWriteToFilesConfigurationResult = lambdaRecorder<WriteToFilesConfiguration, Unit> {
319331
param = it
320332
}
321333
val sut = createDefaultBugReporter(
334+
buildMeta = aBuildMeta(isEnterpriseBuild = true),
322335
tracingService = FakeTracingService(
323336
updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult,
324-
)
337+
),
325338
)
326339
sut.setLogDirectorySubfolder("my.sub.folder")
327340
updateWriteToFilesConfigurationResult.assertions().isCalledOnce()
@@ -333,16 +346,29 @@ class DefaultBugReporterTest {
333346
assertThat((param as WriteToFilesConfiguration.Enabled).filenameSuffix).isEqualTo("log")
334347
}
335348

349+
@Test
350+
fun `foss build - when the log directory is updated, the tracing service is not invoked`() = runTest {
351+
val updateWriteToFilesConfigurationResult = lambdaRecorder<WriteToFilesConfiguration, Unit> {}
352+
val sut = createDefaultBugReporter(
353+
tracingService = FakeTracingService(
354+
updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult,
355+
)
356+
)
357+
sut.setLogDirectorySubfolder("my.sub.folder")
358+
updateWriteToFilesConfigurationResult.assertions().isNeverCalled()
359+
}
360+
336361
@Test
337362
fun `when the log directory is reset, the tracing service is invoked`() = runTest {
338363
var param: WriteToFilesConfiguration? = null
339364
val updateWriteToFilesConfigurationResult = lambdaRecorder<WriteToFilesConfiguration, Unit> {
340365
param = it
341366
}
342367
val sut = createDefaultBugReporter(
368+
buildMeta = aBuildMeta(isEnterpriseBuild = true),
343369
tracingService = FakeTracingService(
344370
updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult,
345-
)
371+
),
346372
)
347373
sut.setLogDirectorySubfolder(null)
348374
updateWriteToFilesConfigurationResult.assertions().isCalledOnce()
@@ -354,6 +380,18 @@ class DefaultBugReporterTest {
354380
assertThat((param as WriteToFilesConfiguration.Enabled).filenameSuffix).isEqualTo("log")
355381
}
356382

383+
@Test
384+
fun `foss build - when the log directory is reset, the tracing service is not invoked`() = runTest {
385+
val updateWriteToFilesConfigurationResult = lambdaRecorder<WriteToFilesConfiguration, Unit> {}
386+
val sut = createDefaultBugReporter(
387+
tracingService = FakeTracingService(
388+
updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult,
389+
)
390+
)
391+
sut.setLogDirectorySubfolder(null)
392+
updateWriteToFilesConfigurationResult.assertions().isNeverCalled()
393+
}
394+
357395
@Test
358396
fun `when a new MatrixClient is created the logs folder is updated`() = runTest {
359397
var param: WriteToFilesConfiguration? = null
@@ -368,6 +406,7 @@ class DefaultBugReporterTest {
368406
)
369407
}
370408
val sut = createDefaultBugReporter(
409+
buildMeta = aBuildMeta(isEnterpriseBuild = true),
371410
matrixAuthenticationService = matrixAuthenticationService,
372411
tracingService = FakeTracingService(
373412
updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult,
@@ -385,15 +424,37 @@ class DefaultBugReporterTest {
385424
assertThat((param as WriteToFilesConfiguration.Enabled).filenameSuffix).isEqualTo("log")
386425
}
387426

427+
@Test
428+
fun `foss build - when a new MatrixClient is created the logs folder is not updated`() = runTest {
429+
val updateWriteToFilesConfigurationResult = lambdaRecorder<WriteToFilesConfiguration, Unit> {}
430+
val matrixAuthenticationService = FakeMatrixAuthenticationService().apply {
431+
givenMatrixClient(
432+
FakeMatrixClient(
433+
userIdServerNameLambda = { "domain.foo.org" },
434+
)
435+
)
436+
}
437+
val sut = createDefaultBugReporter(
438+
matrixAuthenticationService = matrixAuthenticationService,
439+
tracingService = FakeTracingService(
440+
updateWriteToFilesConfigurationResult = updateWriteToFilesConfigurationResult,
441+
)
442+
)
443+
assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs")
444+
matrixAuthenticationService.login("alice", "password")
445+
assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs")
446+
updateWriteToFilesConfigurationResult.assertions().isNeverCalled()
447+
}
448+
388449
private fun TestScope.createDefaultBugReporter(
450+
buildMeta: BuildMeta = aBuildMeta(),
389451
sessionStore: SessionStore = InMemorySessionStore(),
390452
matrixClientProvider: MatrixClientProvider = FakeMatrixClientProvider(),
391453
crashDataStore: CrashDataStore = FakeCrashDataStore(),
392454
server: MockWebServer = MockWebServer(),
393455
tracingService: TracingService = FakeTracingService(),
394456
matrixAuthenticationService: MatrixAuthenticationService = FakeMatrixAuthenticationService(),
395457
): DefaultBugReporter {
396-
val buildMeta = aBuildMeta()
397458
return DefaultBugReporter(
398459
context = RuntimeEnvironment.getApplication(),
399460
screenshotHolder = FakeScreenshotHolder(),

0 commit comments

Comments
 (0)