com.intellij.openapi.roots.SourceFolder Java Examples

The following examples show how to use com.intellij.openapi.roots.SourceFolder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OCamlBinaryRootEditHandler.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public JComponent createPropertiesEditor(@NotNull final SourceFolder folder,
                                         @NotNull final JComponent parentComponent,
                                         @NotNull final ContentRootPanel.ActionCallback callback) {
    final IconActionComponent iconComponent = new IconActionComponent(AllIcons.General.Inline_edit,
            AllIcons.General.Inline_edit_hovered,
            ProjectBundle.message("module.paths.edit.properties.tooltip"),
            () -> {
                OCamlBinaryRootProperties properties = folder.getJpsElement().getProperties(OCamlBinaryRootType.BINARY);
                assert properties != null;
                OCamlBinaryRootEditHandler.ResourceRootPropertiesDialog
                        dialog = new OCamlBinaryRootEditHandler.ResourceRootPropertiesDialog(parentComponent, properties);
                if (dialog.showAndGet()) {
                    callback.onSourceRootPropertiesChanged(folder);
                }
            });
    final JPanel panel = new JPanel(new BorderLayout());
    panel.setOpaque(false);
    panel.add(iconComponent, BorderLayout.CENTER);
    panel.add(Box.createHorizontalStrut(3), BorderLayout.EAST);
    return panel;
}
 
Example #2
Source File: GenerateAction.java    From RIBs with Apache License 2.0 6 votes vote down vote up
private static SourceFolder suitableTestSourceFolders(Project project, Module module) {
  ContentEntry[] contentEntries = ModuleRootManager.getInstance(module).getContentEntries();
  for (ContentEntry contentEntry : contentEntries) {
    List<SourceFolder> testSourceFolders =
        contentEntry.getSourceFolders(JavaSourceRootType.TEST_SOURCE);
    for (SourceFolder testSourceFolder : testSourceFolders) {
      if (testSourceFolder.getFile() != null) {
        if (!JavaProjectRootsUtil.isInGeneratedCode(testSourceFolder.getFile(), project)) {
          return testSourceFolder;
        }
      }
    }
  }

  return null;
}
 
Example #3
Source File: OSSPantsScalaExamplesIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
public void testWelcomeTest() throws Throwable {
  doImport("examples/tests/scala/org/pantsbuild/example/hello/welcome");

  assertFirstSourcePartyModules(
    "examples_src_resources_org_pantsbuild_example_hello_hello",
    "examples_src_java_org_pantsbuild_example_hello_greet_greet",
    "examples_src_scala_org_pantsbuild_example_hello_welcome_welcome",
    "examples_tests_scala_org_pantsbuild_example_hello_welcome_welcome"
  );

  final ContentEntry[] contentRoots = getContentRoots("examples_tests_scala_org_pantsbuild_example_hello_welcome_welcome");
  assertSize(1, contentRoots);
  final List<SourceFolder> testSourceRoots = contentRoots[0].getSourceFolders(JavaSourceRootType.TEST_SOURCE);
  assertSize(1, testSourceRoots);

  assertPantsCompileExecutesAndSucceeds(pantsCompileProject());

  findClassAndAssert("org.pantsbuild.example.hello.welcome.WelSpec");
  assertScalaLibrary("examples_tests_scala_org_pantsbuild_example_hello_welcome_welcome");
}
 
Example #4
Source File: JavaSourceFolderProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public ImmutableMap<File, SourceFolder> initializeSourceFolders(ContentEntry contentEntry) {
  Map<File, SourceFolder> map = new HashMap<>();
  BlazeContentEntry javaContentEntry =
      blazeContentEntries.get(UrlUtil.urlToFile(contentEntry.getUrl()));
  if (javaContentEntry != null) {
    for (BlazeSourceDirectory sourceDirectory : javaContentEntry.sources) {
      File file = sourceDirectory.getDirectory();
      if (map.containsKey(file)) {
        continue;
      }
      SourceFolder sourceFolder = addSourceFolderToContentEntry(contentEntry, sourceDirectory);
      map.put(file, sourceFolder);
    }
  }
  return ImmutableMap.copyOf(map);
}
 
