f7c64d1099f149e6b843b279321c2c1f869c961a
[idea/community.git] / python / educational-python / student-python / src / com / jetbrains / edu / learning / PyStudyTestRunner.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.execution.ExecutionException;
4 import com.intellij.execution.configurations.GeneralCommandLine;
5 import com.intellij.openapi.diagnostic.Logger;
6 import com.intellij.openapi.module.ModuleManager;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.projectRoots.Sdk;
9 import com.intellij.openapi.util.io.FileUtil;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.jetbrains.edu.learning.checker.StudyTestRunner;
12 import com.jetbrains.edu.learning.courseFormat.Course;
13 import com.jetbrains.edu.learning.courseFormat.Task;
14 import com.jetbrains.python.sdk.PythonSdkType;
15 import org.jetbrains.annotations.NotNull;
16
17 import java.io.File;
18 import java.util.Map;
19
20 public class PyStudyTestRunner extends StudyTestRunner {
21   private static final String PYTHONPATH = "PYTHONPATH";
22   private static final Logger LOG = Logger.getInstance(PyStudyTestRunner.class.getName());
23
24   PyStudyTestRunner(@NotNull final Task task, @NotNull final VirtualFile taskDir) {
25     super(task, taskDir);
26   }
27
28   public Process createCheckProcess(@NotNull final Project project, @NotNull final String executablePath) throws ExecutionException {
29     final Sdk sdk = PythonSdkType.findPythonSdk(ModuleManager.getInstance(project).getModules()[0]);
30     Course course = myTask.getLesson().getCourse();
31     StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
32     if (manager == null) {
33       LOG.info("Language manager is null for " + course.getLanguageById().getDisplayName());
34       return null;
35     }
36
37     String testsFileName = manager.getTestFileName();
38     final File testRunner = new File(myTaskDir.getPath(), testsFileName);
39     final GeneralCommandLine commandLine = new GeneralCommandLine();
40     commandLine.withWorkDirectory(myTaskDir.getPath());
41     final Map<String, String> env = commandLine.getEnvironment();
42
43     final VirtualFile courseDir = project.getBaseDir();
44     if (courseDir != null) {
45       env.put(PYTHONPATH, courseDir.getPath());
46     }
47     if (sdk != null) {
48       String pythonPath = sdk.getHomePath();
49       if (pythonPath != null) {
50         commandLine.setExePath(pythonPath);
51         commandLine.addParameter(testRunner.getPath());
52         File resourceFile = new File(course.getCourseDirectory());
53         commandLine.addParameter(resourceFile.getPath());
54         commandLine.addParameter(FileUtil.toSystemDependentName(executablePath));
55         return commandLine.createProcess();
56       }
57     }
58     return null;
59   }
60 }