• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.testing.compile;
18 
19 import static com.google.common.base.Strings.isNullOrEmpty;
20 import static com.google.common.collect.MoreCollectors.onlyElement;
21 import static com.google.common.collect.Streams.stream;
22 import static com.google.testing.compile.Compiler.javac;
23 
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.io.Files;
26 import com.google.testing.compile.Compiler;
27 import java.io.File;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Map;
31 import java.util.NoSuchElementException;
32 import com.tschuchort.compiletesting.KotlinCompilation;
33 import dagger.internal.codegen.ComponentProcessor;
34 
35 /** A helper class for working with java compiler tests. */
36 public final class CompilerTests {
CompilerTests()37   private CompilerTests() {}
38 
39   /** Returns the {@plainlink File jar file} containing the compiler deps. */
compilerDepsJar()40   public static File compilerDepsJar() {
41     try {
42       return stream(Files.fileTraverser().breadthFirst(getRunfilesDir()))
43           .filter(file -> file.getName().endsWith("_compiler_deps_deploy.jar"))
44           .collect(onlyElement());
45     } catch (NoSuchElementException e) {
46       throw new IllegalStateException(
47           "No compiler deps jar found. Are you using the Dagger compiler_test macro?", e);
48     }
49   }
50 
51   /** Returns a {@link Compiler} with the compiler deps jar added to the class path. */
compiler()52   public static Compiler compiler() {
53     return javac().withClasspath(ImmutableList.of(compilerDepsJar()));
54   }
55 
kotlinCompiler()56   public static KotlinCompilation kotlinCompiler() {
57     KotlinCompilation compilation = new KotlinCompilation();
58     compilation.setAnnotationProcessors(ImmutableList.of(new ComponentProcessor()));
59     compilation.setClasspaths(
60         ImmutableList.<java.io.File>builder()
61             .addAll(compilation.getClasspaths())
62             .add(compilerDepsJar())
63             .build()
64     );
65     return compilation;
66   }
67 
getRunfilesDir()68   private static File getRunfilesDir() {
69     return getRunfilesPath().toFile();
70   }
71 
getRunfilesPath()72   private static Path getRunfilesPath() {
73     Path propPath = getRunfilesPath(System.getProperties());
74     if (propPath != null) {
75       return propPath;
76     }
77 
78     Path envPath = getRunfilesPath(System.getenv());
79     if (envPath != null) {
80       return envPath;
81     }
82 
83     Path cwd = Paths.get("").toAbsolutePath();
84     return cwd.getParent();
85   }
86 
getRunfilesPath(Map<?, ?> map)87   private static Path getRunfilesPath(Map<?, ?> map) {
88     String runfilesPath = (String) map.get("TEST_SRCDIR");
89     return isNullOrEmpty(runfilesPath) ? null : Paths.get(runfilesPath);
90   }
91 }
92