Skip to content

Commit

Permalink
Add support for extra test files
Browse files Browse the repository at this point in the history
  • Loading branch information
nygrenh committed Oct 6, 2017
1 parent 3c06b92 commit 3e34e49
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,30 @@ private ImmutableList<Path> searchForExercises(
return listBuilder.build();
}

// TODO: Make extra file handling to apply to all language plugins
@Override
public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path path) {
Configuration configuration = getConfiguration(path);
List<Path> extraStudentFiles = configuration.getExtraStudentFiles();
List<String> extraStudentStrings = new ArrayList<>();
for (Path p : extraStudentFiles) {
extraStudentStrings.add(p.toString());
}
List<String> extraStudentFiles = pathListToStringList(configuration.getExtraStudentFiles());
List<String> extraTestFiles = pathListToStringList(configuration.getExtraTestFiles());

ImmutableList<String> studentFiles =
ImmutableList.<String>builder().add("src").addAll(extraStudentStrings).build();
ImmutableList<String> src = ImmutableList.of("src");
return new ExercisePackagingConfiguration(studentFiles, ImmutableList.of("test"));
ImmutableList.<String>builder().add("src").addAll(extraStudentFiles).build();
ImmutableList<String> testFiles =
ImmutableList.<String>builder().add("test").addAll(extraTestFiles).build();
return new ExercisePackagingConfiguration(studentFiles, testFiles);
}

@Override
public void maybeCopySharedStuff(Path destPath) {
// Ignore by default.
}

private List<String> pathListToStringList(List<Path> paths) {
List<String> strings = new ArrayList<>();
for (Path p : paths) {
strings.add(p.toString());
}
return strings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public List<Path> getExtraStudentFiles() {
return tmcProjectYmlParser.parseExtraStudentFiles(path.resolve(TMC_PROJECT_YML));
}

public List<Path> getExtraTestFiles() {
return tmcProjectYmlParser.parseExtraTestFiles(path.resolve(TMC_PROJECT_YML));
}

/**
* Parse options from the path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,16 @@
public final class TmcProjectYmlParser implements ConfigurationParser {

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

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

log.debug("Parsing extra student files from {}", configFilePath);

extraStudentFiles = new ArrayList<>();

Object yamlSpecifications = getYamlSpecs(configFilePath.toAbsolutePath());

if (!(yamlSpecifications instanceof Map)) {
return extraStudentFiles;
}

Map<?, ?> specsAsMap = (Map<?, ?>) yamlSpecifications;
Object fileMap = specsAsMap.get("extra_student_files");
addFiles(fileMap);

return extraStudentFiles;
public List<Path> parseExtraTestFiles(Path configFilePath) {
return parseExtraFiles("extra_test_files", configFilePath);
}

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

private List<Path> parseExtraFiles(String key, Path configFilePath) {
log.debug("Parsing " + key + " files from {}", configFilePath);

List<Path> extraFiles = new ArrayList<>();

Object yamlSpecifications = getYamlSpecs(configFilePath.toAbsolutePath());

if (!(yamlSpecifications instanceof Map)) {
return extraFiles;
}

Map<?, ?> specsAsMap = (Map<?, ?>) yamlSpecifications;
Object fileMap = specsAsMap.get(key);
addFiles(fileMap, extraFiles);

return extraFiles;
}

private void parseRecursiveDefinitions(
Map<String, ValueObject> options, Map<?, ?> specsAsMap, String preKey) {
for (Object keyObject : specsAsMap.keySet()) {
Expand Down Expand Up @@ -91,25 +97,25 @@ private Object getYamlSpecs(Path path) {
return yaml.load(fileContents);
}

private void addFiles(Object files) {
addAllIfList(files);
addIfString(files);
private void addFiles(Object files, List<Path> extraFiles) {
addAllIfList(files, extraFiles);
addIfString(files, extraFiles);
}

private void addAllIfList(Object files) {
private void addAllIfList(Object files, List<Path> extraFiles) {
if (files instanceof List) {
log.trace("extra_student_files contains a list, parsing");
for (Object value : (List<?>) files) {
addIfString(value);
addIfString(value, extraFiles);
}
}
}

private void addIfString(Object value) {
private void addIfString(Object value, List<Path> extraFiles) {
if (value instanceof String) {
String[] pathParts = ((String) value).split("/");
Path path = constructPathfromArray(pathParts);
extraStudentFiles.add(path);
extraFiles.add(path);
log.trace("Added {} as extra student file", path);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fi.helsinki.cs.tmc.langs.java.maven;

import fi.helsinki.cs.tmc.langs.domain.CompileResult;
import fi.helsinki.cs.tmc.langs.domain.Configuration;
import fi.helsinki.cs.tmc.langs.domain.ExercisePackagingConfiguration;
import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
import fi.helsinki.cs.tmc.langs.io.sandbox.StudentFileAwareSubmissionProcessor;
Expand All @@ -23,6 +24,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

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

// TODO: ADD extra student file support to here too
@Override
public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path path) {
return new ExercisePackagingConfiguration(
ImmutableList.of("src/main"), ImmutableList.of("src/test"));
Configuration configuration = getConfiguration(path);
List<String> extraStudentFiles = pathListToStringList(configuration.getExtraStudentFiles());
List<String> extraTestFiles = pathListToStringList(configuration.getExtraTestFiles());

ImmutableList<String> studentFiles =
ImmutableList.<String>builder().add("src/main").addAll(extraStudentFiles).build();
ImmutableList<String> testFiles =
ImmutableList.<String>builder().add("src/test").addAll(extraTestFiles).build();
return new ExercisePackagingConfiguration(studentFiles, testFiles);
}

@Override
Expand All @@ -129,4 +138,12 @@ public void clean(Path path) {
log.info("Failed to cleaning maven project at {}", path);
}
}

private List<String> pathListToStringList(List<Path> paths) {
List<String> strings = new ArrayList<>();
for (Path p : paths) {
strings.add(p.toString());
}
return strings;
}
}

0 comments on commit 3e34e49

Please sign in to comment.