Search in sources :

Example 36 with BlazeCommandRunConfiguration

use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.

the class BlazeJavaTestMethodConfigurationProducerTest method testConfigWithDifferentFilterIgnored.

@Test
public void testConfigWithDifferentFilterIgnored() {
    // Arrange
    PsiMethod method = setupGenericJunitTestClassAndBlazeTarget();
    ConfigurationContext context = createContextFromPsi(method);
    BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) context.getConfiguration().getConfiguration();
    BlazeCommandRunConfigurationCommonState handlerState = config.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    // modify the test filter, and check that is enough for the producer to class it as different.
    List<String> flags = new ArrayList<>(handlerState.getBlazeFlagsState().getRawFlags());
    flags.removeIf((flag) -> flag.startsWith(BlazeFlags.TEST_FILTER));
    flags.add(BlazeFlags.TEST_FILTER + "=com.google.test.DifferentTestClass#");
    handlerState.getBlazeFlagsState().setRawFlags(flags);
    // Act
    boolean isConfigFromContext = new BlazeJavaTestMethodConfigurationProducer().doIsConfigFromContext(config, context);
    // Assert
    assertThat(isConfigFromContext).isFalse();
}
Also used : BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Example 37 with BlazeCommandRunConfiguration

use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration 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);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) PsiMethod(com.intellij.psi.PsiMethod) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) PsiFile(com.intellij.psi.PsiFile) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Example 38 with BlazeCommandRunConfiguration

use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.

the class MultipleJavaClassesTestConfigurationProducerTest method testProducedFromTestFiles.

@Test
public void testProducedFromTestFiles() {
    MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
    builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setKind("java_test").setLabel("//java/com/google/test:allTests").addSource(sourceRoot("java/com/google/test/TestClass1.java")).addSource(sourceRoot("java/com/google/test/TestClass2.java")).build()).build());
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
    workspace.createPsiDirectory(new WorkspacePath("java/com/google/test"));
    PsiFile testClass1 = createAndIndexFile(new WorkspacePath("java/com/google/test/TestClass1.java"), "package com.google.test;", "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)", "public class TestClass1 {", "  @org.junit.Test", "  public void testMethod() {}", "}");
    PsiFile testClass2 = createAndIndexFile(new WorkspacePath("java/com/google/test/TestClass2.java"), "package com.google.test;", "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)", "public class TestClass2 {", "  @org.junit.Test", "  public void testMethod() {}", "}");
    ConfigurationContext context = createContextFromMultipleElements(new PsiElement[] { testClass1, testClass2 });
    ConfigurationFromContext fromContext = new MultipleJavaClassesTestConfigurationProducer().createConfigurationFromContext(context);
    assertThat(fromContext).isNotNull();
    assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
    BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
    assertThat(config.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//java/com/google/test:allTests"));
    assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass1#|com.google.test.TestClass2#");
    assertThat(config.getName()).isEqualTo("Blaze test TestClass1 and 1 others");
    assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) PsiFile(com.intellij.psi.PsiFile) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Example 39 with BlazeCommandRunConfiguration

use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.

the class MultipleJavaClassesTestConfigurationProducerTest method testNoFilterIfDirectoryAtPackageRoot.

@Test
public void testNoFilterIfDirectoryAtPackageRoot() {
    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()));
    PsiDirectory directory = workspace.createPsiDirectory(new WorkspacePath("java"));
    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 testMethod() {}", "}");
    ConfigurationContext context = createContextFromPsi(directory);
    List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
    assertThat(configurations).hasSize(1);
    ConfigurationFromContext fromContext = configurations.get(0);
    assertThat(fromContext.isProducedBy(MultipleJavaClassesTestConfigurationProducer.class)).isTrue();
    assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
    BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
    assertThat(getTestFilterContents(config)).isNull();
    assertThat(config.getName()).isEqualTo("Blaze test test:TestClass");
    assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) PsiDirectory(com.intellij.psi.PsiDirectory) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Example 40 with BlazeCommandRunConfiguration

use of com.google.idea.blaze.base.run.BlazeCommandRunConfiguration in project intellij by bazelbuild.

the class ClassFileManifestBuilder method buildManifest.

/**
 * Builds a .class file manifest, then diffs against any previously calculated manifest for this
 * debugging session.
 *
 * @return null if no diff is available (either no manifest could be calculated, or no previously
 *     calculated manifest is available.
 */