Example #5
Source File: JavaSourceFolderProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public SourceFolder setSourceFolderForLocation(
    ContentEntry contentEntry, SourceFolder parentFolder, File file, boolean isTestSource) {
  SourceFolder sourceFolder;
  if (isResource(parentFolder)) {
    JavaResourceRootType resourceRootType =
        isTestSource ? JavaResourceRootType.TEST_RESOURCE : JavaResourceRootType.RESOURCE;
    sourceFolder =
        contentEntry.addSourceFolder(UrlUtil.pathToUrl(file.getPath()), resourceRootType);
  } else {
    sourceFolder = contentEntry.addSourceFolder(UrlUtil.pathToUrl(file.getPath()), isTestSource);
  }
  sourceFolder.setPackagePrefix(derivePackagePrefix(file, parentFolder));
  JpsModuleSourceRoot sourceRoot = sourceFolder.getJpsElement();
  JpsElement properties = sourceRoot.getProperties();
  if (properties instanceof JavaSourceRootProperties) {
    ((JavaSourceRootProperties) properties).setForGeneratedSources(isGenerated(parentFolder));
  }
  return sourceFolder;
}
 
Example #6
Source File: PantsProjectCacheTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private void cleanUpsourceFolders() {
  ApplicationManager.getApplication().runWriteAction(
    () -> {
      final ModifiableRootModel modifiableRootModel = ModuleRootManager.getInstance(getModule()).getModifiableModel();
      for (ContentEntry contentEntry : modifiableRootModel.getContentEntries()) {
        final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
        for (SourceFolder sourceFolder : sourceFolders) {
          contentEntry.removeSourceFolder(sourceFolder);
        }
      }
      modifiableRootModel.commit();
    }
  );
}
 
Example #7
Source File: MavenImportingTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertGeneratedSources(String moduleName, String... expectedSources) {
  ContentEntry contentRoot = getContentRoot(moduleName);
  List<ContentFolder> folders = new ArrayList<>();
  for (SourceFolder folder : contentRoot.getSourceFolders(JavaSourceRootType.SOURCE)) {
    JavaSourceRootProperties properties = folder.getJpsElement().getProperties(JavaSourceRootType.SOURCE);
    assertNotNull(properties);
    if (properties.isForGeneratedSources()) {
      folders.add(folder);
    }
  }
  doAssertContentFolders(contentRoot, folders, expectedSources);
}
 
Example #8
Source File: ResolveIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private void assertContainsTestSourceContentEntry(Module module) {
  assertTrue(
    String.format("'%s' does not contain any test source content entry.", module.getName()),
    // flatten all source folders in all module's content entries, then check for test source.
    Arrays.stream(ModuleRootManager.getInstance(module).getContentEntries())
      .flatMap(contentEntry -> Arrays.stream(contentEntry.getSourceFolders()))
      .anyMatch(SourceFolder::isTestSource)
  );
}
 
Example #9
Source File: JavascriptSyncTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static SourceFolder findSourceFolder(ContentEntry entry, VirtualFile file) {
  for (SourceFolder sourceFolder : entry.getSourceFolders()) {
    if (file.equals(sourceFolder.getFile())) {
      return sourceFolder;
    }
  }
  return null;
}
 
