Skip to content

Commit 89259d6

Browse files
committed
Add support for extra test files
1 parent 3c06b92 commit 89259d6

File tree

4 files changed

+69
-34
lines changed

4 files changed

+69
-34
lines changed

tmc-langs-framework/src/main/java/fi/helsinki/cs/tmc/langs/AbstractLanguagePlugin.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,30 @@ private ImmutableList<Path> searchForExercises(
148148
return listBuilder.build();
149149
}
150150

151+
// TODO: Make extra file handling to apply to all language plugins
151152
@Override
152153
public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path path) {
153154
Configuration configuration = getConfiguration(path);
154-
List<Path> extraStudentFiles = configuration.getExtraStudentFiles();
155-
List<String> extraStudentStrings = new ArrayList<>();
156-
for (Path p : extraStudentFiles) {
157-
extraStudentStrings.add(p.toString());
158-
}
155+
List<String> extraStudentFiles = pathListToStringList(configuration.getExtraStudentFiles());
156+
List<String> extraTestFiles = pathListToStringList(configuration.getExtraTestFiles());
157+
159158
ImmutableList<String> studentFiles =
160-
ImmutableList.<String>builder().add("src").addAll(extraStudentStrings).build();
161-
ImmutableList<String> src = ImmutableList.of("src");
162-
return new ExercisePackagingConfiguration(studentFiles, ImmutableList.of("test"));
159+
ImmutableList.<String>builder().add("src").addAll(extraStudentFiles).build();
160+
ImmutableList<String> testFiles =
161+
ImmutableList.<String>builder().add("test").addAll(extraTestFiles).build();
162+
return new ExercisePackagingConfiguration(studentFiles, testFiles);
163163
}
164164

165165
@Override
166166
public void maybeCopySharedStuff(Path destPath) {
167167
// Ignore by default.
168168
}
169+
170+
private List<String> pathListToStringList(List<Path> paths) {
171+
List<String> strings = new ArrayList<>();
172+
for (Path p : paths) {
173+
strings.add(p.toString());
174+
}
175+
return strings;
176+
}
169177
}

tmc-langs-framework/src/main/java/fi/helsinki/cs/tmc/langs/domain/Configuration.java

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public List<Path> getExtraStudentFiles() {
5353
return tmcProjectYmlParser.parseExtraStudentFiles(path.resolve(TMC_PROJECT_YML));
5454
}
5555

56+
public List<Path> getExtraTestFiles() {
57+
return tmcProjectYmlParser.parseExtraTestFiles(path.resolve(TMC_PROJECT_YML));
58+
}
59+
5660
/**
5761
* Parse options from the path.
5862
*

tmc-langs-framework/src/main/java/fi/helsinki/cs/tmc/langs/utils/TmcProjectYmlParser.java

+29-23
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,16 @@
2121
public final class TmcProjectYmlParser implements ConfigurationParser {
2222

2323
private static final Logger log = LoggerFactory.getLogger(TmcProjectYmlParser.class);
24-
private List<Path> extraStudentFiles;
2524

2625
/**
2726
* Parses a list of extra student files from a <tt>.tmcproject.yml</tt> file.
2827
*/
2928
public List<Path> parseExtraStudentFiles(Path configFilePath) {
29+
return parseExtraFiles("extra_student_files", configFilePath);
30+
}
3031

31-
log.debug("Parsing extra student files from {}", configFilePath);
32-
33-
extraStudentFiles = new ArrayList<>();
34-
35-
Object yamlSpecifications = getYamlSpecs(configFilePath.toAbsolutePath());
36-
37-
if (!(yamlSpecifications instanceof Map)) {
38-
return extraStudentFiles;
39-
}
40-
41-
Map<?, ?> specsAsMap = (Map<?, ?>) yamlSpecifications;
42-
Object fileMap = specsAsMap.get("extra_student_files");
43-
addFiles(fileMap);
44-
45-
return extraStudentFiles;
32+
public List<Path> parseExtraTestFiles(Path configFilePath) {
33+
return parseExtraFiles("extra_test_files", configFilePath);
4634
}
4735

4836
@Override
@@ -63,6 +51,24 @@ public Map<String, ValueObject> parseOptions(Path configFile) {
6351
return options;
6452
}
6553

