Search in sources :

Example 31 with TargetInfo

use of com.google.idea.blaze.base.dependencies.TargetInfo in project intellij by bazelbuild.

the class BlazeJavaTestClassConfigurationProducer method getSingleJUnitTestClass.

/**
 * Returns the {@link TestLocation} corresponding to the single selected JUnit test class, or
 * {@code null} if something else is selected.
 */
@Nullable
private TestLocation getSingleJUnitTestClass(ConfigurationContext context) {
    Location<?> location = context.getLocation();
    if (location == null) {
        return null;
    }
    location = JavaExecutionUtil.stepIntoSingleClass(location);
    if (location == null) {
        return null;
    }
    // check for contexts handled by a different producer
    if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
        return null;
    }
    if (JUnitConfigurationUtil.isMultipleElementsSelected(context)) {
        return null;
    }
    if (JavaTestCaseIdentifier.isAnyTestCase(context)) {
        return null;
    }
    PsiClass testClass = JUnitUtil.getTestClass(location);
    if (testClass == null || testClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
        return null;
    }
    TestSize testSize = TestSizeAnnotationMap.getTestSize(testClass);
    TargetInfo target = RunUtil.targetForTestClass(testClass, testSize);
    return target != null ? new TestLocation(testClass, target) : null;
}
Also used : TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) PsiClass(com.intellij.psi.PsiClass) TestSize(com.google.idea.blaze.base.dependencies.TestSize) Nullable(javax.annotation.Nullable)

Example 32 with TargetInfo

use of com.google.idea.blaze.base.dependencies.TargetInfo in project intellij by bazelbuild.

the class RunUtil method targetForTestClass.

/**
 * @return The Blaze test rule containing the target test class. In the case of multiple
 *     containing rules, the first rule sorted alphabetically by label.
 */
@Nullable
public static TargetInfo targetForTestClass(PsiClass testClass, @Nullable TestSize testSize) {
    File testFile = getFileForClass(testClass);
    if (testFile == null) {
        return null;
    }
    Project project = testClass.getProject();
    Collection<TargetInfo> targets = SourceToTargetFinder.findTargetsForSourceFile(project, testFile, Optional.of(RuleType.TEST));
    return TestTargetHeuristic.chooseTestTargetForSourceFile(project, testClass.getContainingFile(), testFile, targets, testSize);
}
Also used : Project(com.intellij.openapi.project.Project) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) Nullable(javax.annotation.Nullable)

Example 33 with TargetInfo

use of com.google.idea.blaze.base.dependencies.TargetInfo in project intellij by bazelbuild.

the class CombinedTestHeuristicTest method testSizeAndJUnit4Combination.

@Test
public void testSizeAndJUnit4Combination() {
    Collection<TargetInfo> targets = ImmutableList.of(createTarget("//foo:SmallJUnit3Tests", TestSize.SMALL), createTarget("//foo:MediumJUnit3Tests", TestSize.MEDIUM), createTarget("//foo:LargeJUnit3Tests", TestSize.LARGE), createTarget("//foo:SmallJUnit4Tests", TestSize.SMALL), createTarget("//foo:MediumJUnit4Tests", TestSize.MEDIUM), createTarget("//foo:LargeJUnit4Tests", TestSize.LARGE));
    PsiFile psiFile = workspace.createPsiFile(new WorkspacePath("java/com/google/lib/JavaTest.java"), "package com.google.lib;", "import org.junit.Test;", "import org.junit.runner.RunWith;", "import org.junit.runners.JUnit4;", "@RunWith(JUnit4.class)", "public class JavaTest {", "  @Test", "  public void testMethod1() {}", "  @Test", "  public void testMethod2() {}", "}");
    File source = new File(psiFile.getVirtualFile().getPath());
    TargetInfo match = TestTargetHeuristic.chooseTestTargetForSourceFile(getProject(), psiFile, source, targets, TestSize.LARGE);
    assertThat(match.label).isEqualTo(Label.create("//foo:LargeJUnit4Tests"));
    match = TestTargetHeuristic.chooseTestTargetForSourceFile(getProject(), psiFile, source, targets, TestSize.MEDIUM);
    assertThat(match.label).isEqualTo(Label.create("//foo:MediumJUnit4Tests"));
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) Test(org.junit.Test)

Example 34 with TargetInfo

use of com.google.idea.blaze.base.dependencies.TargetInfo in project intellij by bazelbuild.

the class QualifiedClassNameHeuristicTest method testMatchesQualifiedClassNames.

@Test
public void testMatchesQualifiedClassNames() {
    PsiFile psiFile = workspace.createPsiFile(new WorkspacePath("com/google/lib/JavaClass.java"), "package com.google.lib;", "public class JavaClass {}");
    File file = new File(psiFile.getVirtualFile().getPath());
    TargetInfo target = TargetIdeInfo.builder().setLabel("//foo:lib.JavaClass").setKind("java_test").build().toTargetInfo();
    assertThat(new QualifiedClassNameHeuristic().matchesSource(getProject(), target, psiFile, file, null)).isTrue();
    target = TargetIdeInfo.builder().setLabel("//foo:google.lib.JavaClass").setKind("java_test").build().toTargetInfo();
    assertThat(new QualifiedClassNameHeuristic().matchesSource(getProject(), target, psiFile, file, null)).isTrue();
    target = TargetIdeInfo.builder().setLabel("//foo:com.google.lib.JavaClass").setKind("java_test").build().toTargetInfo();
    assertThat(new QualifiedClassNameHeuristic().matchesSource(getProject(), target, psiFile, file, null)).isTrue();
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) PsiFile(com.intellij.psi.PsiFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Test(org.junit.Test)

Example 35 with TargetInfo

use of com.google.idea.blaze.base.dependencies.TargetInfo in project intellij by bazelbuild.

the class QualifiedClassNameHeuristicTest method testDoesNotMatchUnqualifiedClassName.

@Test
public void testDoesNotMatchUnqualifiedClassName() {
    PsiFile psiFile = workspace.createPsiFile(new WorkspacePath("com/google/lib/JavaClass.java"), "package com.google.lib;", "public class JavaClass {}");
    File file = new File(psiFile.getVirtualFile().getPath());
    TargetInfo target = TargetIdeInfo.builder().setLabel("//foo:JavaClass").setKind("java_test").build().toTargetInfo();
    assertThat(new QualifiedClassNameHeuristic().matchesSource(getProject(), target, psiFile, file, null)).isFalse();
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) PsiFile(com.intellij.psi.PsiFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Test(org.junit.Test)

Aggregations

TargetInfo (com.google.idea.blaze.base.dependencies.TargetInfo)69 File (java.io.File)43 Test (org.junit.Test)40 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)19 PsiFile (com.intellij.psi.PsiFile)18 Nullable (javax.annotation.Nullable)15 BlazeCommandRunConfigurationCommonState (com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState)14 PsiElement (com.intellij.psi.PsiElement)9 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)7 Label (com.google.idea.blaze.base.model.primitives.Label)7 PsiClass (com.intellij.psi.PsiClass)7 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)6 RuleType (com.google.idea.blaze.base.model.primitives.RuleType)6 BlazeProjectDataManager (com.google.idea.blaze.base.sync.data.BlazeProjectDataManager)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 BlazeTestCase (com.google.idea.blaze.base.BlazeTestCase)5 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)5 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)5 TargetMapBuilder (com.google.idea.blaze.base.ideinfo.TargetMapBuilder)5