Example #10
Source File: JavascriptSyncTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testTestSourcesMissingFromDirectoriesSectionAreAdded() {
  TestUtils.setPlatformPrefix(getTestRootDisposable(), PlatformUtils.IDEA_PREFIX);
  setProjectView(
      "directories:",
      "  common/jslayout",
      "targets:",
      "  //common/jslayout/...:all",
      "test_sources:",
      "  */tests",
      "workspace_type: javascript");

  VirtualFile testDir = workspace.createDirectory(new WorkspacePath("common/jslayout/tests"));

  BlazeSyncParams syncParams =
      BlazeSyncParams.builder()
          .setTitle("Full Sync")
          .setSyncMode(SyncMode.FULL)
          .setSyncOrigin("test")
          .setBlazeBuildParams(BlazeBuildParams.fromProject(getProject()))
          .setAddProjectViewTargets(true)
          .build();
  runBlazeSync(syncParams);

  errorCollector.assertNoIssues();

  ImmutableList<ContentEntry> contentEntries = getWorkspaceContentEntries();
  assertThat(contentEntries).hasSize(1);

  SourceFolder root = findSourceFolder(contentEntries.get(0), testDir.getParent());
  assertThat(root.isTestSource()).isFalse();

  SourceFolder testRoot = findSourceFolder(contentEntries.get(0), testDir);
  assertThat(testRoot).isNotNull();
  assertThat(testRoot.isTestSource()).isTrue();
}
 
Example #11
Source File: JavaSyncTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static SourceFolder findSourceFolder(ContentEntry entry, VirtualFile file) {
  for (SourceFolder sourceFolder : entry.getSourceFolders()) {
    if (file.equals(sourceFolder.getFile())) {
      return sourceFolder;
    }
  }
  return null;
}
 
Example #12
Source File: JavaSyncTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testTestSourceRelativePackagePrefixCalculation() {
  setProjectView(
      "directories:",
      "  java/com/google",
      "targets:",
      "  //java/com/google:lib",
      "test_sources:",
      "  java/com/google/tests/*");

  VirtualFile javatestsRoot =
      workspace.createDirectory(new WorkspacePath("java/com/google/tests"));

  BlazeSyncParams syncParams =
      BlazeSyncParams.builder()
          .setTitle("Full Sync")
          .setSyncMode(SyncMode.FULL)
          .setSyncOrigin("test")
          .setBlazeBuildParams(BlazeBuildParams.fromProject(getProject()))
          .setAddProjectViewTargets(true)
          .build();
  runBlazeSync(syncParams);

  errorCollector.assertNoIssues();

  ContentEntry root = findContentEntry(javatestsRoot.getParent());
  SourceFolder rootSource = findSourceFolder(root, javatestsRoot.getParent());
  assertThat(rootSource.isTestSource()).isFalse();
  assertThat(rootSource.getPackagePrefix()).isEqualTo("com.google");

  SourceFolder childTestSource = findSourceFolder(root, javatestsRoot);
  assertThat(childTestSource.isTestSource()).isTrue();
  assertThat(childTestSource.getPackagePrefix()).isEqualTo("com.google.tests");
}
 
Example #13
Source File: JavaSyncTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testExistingPackagePrefixRetainedForTestSources() {
  setProjectView(
      "directories:",
      "  java/com/google",
      "  javatests/com/google",
      "targets:",
      "  //java/com/google:lib",
      "test_sources:",
      "  javatests/*");

  workspace.createDirectory(new WorkspacePath("java/com/google"));
  VirtualFile javatestsRoot =
      workspace.createDirectory(new WorkspacePath("javatests/com/google"));

  BlazeSyncParams syncParams =
      BlazeSyncParams.builder()
          .setTitle("Full Sync")
          .setSyncMode(SyncMode.FULL)
          .setSyncOrigin("test")
          .setBlazeBuildParams(BlazeBuildParams.fromProject(getProject()))
          .setAddProjectViewTargets(true)
          .build();
  runBlazeSync(syncParams);

  errorCollector.assertNoIssues();

  ContentEntry testRoot = findContentEntry(javatestsRoot);
  SourceFolder testRootSource = findSourceFolder(testRoot, javatestsRoot);
  assertThat(testRootSource.isTestSource()).isTrue();
  assertThat(testRootSource.getPackagePrefix()).isEqualTo("com.google");
}
 
