This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load analyser via absolute path at runtime
- Loading branch information
Andong Liao
committed
Apr 22, 2022
1 parent
0a19a93
commit 52faea2
Showing
12 changed files
with
244 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dependencies/analysers/* |
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
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
15 changes: 15 additions & 0 deletions
15
scanner_cli/src/test/kotlin/org/archguard/scanner/ctl/impl/OfficialAnalyserSpecsTest.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,15 @@ | ||
package org.archguard.scanner.ctl.impl | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class OfficialAnalyserSpecsTest { | ||
@Test | ||
fun `should output the spec with the default jar for all the official analysers`() { | ||
val specs = OfficialAnalyserSpecs.specs() | ||
|
||
assertThat(specs).allMatch { | ||
it.jar == "${it.identifier}-${it.version}-all.jar" | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
scanner_cli/src/test/kotlin/org/archguard/scanner/ctl/loader/AnalyserDispatcherTest.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,86 @@ | ||
package org.archguard.scanner.ctl.loader | ||
|
||
import chapi.domain.core.CodeDataStruct | ||
import io.mockk.clearAllMocks | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import io.mockk.verify | ||
import org.archguard.scanner.core.Analyser | ||
import org.archguard.scanner.core.AnalyserSpec | ||
import org.archguard.scanner.core.context.Context | ||
import org.archguard.scanner.core.sourcecode.SourceCodeAnalyser | ||
import org.archguard.scanner.core.sourcecode.SourceCodeContext | ||
import org.archguard.scanner.ctl.impl.OfficialAnalyserSpecs | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class AnalyserDispatcherTest { | ||
private val context = mockk<SourceCodeContext> { | ||
every { language } returns "lang_kotlin" | ||
every { features } returns listOf("feature1", "feature2") | ||
} | ||
|
||
@BeforeEach | ||
internal fun setUp() { | ||
mockkObject(AnalyserLoader) | ||
} | ||
|
||
@AfterEach | ||
internal fun tearDown() { | ||
clearAllMocks() | ||
} | ||
|
||
@Test | ||
fun `should dispatch to the official source code analyser, pass the ast to following feature analyser`() { | ||
val customized = listOf<AnalyserSpec>( | ||
mockk { every { identifier } returns "feature1" }, | ||
mockk { every { identifier } returns "feature2" }, | ||
) | ||
val languageAnalyser = mockk<SourceCodeAnalyser>() | ||
val feature1Analyser = mockk<SourceCodeAnalyser>() | ||
val feature2Analyser = mockk<SourceCodeAnalyser>() | ||
val ast = mockk<List<CodeDataStruct>>() | ||
every { languageAnalyser.analyse(null) } returns ast | ||
every { feature1Analyser.analyse(ast) } returns null | ||
every { feature2Analyser.analyse(ast) } returns null | ||
every { | ||
AnalyserLoader.load(context, any()) | ||
} returns (languageAnalyser as Analyser<Context>) andThen (feature1Analyser as Analyser<Context>) andThen (feature2Analyser as Analyser<Context>) | ||
|
||
val dispatcher = AnalyserDispatcher(context, customized) | ||
dispatcher.dispatch() | ||
|
||
verify { | ||
AnalyserLoader.load(context, OfficialAnalyserSpecs.LANG_KOTLIN.spec()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should dispatch to the customized source code analyser`() { | ||
val customizedLanguageAnalyser = mockk<AnalyserSpec> { every { identifier } returns "lang_kotlin" } | ||
val customized = listOf<AnalyserSpec>( | ||
customizedLanguageAnalyser, | ||
mockk { every { identifier } returns "feature1" }, | ||
mockk { every { identifier } returns "feature2" }, | ||
) | ||
val languageAnalyser = mockk<SourceCodeAnalyser>() | ||
val feature1Analyser = mockk<SourceCodeAnalyser>() | ||
val feature2Analyser = mockk<SourceCodeAnalyser>() | ||
val ast = mockk<List<CodeDataStruct>>() | ||
every { languageAnalyser.analyse(null) } returns ast | ||
every { feature1Analyser.analyse(ast) } returns null | ||
every { feature2Analyser.analyse(ast) } returns null | ||
every { | ||
AnalyserLoader.load(context, any()) | ||
} returns (languageAnalyser as Analyser<Context>) andThen (feature1Analyser as Analyser<Context>) andThen (feature2Analyser as Analyser<Context>) | ||
|
||
val dispatcher = AnalyserDispatcher(context, customized) | ||
dispatcher.dispatch() | ||
|
||
verify { | ||
AnalyserLoader.load(context, customizedLanguageAnalyser) | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
scanner_cli/src/test/kotlin/org/archguard/scanner/ctl/loader/AnalyserLoaderTest.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,62 @@ | ||
package org.archguard.scanner.ctl.loader | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.archguard.scanner.core.AnalyserSpec | ||
import org.archguard.scanner.core.sourcecode.SourceCodeContext | ||
import org.archguard.scanner.ctl.loader.AnalyserLoader.installPath | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.deleteIfExists | ||
import kotlin.reflect.full.memberFunctions | ||
|
||
internal class AnalyserLoaderTest { | ||
private val fakeJarName = "testonly-lang_kotlin-1.6.1-all.jar" | ||
private val spec = AnalyserSpec( | ||
identifier = "lang_kotlin", | ||
host = "<unknown>", | ||
version = "1.6.1", | ||
jar = fakeJarName, | ||
className = "KotlinAnalyser", | ||
) | ||
private val context = mockk<SourceCodeContext> { | ||
every { client } returns mockk() | ||
} | ||
|
||
private fun fakeInstall(source: File) = source.copyTo(installPath.resolve(spec.jar).toFile(), overwrite = true) | ||
private fun fakeUninstall() = installPath.resolve(spec.jar).deleteIfExists() | ||
|
||
@BeforeEach | ||
internal fun setUp() { | ||
fakeUninstall() | ||
} | ||
|
||
@Test | ||
fun `should load the analyser from remote jar via http url`() { | ||
// FIXME: implement this after upload the test jar | ||
} | ||
|
||
@Test | ||
fun `should load the analyser from local jar via absolute path`() { | ||
val folder = this.javaClass.classLoader.getResource("kotlin")!! | ||
val analyser = AnalyserLoader.load(context, spec.copy(host = folder.path)) | ||
|
||
val kClass = analyser::class | ||
assertThat(kClass.simpleName).isEqualTo("KotlinAnalyser") | ||
assertThat(kClass.memberFunctions.map { it.name }).contains("analyse") | ||
} | ||
|
||
@Test | ||
fun `should load the analyser from existing jar`() { | ||
val folder = this.javaClass.classLoader.getResource("kotlin")!! | ||
fakeInstall(Path(folder.path).resolve(fakeJarName).toFile()) | ||
val analyser = AnalyserLoader.load(context, spec) | ||
|
||
val kClass = analyser::class | ||
assertThat(kClass.simpleName).isEqualTo("KotlinAnalyser") | ||
assertThat(kClass.memberFunctions.map { it.name }).contains("analyse") | ||
} | ||
} |
Binary file not shown.
Binary file added
BIN
+10.9 MB
scanner_cli/src/test/resources/kotlin/testonly-lang_kotlin-1.6.1-all.jar
Binary file not shown.
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 |
---|---|---|
|
@@ -7,4 +7,3 @@ data class AnalyserSpec( | |
val jar: String, | ||
val className: String, // calculate via identifier?? | ||
) | ||
|