use of com.facebook.buck.rules.FakeBuildRule in project buck by facebook.
the class Jsr199JavacIntegrationTest method shouldUseSpecifiedJavacJar.
@Test
public void shouldUseSpecifiedJavacJar() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
BuildRule rule = new FakeBuildRule("//:fake", pathResolver);
resolver.addToIndex(rule);
Path fakeJavacJar = Paths.get("ae036e57-77a7-4356-a79c-0f85b1a3290d", "fakeJavac.jar");
ExecutionContext executionContext = TestExecutionContext.newInstance();
MockClassLoader mockClassLoader = new MockClassLoader(ClassLoader.getSystemClassLoader(), ImmutableMap.of(JavacOptions.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, MockJavac.class));
executionContext.getClassLoaderCache().injectClassLoader(ClassLoader.getSystemClassLoader(), ImmutableList.of(fakeJavacJar.toUri().toURL()), mockClassLoader);
Jsr199Javac javac = createJavac(/* withSyntaxError */
false, Optional.of(fakeJavacJar));
JavacExecutionContext javacExecutionContext = JavacExecutionContext.of(new JavacEventSinkToBuckEventBusBridge(executionContext.getBuckEventBus()), executionContext.getStdErr(), executionContext.getClassLoaderCache(), executionContext.getObjectMapper(), executionContext.getVerbosity(), executionContext.getCellPathResolver(), executionContext.getJavaPackageFinder(), createProjectFilesystem(), NoOpClassUsageFileWriter.instance(), executionContext.getEnvironment(), executionContext.getProcessExecutor(), ImmutableList.of(fakeJavacJar), Optional.empty());
boolean caught = false;
try {
javac.buildWithClasspath(javacExecutionContext, BuildTargetFactory.newInstance("//some:example"), ImmutableList.of(), ImmutableList.of(), SOURCE_PATHS, pathToSrcsList, Optional.empty(), JavacOptions.AbiGenerationMode.CLASS);
fail("Did not expect compilation to succeed");
} catch (UnsupportedOperationException ex) {
if (ex.toString().contains("abcdef")) {
caught = true;
}
}
assertTrue("mock Java compiler should throw", caught);
}
use of com.facebook.buck.rules.FakeBuildRule in project buck by facebook.
the class ProjectTest method testDoNotIgnoreAllOfBuckOut.
@Test
public void testDoNotIgnoreAllOfBuckOut() {
SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
ProjectFilesystem projectFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem("/opt/src/buck");
BuildTarget buildTarget = BuildTarget.builder(projectFilesystem.getRootPath(), "//", "base").build();
BuildRule buildRule = new FakeBuildRule(buildTarget, resolver);
SerializableModule module = new SerializableModule(buildRule, buildTarget);
Project.addRootExcludes(module, buildRule, projectFilesystem);
ImmutableSortedSet.Builder<SourceFolder> expectedExcludeFolders = ImmutableSortedSet.orderedBy(SerializableModule.ALPHABETIZER);
expectedExcludeFolders.add(new SourceFolder("file://$MODULE_DIR$/buck-out/bin", /* isTestSource */
false));
expectedExcludeFolders.add(new SourceFolder("file://$MODULE_DIR$/buck-out/log", /* isTestSource */
false));
expectedExcludeFolders.add(new SourceFolder("file://$MODULE_DIR$/buck-out/tmp", /* isTestSource */
false));
for (Path ignorePath : FluentIterable.from(projectFilesystem.getIgnorePaths()).filter(input -> input.getType() == PathOrGlobMatcher.Type.PATH).transform(PathOrGlobMatcher::getPath)) {
if (!ignorePath.equals(projectFilesystem.getBuckPaths().getBuckOut()) && !ignorePath.equals(projectFilesystem.getBuckPaths().getGenDir())) {
expectedExcludeFolders.add(new SourceFolder("file://$MODULE_DIR$/" + MorePaths.pathWithUnixSeparators(ignorePath), /* isTestSource */
false));
}
}
assertEquals("Specific subfolders of buck-out should be excluded rather than all of buck-out.", expectedExcludeFolders.build(), module.excludeFolders);
}
use of com.facebook.buck.rules.FakeBuildRule in project buck by facebook.
the class JavaLibrarySymbolsFinderTest method onlyNonGeneratedSrcsShouldAffectRuleKey.
@Test
@SuppressWarnings("PMD.PrematureDeclaration")
public void onlyNonGeneratedSrcsShouldAffectRuleKey() throws IOException {
TestDataHelper.createProjectWorkspaceForScenario(this, "java_library_symbols_finder", tmp).setUp();
final ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot());
Function<String, SourcePath> convert = src -> new PathSourcePath(projectFilesystem, Paths.get(src));
SourcePath example1 = convert.apply("Example1.java");
SourcePath example2 = convert.apply("Example2.java");
final BuildTarget fakeBuildTarget = BuildTargetFactory.newInstance("//foo:GenEx.java");
SourcePath generated = new DefaultBuildTargetSourcePath(fakeBuildTarget);
final boolean shouldRecordRequiredSymbols = true;
JavaLibrarySymbolsFinder example1Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1), javaFileParser, shouldRecordRequiredSymbols);
JavaLibrarySymbolsFinder example2Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example2), javaFileParser, shouldRecordRequiredSymbols);
JavaLibrarySymbolsFinder example1AndGeneratedSrcFinder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1, generated), javaFileParser, shouldRecordRequiredSymbols);
// Mock out calls to a SourcePathResolver so we can create a legitimate
// DefaultRuleKeyFactory.
final SourcePathRuleFinder ruleFinder = createMock(SourcePathRuleFinder.class);
final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
expect(ruleFinder.getRule(anyObject(SourcePath.class))).andAnswer(() -> {
SourcePath input = (SourcePath) EasyMock.getCurrentArguments()[0];
if (input instanceof ExplicitBuildTargetSourcePath) {
return Optional.of(new FakeBuildRule(fakeBuildTarget, pathResolver));
} else {
return Optional.empty();
}
}).anyTimes();
// Calculates the RuleKey for a JavaSymbolsRule with the specified JavaLibrarySymbolsFinder.
final FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(projectFilesystem)));
final DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
Function<JavaLibrarySymbolsFinder, RuleKey> createRuleKey = finder -> {
JavaSymbolsRule javaSymbolsRule = new JavaSymbolsRule(BuildTargetFactory.newInstance("//foo:rule"), finder, ImmutableSortedSet.of(), ObjectMappers.newDefaultInstance(), projectFilesystem);
return ruleKeyFactory.build(javaSymbolsRule);
};
RuleKey key1 = createRuleKey.apply(example1Finder);
RuleKey key2 = createRuleKey.apply(example2Finder);
RuleKey key3 = createRuleKey.apply(example1AndGeneratedSrcFinder);
assertNotNull(key1);
assertNotNull(key2);
assertNotNull(key3);
assertNotEquals("Two instances of a JavaSymbolsRule with different srcs should change the RuleKey.", key1, key2);
assertEquals("Introducing an extra generated .java file to the srcs should not change the RuleKey.", key1, key3);
}
use of com.facebook.buck.rules.FakeBuildRule in project buck by facebook.
the class InputBasedRuleKeyFactoryTest method ruleKeyDoesNotChangeWhenOnlyDependencyRuleKeyChanges.
@Test
public void ruleKeyDoesNotChangeWhenOnlyDependencyRuleKeyChanges() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
Path depOutput = Paths.get("output");
FakeBuildRule dep = resolver.addToIndex(new FakeBuildRule(BuildTargetFactory.newInstance("//:dep"), filesystem, pathResolver));
dep.setOutputFile(depOutput.toString());
filesystem.writeContentsToPath("hello", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
FakeFileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of(filesystem.resolve(depOutput), HashCode.fromInt(0)));
BuildRule rule = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:rule")).setOut("out").setSrcs(ImmutableList.of(dep.getSourcePathToOutput())).build(resolver, filesystem);
RuleKey inputKey1 = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(rule);
RuleKey inputKey2 = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(rule);
assertThat(inputKey1, Matchers.equalTo(inputKey2));
}
use of com.facebook.buck.rules.FakeBuildRule in project buck by facebook.
the class ContentAgnosticRuleKeyFactoryTest method createRuleKey.
private RuleKey createRuleKey(ProjectFilesystem fileSystem, String filename, String fileContents) throws Exception {
RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(0);
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
Path depOutput = Paths.get(filename);
FakeBuildRule dep = resolver.addToIndex(new FakeBuildRule(BuildTargetFactory.newInstance("//:dep"), fileSystem, pathResolver));
dep.setOutputFile(depOutput.toString());
fileSystem.writeContentsToPath(fileContents, pathResolver.getRelativePath(dep.getSourcePathToOutput()));
BuildRule rule = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:rule")).setOut(filename).setSrcs(ImmutableList.of(dep.getSourcePathToOutput())).build(resolver, fileSystem);
return new ContentAgnosticRuleKeyFactory(fieldLoader, pathResolver, ruleFinder).build(rule);
}
Aggregations