Skip to content

Commit 1ef1de2

Browse files
samuvaittmoo
authored andcommitted
runTests implemented and added a way to test to TestMain
1 parent 31b9ab2 commit 1ef1de2

File tree

3 files changed

+154
-4
lines changed

3 files changed

+154
-4
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RTestResultParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private TestResult toTestResult(JsonNode node) {
6767
backTrace.add(line.asText());
6868
}
6969

70-
boolean passed = node.get("status").asText().equals("passed");
70+
boolean passed = node.get("status").asText().equals("pass");
7171

7272
return new TestResult(
7373
node.get("name").asText(),

tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/TestMain.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import fi.helsinki.cs.tmc.langs.domain.RunResult;
44
import fi.helsinki.cs.tmc.langs.domain.TestResult;
5+
import fi.helsinki.cs.tmc.langs.utils.ProcessRunner;
6+
7+
import org.apache.commons.lang3.ArrayUtils;
8+
import org.apache.commons.lang3.SystemUtils;
59

610
import java.io.IOException;
711

@@ -12,21 +16,61 @@ public class TestMain {
1216

1317
/**
1418
* Just testing.
19+
*
1520
* @param args Nothing.
1621
*/
1722
public static void main(String[] args) {
1823

19-
Path path = Paths.get(".");
20-
24+
//For now, add the path you want to test here fully,
25+
//for example: pathToGithubFolder/tmc-r/example_projects/example_project1
26+
String exampleProjectLocation = "/tmc-r/example_projects/example_project1";
27+
Path path = Paths.get(exampleProjectLocation);
28+
RunResult runRes = runTests(path);
29+
printTestResult(runRes);
2130
RunResult rr;
2231

23-
try {
32+
/* try {
2433
rr = new RTestResultParser(path).parse();
2534
for (TestResult tr : rr.testResults) {
2635
System.out.println(tr.toString());
2736
}
2837
} catch (IOException e) {
2938
System.out.println("Something wrong: " + e.getMessage());
39+
}*/
40+
}
41+
42+
public static void printTestResult(RunResult rr) {
43+
for (TestResult tr : rr.testResults) {
44+
System.out.println(tr.toString());
45+
}
46+
}
47+
48+
49+
public static RunResult runTests(Path path) {
50+
51+
ProcessRunner runner = new ProcessRunner(getTestCommand(), path);
52+
try {
53+
runner.call();
54+
} catch (Exception e) {
55+
System.out.println("Something wrong: " + e.getMessage());
56+
}
57+
58+
try {
59+
return new RTestResultParser(path).parse();
60+
} catch (IOException e) {
61+
System.out.println("Something wrong: " + e.getMessage());
62+
}
63+
return null;
64+
}
65+
66+
private static String[] getTestCommand() {
67+
String[] rscr = new String[]{"Rscript", "-e"};
68+
String[] command;
69+
if (SystemUtils.IS_OS_WINDOWS) {
70+
command = new String[] {"\"library('tmcRtestrunner');runTestsWithDefault(TRUE)\""};
71+
} else {
72+
command = new String[] {"\"library(tmcRtestrunner);runTests(\"$PWD\", print=TRUE)\""};
3073
}
74+
return ArrayUtils.addAll(rscr, command);
3175
}
3276
}

0 commit comments

Comments
 (0)