Example #14
Source File: JavaSourceFolderProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static SourceFolder addSourceFolderToContentEntry(
    ContentEntry contentEntry, BlazeSourceDirectory sourceDirectory) {
  File sourceDir = sourceDirectory.getDirectory();

  // Create the source folder
  SourceFolder sourceFolder;
  if (sourceDirectory.isResource()) {
    sourceFolder =
        contentEntry.addSourceFolder(
            UrlUtil.pathToUrl(sourceDir.getPath()), JavaResourceRootType.RESOURCE);
  } else {
    sourceFolder = contentEntry.addSourceFolder(UrlUtil.pathToUrl(sourceDir.getPath()), false);
  }
  JpsModuleSourceRoot sourceRoot = sourceFolder.getJpsElement();
  JpsElement properties = sourceRoot.getProperties();
  if (properties instanceof JavaSourceRootProperties) {
    JavaSourceRootProperties rootProperties = (JavaSourceRootProperties) properties;
    if (sourceDirectory.isGenerated()) {
      rootProperties.setForGeneratedSources(true);
    }
  }
  String packagePrefix = sourceDirectory.getPackagePrefix();
  if (!Strings.isNullOrEmpty(packagePrefix)) {
    sourceFolder.setPackagePrefix(packagePrefix);
  }
  return sourceFolder;
}
 
Example #15
Source File: JavaSourceFolderProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static String derivePackagePrefix(File file, SourceFolder parentFolder) {
  String parentPackagePrefix = parentFolder.getPackagePrefix();
  String parentPath = VirtualFileManager.extractPath(parentFolder.getUrl());
  String relativePath =
      FileUtil.toCanonicalPath(
          FileUtil.getRelativePath(parentPath, file.getPath(), File.separatorChar));
  if (Strings.isNullOrEmpty(relativePath)) {
    return parentPackagePrefix;
  }
  // FileUtil.toCanonicalPath already replaces File.separatorChar with '/'
  relativePath = relativePath.replace('/', '.');
  return Strings.isNullOrEmpty(parentPackagePrefix)
      ? relativePath
      : parentPackagePrefix + "." + relativePath;
}
 
Example #16
Source File: ContentEntryEditor.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void walkFileSystem(
    WorkspaceRoot workspaceRoot,
    SourceTestConfig testConfig,
    Collection<WorkspacePath> excludedDirectories,
    ContentEntry contentEntry,
    SourceFolderProvider provider,
    ImmutableMap<File, SourceFolder> sourceFolders,
    @Nullable SourceFolder parent,
    WorkspacePath workspacePath,
    DirectoryStructure directoryStructure) {
  if (excludedDirectories.contains(workspacePath)) {
    return;
  }
  File file = workspaceRoot.fileForPath(workspacePath);
  boolean isTest = testConfig.isTestSource(workspacePath.relativePath());
  SourceFolder current = sourceFolders.get(new File(file.getPath()));
  SourceFolder currentOrParent = current != null ? current : parent;
  if (currentOrParent != null && isTest != currentOrParent.isTestSource()) {
    currentOrParent =
        provider.setSourceFolderForLocation(contentEntry, currentOrParent, file, isTest);
    if (current != null) {
      contentEntry.removeSourceFolder(current);
    }
  }
  for (Map.Entry<WorkspacePath, DirectoryStructure> child :
      directoryStructure.directories.entrySet()) {
    walkFileSystem(
        workspaceRoot,
        testConfig,
        excludedDirectories,
        contentEntry,
        provider,
        sourceFolders,
        currentOrParent,
        child.getKey(),
        child.getValue());
  }
}
 
Example #17
Source File: ResourceModuleContentRootCustomizer.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void setupSampleDataContentRoot(
    @NotNull ModifiableRootModel model, @NotNull SourceFolder resSourceFolder) {
  VirtualFile resFolder = resSourceFolder.getFile();
  if (resFolder == null) {
    return;
  }

  VirtualFile sampleDataDirectory =
      BlazeSampleDataDirectoryProvider.getSampleDataDirectoryForResFolder(resFolder);
  if (sampleDataDirectory != null && sampleDataDirectory.exists()) {
    model.addContentEntry(sampleDataDirectory);
  }
}
 
