Skip to content

Commit 59a58a5

Browse files
anttihaaptmoo
authored andcommitted
Changed code according to suggestions.
1 parent fc19d17 commit 59a58a5

File tree

44 files changed

+542
-1184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+542
-1184
lines changed

tmc-langs-r/pom.xml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
<version>0.7.7-SNAPSHOT</version>
88
</parent>
99
<artifactId>tmc-langs-r</artifactId>
10-
<version>0.7.7-SNAPSHOT</version>
1110
<packaging>jar</packaging>
1211

1312
<dependencies>
14-
<dependency>
15-
<groupId>fi.helsinki.cs.tmc</groupId>
16-
<artifactId>tmc-langs-framework</artifactId>
17-
<version>0.7.7-SNAPSHOT</version>
18-
</dependency>
19-
<dependency>
20-
<groupId>fi.helsinki.cs.tmc</groupId>
21-
<artifactId>tmc-langs-util</artifactId>
22-
<version>0.7.7-SNAPSHOT</version>
23-
</dependency>
24-
</dependencies>
25-
26-
</project>
13+
<dependency>
14+
<groupId>fi.helsinki.cs.tmc</groupId>
15+
<artifactId>tmc-langs-framework</artifactId>
16+
<version>${project.version}</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>fi.helsinki.cs.tmc</groupId>
20+
<artifactId>tmc-langs-util</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
</dependencies>
24+
</project>

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package fi.helsinki.cs.tmc.langs.r;
32

43
import fi.helsinki.cs.tmc.langs.domain.TestDesc;
@@ -15,23 +14,21 @@
1514
import java.util.List;
1615
import java.util.Map;
1716

