Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arpita184 committed Sep 20, 2023
1 parent c53ae2c commit 1b83c8f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
7 changes: 5 additions & 2 deletions skate-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ repositories { mavenCentral() }

// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij { plugins.add("org.intellij.plugins.markdown") }
intellij {
plugins.add("org.intellij.plugins.markdown")
plugins.add("org.jetbrains.kotlin")
}

fun isGitHash(hash: String): Boolean {
if (hash.length != 40) {
Expand Down Expand Up @@ -85,7 +88,7 @@ tasks
// endregion

dependencies {
implementation(libs.kotlin.compilerEmbeddable)
compileOnly(libs.kotlin.compilerEmbeddable)
implementation(libs.bugsnag) { exclude(group = "org.slf4j") }
testImplementation(libs.junit)
testImplementation(libs.truth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ class FeatureFlagExtractor {
* @return A list of feature flag names in a file
*/
fun extractFeatureFlags(psiFile: com.intellij.psi.PsiFile): List<String> {
if (!isKtFile(psiFile)) {
LOG.info("$psiFile is not a KtFile")
return emptyList()
}
LOG.info("Getting Enum Entries")
val enumsWithAnnotation = findAnnotatedEnums(psiFile)
LOG.info("Enums with Annotations: $enumsWithAnnotation")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2023 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.slack.sgp.intellij.featureflags

import com.google.common.truth.Truth.assertThat
import com.intellij.openapi.components.service
import com.intellij.psi.PsiFile
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixture4TestCase
import com.slack.sgp.intellij.SkatePluginSettings
import org.junit.Test

class FeatureFlagAnnotatortest : LightPlatformCodeInsightFixture4TestCase() {

@Test
fun `test returns empty list when linkify is disabled`() {
assertThat(runAnnotator(enabled = false)).isEmpty()
}

@Test
fun `test report symbols when linkify is enabled`() {
assertThat(runAnnotator(enabled = true)).isNotEmpty()
}

@Test
fun `test extraction of feature flags from provided content`() {
val featureFlagExtractor = FeatureFlagExtractor()
val content =
"""
@FeatureFlags(ForComplianceFeature::class)
enum class ComplianceFeature(override val dependencies: Set<FeatureFlagEnum> = emptySet()) :
FeatureFlagEnum {
/** Enables validation of app-scoped environment variant */
@FeatureFlag(defaultValue = false, minimization = UNAUTHENTICATED) ANDROID_ENV_VARIANT_VALIDATION,
@FeatureFlag(defaultValue = false, minimization = UNAUTHENTICATED)
ANDROID_GOV_SLACK_CUSTOM_AWARENESS,
@FeatureFlag(defaultValue = false, minimization = AUTHENTICATED)
ANDROID_GOV_SLACK_CUSTOM_AWARENESS_TEAM_SWITCH;
override val key: String by computeKey()
}
"""

// Create a KtFile from the content
val psiFile = createKotlinFile("ComplianceFeature.kt", content)
val featureFlags = featureFlagExtractor.extractFeatureFlags(psiFile)
// Assertions
assertTrue(featureFlags.contains("ANDROID_ENV_VARIANT_VALIDATION"))
assertTrue(featureFlags.contains("ANDROID_GOV_SLACK_CUSTOM_AWARENESS"))
assertTrue(featureFlags.contains("ANDROID_GOV_SLACK_CUSTOM_AWARENESS_TEAM_SWITCH"))
}

private fun runAnnotator(enabled: Boolean): List<FeatureFlagSymbol> {
project.service<SkatePluginSettings>().isLinkifiedFeatureFlagsEnabled = enabled
val content =
"""
@FeatureFlags(ForComplianceFeature::class)
enum class ComplianceFeature(override val dependencies: Set<FeatureFlagEnum> = emptySet()) :
FeatureFlagEnum {
/** Enables validation of app-scoped environment variant */
@FeatureFlag(defaultValue = false, minimization = UNAUTHENTICATED) ANDROID_ENV_VARIANT_VALIDATION,
@FeatureFlag(defaultValue = false, minimization = UNAUTHENTICATED)
ANDROID_GOV_SLACK_CUSTOM_AWARENESS,
@FeatureFlag(defaultValue = false, minimization = AUTHENTICATED)
ANDROID_GOV_SLACK_CUSTOM_AWARENESS_TEAM_SWITCH;
override val key: String by computeKey()
}
"""
val file = createKotlinFile("ComplianceFeature.kt", content)
FeatureFlagAnnotator().collectInformation(file)
return FeatureFlagAnnotator().doAnnotate(file)
}

private fun createKotlinFile(
name: String,
text: String,
): PsiFile {
return myFixture.configureByText(name, text)
}
}

0 comments on commit 1b83c8f

Please sign in to comment.