Skip to content

Commit edb5624

Browse files
committed
Convert c2cpg test fixtures to use inmemory cpg creation
1 parent bcce6a7 commit edb5624

File tree

3 files changed

+42
-50
lines changed

3 files changed

+42
-50
lines changed

joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/ExcludeTests.scala

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,17 @@ class ExcludeTests extends AnyWordSpec with Matchers with TableDrivenPropertyChe
4848
override def afterAll(): Unit = FileUtil.delete(projectUnderTest, swallowIoExceptions = true)
4949

5050
private def testWithArguments(exclude: Seq[String], excludeRegex: String, expectedFiles: Set[String]): Unit = {
51-
FileUtil.usingTemporaryFile(suffix = "-c2cpg.bin") { tmpCpg =>
52-
val config = Config()
53-
.withInputPath(projectUnderTest.toString)
54-
.withOutputPath(tmpCpg.toString)
55-
.withIgnoredFiles(exclude)
56-
.withIgnoredFilesRegex(excludeRegex)
57-
val c2cpg = new C2Cpg()
58-
val cpg = c2cpg.createCpg(config).get
51+
val config = Config()
52+
.withInputPath(projectUnderTest.toString)
53+
.withIgnoredFiles(exclude)
54+
.withIgnoredFilesRegex(excludeRegex)
55+
val c2cpg = new C2Cpg()
56+
val cpg = c2cpg.createCpg(config).get
5957

60-
X2Cpg.applyDefaultOverlays(cpg)
61-
cpg.file.nameNot(FileTraversal.UNKNOWN, "<includes>").name.l should contain theSameElementsAs expectedFiles.map(
62-
_.replace("/", java.io.File.separator)
63-
)
64-
}
58+
X2Cpg.applyDefaultOverlays(cpg)
59+
cpg.file.nameNot(FileTraversal.UNKNOWN, "<includes>").name.l should contain theSameElementsAs expectedFiles.map(
60+
_.replace("/", java.io.File.separator)
61+
)
6562
}
6663

6764
"Using case sensitive excludes" should {

joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/testfixtures/AstC2CpgFrontend.scala

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,32 @@ trait AstC2CpgFrontend extends LanguageFrontend {
1818
override type ConfigType = Config
1919

2020
def execute(sourceCodePath: java.io.File): Cpg = {
21-
FileUtil.usingTemporaryFile(suffix = "cpg.bin") { tmpCpg =>
22-
val cpg = newEmptyCpg(Option(tmpCpg.toString))
23-
val pathAsString = sourceCodePath.getAbsolutePath
24-
val config = getConfig()
25-
.fold(Config())(_.asInstanceOf[Config])
26-
.withInputPath(pathAsString)
27-
.withOutputPath(pathAsString)
28-
.withSchemaValidation(ValidationMode.Enabled)
29-
val global = new CGlobal()
21+
val cpg = newEmptyCpg()
22+
val pathAsString = sourceCodePath.getAbsolutePath
23+
val config = getConfig()
24+
.fold(Config())(_.asInstanceOf[Config])
25+
.withInputPath(pathAsString)
26+
.withOutputPath(pathAsString)
27+
.withSchemaValidation(ValidationMode.Enabled)
28+
val global = new CGlobal()
3029

31-
val preprocessedFiles = if (config.withPreprocessedFiles) {
32-
SourceFiles
33-
.determine(
34-
config.inputPath,
35-
Set(FileDefaults.PreprocessedExt),
36-
ignoredDefaultRegex = Option(DefaultIgnoredFolders),
37-
ignoredFilesRegex = Option(config.ignoredFilesRegex),
38-
ignoredFilesPath = Option(config.ignoredFiles)
39-
)
40-
} else List.empty
30+
val preprocessedFiles = if (config.withPreprocessedFiles) {
31+
SourceFiles
32+
.determine(
33+
config.inputPath,
34+
Set(FileDefaults.PreprocessedExt),
35+
ignoredDefaultRegex = Option(DefaultIgnoredFolders),
36+
ignoredFilesRegex = Option(config.ignoredFilesRegex),
37+
ignoredFilesPath = Option(config.ignoredFiles)
38+
)
39+
} else List.empty
4140

42-
val sourceFileExtensions = FileDefaults.SourceFileExtensions ++
43-
Option.when(config.withPreprocessedFiles)(FileDefaults.PreprocessedExt).toList
44-
new AstCreationPass(cpg, preprocessedFiles, sourceFileExtensions, config, global).createAndApply()
45-
new AstCreationPass(cpg, preprocessedFiles, FileDefaults.HeaderFileExtensions, config, global).createAndApply()
46-
new FunctionDeclNodePass(cpg, global.unhandledMethodDeclarations(), config).createAndApply()
47-
new PostFrontendValidator(cpg, false).run()
48-
cpg
49-
}
41+
val sourceFileExtensions = FileDefaults.SourceFileExtensions ++
42+
Option.when(config.withPreprocessedFiles)(FileDefaults.PreprocessedExt).toList
43+
new AstCreationPass(cpg, preprocessedFiles, sourceFileExtensions, config, global).createAndApply()
44+
new AstCreationPass(cpg, preprocessedFiles, FileDefaults.HeaderFileExtensions, config, global).createAndApply()
45+
new FunctionDeclNodePass(cpg, global.unhandledMethodDeclarations(), config).createAndApply()
46+
new PostFrontendValidator(cpg, false).run()
47+
cpg
5048
}
5149
}

joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/testfixtures/C2CpgFrontend.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ trait C2CpgFrontend extends LanguageFrontend {
1111
override type ConfigType = Config
1212

1313
def execute(sourceCodePath: java.io.File): Cpg = {
14-
FileUtil.usingTemporaryFile(suffix = "cpg.bin") { tmpCpg =>
15-
val c2cpg = new C2Cpg()
16-
val config = getConfig()
17-
.fold(Config())(_.asInstanceOf[Config])
18-
.withInputPath(sourceCodePath.getAbsolutePath)
19-
.withOutputPath(tmpCpg.toString)
20-
val res = c2cpg.createCpg(config).get
21-
new PostFrontendValidator(res, false).run()
22-
res
23-
}
14+
val c2cpg = new C2Cpg()
15+
val config = getConfig()
16+
.fold(Config())(_.asInstanceOf[Config])
17+
.withInputPath(sourceCodePath.getAbsolutePath)
18+
val res = c2cpg.createCpg(config).get
19+
new PostFrontendValidator(res, false).run()
20+
res
2421
}
2522
}

0 commit comments

Comments
 (0)