ebda0f8976f1a3fdc170a6b64b8dd7fe114c6134
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / actions / PyCCRunTestsAction.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.jetbrains.edu.coursecreator.actions;
17
18 import com.intellij.execution.Executor;
19 import com.intellij.execution.ProgramRunnerUtil;
20 import com.intellij.execution.RunManager;
21 import com.intellij.execution.RunnerAndConfigurationSettings;
22 import com.intellij.execution.configurations.ConfigurationFactory;
23 import com.intellij.execution.executors.DefaultRunExecutor;
24 import com.intellij.ide.projectView.ProjectView;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.openapi.vfs.VirtualFileManager;
28 import com.intellij.util.PathUtil;
29 import com.jetbrains.python.run.PythonConfigurationType;
30 import com.jetbrains.python.run.PythonRunConfiguration;
31 import org.jetbrains.annotations.NotNull;
32
33 public class PyCCRunTestsAction extends CCRunTestsAction {
34
35   @Override
36   protected void executeTests(@NotNull final Project project,
37                                    @NotNull final VirtualFile virtualFile,
38                                    @NotNull final VirtualFile taskDir,
39                                    @NotNull final VirtualFile testFile) {
40     final ConfigurationFactory factory = PythonConfigurationType.getInstance().getConfigurationFactories()[0];
41     final RunnerAndConfigurationSettings settings =
42       RunManager.getInstance(project).createRunConfiguration("test", factory);
43
44     final PythonRunConfiguration configuration = (PythonRunConfiguration)settings.getConfiguration();
45     configuration.setScriptName(testFile.getPath());
46     configuration.setWorkingDirectory(taskDir.getPath());
47     String taskFileName = virtualFile.getName();
48     VirtualFile userFile = taskDir.findChild(taskFileName);
49     if (userFile == null) {
50       return;
51     }
52     VirtualFile ideaDir = project.getBaseDir().findChild(".idea");
53     if (ideaDir == null) {
54       return;
55     }
56     VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
57     ProjectView.getInstance(project).refresh();
58     configuration.setScriptParameters(PathUtil.toSystemDependentName(project.getBasePath()) + " " + PathUtil.toSystemDependentName(userFile.getPath()));
59     Executor executor = DefaultRunExecutor.getRunExecutorInstance();
60     ProgramRunnerUtil.executeConfiguration(project, settings, executor);
61   }
62 }