Example #18
Source File: JavaSourceFolderProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static boolean isGenerated(SourceFolder folder) {
  JpsElement properties = folder.getJpsElement().getProperties();
  return properties instanceof JavaSourceRootProperties
      && ((JavaSourceRootProperties) properties).isForGeneratedSources();
}
 
Example #19
Source File: JavaSourceFolderProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static boolean isResource(SourceFolder folder) {
  return folder.getRootType() instanceof JavaResourceRootType;
}
 
Example #20
Source File: JavaSyncTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testNestedTestSourcesAreAdded() {
  setProjectView(
      "directories:",
      "  java/com/google",
      "targets:",
      "  //java/com/google:lib",
      "test_sources:",
      "  java/com/google/tests/*",
      "  java/com/google/moretests");

  workspace.createDirectory(new WorkspacePath("java/com/google"));

  VirtualFile testFile =
      workspace.createFile(new WorkspacePath("java/com/google/tests/ExampleTest.java"));
  VirtualFile moreTestsFile =
      workspace.createFile(
          new WorkspacePath("java/com/google/moretests/AnotherExampleTest.java"));

  BlazeSyncParams syncParams =
      BlazeSyncParams.builder()
          .setTitle("Full Sync")
          .setSyncMode(SyncMode.FULL)
          .setSyncOrigin("test")
          .setBlazeBuildParams(BlazeBuildParams.fromProject(getProject()))
          .setAddProjectViewTargets(true)
          .build();
  runBlazeSync(syncParams);

  errorCollector.assertNoIssues();

  ImmutableList<ContentEntry> contentEntries = getWorkspaceContentEntries();
  assertThat(contentEntries).hasSize(1);

  SourceFolder testRoot = findSourceFolder(contentEntries.get(0), testFile.getParent());
  SourceFolder moreTestsRoot = findSourceFolder(contentEntries.get(0), moreTestsFile.getParent());

  assertThat(testRoot).isNotNull();
  assertThat(testRoot.isTestSource()).isTrue();
  assertThat(testRoot.getPackagePrefix()).isEqualTo("com.google.tests");

  assertThat(moreTestsRoot).isNotNull();
  assertThat(moreTestsRoot.isTestSource()).isTrue();
  assertThat(moreTestsRoot.getPackagePrefix()).isEqualTo("com.google.moretests");
}
 
Example #21
Source File: ContentEntryEditor.java    From intellij with Apache License 2.0 4 votes vote down vote up
public static void createContentEntries(
    Project project,
    WorkspaceRoot workspaceRoot,
    ProjectViewSet projectViewSet,
    BlazeProjectData blazeProjectData,
    DirectoryStructure rootDirectoryStructure,
    ModifiableRootModel modifiableRootModel) {
  ImportRoots importRoots =
      ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project))
          .add(projectViewSet)
          .build();
  Collection<WorkspacePath> rootDirectories = importRoots.rootDirectories();
  Collection<WorkspacePath> excludeDirectories = importRoots.excludeDirectories();
  Multimap<WorkspacePath, WorkspacePath> excludesByRootDirectory =
      sortExcludesByRootDirectory(rootDirectories, excludeDirectories);

  SourceTestConfig testConfig = new SourceTestConfig(projectViewSet);
  SourceFolderProvider provider = SourceFolderProvider.getSourceFolderProvider(blazeProjectData);

  for (WorkspacePath rootDirectory : rootDirectories) {
    File rootFile = workspaceRoot.fileForPath(rootDirectory);
    ContentEntry contentEntry =
        modifiableRootModel.addContentEntry(UrlUtil.pathToUrl(rootFile.getPath()));

    for (WorkspacePath exclude : excludesByRootDirectory.get(rootDirectory)) {
      File excludeFolder = workspaceRoot.fileForPath(exclude);
      contentEntry.addExcludeFolder(UrlUtil.fileToIdeaUrl(excludeFolder));
    }

    ImmutableMap<File, SourceFolder> sourceFolders =
        provider.initializeSourceFolders(contentEntry);
    SourceFolder rootSource = sourceFolders.get(rootFile);
    walkFileSystem(
        workspaceRoot,
        testConfig,
        excludesByRootDirectory.get(rootDirectory),
        contentEntry,
        provider,
        sourceFolders,
        rootSource,
        rootDirectory,
        rootDirectoryStructure.directories.get(rootDirectory));
  }
}
 
