Skip to content

Commit 2c53c7b

Browse files
bazel: make //codepropertygraph:tests run the actual tests (#1870)
The target globbed src/main/scala and therefore ran 0 tests. - use src/test/scala and ship src/test/resources as data - add better-files (repin) and link the domain classes at runtime - resolve test resources via the Bazel runfiles lookup (CpgRunfilesLocator, same mechanism as in joern); ProjectRoot.relativise remains the fallback for sbt/IDE only
1 parent 301c401 commit 2c53c7b

7 files changed

Lines changed: 100 additions & 4 deletions

File tree

‎MODULE.bazel‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module(name = "codepropertygraph")
22

33
bazel_dep(name = "protobuf", version = "34.1")
44
bazel_dep(name = "bazel_tooling")
5+
bazel_dep(name = "rules_java", version = "9.6.1")
56
bazel_dep(name = "rules_jvm_external", version = "7.0")
67
bazel_dep(name = "aspect_rules_lint", version = "2.5.2")
78
bazel_dep(name = "buildifier_prebuilt", version = "8.5.1.2")
@@ -46,6 +47,7 @@ maven.install(
4647
"com.google.protobuf:protobuf-java:4.34.1",
4748
"com.lihaoyi:ujson_3:4.4.3",
4849
"com.lihaoyi:os-lib_3:0.11.4",
50+
"com.github.pathikrit:better-files_3:3.9.2",
4951
],
5052
excluded_artifacts = [
5153
# Exclude any scala libraries which might come in transitively.

‎codepropertygraph/BUILD‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@rules_java//java:java_library.bzl", "java_library")
12
load("//bazel:scala.bzl", "scala_library", "scala_test")
23

34
scala_library(
@@ -19,10 +20,32 @@ scala_library(
1920
],
2021
)
2122

23+
# We need to use the java_library rule because the dependency
24+
# @rules_java//java/runfiles brings in an annotation processor and
25+
# those only get picked up and executed by java_library and not
26+
# scala_library.
27+
java_library(
28+
name = "tests_java",
29+
testonly = True,
30+
srcs = glob(["src/test/javaBazel/**/*.java"]),
31+
deps = [
32+
"@rules_java//java/runfiles",
33+
],
34+
)
35+
2236
scala_test(
2337
name = "tests",
24-
srcs = glob(["src/main/scala/**/*.scala"]),
38+
srcs = glob(["src/test/scala/**/*.scala"]),
39+
data = glob(["src/test/resources/**"]),
40+
# The compile time domain classes are neverlink, so the tests need the linked variant at runtime.
41+
runtime_deps = ["//schema:domainClassesLinked"],
2542
deps = [
2643
":codepropertygraph",
44+
":tests_java",
45+
"//schema:domainClassesNeverLinked",
46+
"//schema:java_proto",
47+
"@maven//:com_github_pathikrit_better_files_3",
48+
"@maven//:io_joern_flatgraph_core_3",
49+
"@maven//:io_joern_flatgraph_help_3",
2750
],
2851
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.shiftleft.utils;
2+
3+
import java.io.IOException;
4+
import java.util.Optional;
5+
6+
// This class is only used in the sbt build where the runfiles location is not a
7+
// build concept and thus handled on a case by case basis.
8+
// The sbt implementation just returns empty to indicate that the runfiles
9+
// cannot be retrieved.
10+
class CpgRunfilesLocatorJava {
11+
static Optional<String> resolve(String path) throws IOException {
12+
return Optional.empty();
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.shiftleft.utils;
2+
3+
import com.google.devtools.build.runfiles.AutoBazelRepository;
4+
import com.google.devtools.build.runfiles.Runfiles;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Optional;
10+
11+
// This class is only part of the Bazel build.
12+
@AutoBazelRepository
13+
class CpgRunfilesLocatorJava {
14+
static Optional<String> resolve(String path) throws IOException {
15+
// The AutoBazelRepository_CpgRunfilesLocatorJava class is auto generated based on the @AutoBazelRepository
16+
// annotation.
17+
Runfiles runfiles = Runfiles.preload().withSourceRepository(AutoBazelRepository_CpgRunfilesLocatorJava.NAME);
18+
String resolvedPath = runfiles.rlocation(path);
19+
if (resolvedPath != null && Files.exists(Path.of(resolvedPath))) {
20+
return Optional.of(resolvedPath);
21+
} else {
22+
return Optional.empty();
23+
}
24+
}
25+
}

‎codepropertygraph/src/test/scala/io/shiftleft/codepropertygraph/cpgloading/CpgLoaderTests.scala‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.shiftleft.codepropertygraph.cpgloading
33
import flatgraph.{Accessors, DiffGraphApplier}
44
import io.shiftleft.codepropertygraph.generated.Cpg
55
import io.shiftleft.codepropertygraph.generated.nodes.NewMethod
6-
import io.shiftleft.utils.{ProjectRoot, TempFileCopy}
6+
import io.shiftleft.utils.{CpgRunfilesLocator, ProjectRoot, TempFileCopy}
77
import io.shiftleft.codepropertygraph.generated.nodes.Type
88
import org.scalatest.BeforeAndAfterAll
99
import org.scalatest.matchers.should.Matchers
@@ -37,7 +37,7 @@ class CpgLoaderTests extends AnyWordSpec with Matchers with BeforeAndAfterAll {
3737
}
3838

3939
"allow loading of CPG in overflowdb format" in {
40-
val odbCpg = ProjectRoot.relativise("codepropertygraph/src/test/resources/cpg.odb")
40+
val odbCpg = testResource("cpg.odb")
4141
Using.resource(CpgLoader.load(odbCpg)) { cpg =>
4242
cpg.graph.nodes("METHOD").size shouldBe 7
4343
}
@@ -75,7 +75,13 @@ class CpgLoaderTests extends AnyWordSpec with Matchers with BeforeAndAfterAll {
7575
}
7676

7777
/** this test graph was created by c2cpg for https://github.com/joernio/joern/blob/master/tests/code/c/test.c */
78-
val flatgraphCpg = Paths.get(ProjectRoot.relativise("codepropertygraph/src/test/resources/cpg.fg"))
78+
val flatgraphCpg = Paths.get(testResource("cpg.fg"))
79+
80+
/** Bazel runfiles lookup with fallback to the project root relative path (sbt, IDE) */
81+
private def testResource(name: String): String =
82+
CpgRunfilesLocator
83+
.resolve(s"codepropertygraph/codepropertygraph/src/test/resources/$name")
84+
.getOrElse(ProjectRoot.relativise(s"codepropertygraph/src/test/resources/$name"))
7985

8086
/** flatgraph writes to this file on 'close', so we'll create a temporary copy */
8187
private def temporaryFlatgraphCpg(): TempFileCopy =
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.shiftleft.utils
2+
3+
import scala.jdk.OptionConverters.RichOptional
4+
5+
/** Resolves files via the Bazel runfiles lookup. Only meant for tests inside of this repository. The sbt build always
6+
* returns None, so callers need to fall back to [[ProjectRoot.relativise]].
7+
*/
8+
private[shiftleft] object CpgRunfilesLocator {
9+
def resolve(path: String): Option[String] =
10+
CpgRunfilesLocatorJava.resolve(path).toScala
11+
}

‎maven_install.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
33
"__INPUT_ARTIFACTS_HASH": {
4+
"com.github.pathikrit:better-files_3": 2078137804,
45
"com.google.code.findbugs:jsr305": 495355163,
56
"com.google.code.gson:gson": 597770368,
67
"com.google.errorprone:error_prone_annotations": -1035138750,
@@ -22,6 +23,8 @@
2223
"__RESOLVED_ARTIFACTS_HASH": {
2324
"com.github.luben:zstd-jni": 1886873774,
2425
"com.github.luben:zstd-jni:jar:sources": -1190847340,
26+
"com.github.pathikrit:better-files_3": -730147472,
27+
"com.github.pathikrit:better-files_3:jar:sources": -1753499885,
2528
"com.github.scopt:scopt_3": 148843063,
2629
"com.github.scopt:scopt_3:jar:sources": 776003799,
2730
"com.google.code.findbugs:jsr305": 870839855,
@@ -97,6 +100,13 @@
97100
},
98101
"version": "1.5.7-11"
99102
},
103+
"com.github.pathikrit:better-files_3": {
104+
"shasums": {
105+
"jar": "136c1770a63aa33b2710745bf7f8441cbbe0eeef0cab6b832bd0a46804158989",
106+
"sources": "5a3d4cc0c63605353b1c528abfdc180ae4d74b6c50d6693eee4389dcec923e61"
107+
},
108+
"version": "3.9.2"
109+
},
100110
"com.github.scopt:scopt_3": {
101111
"shasums": {
102112
"jar": "7cfb80743b761b3df8630a818265eb91fd93cd77b32ed5ebd6fba1bb7cff6a3f",
@@ -385,6 +395,9 @@
385395
"com.github.luben.zstd",
386396
"com.github.luben.zstd.util"
387397
],
398+
"com.github.pathikrit:better-files_3": [
399+
"better.files"
400+
],
388401
"com.github.scopt:scopt_3": [
389402
"scopt"
390403
],
@@ -760,6 +773,8 @@
760773
"https://repo1.maven.org/maven2/": [
761774
"com.github.luben:zstd-jni",
762775
"com.github.luben:zstd-jni:jar:sources",
776+
"com.github.pathikrit:better-files_3",
777+
"com.github.pathikrit:better-files_3:jar:sources",
763778
"com.github.scopt:scopt_3",
764779
"com.github.scopt:scopt_3:jar:sources",
765780
"com.google.code.findbugs:jsr305",

0 commit comments

Comments
 (0)