-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
114 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/com/twitter/intellij/pants/util/ProjectTestJvms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package com.twitter.intellij.pants.util; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.JavaPsiFacade; | ||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.search.GlobalSearchScope; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Stream; | ||
|
||
public final class ProjectTestJvms { | ||
private static final String PACKAGE_NAME = "org.pantsbuild.testproject.testjvms"; | ||
|
||
public static PsiClass anyTestClass(Project project, String projectPath) throws IOException { | ||
return testClasses(project, projectPath) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("Couldn't find a test class")); | ||
} | ||
|
||
public static Stream<PsiClass> testClasses(Project project, String projectPath) throws IOException { | ||
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); | ||
return testClassFiles(projectPath) | ||
.map(path -> path.getFileName().toString()) | ||
.map(name -> name.substring(0, name.length() - ".java".length())) | ||
.map(name -> PACKAGE_NAME + "." + name) | ||
.map(qualifiedName -> psiFacade.findClass(qualifiedName, GlobalSearchScope.allScope(project))); | ||
} | ||
|
||
private static Stream<Path> testClassFiles(String projectPath) throws IOException { | ||
return Files.list(Paths.get(projectPath)) | ||
.filter(path -> path.getFileName().toString().endsWith(".java")) | ||
.filter(path -> !path.getFileName().toString().equals("TestBase.java")); | ||
} | ||
} |