-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TelemetryData at KotlinSensor level, passed to the MetricVisitor
- Loading branch information
1 parent
ba412e6
commit db5c66e
Showing
5 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/telemetry/TelemetryData.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,31 @@ | ||
/* | ||
* SonarSource Kotlin | ||
* Copyright (C) 2018-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the Sonar Source-Available License for more details. | ||
* | ||
* You should have received a copy of the Sonar Source-Available License | ||
* along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
*/ | ||
package org.sonarsource.kotlin.api.telemetry | ||
|
||
import org.sonar.api.batch.sensor.SensorContext | ||
|
||
class TelemetryData { | ||
var hasAndroidImports = false | ||
var hasAndroidImportsReportedAsTrue = false | ||
|
||
fun report(sensorContext: SensorContext) { | ||
if (!hasAndroidImportsReportedAsTrue) { | ||
sensorContext.addTelemetryProperty("kotlin.android", if (hasAndroidImports) "1" else "0") | ||
hasAndroidImportsReportedAsTrue = hasAndroidImports | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/telemetry/TelemetryDataTest.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,50 @@ | ||
/* | ||
* SonarSource Kotlin | ||
* Copyright (C) 2018-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the Sonar Source-Available License for more details. | ||
* | ||
* You should have received a copy of the Sonar Source-Available License | ||
* along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
*/ | ||
package org.sonarsource.kotlin.api.telemetry | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
import org.sonar.api.batch.sensor.SensorContext | ||
import org.sonar.api.batch.sensor.internal.SensorContextTester | ||
import java.nio.file.Path | ||
|
||
class TelemetryDataTest { | ||
@Test | ||
fun hasAndroidImportsReportedAsTrue() { | ||
val data = TelemetryData() | ||
assertFalse(data.hasAndroidImports) | ||
assertFalse(data.hasAndroidImportsReportedAsTrue) | ||
data.hasAndroidImports = false | ||
assertFalse(data.hasAndroidImportsReportedAsTrue) | ||
val sensorContext = MockSensorContext(SensorContextTester.create(Path.of("."))) | ||
data.report(sensorContext) | ||
assertFalse(data.hasAndroidImportsReportedAsTrue) | ||
data.hasAndroidImports = true | ||
data.report(sensorContext) | ||
assertTrue(data.hasAndroidImportsReportedAsTrue) | ||
data.hasAndroidImports = false | ||
data.report(sensorContext) | ||
assertTrue(data.hasAndroidImportsReportedAsTrue) | ||
} | ||
} | ||
|
||
// Mock to SensorContextTester delegation needed as addTelemetryProperty raises | ||
// UnsupportedOperationException and SensorContextTester has private constructors | ||
private class MockSensorContext(wrapped: SensorContextTester) : SensorContext by wrapped { | ||
override fun addTelemetryProperty(key: String, value: String) { } | ||
} |
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
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