Search in sources :

Example 1 with SourceFolder

use of com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder in project buck by facebook.

the class ProjectTest method testSrcRoots.

/**
   * A project_config()'s src_roots argument can be {@code []}, or a non-empty array.
   * Each of these should be treated differently.
   */
@Test
public void testSrcRoots() throws Exception {
    // Create a project_config() with src_roots=[].
    BuildRuleResolver ruleResolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildRule baseBuildRule = AndroidLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:base")).build(ruleResolver2);
    ProjectConfig inPackageProjectConfig = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:project_config")).setSrcRule(baseBuildRule.getBuildTarget()).setSrcRoots(ImmutableList.of()).build(ruleResolver2);
    // Verify that the correct source folders are created.
    JavaPackageFinder javaPackageFinder = EasyMock.createMock(JavaPackageFinder.class);
    EasyMock.expect(javaPackageFinder.findJavaPackage(Paths.get("java/com/example/base/module_java_com_example_base.iml"))).andReturn("com.example.base");
    EasyMock.replay(javaPackageFinder);
    ProjectWithModules projectWithModules2 = getModulesForActionGraph(ruleResolver2, ImmutableSortedSet.of(inPackageProjectConfig), javaPackageFinder, null);
    EasyMock.verify(javaPackageFinder);
    assertEquals(1, projectWithModules2.modules.size());
    SerializableModule moduleWithPackagePrefix = projectWithModules2.modules.get(0);
    assertListEquals("The current directory should be a source tmp with a package prefix " + "as well as the gen/ directory.", ImmutableList.of(new SourceFolder("file://$MODULE_DIR$", false, /* isTestSource */
    "com.example.base"), SerializableModule.SourceFolder.GEN), moduleWithPackagePrefix.sourceFolders);
    // Create a project_config() with src_roots=['src'].
    BuildRuleResolver ruleResolver3 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildRule baseBuildRule3 = AndroidLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:base")).build(ruleResolver3);
    ProjectConfig hasSrcFolderProjectConfig = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:project_config")).setSrcRule(baseBuildRule3.getBuildTarget()).setSrcRoots(ImmutableList.of("src")).build(ruleResolver3);
    ProjectWithModules projectWithModules3 = getModulesForActionGraph(ruleResolver3, ImmutableSortedSet.of(hasSrcFolderProjectConfig), null, /* javaPackageFinder */
    null);
    // Verify that the correct source folders are created.
    assertEquals(1, projectWithModules3.modules.size());
    SerializableModule moduleHasSrcFolder = projectWithModules3.modules.get(0);
    assertListEquals("Both src/ and gen/ should be source folders.", ImmutableList.of(new SourceFolder("file://$MODULE_DIR$/src", false), SerializableModule.SourceFolder.GEN), moduleHasSrcFolder.sourceFolders);
}
Also used : ProjectConfig(com.facebook.buck.rules.ProjectConfig) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) FakeJavaPackageFinder(com.facebook.buck.jvm.java.FakeJavaPackageFinder) SourceFolder(com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 2 with SourceFolder

use of com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder in project buck by facebook.

the class ProjectTest method testProject.

/**
   * This is an important test that verifies that the {@code no_dx} argument for an
   * {@code android_binary} is handled appropriately when generating an IntelliJ project.
   */
@Test
public void testProject() throws Exception {
    JavaPackageFinder javaPackageFinder = EasyMock.createMock(JavaPackageFinder.class);
    EasyMock.expect(javaPackageFinder.findJavaPackage(Paths.get("foo/module_foo.iml"))).andReturn("");
    EasyMock.expect(javaPackageFinder.findJavaPackage(Paths.get("bar/module_bar.iml"))).andReturn("");
    EasyMock.replay(javaPackageFinder);
    Pair<ProjectWithModules, BuildRuleResolver> projectWithModules = createActionGraphForTesting(javaPackageFinder);
    Project project = projectWithModules.getFirst().project;
    BuildRuleResolver resolver = projectWithModules.getSecond();
    List<SerializableModule> modules = projectWithModules.getFirst().modules;
    assertEquals("Should be one module for the java_library, one for the android_library, " + "one module for the android_resource, and one for each android_binary", 5, modules.size());
    // Check the values of the module that corresponds to the android_library.
    SerializableModule javaLibraryModule = modules.get(4);
    assertSame(getRuleByBuildTarget("//java/src/com/facebook/exportlib:exportlib", resolver), javaLibraryModule.srcRule);
    assertEquals("module_java_src_com_facebook_exportlib", javaLibraryModule.name);
    assertEquals(Paths.get("java/src/com/facebook/exportlib/module_java_src_com_facebook_exportlib.iml"), javaLibraryModule.pathToImlFile);
    assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.SRC), javaLibraryModule.sourceFolders);
    // Check the dependencies.
    SerializableDependentModule inheritedJdk = SerializableDependentModule.newInheritedJdk();
    SerializableDependentModule guavaAsProvidedDep = SerializableDependentModule.newLibrary(guava.getBuildTarget(), "buck_out_gen_third_party_guava___guava___guava_10_0_1_jar");
    guavaAsProvidedDep.scope = "PROVIDED";
    assertListEquals(ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsProvidedDep, SerializableDependentModule.newStandardJdk(Optional.empty(), Optional.empty())), javaLibraryModule.getDependencies());
    // Check the values of the module that corresponds to the android_library.
    SerializableModule androidLibraryModule = modules.get(3);
    assertSame(getRuleByBuildTarget("//java/src/com/facebook/base:base", resolver), androidLibraryModule.srcRule);
    assertEquals("module_java_src_com_facebook_base", androidLibraryModule.name);
    assertEquals(Paths.get("java/src/com/facebook/base/module_java_src_com_facebook_base.iml"), androidLibraryModule.pathToImlFile);
    assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.SRC, new SourceFolder("file://$MODULE_DIR$/src-gen", false), SerializableModule.SourceFolder.GEN), androidLibraryModule.sourceFolders);
    assertTrue(androidLibraryModule.hasAndroidFacet);
    assertTrue(androidLibraryModule.isAndroidLibraryProject);
    assertEquals(null, androidLibraryModule.proguardConfigPath);
    assertEquals(null, androidLibraryModule.resFolder);
    // Check the dependencies.
    SerializableDependentModule androidResourceAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//android_res/base:res"), "module_android_res_base");
    SerializableDependentModule childAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//java/src/com/facebook/child:child"), "module_java_src_com_facebook_child");
    SerializableDependentModule exportDepsAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//java/src/com/facebook/exportlib:exportlib"), "module_java_src_com_facebook_exportlib");
    assertListEquals(ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsProvidedDep, androidResourceAsProvidedDep, childAsProvidedDep, exportDepsAsProvidedDep, inheritedJdk), androidLibraryModule.getDependencies());
    // Check the values of the module that corresponds to the android_binary that uses no_dx.
    SerializableModule androidResourceModule = modules.get(0);
    assertSame(getRuleByBuildTarget("//android_res/base:res", resolver), androidResourceModule.srcRule);
    assertEquals(Paths.get("res"), androidResourceModule.resFolder);
    // Check the values of the module that corresponds to the android_binary that uses no_dx.
    SerializableModule androidBinaryModuleNoDx = modules.get(2);
    assertSame(getRuleByBuildTarget("//foo:app", resolver), androidBinaryModuleNoDx.srcRule);
    assertEquals("module_foo", androidBinaryModuleNoDx.name);
    assertEquals(Paths.get("foo/module_foo.iml"), androidBinaryModuleNoDx.pathToImlFile);
    assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.GEN), androidBinaryModuleNoDx.sourceFolders);
    assertTrue(androidBinaryModuleNoDx.hasAndroidFacet);
    assertFalse(androidBinaryModuleNoDx.isAndroidLibraryProject);
    assertEquals(null, androidBinaryModuleNoDx.proguardConfigPath);
    assertEquals(null, androidBinaryModuleNoDx.resFolder);
    assertEquals(Paths.get("../keystore/debug.keystore"), androidBinaryModuleNoDx.keystorePath);
    // Check the moduleDependencies.
    SerializableDependentModule grandchildAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//java/src/com/facebook/grandchild:grandchild"), "module_java_src_com_facebook_grandchild");
    SerializableDependentModule androidLibraryDep = SerializableDependentModule.newModule(androidLibraryModule.srcRule.getBuildTarget(), "module_java_src_com_facebook_base");
    assertEquals(ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsProvidedDep, androidLibraryDep, androidResourceAsProvidedDep, childAsProvidedDep, exportDepsAsProvidedDep, grandchildAsProvidedDep, inheritedJdk), androidBinaryModuleNoDx.getDependencies());
    // Check the values of the module that corresponds to the android_binary with an empty no_dx.
    SerializableModule androidBinaryModuleEmptyNoDx = modules.get(1);
    assertSame(getRuleByBuildTarget("//bar:app", resolver), androidBinaryModuleEmptyNoDx.srcRule);
    assertEquals("module_bar", androidBinaryModuleEmptyNoDx.name);
    assertEquals(Paths.get("bar/module_bar.iml"), androidBinaryModuleEmptyNoDx.pathToImlFile);
    assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.GEN), androidBinaryModuleEmptyNoDx.sourceFolders);
    assertTrue(androidBinaryModuleEmptyNoDx.hasAndroidFacet);
    assertFalse(androidBinaryModuleEmptyNoDx.isAndroidLibraryProject);
    assertEquals(null, androidBinaryModuleEmptyNoDx.proguardConfigPath);
    assertEquals(null, androidBinaryModuleEmptyNoDx.resFolder);
    assertEquals(Paths.get("../keystore/debug.keystore"), androidBinaryModuleEmptyNoDx.keystorePath);
    // Check the moduleDependencies.
    SerializableDependentModule guavaAsCompiledDep = SerializableDependentModule.newLibrary(guava.getBuildTarget(), "buck_out_gen_third_party_guava___guava___guava_10_0_1_jar");
    assertEquals("Important that Guava is listed as a 'COMPILED' dependency here because it is " + "only listed as a 'PROVIDED' dependency earlier.", ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsCompiledDep, androidLibraryDep, androidResourceAsProvidedDep, childAsProvidedDep, exportDepsAsProvidedDep, grandchildAsProvidedDep, inheritedJdk), androidBinaryModuleEmptyNoDx.getDependencies());
    // Check that the correct data was extracted to populate the .idea/libraries directory.
    BuildRule guava = getRuleByBuildTarget("//third_party/guava:guava", resolver);
    assertSame(guava, Iterables.getOnlyElement(project.getLibraryJars()));
}
Also used : JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) FakeJavaPackageFinder(com.facebook.buck.jvm.java.FakeJavaPackageFinder) SourceFolder(com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 3 with SourceFolder

use of com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder 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);
}
Also used : Path(java.nio.file.Path) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) ProjectConfig(com.facebook.buck.rules.ProjectConfig) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ObjectMappers(com.facebook.buck.util.ObjectMappers) Assert.assertThat(org.junit.Assert.assertThat) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) KeystoreBuilder(com.facebook.buck.jvm.java.KeystoreBuilder) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) InMemoryBuildFileTree(com.facebook.buck.model.InMemoryBuildFileTree) Pair(com.facebook.buck.model.Pair) Path(java.nio.file.Path) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) JavaLibraryBuilder(com.facebook.buck.jvm.java.JavaLibraryBuilder) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) AndroidLibraryBuilder(com.facebook.buck.android.AndroidLibraryBuilder) TargetGraph(com.facebook.buck.rules.TargetGraph) PathOrGlobMatcher(com.facebook.buck.io.PathOrGlobMatcher) BuildTarget(com.facebook.buck.model.BuildTarget) List(java.util.List) NdkLibraryBuilder(com.facebook.buck.android.NdkLibraryBuilder) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Iterables(com.google.common.collect.Iterables) FakeJavaPackageFinder(com.facebook.buck.jvm.java.FakeJavaPackageFinder) AndroidResourceBuilder(com.facebook.buck.android.AndroidResourceBuilder) JavaTestBuilder(com.facebook.buck.jvm.java.JavaTestBuilder) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) BuildRule(com.facebook.buck.rules.BuildRule) ExecutionContext(com.facebook.buck.step.ExecutionContext) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ProjectConfigBuilder(com.facebook.buck.rules.ProjectConfigBuilder) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) AndroidBinaryBuilder(com.facebook.buck.android.AndroidBinaryBuilder) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) OutputStream(java.io.OutputStream) Properties(java.util.Properties) TargetNode(com.facebook.buck.rules.TargetNode) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) EasyMock(org.easymock.EasyMock) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) MorePaths(com.facebook.buck.io.MorePaths) Paths(java.nio.file.Paths) TargetGraphFactory(com.facebook.buck.testutil.TargetGraphFactory) MoreAsserts.assertListEquals(com.facebook.buck.testutil.MoreAsserts.assertListEquals) Preconditions(com.google.common.base.Preconditions) NdkLibrary(com.facebook.buck.android.NdkLibrary) PrebuiltJarBuilder(com.facebook.buck.jvm.java.PrebuiltJarBuilder) Assert.assertEquals(org.junit.Assert.assertEquals) SourceFolder(com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) SourceFolder(com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder) BuildTarget(com.facebook.buck.model.BuildTarget) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PathOrGlobMatcher(com.facebook.buck.io.PathOrGlobMatcher) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