Example #22
Source File: JavaSourceFolderProviderTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testInitializeSourceFolders() {
  ImmutableList<BlazeContentEntry> contentEntries =
      ImmutableList.of(
          BlazeContentEntry.builder("/src/workspace/java/apps")
              .addSource(
                  BlazeSourceDirectory.builder("/src/workspace/java/apps")
                      .setPackagePrefix("apps")
                      .build())
              .addSource(
                  BlazeSourceDirectory.builder("/src/workspace/java/apps/gen")
                      .setPackagePrefix("apps.gen")
                      .setGenerated(true)
                      .build())
              .addSource(
                  BlazeSourceDirectory.builder("/src/workspace/java/apps/resources")
                      .setPackagePrefix("apps.resources")
                      .setResource(true)
                      .build())
              .build(),
          BlazeContentEntry.builder("/src/workspace/javatests/apps/example")
              .addSource(
                  BlazeSourceDirectory.builder("/src/workspace/javatests/apps/example")
                      .setPackagePrefix("apps.example")
                      .build())
              .build());

  JavaSourceFolderProvider provider =
      new JavaSourceFolderProvider(
          new BlazeJavaSyncData(
              BlazeJavaImportResult.builder()
                  .setContentEntries(contentEntries)
                  .setLibraries(ImmutableMap.of())
                  .setBuildOutputJars(ImmutableList.of())
                  .setJavaSourceFiles(ImmutableSet.of())
                  .setSourceVersion(null)
                  .setEmptyJarTracker(EmptyJarTracker.builder().build())
                  .build(),
              new GlobSet(ImmutableList.of())));

  VirtualFile root = workspace.createDirectory(new WorkspacePath("java/apps"));
  VirtualFile gen = workspace.createDirectory(new WorkspacePath("java/apps/gen"));
  VirtualFile res = workspace.createDirectory(new WorkspacePath("java/apps/resources"));

  ImmutableMap<File, SourceFolder> sourceFolders =
      provider.initializeSourceFolders(getContentEntry(root));
  assertThat(sourceFolders).hasSize(3);

  SourceFolder rootSource = sourceFolders.get(new File(root.getPath()));
  assertThat(rootSource.getPackagePrefix()).isEqualTo("apps");
  assertThat(JavaSourceFolderProvider.isGenerated(rootSource)).isFalse();
  assertThat(JavaSourceFolderProvider.isResource(rootSource)).isFalse();

  SourceFolder genSource = sourceFolders.get(new File(gen.getPath()));
  assertThat(genSource.getPackagePrefix()).isEqualTo("apps.gen");
  assertThat(JavaSourceFolderProvider.isGenerated(genSource)).isTrue();
  assertThat(JavaSourceFolderProvider.isResource(genSource)).isFalse();

  SourceFolder resSource = sourceFolders.get(new File(res.getPath()));
  assertThat(JavaSourceFolderProvider.isGenerated(resSource)).isFalse();
  assertThat(JavaSourceFolderProvider.isResource(resSource)).isTrue();

  VirtualFile testRoot = workspace.createDirectory(new WorkspacePath("javatests/apps/example"));
  sourceFolders = provider.initializeSourceFolders(getContentEntry(testRoot));

  assertThat(sourceFolders).hasSize(1);
  assertThat(sourceFolders.get(new File(testRoot.getPath())).getPackagePrefix())
      .isEqualTo("apps.example");
}
 
