|
| 1 | +package fi.helsinki.cs.tmc.langs.r; |
| 2 | + |
| 3 | +import fi.helsinki.cs.tmc.langs.AbstractLanguagePlugin; |
| 4 | +import fi.helsinki.cs.tmc.langs.abstraction.ValidationResult; |
| 5 | +import fi.helsinki.cs.tmc.langs.domain.ExerciseBuilder; |
| 6 | +import fi.helsinki.cs.tmc.langs.domain.ExerciseDesc; |
| 7 | +import fi.helsinki.cs.tmc.langs.domain.RunResult; |
| 8 | +import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy; |
| 9 | +import fi.helsinki.cs.tmc.langs.io.sandbox.StudentFileAwareSubmissionProcessor; |
| 10 | +import fi.helsinki.cs.tmc.langs.io.sandbox.SubmissionProcessor; |
| 11 | +import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareUnzipper; |
| 12 | +import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareZipper; |
| 13 | +import fi.helsinki.cs.tmc.langs.io.zip.Unzipper; |
| 14 | +import fi.helsinki.cs.tmc.langs.io.zip.Zipper; |
| 15 | +import fi.helsinki.cs.tmc.langs.python3.Python3TestResultParser; |
| 16 | +import fi.helsinki.cs.tmc.langs.utils.ProcessRunner; |
| 17 | + |
| 18 | +import com.google.common.base.Optional; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.ArrayUtils; |
| 21 | +import org.apache.commons.lang3.SystemUtils; |
| 22 | + |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.Locale; |
| 29 | + |
| 30 | +public final class RPlugin extends AbstractLanguagePlugin { |
| 31 | + |
| 32 | + private static final String CANNOT_RUN_TESTS_MESSAGE = "Failed to run tests."; |
| 33 | + private static final String CANNOT_PARSE_TEST_RESULTS_MESSAGE = "Failed to read test results."; |
| 34 | + private static final String CANNOT_SCAN_EXERCISE_MESSAGE = "Failed to scan exercise."; |
| 35 | + private static final String CANNOT_PARSE_EXERCISE_DESCRIPTION_MESSAGE = |
| 36 | + "Failed to parse exercise description."; |
| 37 | + |
| 38 | + private static Logger log = LoggerFactory.getLogger(RPlugin.class); |
| 39 | + |
| 40 | + public RPlugin() { |
| 41 | + super( |
| 42 | + new ExerciseBuilder(), |
| 43 | + new StudentFileAwareSubmissionProcessor(), |
| 44 | + new StudentFileAwareZipper(), |
| 45 | + new StudentFileAwareUnzipper()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isExerciseTypeCorrect(Path path) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected StudentFilePolicy getStudentFilePolicy(Path projectPath) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getPluginName() { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Optional<ExerciseDesc> scanExercise(Path path, String exerciseName) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public RunResult runTests(Path path) { |
| 70 | + |
| 71 | + ProcessRunner runner = new ProcessRunner(getTestCommand(), path); |
| 72 | + try { |
| 73 | + runner.call(); |
| 74 | + } catch (Exception e) { |
| 75 | + log.error(CANNOT_RUN_TESTS_MESSAGE, e); |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + return new RTestResultParser(path).parse(); |
| 80 | + } catch (IOException e) { |
| 81 | + log.error(CANNOT_PARSE_TEST_RESULTS_MESSAGE, e); |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public ValidationResult checkCodeStyle(Path path, Locale messageLocale) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + private String[] getTestCommand() { |
| 92 | + String[] rscr = new String[] {"Rscript", "-e"}; |
| 93 | + String[] command; |
| 94 | + if (SystemUtils.IS_OS_WINDOWS) { |
| 95 | + command = new String[] {"\"library('tmcRtestrunner');runTestsWithDefault(TRUE)\""}; |
| 96 | + } else { |
| 97 | + command = new String[] {"\"library(tmcRtestrunner);runTests(\"$PWD\", print=TRUE)\""}; |
| 98 | + } |
| 99 | + return ArrayUtils.addAll(rscr, command); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void clean(Path path) { |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments