Skip to content

Commit 6910929

Browse files
committed
Use index-dependency command for Kotlin tests
1 parent 0f8e84b commit 6910929

File tree

3 files changed

+116
-46
lines changed

3 files changed

+116
-46
lines changed

lsif-java/src/main/scala/com/sourcegraph/lsif_java/buildtools/LsifBuildTool.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,14 @@ class LsifBuildTool(index: IndexCommand) extends BuildTool("LSIF", index) {
163163
return CommandResult(0, Nil)
164164
}
165165

166-
val errors = ListBuffer.empty[Try[Unit]]
167-
errors += compileJavaFiles(tmp, deps, config, javaFiles)
168-
errors += compileScalaFiles(deps, scalaFiles)
169-
errors += compileKotlinFiles(deps, config, kotlinFiles)
166+
val compileAttemtps = ListBuffer.empty[Try[Unit]]
167+
compileAttemtps += compileJavaFiles(tmp, deps, config, javaFiles)
168+
compileAttemtps += compileScalaFiles(deps, scalaFiles)
169+
compileAttemtps += compileKotlinFiles(deps, config, kotlinFiles)
170+
val errors = compileAttemtps.collect { case Failure(exception) =>
171+
exception
172+
}
173+
170174
if (index.cleanup) {
171175
Files.walkFileTree(tmp, new DeleteVisitor)
172176
}
@@ -182,9 +186,11 @@ class LsifBuildTool(index: IndexCommand) extends BuildTool("LSIF", index) {
182186
.info(
183187
"Some SemanticDB files got generated even if there were compile errors. " +
184188
"In most cases, this means that lsif-java managed to index everything " +
185-
"except the locations that had compile errors and you can ignore the compile errors.\n" +
186-
errors.mkString("\n")
189+
"except the locations that had compile errors and you can ignore the compile errors."
187190
)
191+
errors.foreach { error =>
192+
index.app.reporter.info(error.getMessage())
193+
}
188194
}
189195
CommandResult(0, Nil)
190196
}
@@ -282,9 +288,6 @@ class LsifBuildTool(index: IndexCommand) extends BuildTool("LSIF", index) {
282288

283289
parseCommandLineArguments(args.asJava, kargs)
284290

285-
if (true)
286-
println(s"ARGS ${args.mkString(" ")}")
287-
288291
val exit = new K2JVMCompiler().exec(
289292
new MessageCollector {
290293
private val errors = new util.LinkedList[String]
@@ -305,7 +308,7 @@ class LsifBuildTool(index: IndexCommand) extends BuildTool("LSIF", index) {
305308
val msg = MessageRenderer
306309
.PLAIN_FULL_PATHS
307310
.render(compilerMessageSeverity, s, compilerMessageSourceLocation)
308-
System.err.println(msg)
311+
index.app.reporter.debug(msg)
309312
errors.push(msg)
310313
}
311314
},

lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/IndexDependencyCommand.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,26 @@ import com.sourcegraph.lsif_java.Dependencies
1313
import com.sourcegraph.lsif_semanticdb.JavaVersion
1414
import moped.cli.Command
1515
import moped.cli.CommandParser
16+
import moped.annotations.DeprecatedName
1617

1718
final case class IndexDependencyCommand(
18-
target: Path = Paths.get("maven"),
19+
@DeprecatedName("target", "Use --output instead", "0.6.10") output: Path =
20+
Paths.get("maven"),
1921
index: IndexCommand = IndexCommand(),
20-
dependency: String = ""
22+
dependency: String = "",
23+
provided: List[String] = Nil
2124
) extends Command {
2225
def app = index.app
2326
private val absoluteTarget = AbsolutePath
24-
.of(target, app.env.workingDirectory)
27+
.of(output, app.env.workingDirectory)
2528
.resolve(dependency.replace(":", "__"))
2629
def run(): Int = {
2730
if (dependency == "") {
2831
app.reporter.error("dependency can't be empty")
2932
1
3033
} else {
3134
val deps = Dependencies
32-
.resolveDependencies(List(dependency), transitive = false)
35+
.resolveDependencies(dependency :: provided, transitive = false)
3336
deps.sources.headOption match {
3437
case Some(sources) =>
3538
unzipJar(sources)
@@ -39,8 +42,10 @@ final case class IndexDependencyCommand(
3942
JavaVersion.classfileJvmVersion(classpath).orElse(8)
4043
) match {
4144
case Some(jvmVersion) =>
45+
val roundedVersion = JavaVersion
46+
.roundToNearestStableRelease(jvmVersion)
4247
val config =
43-
s"""{"kind":"maven","jvm":"$jvmVersion","dependencies":["$dependency"]}"""
48+
s"""{"kind":"maven","jvm":"${roundedVersion}","dependencies":["$dependency"]}"""
4449
Files.createDirectories(absoluteTarget)
4550
Files.write(
4651
absoluteTarget.resolve("lsif-java.json"),
@@ -71,7 +76,6 @@ final case class IndexDependencyCommand(
7176
app.reporter.error(s"no sources for dependency '$dependency'")
7277
1
7378
}
74-
1
7579
}
7680
}
7781

tests/snapshots/src/main/scala/tests/LibrarySnapshotGenerator.scala

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,119 @@ import scala.meta.io.AbsolutePath
1515
import com.sourcegraph.io.DeleteVisitor
1616
import com.sourcegraph.lsif_java.Dependencies
1717
import com.sourcegraph.lsif_java.SemanticdbPrinters
18+
import com.sourcegraph.lsif_java.LsifJava
19+
import java.nio.file.SimpleFileVisitor
20+
import java.nio.file.FileVisitResult
21+
import java.nio.file.attribute.BasicFileAttributes
22+
import java.io.PrintStream
23+
import java.io.ByteArrayOutputStream
24+
import moped.reporters.ConsoleReporter
1825

1926
class LibrarySnapshotGenerator extends SnapshotGenerator {
2027
val scalaPattern = FileSystems.getDefault.getPathMatcher("glob:**.scala")
2128
val javaPattern = FileSystems.getDefault.getPathMatcher("glob:**.java")
2229
private case class IndexMetrics(occurrenceCount: Int, linesOfCode: Int)
30+
def runLsifJava(arguments: List[String]): Unit = {
31+
val baos = new ByteArrayOutputStream
32+
val exitCode = LsifJava
33+
.app
34+
.withReporter(ConsoleReporter(new PrintStream(baos)))
35+
.withEnv(
36+
LsifJava
37+
.app
38+
.env
39+
.withStandardOutput(new PrintStream(baos))
40+
.withStandardError(new PrintStream(baos))
41+
.withExit(i => throw new RuntimeException(i.toString()))
42+
)
43+
.run(arguments)
44+
if (exitCode != 0) {
45+
throw new RuntimeException(baos.toString())
46+
}
47+
}
2348

2449
override def run(context: SnapshotContext, handler: SnapshotHandler): Unit = {
2550
val gen = new Gen(context, handler)
2651
gen.checkLibrary(
27-
"com.airbnb.android:epoxy:4.3.1",
28-
isIncluded = jar => jar.contains("epoxy")
52+
"com.airbnb.android:epoxy:4.3.1"
53+
// isIncluded = jar => jar.contains("epoxy")
2954
)
55+
// gen.checkLibrary("org.jetbrains.kotlin:kotlin-allopen:1.4.32")
3056
gen.checkLibrary(
3157
"com.lihaoyi:ujson_2.13:1.4.0",
3258
provided = List(
3359
s"org.scala-lang:scala-library:${Properties.versionNumberString}"
34-
),
35-
isIncluded = jar => jar.contains("ujson")
60+
)
3661
)
3762
}
63+
3864
private class Gen(context: SnapshotContext, handler: SnapshotHandler) {
39-
def checkLibrary(
40-
name: String,
41-
provided: List[String] = Nil,
42-
isIncluded: String => Boolean = _ => true
43-
): Unit = {
65+
def checkLibrary(name: String, provided: List[String] = Nil): Unit = {
4466
println(s"indexing library '$name'")
45-
val deps = Dependencies.resolveDependencies(name :: provided)
46-
val counter = new AtomicInteger()
67+
val providedArguments = provided.flatMap(p => List("--provided", p))
4768
val targetroot = Files.createTempDirectory("semanticdb-javac")
48-
49-
val compiler =
50-
new TestCompiler(
51-
deps.classpathSyntax,
52-
javacOptions = List("-Xlint:none"),
53-
scalacOptions = Nil,
54-
targetroot
69+
val indexDir = Files.createTempDirectory("semanticdb-javac")
70+
runLsifJava(
71+
List(
72+
"index-dependency",
73+
"--dependency",
74+
name,
75+
"--output",
76+
indexDir.toString()
77+
) ++ providedArguments
78+
)
79+
val snapshotDir = Files.createTempDirectory("semanticdb-javac")
80+
val dumpDir = indexDir.toFile().listFiles().head.toPath
81+
val outputDir = snapshotDir.resolve(dumpDir.getFileName())
82+
runLsifJava(
83+
List(
84+
"snapshot-lsif",
85+
"--cwd",
86+
dumpDir.toString(),
87+
"--output",
88+
outputDir.toString()
5589
)
56-
val timer = new Timer()
57-
val toIndex = deps.sources.filter(p => isIncluded(p.getFileName.toString))
58-
toIndex.foreach { source =>
59-
val metrics = compileSourcesJar(source, compiler)
60-
val i = counter.incrementAndGet()
61-
val message =
62-
f"$i%3s/${toIndex.size} jars; $timer%6s; " +
63-
f"${metrics.occurrenceCount}%,.0f occurrences; " +
64-
f"${metrics.linesOfCode}%,.0f loc; " + f"${source.getFileName}"
65-
println(message)
66-
}
67-
Files.walkFileTree(targetroot, new DeleteVisitor())
90+
)
91+
Files.walkFileTree(
92+
outputDir,
93+
new SimpleFileVisitor[Path] {
94+
override def visitFile(
95+
file: Path,
96+
attrs: BasicFileAttributes
97+
): FileVisitResult = {
98+
val print =
99+
new String(Files.readAllBytes(file), StandardCharsets.UTF_8)
100+
val out = context
101+
.expectDirectory
102+
.resolve(outputDir.relativize(file))
103+
handler.onSnapshotTest(context, out, () => print)
104+
super.visitFile(file, attrs)
105+
}
106+
}
107+
)
108+
// Files.walkFileTree(indexDir, new DeleteVisitor())
109+
// Files.walkFileTree(snapshotDir, new DeleteVisitor())
110+
// val deps = Dependencies.resolveDependencies(name :: provided)
111+
// val counter = new AtomicInteger()
112+
113+
// val compiler =
114+
// new TestCompiler(
115+
// deps.classpathSyntax,
116+
// javacOptions = List("-Xlint:none"),
117+
// scalacOptions = Nil,
118+
// targetroot
119+
// )
120+
// val timer = new Timer()
121+
// val toIndex = deps.sources.filter(p => isIncluded(p.getFileName.toString))
122+
// toIndex.foreach { source =>
123+
// val metrics = compileSourcesJar(source, compiler)
124+
// val i = counter.incrementAndGet()
125+
// val message =
126+
// f"$i%3s/${toIndex.size} jars; $timer%6s; " +
127+
// f"${metrics.occurrenceCount}%,.0f occurrences; " +
128+
// f"${metrics.linesOfCode}%,.0f loc; " + f"${source.getFileName}"
129+
// println(message)
130+
// }
68131
}
69132

70133
private def compileSourcesJar(

0 commit comments

Comments
 (0)