Example #23
Source File: JavaSourceFolderProviderTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testRelativePackagePrefix() {
  ImmutableList<BlazeContentEntry> contentEntries =
      ImmutableList.of(
          BlazeContentEntry.builder("/src/workspace/java/apps")
              .addSource(
                  BlazeSourceDirectory.builder("/src/workspace/java/apps")
                      .setPackagePrefix("apps")
                      .build())
              .build());

  JavaSourceFolderProvider provider =
      new JavaSourceFolderProvider(
          new BlazeJavaSyncData(
              BlazeJavaImportResult.builder()
                  .setContentEntries(contentEntries)
                  .setLibraries(ImmutableMap.of())
                  .setBuildOutputJars(ImmutableList.of())
                  .setJavaSourceFiles(ImmutableSet.of())
                  .setSourceVersion(null)
                  .setEmptyJarTracker(EmptyJarTracker.builder().build())
                  .build(),
              new GlobSet(ImmutableList.of())));

  VirtualFile root = workspace.createDirectory(new WorkspacePath("java/apps"));
  ContentEntry contentEntry = getContentEntry(root);

  ImmutableMap<File, SourceFolder> sourceFolders = provider.initializeSourceFolders(contentEntry);
  assertThat(sourceFolders).hasSize(1);

  VirtualFile testRoot = workspace.createDirectory(new WorkspacePath("java/apps/tests/model"));

  SourceFolder testSourceChild =
      provider.setSourceFolderForLocation(
          contentEntry,
          sourceFolders.get(new File(root.getPath())),
          new File(testRoot.getPath()),
          true);
  assertThat(testSourceChild.isTestSource()).isTrue();
  assertThat(testSourceChild.getPackagePrefix()).isEqualTo("apps.tests.model");
}
 
Example #24
Source File: JavaSourceFolderProviderTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testRelativePackagePrefixWithoutParentPrefix() {
  ImmutableList<BlazeContentEntry> contentEntries =
      ImmutableList.of(
          BlazeContentEntry.builder("/src/workspace/java")
              .addSource(
                  BlazeSourceDirectory.builder("/src/workspace/java")
                      .setPackagePrefix("")
                      .build())
              .build());

  JavaSourceFolderProvider provider =
      new JavaSourceFolderProvider(
          new BlazeJavaSyncData(
              BlazeJavaImportResult.builder()
                  .setContentEntries(contentEntries)
                  .setLibraries(ImmutableMap.of())
                  .setBuildOutputJars(ImmutableList.of())
                  .setJavaSourceFiles(ImmutableSet.of())
                  .setSourceVersion(null)
                  .setEmptyJarTracker(EmptyJarTracker.builder().build())
                  .build(),
              new GlobSet(ImmutableList.of())));

  VirtualFile root = workspace.createDirectory(new WorkspacePath("java"));
  ContentEntry contentEntry = getContentEntry(root);

  ImmutableMap<File, SourceFolder> sourceFolders = provider.initializeSourceFolders(contentEntry);
  assertThat(sourceFolders).hasSize(1);

  VirtualFile testRoot = workspace.createDirectory(new WorkspacePath("java/apps/tests"));

  SourceFolder testSourceChild =
      provider.setSourceFolderForLocation(
          contentEntry,
          sourceFolders.get(new File(root.getPath())),
          new File(testRoot.getPath()),
          true);
  assertThat(testSourceChild.isTestSource()).isTrue();
  assertThat(testSourceChild.getPackagePrefix()).isEqualTo("apps.tests");
}
 
