Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Borja Lorente committed Jan 25, 2024
1 parent 95d4364 commit 5136a16
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static Optional<JUnitVersion> jUnitVersion(PsiClass psiClass) {
} else if (JUnitUtil.isJUnit3TestClass(psiClass)) {
return Optional.of(JUnitVersion.JUNIT_3);
} else {
return Optional.empty();
// Fall back to Junit3
return Optional.of(JUnitVersion.JUNIT_3);
}
}

Expand Down Expand Up @@ -82,7 +83,7 @@ public boolean matchesSource(
File sourceFile,
@Nullable TestSize testSize) {
Optional<JUnitVersion> sourceVersion = junitVersion(sourcePsiFile);
if (sourceVersion == null || sourceVersion.isEmpty()) {
if (sourceVersion.isEmpty()) {
return false;
}
String targetName = target.label.targetName().toString().toLowerCase();
Expand All @@ -97,15 +98,13 @@ public boolean matchesSource(
return false;
}

@Nullable
private Optional<JUnitVersion> junitVersion(@Nullable PsiFile psiFile) {
if (!(psiFile instanceof PsiClassOwner)) {
return null;
return Optional.empty();
}
return ReadAction.compute(() -> junitVersion((PsiClassOwner) psiFile));
}

@Nullable
private Optional<JUnitVersion> junitVersion(PsiClassOwner classOwner) {
for (PsiClass psiClass : classOwner.getClasses()) {
Optional<JUnitVersion> version = jUnitVersion(psiClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static String testFilterForClassesAndMethods(
// java_test target they're run from.
Optional<JUnitVersion> version = JUnitTestHeuristic.jUnitVersion(methodsPerClass.keySet());
if (version.isEmpty()) {
return null;
return testFilterForClassesAndMethods(methodsPerClass, JUnitVersion.JUNIT_3);
}
return testFilterForClassesAndMethods(methodsPerClass, version.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;

import java.util.Arrays;
import java.util.List;
import org.junit.After;
Expand Down Expand Up @@ -88,6 +89,11 @@ public final void removeSourceFolder() {
final ModifiableRootModel model =
ModuleRootManager.getInstance(testFixture.getModule()).getModifiableModel();
ContentEntry contentEntry = model.getContentEntries()[0];
// There is a global content entry, `//src`, which is necessary to infer targets from test classes.
// This is set outside of these test (somewhere in the superclass) once per run, not once per test.
// If we just use contentEntry.clearSourceDirs() here, it won't be re-created, and tests that assume
// it exists will fail.
// Therefore, we only delete the content entries that this test has created.
Arrays.stream(contentEntry.getSourceFolders()).forEach((folder) -> {
if (folder.getFile().equals(pkgRoot)) {
contentEntry.removeSourceFolder(folder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,25 @@ public void testSingleJUnit3ClassFilter() {
}

@Test
public void testParameterizedIgnoredForSingleClass() {
public void testParameterizedIgnoredForSingleClassJUnit4() {
assertThat(
BlazeJUnitTestFilterFlags.testFilterForClassAndMethods(
"com.google.idea.ClassName", JUnitVersion.JUNIT_4, ImmutableList.of(), null))
.isEqualTo("com.google.idea.ClassName#");
"com.google.idea.ClassName",
JUnitVersion.JUNIT_4,
ImmutableList.of(),
ParameterizedTestInfo.create("ignored", "-ignored")))
.isEqualTo("com.google.idea.ClassName#");
}

@Test
public void testParameterizedIgnoredForSingleClassJUnit5() {
assertThat(
BlazeJUnitTestFilterFlags.testFilterForClassAndMethods(
"com.google.idea.ClassName", JUnitVersion.JUNIT_5, ImmutableList.of(), null))
.isEqualTo("com.google.idea.ClassName#");
BlazeJUnitTestFilterFlags.testFilterForClassAndMethods(
"com.google.idea.ClassName",
JUnitVersion.JUNIT_5,
ImmutableList.of(),
ParameterizedTestInfo.create("ignored", "-ignored")))
.isEqualTo("com.google.idea.ClassName#");
}

@Test
Expand Down

0 comments on commit 5136a16

Please sign in to comment.