54+
private List<Path> parseExtraFiles(String key, Path configFilePath) {
55+
log.debug("Parsing " + key + " files from {}", configFilePath);
56+
57+
List<Path> extraFiles = new ArrayList<>();
58+
59+
Object yamlSpecifications = getYamlSpecs(configFilePath.toAbsolutePath());
60+
61+
if (!(yamlSpecifications instanceof Map)) {
62+
return extraFiles;
63+
}
64+
65+
Map<?, ?> specsAsMap = (Map<?, ?>) yamlSpecifications;
66+
Object fileMap = specsAsMap.get(key);
67+
addFiles(fileMap, extraFiles);
68+
69+
return extraFiles;
70+
}
71+
6672
private void parseRecursiveDefinitions(
6773
Map<String, ValueObject> options, Map<?, ?> specsAsMap, String preKey) {
6874
for (Object keyObject : specsAsMap.keySet()) {
@@ -91,25 +97,25 @@ private Object getYamlSpecs(Path path) {
9197
return yaml.load(fileContents);
9298
}
9399

94-
private void addFiles(Object files) {
95-
addAllIfList(files);
96-
addIfString(files);
100+
private void addFiles(Object files, List<Path> extraFiles) {
101+
addAllIfList(files, extraFiles);
102+
addIfString(files, extraFiles);
97103
}
98104

99-
private void addAllIfList(Object files) {
105+
private void addAllIfList(Object files, List<Path> extraFiles) {
100106
if (files instanceof List) {
101107
log.trace("extra_student_files contains a list, parsing");
102108
for (Object value : (List<?>) files) {
103-
addIfString(value);
109+
addIfString(value, extraFiles);
104110
}
105111
}
106112
}
107113

108-
private void addIfString(Object value) {
114+
private void addIfString(Object value, List<Path> extraFiles) {
109115
if (value instanceof String) {
110116
String[] pathParts = ((String) value).split("/");
111117
Path path = constructPathfromArray(pathParts);
112-
extraStudentFiles.add(path);
118+
extraFiles.add(path);
113119
log.trace("Added {} as extra student file", path);
114120
}
115121
}

tmc-langs-java/src/main/java/fi/helsinki/cs/tmc/langs/java/maven/MavenPlugin.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fi.helsinki.cs.tmc.langs.java.maven;
22

33
import fi.helsinki.cs.tmc.langs.domain.CompileResult;
4+
import fi.helsinki.cs.tmc.langs.domain.Configuration;
45
import fi.helsinki.cs.tmc.langs.domain.ExercisePackagingConfiguration;
56
import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
67
import fi.helsinki.cs.tmc.langs.io.sandbox.StudentFileAwareSubmissionProcessor;
@@ -23,6 +24,8 @@
2324
import java.nio.file.Files;
2425
import java.nio.file.Path;
2526
import java.nio.file.Paths;
27+
import java.util.ArrayList;
28+
import java.util.List;
2629

2730
/**
2831
* A {@link fi.helsinki.cs.tmc.langs.LanguagePlugin} that defines the behaviour
@@ -109,11 +112,17 @@ protected TestRunFileAndLogs createRunResultFile(Path path)
109112
result.getStdErr());
110113
}
111114

112-
// TODO: ADD extra student file support to here too
113115
@Override
114116
public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path path) {
115-
return new ExercisePackagingConfiguration(
116-
ImmutableList.of("src/main"), ImmutableList.of("src/test"));
117+
Configuration configuration = getConfiguration(path);
118+
List<String> extraStudentFiles = pathListToStringList(configuration.getExtraStudentFiles());
119+
List<String> extraTestFiles = pathListToStringList(configuration.getExtraTestFiles());
120+
121+
ImmutableList<String> studentFiles =
122+
ImmutableList.<String>builder().add("src/main").addAll(extraStudentFiles).build();
123+
ImmutableList<String> testFiles =
124+
ImmutableList.<String>builder().add("src/test").addAll(extraTestFiles).build();
125+
return new ExercisePackagingConfiguration(studentFiles, testFiles);
117126
}
118127

119128
@Override
@@ -129,4 +138,12 @@ public void clean(Path path) {
129138
log.info("Failed to cleaning maven project at {}", path);
130139
}
131140
}
141+
142+
private List<String> pathListToStringList(List<Path> paths) {
143+
List<String> strings = new ArrayList<>();
144+
for (Path p : paths) {
145+
strings.add(p.toString());
146+
}
147+
return strings;
148+
}
132149
}

0 commit comments

Comments
 (0)