Skip to content

Commit

Permalink
[javasrc2cpg] Preserve path in ConfigFile name (#4543)
Browse files Browse the repository at this point in the history
* ConfigFile - preserve path in their .name

* Avoid hardcoded path separator
  • Loading branch information
xavierpinho authored May 7, 2024
1 parent 3917c67 commit 7e92698
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,25 @@ class ConfigFileCreationPassTests extends JavaSrcCode2CpgFixture {
}
}

"it should populate config files correctly when they are nested" in {
val configFile1Path = Paths.get("config.properties").toString
val configFile2Path = Paths.get("someDir", "config.properties").toString
val cpg = code("""public class Foo{}
|""".stripMargin)
.moreCode(code = "config.file=1", fileName = configFile1Path)
.moreCode(code = "config.file=2", fileName = configFile2Path)

cpg.typeDecl.name("Foo").nonEmpty shouldBe true
cpg.configFile.sortBy(_.name).l match {
case List(configFile1, configFile2) =>
configFile1.name shouldBe configFile1Path
configFile1.content shouldBe "config.file=1"

configFile2.name shouldBe configFile2Path
configFile2.content shouldBe "config.file=2"

case result => fail(s"Expected two config files but got $result")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class XConfigFileCreationPass(cpg: Cpg, private val rootDir: Option[Str
}

private def configFileName(configFile: File): String = {
Try(Paths.get(rootDir.get).toAbsolutePath)
Try(Paths.get(rootDir.getOrElse(cpg.metaData.root.head)).toAbsolutePath)
.map(_.relativize(configFile.path.toAbsolutePath).toString)
.getOrElse(configFile.name)
}
Expand Down

0 comments on commit 7e92698

Please sign in to comment.