17+
public class RExerciseDescParser {
1818

19-
class RExerciseDescParser {
20-
21-
private static Path RESULT_FILE = Paths.get(".available_points.json");
19+
private static final Path RESULT_FILE = Paths.get(".available_points.json");
2220
private static final TypeReference<Map<String, List<String>>> MAP_TYPE_REFERENCE =
2321
new TypeReference<Map<String, List<String>>>() {};
24-
private Path path;
25-
private ObjectMapper mapper;
26-
22+
private final Path path;
23+
private final ObjectMapper mapper;
24+
2725
public RExerciseDescParser(Path path) {
2826
this.path = path;
2927
this.mapper = new ObjectMapper();
3028
}
31-
29+
3230
public ImmutableList<TestDesc> parse() throws IOException {
3331
List<TestDesc> testDescs = new ArrayList<>();
34-
3532
byte[] json = Files.readAllBytes(path.resolve(RESULT_FILE));
3633
Map<String, List<String>> parse = mapper.readValue(json, MAP_TYPE_REFERENCE);
3734

@@ -42,5 +39,4 @@ public ImmutableList<TestDesc> parse() throws IOException {
4239

4340
return ImmutableList.copyOf(testDescs);
4441
}
45-
4642
}
Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,60 @@
11
package fi.helsinki.cs.tmc.langs.r;
22

3-
43
import fi.helsinki.cs.tmc.langs.AbstractLanguagePlugin;
4+
import fi.helsinki.cs.tmc.langs.abstraction.Strategy;
5+
import fi.helsinki.cs.tmc.langs.abstraction.ValidationError;
56
import fi.helsinki.cs.tmc.langs.abstraction.ValidationResult;
67
import fi.helsinki.cs.tmc.langs.domain.ExerciseBuilder;
78
import fi.helsinki.cs.tmc.langs.domain.ExerciseDesc;
89
import fi.helsinki.cs.tmc.langs.domain.RunResult;
10+
import fi.helsinki.cs.tmc.langs.domain.SpecialLogs;
911
import fi.helsinki.cs.tmc.langs.domain.TestDesc;
12+
import fi.helsinki.cs.tmc.langs.domain.TestResult;
1013
import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
1114
import fi.helsinki.cs.tmc.langs.io.sandbox.StudentFileAwareSubmissionProcessor;
1215
import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareUnzipper;
1316
import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareZipper;
14-
1517
import fi.helsinki.cs.tmc.langs.utils.ProcessRunner;
1618

17-
1819
import com.google.common.base.Optional;
1920
import com.google.common.collect.ImmutableList;
20-
21+
import com.google.common.collect.ImmutableMap;
22+
import com.google.common.collect.Maps;
2123

2224
import org.apache.commons.lang3.ArrayUtils;
2325
import org.apache.commons.lang3.SystemUtils;
26+
import org.apache.commons.lang3.exception.ExceptionUtils;
2427

2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
2730

31+
import java.io.File;
2832
import java.io.IOException;
33+
import java.nio.file.Files;
2934
import java.nio.file.Path;
35+
import java.nio.file.Paths;
36+
import java.util.ArrayList;
37+
import java.util.HashMap;
38+
import java.util.List;
3039
import java.util.Locale;
40+
import java.util.Map;
3141

42+
public final class RPlugin extends AbstractLanguagePlugin {
3243

33-
34-
public class RPlugin extends AbstractLanguagePlugin{
35-
36-
// Various static final Path-variables for filepaths
37-
// to various folders and files in a R exercise project here
44+
/**
45+
* R folder contains the actual R files used in the
46+
* project/package. It is automatically included when creating a
47+
* R package but now when making a regular project in RStudio.
48+
*/
3849
private static final Path R_FOLDER_PATH = Paths.get("R");
50+
51+
/**
52+
* test/testthat folder contains the unit testing
53+
* files which use the testThat library for the R project.
54+
*/
3955
private static final Path TEST_FOLDER_PATH = Paths.get("tests");
4056
private static final Path TESTTHAT_FOLDER_PATH = Paths.get("testthat");
41-
private static final Path TESTTHAT_FILE_PATH = Paths.get("testthat.R");
42-
private static final Path DESCRIPTION_PATH = Paths.get("DESCRIPTION");
43-
private static final Path RESULT_R_PATH = Paths.get("result.R");
44-
4557

46-
47-
// Various static final String-variables for
48-
// error messages related to parsing and running R tests here
4958
private static final String CANNOT_RUN_TESTS_MESSAGE = "Failed to run tests.";
5059
private static final String CANNOT_PARSE_TEST_RESULTS_MESSAGE = "Failed to read test results.";
5160
private static final String CANNOT_SCAN_EXERCISE_MESSAGE = "Failed to scan exercise.";
@@ -62,33 +71,14 @@ public RPlugin() {
6271
new StudentFileAwareUnzipper());
6372
}
6473

74+
/**
75+
* NOTE: Files.exists does not seem to be able to verify the R and
76+
* testthat folder's existence if they are empty.
77+
*/
6578
@Override
6679
public boolean isExerciseTypeCorrect(Path path) {
6780
return Files.exists(path.resolve(R_FOLDER_PATH))
68-
|| Files.exists(path.resolve(TEST_FOLDER_PATH).resolve(TESTTHAT_FOLDER_PATH))
69-
|| Files.exists(path.resolve(TEST_FOLDER_PATH).resolve(TESTTHAT_FILE_PATH))
70-
|| Files.exists(path.resolve(DESCRIPTION_PATH));
71-
/*
72-
R folder contains the actual R files used in the
73-
project/package. It is automatically included when creating a
74-
R package but now when making a regular project in RStudio.
75-
76-
test/testthat folder contains the unit testing
77-
files which use the testThat library for the R project.
78-
79-
DESCRIPTION file contains package information.
80-
Included automatically when making a new package, but not
81-
included when making a regular project in RStudio.
82-
83-
.RHistory file contains the history of executed code on
84-
the R terminal. Generated after running code on the R
85-
terminal for the first time.
86-
87-
tmc/result.R contains the call to tmcRtestrunner's runTests function.
88-
89-
NOTE: Files.exists does not seem to be able to verify the R and
90-
testthat folder's existence if they are empty.
91-
*/
81+
|| Files.exists(path.resolve(TEST_FOLDER_PATH).resolve(TESTTHAT_FOLDER_PATH));
9282
}
9383

9484
@Override
@@ -104,43 +94,67 @@ public String getPluginName() {
10494
@Override
10595
public Optional<ExerciseDesc> scanExercise(Path path, String exerciseName) {
10696
ProcessRunner runner = new ProcessRunner(this.getAvailablePointsCommand(), path);
97+
10798
try {
10899
runner.call();
109100
} catch (Exception e) {
110-
System.out.println(e);
111101
log.error(CANNOT_SCAN_EXERCISE_MESSAGE, e);
102+
return Optional.absent();
112103
}
104+
113105
try {
114106
ImmutableList<TestDesc> testDescs = new RExerciseDescParser(path).parse();
115107
return Optional.of(new ExerciseDesc(exerciseName, testDescs));
116108
} catch (IOException e) {
117109
log.error(CANNOT_PARSE_EXERCISE_DESCRIPTION_MESSAGE, e);
118110
}
111+
119112
return Optional.absent();
120113
}
121114

122115
@Override
123116
public RunResult runTests(Path path) {
124-
125117
ProcessRunner runner = new ProcessRunner(getTestCommand(), path);
118+
126119
try {
127120
runner.call();
128121
} catch (Exception e) {
129122
log.error(CANNOT_RUN_TESTS_MESSAGE, e);
123+
return getGenericErrorRunResult(e);
130124
}
131125

132126
try {
133127
return new RTestResultParser(path).parse();
134128
} catch (IOException e) {
135129
log.error(CANNOT_PARSE_TEST_RESULTS_MESSAGE, e);
130+
return getGenericErrorRunResult(e);
136131
}
137-
return null;
132+
}
133+
134+
private RunResult getGenericErrorRunResult(Throwable exception) {
135+
Map<String, byte[]> logMap = new HashMap<>();
136+
byte[] stackTraceAsByteArray = ExceptionUtils.getStackTrace(exception).getBytes();
137+
logMap.put(SpecialLogs.GENERIC_ERROR_MESSAGE, stackTraceAsByteArray);
138+
139+
ImmutableMap<String, byte[]> logs = ImmutableMap.copyOf(logMap);
140+
141+
return new RunResult(RunResult.Status.GENERIC_ERROR,
142+
ImmutableList.copyOf(new ArrayList<TestResult>()), logs);
138143
}
139144

140145
@Override
141-
public ValidationResult checkCodeStyle(Path path, Locale messageLocale) throws UnsupportedOperationException {
142-
// TO DO
143-
return null;
146+
public ValidationResult checkCodeStyle(Path path, Locale messageLocale) {
147+
return new ValidationResult() {
148+
@Override
149+
public Strategy getStrategy() {
150+
return Strategy.DISABLED;
151+
}
152+
153+
@Override
154+
public Map<File, List<ValidationError>> getValidationErrors() {
155+
return Maps.newHashMap();
156+
}
157+
};
144158
}
145159

146160
public String[] getTestCommand() {
@@ -165,9 +179,10 @@ public String[] getAvailablePointsCommand() {
165179
return ArrayUtils.addAll(command, args);
166180
}
167181

168-
182+
/**
183+
* No operation for now. To be possibly implemented later: remove .Rdata, .Rhistory etc
184+
*/
169185
@Override
170186
public void clean(Path path) {
171-
// TO DO
172187
}
173188
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.file.Paths;
77

88
public class RStudentFilePolicy extends ConfigurableStudentFilePolicy {
9+
910
public RStudentFilePolicy(Path configFileParent) {
1011
super(configFileParent);
1112
}

0 commit comments

Comments
 (0)