JavaPackageFinder (com.facebook.buck.jvm.core.JavaPackageFinder)3 FakeJavaPackageFinder (com.facebook.buck.jvm.java.FakeJavaPackageFinder)3 SourceFolder (com.facebook.buck.jvm.java.intellij.SerializableModule.SourceFolder)3 BuildRule (com.facebook.buck.rules.BuildRule)3 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)3 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)3 Test (org.junit.Test)3 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)2 ProjectConfig (com.facebook.buck.rules.ProjectConfig)2 AndroidBinaryBuilder (com.facebook.buck.android.AndroidBinaryBuilder)1 AndroidLibraryBuilder (com.facebook.buck.android.AndroidLibraryBuilder)1 AndroidResourceBuilder (com.facebook.buck.android.AndroidResourceBuilder)1 NdkLibrary (com.facebook.buck.android.NdkLibrary)1 NdkLibraryBuilder (com.facebook.buck.android.NdkLibraryBuilder)1 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)1 MorePaths (com.facebook.buck.io.MorePaths)1 PathOrGlobMatcher (com.facebook.buck.io.PathOrGlobMatcher)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 JavaLibraryBuilder (com.facebook.buck.jvm.java.JavaLibraryBuilder)1 JavaTestBuilder (com.facebook.buck.jvm.java.JavaTestBuilder)1