Search in sources :

Example 1 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class JUnit4IntegrationTest method ignoredTestMethod.

@Test
public void ignoredTestMethod() throws Throwable {
    EdtTestUtil.runInEdtAndWait(() -> {
        PsiClass psiClass = findClass(getModule1(), CLASS_NAME);
        assertNotNull(psiClass);
        PsiMethod testMethod = psiClass.findMethodsByName(METHOD_NAME, false)[0];
        JUnitConfiguration configuration = createConfiguration(testMethod);
        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(RunManagerImpl.getInstanceImpl(getProject()), configuration, false);
        ExecutionEnvironment environment = new ExecutionEnvironment(executor, ProgramRunnerUtil.getRunner(DefaultRunExecutor.EXECUTOR_ID, settings), settings, getProject());
        TestObject state = configuration.getState(executor, environment);
        JavaParameters parameters = state.getJavaParameters();
        parameters.setUseDynamicClasspath(getProject());
        GeneralCommandLine commandLine = parameters.toCommandLine();
        StringBuffer buf = new StringBuffer();
        StringBuffer err = new StringBuffer();
        OSProcessHandler process = new OSProcessHandler(commandLine);
        process.addProcessListener(new ProcessAdapter() {

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
                String text = event.getText();
                try {
                    if (outputType == ProcessOutputTypes.STDOUT && !text.isEmpty() && ServiceMessage.parse(text.trim()) == null) {
                        buf.append(text);
                    }
                    if (outputType == ProcessOutputTypes.STDERR) {
                        err.append(text);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        process.startNotify();
        process.waitFor();
        process.destroyProcess();
        String testOutput = buf.toString();
        assertEmpty(err.toString());
        switch(myJUnitVersion) {
            //shouldn't work for old versions
            case "4.4":
            //shouldn't work for old versions
            case "4.5":
                break;
            default:
                assertTrue(testOutput, testOutput.contains("Test1"));
        }
    });
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) PsiMethod(com.intellij.psi.PsiMethod) ProcessEvent(com.intellij.execution.process.ProcessEvent) JUnitConfiguration(com.intellij.execution.junit.JUnitConfiguration) PsiClass(com.intellij.psi.PsiClass) TestObject(com.intellij.execution.junit.TestObject) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) Executor(com.intellij.execution.Executor) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunnerAndConfigurationSettingsImpl(com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl) JavaParameters(com.intellij.execution.configurations.JavaParameters) ParseException(java.text.ParseException) Key(com.intellij.openapi.util.Key) Test(org.junit.Test)

Example 2 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class PyAbstractTestProcessRunner method rerunFailedTests.

/**
   * Rerun current tests. Make sure there is at least one failed test.
   * <strong>Run in AWT thread only!</strong>
   */
public void rerunFailedTests() {
    assert getFailedTestsCount() > 0 : "No failed tests. What you want to rerun?";
    assert myLastProcessDescriptor != null : "No last run descriptor. First run tests at least one time";
    final List<ProgramRunner<?>> run = getAvailableRunnersForLastRun();
    Assert.assertFalse("No runners to rerun", run.isEmpty());
    final ProgramRunner<?> runner = run.get(0);
    final ExecutionEnvironment restartAction = RerunFailedActionsTestTools.findRestartAction(myLastProcessDescriptor);
    Assert.assertNotNull("No restart action", restartAction);
    final Ref<ProcessHandler> handlerRef = new Ref<>();
    try {
        runner.execute(restartAction, descriptor -> handlerRef.set(descriptor.getProcessHandler()));
    } catch (final ExecutionException e) {
        throw new AssertionError("ExecutionException can't be thrown in tests. Probably, API changed. Got: " + e);
    }
    final ProcessHandler handler = handlerRef.get();
    if (handler == null) {
        return;
    }
    handler.waitFor();
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) Ref(com.intellij.openapi.util.Ref) ProcessHandler(com.intellij.execution.process.ProcessHandler) ProgramRunner(com.intellij.execution.runners.ProgramRunner) ExecutionException(com.intellij.execution.ExecutionException)

Example 3 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class RerunFailedActionsTestTools method findRestartActionState.

/**
   * Searches for "rerun failed tests" action and fetches state from it
   *
   * @param descriptor previous run descriptor
   * @return state (if found)
   */
@Nullable
public static RunProfileState findRestartActionState(@NotNull final RunContentDescriptor descriptor) {
    final ExecutionEnvironment action = findRestartAction(descriptor);
    if (action == null) {
        return null;
    }
    final Ref<RunProfileState> stateRef = new Ref<>();
    ApplicationManager.getApplication().invokeAndWait(() -> {
        try {
            stateRef.set(action.getState());
        } catch (final ExecutionException e) {
            throw new IllegalStateException("Error obtaining execution state", e);
        }
    }, ModalityState.NON_MODAL);
    return stateRef.get();
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) Ref(com.intellij.openapi.util.Ref) RunProfileState(com.intellij.execution.configurations.RunProfileState) ExecutionException(com.intellij.execution.ExecutionException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class ConfigurationBasedProcessRunner method runProcess.

@Override
final void runProcess(@NotNull final String sdkPath, @NotNull final Project project, @NotNull final ProcessListener processListener, @NotNull final String tempWorkingPath) throws ExecutionException {
    ensureConsoleOk(myConsole);
    // Do not create new environment from factory, if child provided environment to rerun
    final ExecutionEnvironment executionEnvironment = // TODO: RENAME
    (myRerunExecutionEnvironment != null ? myRerunExecutionEnvironment : createExecutionEnvironment(sdkPath, project, tempWorkingPath));
    // Engine to be run after process end to post process console
    final ProcessListener consolePostprocessor = new ProcessAdapter() {

        @Override
        public void processTerminated(final ProcessEvent event) {
            super.processTerminated(event);
            ApplicationManager.getApplication().invokeAndWait(() -> prepareConsoleAfterProcessEnd(), ModalityState.NON_MODAL);
        }
    };
    /// Find all available runners to report them to the test
    myAvailableRunnersForLastRun.clear();
    for (final ProgramRunner<?> runner : ProgramRunner.PROGRAM_RUNNER_EP.getExtensions()) {
        for (final Executor executor : Executor.EXECUTOR_EXTENSION_NAME.getExtensions()) {
            if (runner.canRun(executor.getId(), executionEnvironment.getRunProfile())) {
                myAvailableRunnersForLastRun.add(runner);
            }
        }
    }
    executionEnvironment.getRunner().execute(executionEnvironment, new ProgramRunner.Callback() {

        @Override
        public void processStarted(final RunContentDescriptor descriptor) {
            final ProcessHandler handler = descriptor.getProcessHandler();
            assert handler != null : "No process handler";
            handler.addProcessListener(consolePostprocessor);
            handler.addProcessListener(processListener);
            myConsole = null;
            fetchConsoleAndSetToField(descriptor);
            assert myConsole != null : "fetchConsoleAndSetToField did not set console!";
            // Console does not work with out of this method
            final JComponent component = myConsole.getComponent();
            assert component != null;
            myLastProcessDescriptor = descriptor;
        }
    });
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) ProcessHandler(com.intellij.execution.process.ProcessHandler) ProgramRunner(com.intellij.execution.runners.ProgramRunner)

Example 5 with ExecutionEnvironment

use of com.intellij.execution.runners.ExecutionEnvironment in project intellij-community by JetBrains.

the class PyDebuggerTask method runTestOn.

public void runTestOn(String sdkHome) throws Exception {
    final Project project = getProject();
    final ConfigurationFactory factory = PythonConfigurationType.getInstance().getConfigurationFactories()[0];
    final RunnerAndConfigurationSettings settings = RunManager.getInstance(project).createRunConfiguration("test", factory);
    myRunConfiguration = (PythonRunConfiguration) settings.getConfiguration();
    myRunConfiguration.setSdkHome(sdkHome);
    myRunConfiguration.setScriptName(getScriptName());
    myRunConfiguration.setWorkingDirectory(myFixture.getTempDirPath());
    myRunConfiguration.setScriptParameters(getScriptParameters());
    new WriteAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            RunManagerEx.getInstanceEx(project).addConfiguration(settings, false);
            RunManagerEx.getInstanceEx(project).setSelectedConfiguration(settings);
            Assert.assertSame(settings, RunManagerEx.getInstanceEx(project).getSelectedConfiguration());
        }
    }.execute();
    final PyDebugRunner runner = (PyDebugRunner) ProgramRunnerUtil.getRunner(getExecutorId(), settings);
    Assert.assertTrue(runner.canRun(getExecutorId(), myRunConfiguration));
    final Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();
    final ExecutionEnvironment env = new ExecutionEnvironment(executor, runner, settings, project);
    final PythonCommandLineState pyState = (PythonCommandLineState) myRunConfiguration.getState(executor, env);
    assert pyState != null;
    pyState.setMultiprocessDebug(isMultiprocessDebug());
    final ServerSocket serverSocket;
    try {
        //noinspection SocketOpenedButNotSafelyClosed
        serverSocket = new ServerSocket(0);
    } catch (IOException e) {
        throw new ExecutionException("Failed to find free socket port", e);
    }
    final int serverLocalPort = serverSocket.getLocalPort();
    final RunProfile profile = env.getRunProfile();
    //turn off exception breakpoints by default
    PythonDebuggerTest.createExceptionBreak(myFixture, false, false, false);
    before();
    setProcessCanTerminate(false);
    myTerminateSemaphore = new Semaphore(0);
    new WriteAction<ExecutionResult>() {

        @Override
        protected void run(@NotNull Result<ExecutionResult> result) throws Throwable {
            myExecutionResult = pyState.execute(executor, runner.createCommandLinePatchers(myFixture.getProject(), pyState, profile, serverLocalPort));
            mySession = XDebuggerManager.getInstance(getProject()).startSession(env, new XDebugProcessStarter() {

                @NotNull
                public XDebugProcess start(@NotNull final XDebugSession session) {
                    myDebugProcess = new PyDebugProcess(session, serverSocket, myExecutionResult.getExecutionConsole(), myExecutionResult.getProcessHandler(), isMultiprocessDebug());
                    StringBuilder output = new StringBuilder();
                    myDebugProcess.getProcessHandler().addProcessListener(new ProcessAdapter() {

                        @Override
                        public void onTextAvailable(ProcessEvent event, Key outputType) {
                            output.append(event.getText());
                        }

                        @Override
                        public void processTerminated(ProcessEvent event) {
                            myTerminateSemaphore.release();
                            if (event.getExitCode() != 0 && !myProcessCanTerminate) {
                                Assert.fail("Process terminated unexpectedly\n" + output.toString());
                            }
                        }
                    });
                    myDebugProcess.getProcessHandler().startNotify();
                    return myDebugProcess;
                }
            });
            result.setResult(myExecutionResult);
        }
    }.execute().getResultObject();
    OutputPrinter myOutputPrinter = null;
    if (shouldPrintOutput) {
        myOutputPrinter = new OutputPrinter();
        myOutputPrinter.start();
    }
    myPausedSemaphore = new Semaphore(0);
    mySession.addSessionListener(new XDebugSessionListener() {

        @Override
        public void sessionPaused() {
            if (myPausedSemaphore != null) {
                myPausedSemaphore.release();
            }
        }
    });
    doTest(myOutputPrinter);
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) PyDebugRunner(com.jetbrains.python.debugger.PyDebugRunner) Semaphore(java.util.concurrent.Semaphore) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) PyDebugProcess(com.jetbrains.python.debugger.PyDebugProcess) DefaultDebugExecutor(com.intellij.execution.executors.DefaultDebugExecutor) ConfigurationFactory(com.intellij.execution.configurations.ConfigurationFactory) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) PythonCommandLineState(com.jetbrains.python.run.PythonCommandLineState) WriteAction(com.intellij.openapi.application.WriteAction) ProcessEvent(com.intellij.execution.process.ProcessEvent) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) RunProfile(com.intellij.execution.configurations.RunProfile) Project(com.intellij.openapi.project.Project) Key(com.intellij.openapi.util.Key)

Aggregations

ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)62 ExecutionException (com.intellij.execution.ExecutionException)17 Project (com.intellij.openapi.project.Project)17 ProcessHandler (com.intellij.execution.process.ProcessHandler)15 NotNull (org.jetbrains.annotations.NotNull)15 Executor (com.intellij.execution.Executor)14 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)13 Nullable (org.jetbrains.annotations.Nullable)13 ProcessEvent (com.intellij.execution.process.ProcessEvent)12 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)12 ProgramRunner (com.intellij.execution.runners.ProgramRunner)12 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)11 RunProfile (com.intellij.execution.configurations.RunProfile)10 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)10 Key (com.intellij.openapi.util.Key)10 DefaultDebugExecutor (com.intellij.execution.executors.DefaultDebugExecutor)8 Module (com.intellij.openapi.module.Module)8 Ref (com.intellij.openapi.util.Ref)7 IOException (java.io.IOException)7 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)6