summaryrefslogtreecommitdiff
path: root/python/edu/learn-python/src/com/jetbrains/python/edu/StudyTestRunner.java
blob: b0cd5ba89feddeebf1220944fdb36511342ce9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.jetbrains.python.edu;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.edu.course.Course;
import com.jetbrains.python.edu.course.Task;
import com.jetbrains.python.sdk.PythonSdkType;

import java.io.*;
import java.util.Map;

public class StudyTestRunner {
  public static final String TEST_OK = "#study_plugin test OK";
  private static final String TEST_FAILED = "#study_plugin FAILED + ";
  private static final String PYTHONPATH = "PYTHONPATH";
  private static final Logger LOG = Logger.getInstance(StudyTestRunner.class);
  private final Task myTask;
  private final VirtualFile myTaskDir;

  public StudyTestRunner(Task task, VirtualFile taskDir) {
    myTask = task;
    myTaskDir = taskDir;
  }

  public Process launchTests(Project project, String executablePath) throws ExecutionException {
    Sdk sdk = PythonSdkType.findPythonSdk(ModuleManager.getInstance(project).getModules()[0]);
    File testRunner = new File(myTaskDir.getPath(), myTask.getTestFile());
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(myTaskDir.getPath());
    final Map<String, String> env = commandLine.getEnvironment();
    final VirtualFile courseDir = project.getBaseDir();
    if (courseDir != null) {
      env.put(PYTHONPATH, courseDir.getPath());
    }
    if (sdk != null) {
      String pythonPath = sdk.getHomePath();
      if (pythonPath != null) {
        commandLine.setExePath(pythonPath);
        commandLine.addParameter(testRunner.getPath());
        final Course course = StudyTaskManager.getInstance(project).getCourse();
        assert course != null;
        commandLine.addParameter(new File(course.getResourcePath()).getParent());
        commandLine.addParameter(FileUtil.toSystemDependentName(executablePath));
        return commandLine.createProcess();
      }
    }
    return null;
  }


  public String getPassedTests(Process p) {
    InputStream testOutput = p.getInputStream();
    BufferedReader testOutputReader = new BufferedReader(new InputStreamReader(testOutput));
    String line;
    try {
      while ((line = testOutputReader.readLine()) != null) {
        if (line.contains(TEST_FAILED)) {
          return line.substring(TEST_FAILED.length(), line.length());
        }
      }
    }
    catch (IOException e) {
      LOG.error(e);
    }
    finally {
      StudyUtils.closeSilently(testOutputReader);
    }
    return TEST_OK;
  }
}