use of com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState in project intellij by bazelbuild.
the class BlazeJavaTestMethodConfigurationProducerTest method testProducedFromPsiMethod.
@Test
public void testProducedFromPsiMethod() {
// Arrange
PsiFile javaFile = createAndIndexFile(new WorkspacePath("java/com/google/test/TestClass.java"), "package com.google.test;", "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)", "public class TestClass {", " @org.junit.Test", " public void testMethod1() {}", "}");
MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setKind("java_test").setLabel("//java/com/google/test:TestClass").addSource(sourceRoot("java/com/google/test/TestClass.java")).build()).build());
registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
PsiMethod method = PsiUtils.findFirstChildOfClassRecursive(javaFile, PsiMethod.class);
// Act
ConfigurationContext context = createContextFromPsi(method);
List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
ConfigurationFromContext fromContext = configurations.get(0);
// Assert
assertThat(configurations).hasSize(1);
assertThat(fromContext.isProducedBy(BlazeJavaTestMethodConfigurationProducer.class)).isTrue();
assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
assertThat(config.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//java/com/google/test:TestClass"));
assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#testMethod1$");
assertThat(config.getName()).isEqualTo("Blaze test TestClass.testMethod1");
assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
BlazeCommandRunConfigurationCommonState state = config.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
assertThat(state.getBlazeFlagsState().getRawFlags()).contains(BlazeFlags.DISABLE_TEST_SHARDING);
}
use of com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState in project intellij by bazelbuild.
the class HotSwapUtils method canHotSwap.
public static boolean canHotSwap(ExecutionEnvironment env) {
if (!isDebugging(env) || !enableHotSwapping.getValue()) {
return false;
}
BlazeCommandRunConfiguration configuration = getConfiguration(env);
BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
Kind kind = configuration.getTargetKind();
return BlazeCommandName.RUN.equals(Preconditions.checkNotNull(handlerState).getCommandState().getCommand()) && kind != null && kind.isOneOf(SUPPORTED_BINARIES);
}
use of com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState 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);
}
use of com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState in project intellij by bazelbuild.
the class BlazeJavaTestClassConfigurationProducer method doSetupConfigFromContext.
@Override
protected boolean doSetupConfigFromContext(BlazeCommandRunConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
TestLocation location = getSingleJUnitTestClass(context);
if (location == null) {
return false;
}
sourceElement.set(location.testClass);
configuration.setTargetInfo(location.blazeTarget);
BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
if (handlerState == null) {
return false;
}
String testFilter = getTestFilter(location.testClass);
if (testFilter == null) {
return false;
}
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));
flags.add(BlazeFlags.TEST_FILTER + "=" + testFilter);
handlerState.getBlazeFlagsState().setRawFlags(flags);
String name = new BlazeConfigurationNameBuilder(configuration).setTargetString(location.testClass.getName()).build();
configuration.setName(name);
// don't revert to generated name
configuration.setNameChangedByUser(true);
return true;
}
use of com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState in project intellij by bazelbuild.
the class BlazeJavaTestClassConfigurationProducer method checkIfAttributesAreTheSame.
private boolean checkIfAttributesAreTheSame(BlazeCommandRunConfiguration configuration, TestLocation location) {
if (!location.blazeTarget.label.equals(configuration.getTarget())) {
return false;
}
BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
if (handlerState == null) {
return false;
}
if (!Objects.equals(handlerState.getCommandState().getCommand(), BlazeCommandName.TEST)) {
return false;
}
String filter = getTestFilter(location.testClass);
return filter != null && Objects.equals(BlazeFlags.TEST_FILTER + "=" + filter, handlerState.getTestFilterFlag());
}
Aggregations