use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.
the class BlazePyTestConfigurationProducerTest method testProducedFromPyClass.
@Test
public void testProducedFromPyClass() {
PsiFile pyFile = createAndIndexFile(new WorkspacePath("py/test/unittest.py"), "class UnitTest(googletest.TestCase):", " def testSomething():", " return");
MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setKind("py_test").setLabel("//py/test:unittests").addSource(sourceRoot("py/test/unittest.py")).build()).build());
registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
PyClass pyClass = PsiUtils.findFirstChildOfClassRecursive(pyFile, PyClass.class);
assertThat(pyClass).isNotNull();
ConfigurationContext context = createContextFromPsi(pyClass);
List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
assertThat(configurations).hasSize(1);
ConfigurationFromContext fromContext = configurations.get(0);
assertThat(fromContext.isProducedBy(BlazePyTestConfigurationProducer.class)).isTrue();
assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
assertThat(config.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//py/test:unittests"));
assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=UnitTest");
assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.
the class BlazePyRunConfigurationRunner method getExecutableToDebug.
/**
* Builds blaze python target and returns the output build artifact.
*
* @throws ExecutionException if the target cannot be debugged.
*/
private static File getExecutableToDebug(ExecutionEnvironment env) throws ExecutionException {
BlazeCommandRunConfiguration configuration = getConfiguration(env);
Project project = configuration.getProject();
BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (blazeProjectData == null) {
throw new ExecutionException("Not synced yet, please sync project");
}
String validationError = BlazePyDebugHelper.validateDebugTarget(env.getProject(), configuration.getTarget());
if (validationError != null) {
throw new WithBrowserHyperlinkExecutionException(validationError);
}
try (BuildResultHelper buildResultHelper = BuildResultHelper.forFiles(file -> true)) {
ListenableFuture<BuildResult> buildOperation = BlazeBeforeRunCommandHelper.runBlazeBuild(configuration, buildResultHelper, BlazePyDebugHelper.getAllBlazeDebugFlags(Blaze.getBuildSystem(project)), ImmutableList.of(), "Building debug binary");
try {
SaveUtil.saveAllFiles();
BuildResult result = buildOperation.get();
if (result.status != BuildResult.Status.SUCCESS) {
throw new ExecutionException("Blaze failure building debug binary");
}
} catch (InterruptedException | CancellationException e) {
buildOperation.cancel(true);
throw new RunCanceledByUserException();
} catch (java.util.concurrent.ExecutionException e) {
throw new ExecutionException(e);
}
List<File> candidateFiles = buildResultHelper.getBuildArtifactsForTarget((Label) configuration.getTarget()).stream().filter(File::canExecute).collect(Collectors.toList());
if (candidateFiles.isEmpty()) {
throw new ExecutionException(String.format("No output artifacts found when building %s", configuration.getTarget()));
}
File file = findExecutable((Label) configuration.getTarget(), candidateFiles);
if (file == null) {
throw new ExecutionException(String.format("More than 1 executable was produced when building %s; " + "don't know which one to debug", configuration.getTarget()));
}
LocalFileSystem.getInstance().refreshIoFiles(ImmutableList.of(file));
return file;
}
}
use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.
the class BlazeFilterExistingRunConfigurationProducer method getTestFilter.
private static Optional<String> getTestFilter(ConfigurationContext context) {
RunConfiguration base = context.getOriginalConfiguration(null);
if (!(base instanceof BlazeCommandRunConfiguration)) {
return Optional.empty();
}
TargetExpression target = ((BlazeCommandRunConfiguration) base).getTarget();
if (target == null) {
return Optional.empty();
}
List<Location<?>> selectedElements = SmRunnerUtils.getSelectedSmRunnerTreeElements(context);
if (selectedElements.isEmpty()) {
return null;
}
Optional<BlazeTestEventsHandler> testEventsHandler = BlazeTestEventsHandler.getHandlerForTarget(context.getProject(), target);
return testEventsHandler.map(handler -> handler.getTestFilter(context.getProject(), selectedElements));
}
use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.
the class BlazeRerunFailedTestsAction method getRunProfile.
@Override
@Nullable
protected MyRunProfile getRunProfile(ExecutionEnvironment environment) {
final TestFrameworkRunningModel model = getModel();
if (model == null) {
return null;
}
BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) model.getProperties().getConfiguration();
return new BlazeRerunTestRunProfile(config.clone());
}
use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.
the class BlazeJavaAbstractTestCaseConfigurationProducerTest method testConfigurationCreatedFromMethodInAbstractClass.
@Test
public void testConfigurationCreatedFromMethodInAbstractClass() {
PsiFile abstractClassFile = createAndIndexFile(new WorkspacePath("java/com/google/test/AbstractTestCase.java"), "package com.google.test;", "public abstract class AbstractTestCase {", " @org.junit.Test", " public void testMethod() {}", "}");
createAndIndexFile(new WorkspacePath("java/com/google/test/TestClass.java"), "package com.google.test;", "import com.google.test.AbstractTestCase;", "import org.junit.runner.RunWith;", "import org.junit.runners.JUnit4;", "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)", "public class TestClass extends AbstractTestCase {}");
PsiClass javaClass = ((PsiClassOwner) abstractClassFile).getClasses()[0];
PsiMethod method = PsiUtils.findFirstChildOfClassRecursive(javaClass, PsiMethod.class);
assertThat(method).isNotNull();
ConfigurationContext context = createContextFromPsi(method);
List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
assertThat(configurations).hasSize(1);
ConfigurationFromContext fromContext = configurations.get(0);
assertThat(fromContext.isProducedBy(BlazeJavaAbstractTestCaseConfigurationProducer.class)).isTrue();
assertThat(fromContext.getSourceElement()).isEqualTo(method);
RunConfiguration config = fromContext.getConfiguration();
assertThat(config).isInstanceOf(BlazeCommandRunConfiguration.class);
BlazeCommandRunConfiguration blazeConfig = (BlazeCommandRunConfiguration) config;
assertThat(blazeConfig.getTarget()).isNull();
assertThat(blazeConfig.getName()).isEqualTo("Choose subclass for AbstractTestCase.testMethod");
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()));
BlazeJavaAbstractTestCaseConfigurationProducer.chooseSubclass(fromContext, context, EmptyRunnable.INSTANCE);
assertThat(blazeConfig.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//java/com/google/test:TestClass"));
assertThat(getTestFilterContents(blazeConfig)).isEqualTo(BlazeFlags.TEST_FILTER + "=com.google.test.TestClass#testMethod$");
}
Aggregations