-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exclusion filter for directories (#1828)
* Add exclusion filter for directories * Revert changes from other branch * Refactor filter * Add separate props for exclusion by regex and strings * Overload functions * Changed method parameter types to vararg * Added tests --------- Co-authored-by: Christian Banse <[email protected]> Co-authored-by: Christian Banse <[email protected]>
- Loading branch information
1 parent
71fc658
commit 897955d
Showing
7 changed files
with
196 additions
and
8 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
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
149 changes: 149 additions & 0 deletions
149
cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/ExclusionTest.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,149 @@ | ||
/* | ||
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 de.fraunhofer.aisec.cpg | ||
|
||
import de.fraunhofer.aisec.cpg.frontends.Language | ||
import de.fraunhofer.aisec.cpg.frontends.TestLanguage | ||
import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend | ||
import de.fraunhofer.aisec.cpg.graph.Node | ||
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.newTranslationUnitDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.types.Type | ||
import de.fraunhofer.aisec.cpg.graph.unknownType | ||
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation | ||
import java.io.File | ||
import kotlin.reflect.KClass | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
|
||
class TestFileLanguage : TestLanguage() { | ||
override val fileExtensions: List<String> | ||
get() = listOf("file") | ||
|
||
override val frontend: KClass<out TestLanguageFrontend> | ||
get() = TestFileLanguageFrontend::class | ||
} | ||
|
||
/** Just a test frontend that "reads" a file and returns an empty [TranslationUnitDeclaration]. */ | ||
class TestFileLanguageFrontend( | ||
language: Language<TestLanguageFrontend> = TestFileLanguage(), | ||
ctx: TranslationContext = | ||
TranslationContext( | ||
TranslationConfiguration.builder().build(), | ||
ScopeManager(), | ||
TypeManager() | ||
), | ||
) : TestLanguageFrontend("::", language, ctx) { | ||
override fun parse(file: File): TranslationUnitDeclaration { | ||
return newTranslationUnitDeclaration(file.name) | ||
} | ||
|
||
override fun typeOf(type: Any): Type { | ||
return unknownType() | ||
} | ||
|
||
override fun codeOf(astNode: Any): String? { | ||
return null | ||
} | ||
|
||
override fun locationOf(astNode: Any): PhysicalLocation? { | ||
return null | ||
} | ||
|
||
override fun setComment(node: Node, astNode: Any) {} | ||
} | ||
|
||
class ExclusionTest { | ||
@Test | ||
fun testExclusionPatternStringDirectory() { | ||
val topLevel = File("src/test/resources/exclusion") | ||
val result = | ||
TranslationManager.builder() | ||
.config( | ||
TranslationConfiguration.builder() | ||
.topLevel(topLevel) | ||
.sourceLocations(topLevel) | ||
.defaultPasses() | ||
.exclusionPatterns("tests") | ||
.registerLanguage<TestFileLanguage>() | ||
.build() | ||
) | ||
.build() | ||
.analyze() | ||
.get() | ||
|
||
val tus = result.translationUnits | ||
assertNotNull(tus) | ||
assertEquals(1, tus.size) | ||
} | ||
|
||
@Test | ||
fun testExclusionPatternStringFile() { | ||
val topLevel = File("src/test/resources/exclusion") | ||
val result = | ||
TranslationManager.builder() | ||
.config( | ||
TranslationConfiguration.builder() | ||
.topLevel(topLevel) | ||
.sourceLocations(topLevel) | ||
.defaultPasses() | ||
.exclusionPatterns("test.file") | ||
.registerLanguage<TestFileLanguage>() | ||
.build() | ||
) | ||
.build() | ||
.analyze() | ||
.get() | ||
|
||
val tus = result.translationUnits | ||
assertNotNull(tus) | ||
assertEquals(1, tus.size) | ||
} | ||
|
||
@Test | ||
fun testExclusionPatternRegex() { | ||
val topLevel = File("src/test/resources/exclusion") | ||
val result = | ||
TranslationManager.builder() | ||
.config( | ||
TranslationConfiguration.builder() | ||
.topLevel(topLevel) | ||
.sourceLocations(topLevel) | ||
.defaultPasses() | ||
.exclusionPatterns("""(.*)est.file""".toRegex()) | ||
.registerLanguage<TestFileLanguage>() | ||
.build() | ||
) | ||
.build() | ||
.analyze() | ||
.get() | ||
|
||
val tus = result.translationUnits | ||
assertNotNull(tus) | ||
assertEquals(1, tus.size) | ||
} | ||
} |
Empty file.
Empty file.
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