Search in sources :

Example 41 with TargetInfo

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

the class BlazePyTestConfigurationProducer method doSetupConfigFromContext.

@Override
protected boolean doSetupConfigFromContext(BlazeCommandRunConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    PsiElement element = selectedPsiElement(context);
    if (element == null) {
        return false;
    }
    PsiFile file = element.getContainingFile();
    if (!(file instanceof PyFile) || !PyTestUtils.isTestFile((PyFile) file)) {
        return false;
    }
    TargetInfo testTarget = TestTargetHeuristic.testTargetForPsiElement(element);
    if (testTarget == null) {
        return false;
    }
    configuration.setTargetInfo(testTarget);
    TestLocation testLocation = testLocation(element);
    sourceElement.set(testLocation.sourceElement(file));
    BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    if (handlerState == null) {
        return false;
    }
    handlerState.getCommandState().setCommand(BlazeCommandName.TEST);
    // remove conflicting flags from initial configuration
    List<String> flags = new ArrayList<>(handlerState.getBlazeFlagsState().getRawFlags());
    flags.removeIf((flag) -> flag.startsWith(BlazeFlags.TEST_FILTER));
    String filter = testLocation.testFilter();
    if (filter != null) {
        flags.add(BlazeFlags.TEST_FILTER + "=" + filter);
    }
    handlerState.getBlazeFlagsState().setRawFlags(flags);
    BlazeConfigurationNameBuilder nameBuilder = new BlazeConfigurationNameBuilder(configuration);
    nameBuilder.setTargetString(filter != null ? String.format("%s (%s)", filter, testTarget.label.toString()) : testTarget.label.toString());
    configuration.setName(nameBuilder.build());
    // don't revert to generated name
    configuration.setNameChangedByUser(true);
    return true;
}
Also used : BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) BlazeConfigurationNameBuilder(com.google.idea.blaze.base.run.BlazeConfigurationNameBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 42 with TargetInfo

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

the class BlazeBuildTargetRunConfigurationFactory method setupConfiguration.

@Override
public void setupConfiguration(RunConfiguration configuration, Label label) {
    BlazeCommandRunConfiguration blazeConfig = (BlazeCommandRunConfiguration) configuration;
    TargetInfo target = findProjectTarget(configuration.getProject(), label);
    blazeConfig.setTargetInfo(target);
    if (target == null) {
        return;
    }
    BlazeCommandRunConfigurationCommonState state = blazeConfig.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    if (state != null) {
        state.getCommandState().setCommand(commandForRuleType(target.getRuleType()));
    }
    blazeConfig.setGeneratedName();
}
Also used : BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo)

Example 43 with TargetInfo

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

the class BlazeCidrTestConfigurationProducer method doIsConfigFromContext.

@Override
protected boolean doIsConfigFromContext(BlazeCommandRunConfiguration configuration, ConfigurationContext context) {
    BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    if (handlerState == null) {
        return false;
    }
    if (!Objects.equals(handlerState.getCommandState().getCommand(), BlazeCommandName.TEST)) {
        return false;
    }
    PsiElement element = selectedPsiElement(context);
    if (element == null) {
        return false;
    }
    GoogleTestLocation test = GoogleTestLocation.findGoogleTest(element);
    if (test == null) {
        return false;
    }
    TargetInfo target = getTestTarget(test.getPsiElement());
    if (target == null) {
        return false;
    }
    return target.label.equals(configuration.getTarget()) && Objects.equals(handlerState.getTestFilterFlag(), test.getTestFilterFlag());
}
Also used : BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) GoogleTestLocation(com.google.idea.blaze.clwb.run.test.GoogleTestLocation) PsiElement(com.intellij.psi.PsiElement)

Example 44 with TargetInfo

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

the class BlazeJavaAbstractTestCaseConfigurationProducer method setupContext.

private static void setupContext(BlazeCommandRunConfiguration configuration, PsiClass subClass, @Nullable PsiMethod method) {
    TestSize testSize = method != null ? TestSizeAnnotationMap.getTestSize(method) : TestSizeAnnotationMap.getTestSize(subClass);
    TargetInfo target = RunUtil.targetForTestClass(subClass, testSize);
    if (target == null) {
        return;
    }
    configuration.setTargetInfo(target);
    BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    if (handlerState == null) {
        return;
    }
    handlerState.getCommandState().setCommand(BlazeCommandName.TEST);
    // remove old test filter flag if present
    List<String> flags = new ArrayList<>(handlerState.getBlazeFlagsState().getRawFlags());
    flags.removeIf((flag) -> flag.startsWith(BlazeFlags.TEST_FILTER));
    String testFilter = BlazeJUnitTestFilterFlags.testFilterForClassAndMethods(subClass, method == null ? ImmutableList.of() : ImmutableList.of(method));
    if (testFilter == null) {
        return;
    }
    flags.add(BlazeFlags.TEST_FILTER + "=" + testFilter);
    handlerState.getBlazeFlagsState().setRawFlags(flags);
    BlazeConfigurationNameBuilder nameBuilder = new BlazeConfigurationNameBuilder(configuration);
    nameBuilder.setTargetString(configName(subClass, method));
    configuration.setName(nameBuilder.build());
    // don't revert to generated name
    configuration.setNameChangedByUser(true);
}
Also used : BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) ArrayList(java.util.ArrayList) BlazeConfigurationNameBuilder(com.google.idea.blaze.base.run.BlazeConfigurationNameBuilder) TestSize(com.google.idea.blaze.base.dependencies.TestSize)

Example 45 with TargetInfo

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

the class BlazeJavaTestMethodConfigurationProducer method getSelectedMethodContext.

@Nullable
private static TestMethodContext getSelectedMethodContext(ConfigurationContext context) {
    if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
        // handled by a different producer
        return null;
    }
    final List<PsiMethod> selectedMethods = TestMethodSelectionUtil.getSelectedMethods(context);
    if (selectedMethods == null) {
        return null;
    }
    assert selectedMethods.size() > 0;
    final PsiMethod firstMethod = selectedMethods.get(0);
    final PsiClass containingClass = firstMethod.getContainingClass();
    if (containingClass == null) {
        return null;
    }
    for (PsiMethod method : selectedMethods) {
        if (!containingClass.equals(method.getContainingClass())) {
            return null;
        }
    }
    TestSize testSize = TestSizeAnnotationMap.getTestSize(firstMethod);
    TargetInfo target = RunUtil.targetForTestClass(containingClass, testSize);
    if (target == null) {
        return null;
    }
    String testFilter = BlazeJUnitTestFilterFlags.testFilterForClassAndMethods(containingClass, selectedMethods);
    if (testFilter == null) {
        return null;
    }
    // Sort so multiple configurations created with different selection orders are the same.
    List<String> methodNames = selectedMethods.stream().map(PsiMethod::getName).sorted().collect(Collectors.toList());
    final String testFilterFlag = BlazeFlags.TEST_FILTER + "=" + testFilter;
    return new TestMethodContext(firstMethod, containingClass, methodNames, testFilterFlag, target);
}
Also used : TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) TestSize(com.google.idea.blaze.base.dependencies.TestSize) Nullable(javax.annotation.Nullable)

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