Skip to content

Commit b9c1e0c

Browse files
committed
Add test suite for -targetroot:javac-classes-directory
1 parent 50dab02 commit b9c1e0c

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbJavacOptions.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sourcegraph.semanticdb_javac;
22

3+
import java.io.ByteArrayOutputStream;
4+
import java.io.PrintStream;
35
import java.nio.file.Path;
46
import java.nio.file.Paths;
57
import java.util.ArrayList;
@@ -22,7 +24,7 @@ public class SemanticdbJavacOptions {
2224
public boolean verboseEnabled = false;
2325
public final ArrayList<String> errors;
2426

25-
public static String stubClassName = "META-INF-stub";
27+
public static String stubClassName = "META-INF-semanticdb-stub";
2628

2729
public SemanticdbJavacOptions() {
2830
errors = new ArrayList<>();
@@ -83,10 +85,12 @@ private static Path getJavacClassesDir(SemanticdbJavacOptions result, Context ct
8385
fm.getJavaFileForOutput(CLASS_OUTPUT, stubClassName, JavaFileObject.Kind.CLASS, null);
8486
outputDir = Paths.get(outputDirStub.toUri()).toAbsolutePath().getParent();
8587
} catch (Exception e) {
88+
ByteArrayOutputStream out = new ByteArrayOutputStream();
89+
e.printStackTrace(new PrintStream(out));
8690
String errorMsg =
8791
String.format(
88-
"-targetroot:%s passed but could not get the class output directory: %s",
89-
JAVAC_CLASSES_DIR_ARG, e.getMessage());
92+
"exception while processing SemanticDB option '-targetroot:%s'\n%s",
93+
JAVAC_CLASSES_DIR_ARG, out.toString());
9094
result.errors.add(errorMsg);
9195
}
9296
return outputDir;

tests/unit/src/main/scala/tests/SimpleFileManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests;
22

33
import java.net.URI;
4+
import java.nio.file.Path;
45
import java.util.ArrayList;
56
import java.util.List;
67
import javax.tools.FileObject;
@@ -13,19 +14,21 @@
1314
public class SimpleFileManager
1415
extends ForwardingJavaFileManager<StandardJavaFileManager> {
1516

16-
public List<SimpleClassFile> compiled = new ArrayList<>();
17+
public final List<SimpleClassFile> compiled = new ArrayList<>();
18+
public final Path targetroot;
1719

18-
protected SimpleFileManager(StandardJavaFileManager fileManager) {
20+
protected SimpleFileManager(StandardJavaFileManager fileManager, Path targetroot) {
1921
super(fileManager);
22+
this.targetroot = targetroot;
2023
}
2124

2225
// standard constructors/getters
2326

2427
@Override
2528
public JavaFileObject getJavaFileForOutput(Location location,
2629
String className, JavaFileObject.Kind kind, FileObject sibling) {
27-
SimpleClassFile result = new SimpleClassFile(
28-
URI.create("string://" + className));
30+
URI uri = targetroot.resolve(className).toUri();
31+
SimpleClassFile result = new SimpleClassFile(uri);
2932
if (!className.equals(SemanticdbJavacOptions.stubClassName)) {
3033
compiled.add(result);
3134
}

tests/unit/src/main/scala/tests/TestCompiler.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class TestCompiler(
2828

2929
private val compiler = ToolProvider.getSystemJavaCompiler
3030
private val fileManager =
31-
new SimpleFileManager(compiler.getStandardFileManager(null, null, null))
31+
new SimpleFileManager(
32+
compiler.getStandardFileManager(null, null, null),
33+
targetroot
34+
)
3235

3336
def this(targetroot: Path) {
3437
this(TestCompiler.PROCESSOR_PATH, Nil, targetroot)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tests
2+
3+
import java.nio.file.Files
4+
import java.nio.file.Paths
5+
6+
import scala.meta.inputs.Input
7+
8+
import munit.FunSuite
9+
10+
class JavacClassesDirectorySuite extends FunSuite with TempDirectories {
11+
val sourceroot = new DirectoryFixture()
12+
13+
override def munitFixtures: Seq[Fixture[_]] =
14+
super.munitFixtures ++ List(sourceroot)
15+
16+
test("targetroot:javac-classes-directory") {
17+
val compiler =
18+
new TestCompiler(
19+
classpath = TestCompiler.PROCESSOR_PATH,
20+
options = Nil,
21+
targetroot = sourceroot(),
22+
sourceroot = sourceroot()
23+
)
24+
val compileResult = compiler.compile(
25+
List(
26+
Input.VirtualFile(
27+
"example/Example.java",
28+
"""package example;
29+
|public class Example{}""".stripMargin
30+
)
31+
),
32+
List(
33+
s"-Xplugin:semanticdb -sourceroot:${sourceroot()} -targetroot:javac-classes-directory",
34+
"-d",
35+
sourceroot().toString
36+
)
37+
)
38+
assert(clue(compileResult).isSuccess)
39+
val semanticdbPath = Paths
40+
.get("META-INF")
41+
.resolve("semanticdb")
42+
.resolve("example")
43+
.resolve("Example.java.semanticdb")
44+
assert(Files.isRegularFile(clue(sourceroot().resolve(semanticdbPath))))
45+
}
46+
47+
}

0 commit comments

Comments
 (0)