@Nullable
public static ClassFileManifest.Diff buildManifest(ExecutionEnvironment env, @Nullable HotSwapProgress progress) throws ExecutionException {
    if (!HotSwapUtils.canHotSwap(env)) {
        return null;
    }
    BlazeCommandRunConfiguration configuration = getConfiguration(env);
    Project project = configuration.getProject();
    BlazeProjectData projectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (projectData == null) {
        throw new ExecutionException("Not synced yet; please sync project");
    }
    JavaClasspathAspectStrategy aspectStrategy = JavaClasspathAspectStrategy.findStrategy(projectData.blazeVersionData);
    if (aspectStrategy == null) {
        return null;
    }
    try (BuildResultHelper buildResultHelper = BuildResultHelper.forFiles(file -> true)) {
        ListenableFuture<BuildResult> buildOperation = BlazeBeforeRunCommandHelper.runBlazeBuild(configuration, buildResultHelper, aspectStrategy.getBuildFlags(), ImmutableList.of(), "Building debug binary");
        if (progress != null) {
            progress.setCancelWorker(() -> buildOperation.cancel(true));
        }
        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);
        }
        ImmutableList<File> jars = buildResultHelper.getArtifactsForOutputGroups(ImmutableSet.of(JavaClasspathAspectStrategy.OUTPUT_GROUP)).stream().filter(f -> f.getName().endsWith(".jar")).collect(toImmutableList());
        ClassFileManifest oldManifest = getManifest(env);
        ClassFileManifest newManifest = ClassFileManifest.build(jars, oldManifest);
        env.getCopyableUserData(MANIFEST_KEY).set(newManifest);
        return oldManifest != null ? ClassFileManifest.modifiedClasses(oldManifest, newManifest) : null;
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CancellationException(java.util.concurrent.CancellationException) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key) BuildResult(com.google.idea.blaze.base.sync.aspects.BuildResult) RunProfile(com.intellij.execution.configurations.RunProfile) BuildResultHelper(com.google.idea.blaze.base.command.buildresult.BuildResultHelper) AtomicReference(java.util.concurrent.atomic.AtomicReference) BlazeProjectDataManager(com.google.idea.blaze.base.sync.data.BlazeProjectDataManager) File(java.io.File) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) WrappingRunConfiguration(com.intellij.execution.configurations.WrappingRunConfiguration) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) BlazeBeforeRunCommandHelper(com.google.idea.blaze.base.run.BlazeBeforeRunCommandHelper) RunCanceledByUserException(com.intellij.execution.RunCanceledByUserException) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ImmutableList(com.google.common.collect.ImmutableList) HotSwapProgress(com.intellij.debugger.impl.HotSwapProgress) Project(com.intellij.openapi.project.Project) SaveUtil(com.google.idea.blaze.base.util.SaveUtil) GuavaHelper.toImmutableList(com.google.idea.common.guava.GuavaHelper.toImmutableList) Nullable(javax.annotation.Nullable) RunCanceledByUserException(com.intellij.execution.RunCanceledByUserException) Project(com.intellij.openapi.project.Project) BuildResult(com.google.idea.blaze.base.sync.aspects.BuildResult) BuildResultHelper(com.google.idea.blaze.base.command.buildresult.BuildResultHelper) CancellationException(java.util.concurrent.CancellationException) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Nullable(javax.annotation.Nullable)

Aggregations

BlazeCommandRunConfiguration (com.google.idea.blaze.base.run.BlazeCommandRunConfiguration)52 Test (org.junit.Test)36 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)33 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)29 PsiFile (com.intellij.psi.PsiFile)26 ConfigurationFromContext (com.intellij.execution.actions.ConfigurationFromContext)24 MockBlazeProjectDataBuilder (com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder)23 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)23 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)9 BlazeCommandRunConfigurationCommonState (com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState)8 PsiClass (com.intellij.psi.PsiClass)8 PsiMethod (com.intellij.psi.PsiMethod)5 StringLiteral (com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral)4 PsiDirectory (com.intellij.psi.PsiDirectory)4 Nullable (javax.annotation.Nullable)4 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)3 BlazeBuildFileRunConfigurationProducer (com.google.idea.blaze.base.run.producers.BlazeBuildFileRunConfigurationProducer)3 File (java.io.File)3 ScalaFile (org.jetbrains.plugins.scala.lang.psi.api.ScalaFile)3 BlazeCommandName (com.google.idea.blaze.base.command.BlazeCommandName)2