Example #25
Source File: JavascriptSyncTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimpleTestSourcesIdentified() {
  TestUtils.setPlatformPrefix(getTestRootDisposable(), PlatformUtils.IDEA_PREFIX);
  setProjectView(
      "directories:",
      "  common/jslayout/calendar",
      "  common/jslayout/tests",
      "targets:",
      "  //common/jslayout/...:all",
      "test_sources:",
      "  common/jslayout/tests",
      "workspace_type: javascript");

  VirtualFile rootFile = workspace.createDirectory(new WorkspacePath("common/jslayout/calendar"));

  VirtualFile testFile =
      workspace.createFile(new WorkspacePath("common/jslayout/tests/date_formatter_test.js"));

  BlazeSyncParams syncParams =
      BlazeSyncParams.builder()
          .setTitle("Full Sync")
          .setSyncMode(SyncMode.FULL)
          .setSyncOrigin("test")
          .setBlazeBuildParams(BlazeBuildParams.fromProject(getProject()))
          .setAddProjectViewTargets(true)
          .build();
  runBlazeSync(syncParams);

  errorCollector.assertNoIssues();

  assertThat(getWorkspaceContentEntries()).hasSize(2);

  ContentEntry sourceEntry = findContentEntry(rootFile);
  assertThat(sourceEntry.getSourceFolders()).hasLength(1);

  SourceFolder nonTestSource = findSourceFolder(sourceEntry, rootFile);
  assertThat(nonTestSource.isTestSource()).isFalse();

  ContentEntry testEntry = findContentEntry(testFile.getParent());
  assertThat(testEntry.getSourceFolders()).hasLength(1);

  SourceFolder testSource = findSourceFolder(testEntry, testFile.getParent());
  assertThat(testSource.isTestSource()).isTrue();
}
 
Example #26
Source File: GenericSourceFolderProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public SourceFolder setSourceFolderForLocation(
    ContentEntry contentEntry, SourceFolder parentFolder, File file, boolean isTestSource) {
  return contentEntry.addSourceFolder(UrlUtil.fileToIdeaUrl(file), isTestSource);
}
 
Example #27
Source File: JavascriptSyncTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testTestSourceChildrenAreNotAddedAsSourceFolders() {
  TestUtils.setPlatformPrefix(getTestRootDisposable(), PlatformUtils.IDEA_PREFIX);
  // child directories of test sources are always test sources, so they should never
  // appear as separate SourceFolders.
  setProjectView(
      "directories:",
      "  common/jslayout",
      "targets:",
      "  //common/jslayout/...:all",
      "test_sources:",
      "  */tests/*",
      "workspace_type: javascript");

  VirtualFile rootDir = workspace.createDirectory(new WorkspacePath("common/jslayout"));
  VirtualFile nestedTestDir =
      workspace.createDirectory(new WorkspacePath("common/jslayout/tests/foo"));

  BlazeSyncParams syncParams =
      BlazeSyncParams.builder()
          .setTitle("Full Sync")
          .setSyncMode(SyncMode.FULL)
          .setSyncOrigin("test")
          .setBlazeBuildParams(BlazeBuildParams.fromProject(getProject()))
          .setAddProjectViewTargets(true)
          .build();
  runBlazeSync(syncParams);

  errorCollector.assertNoIssues();

  ImmutableList<ContentEntry> contentEntries = getWorkspaceContentEntries();
  assertThat(contentEntries).hasSize(1);

  SourceFolder root = findSourceFolder(contentEntries.get(0), rootDir);
  assertThat(root.isTestSource()).isFalse();

  SourceFolder child = findSourceFolder(contentEntries.get(0), nestedTestDir);
  assertThat(child).isNull();

  SourceFolder testRoot = findSourceFolder(contentEntries.get(0), nestedTestDir.getParent());
  assertThat(testRoot).isNotNull();
  assertThat(testRoot.isTestSource()).isTrue();
}
 
Example #28
Source File: GenericSourceFolderProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableMap<File, SourceFolder> initializeSourceFolders(ContentEntry contentEntry) {
  String url = contentEntry.getUrl();
  return ImmutableMap.of(UrlUtil.urlToFile(url), contentEntry.addSourceFolder(url, false));
}
 
Example #29
Source File: SourceFolderProvider.java    From intellij with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the source folder for the given file, incorporating the test information as appropriate.
 */
SourceFolder setSourceFolderForLocation(
    ContentEntry contentEntry, SourceFolder parentFolder, File file, boolean isTestSource);
 
Example #30
Source File: SourceFolderProvider.java    From intellij with Apache License 2.0 2 votes vote down vote up
/**
 * Creates the initial source folders for the given {@link ContentEntry}. These source folders are
 * 'initial' because the 'is test' property (and potentially additional test source folders) are
 * added later.
 */
ImmutableMap<File, SourceFolder> initializeSourceFolders(ContentEntry contentEntry);