Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit c9b1953

Browse files
author
Aleksi Salmela
committed
Try to simplify the Workdir code.
1 parent 8d19ec9 commit c9b1953

File tree

9 files changed

+105
-131
lines changed

9 files changed

+105
-131
lines changed

src/main/java/fi/helsinki/cs/tmc/cli/command/InfoCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ private void printLocalInfo(String[] stringArgs) {
9595

9696
if (useWorkingDirectory) {
9797
// if in exercise directory, print info for that exercise.
98-
String exerciseName = getCurrentExercise(workDir);
99-
if (exerciseName == null) {
98+
Exercise exercise = getCurrentExercise(workDir);
99+
if (exercise == null) {
100100
printCourse(info.getCourse());
101101
} else {
102-
printExercise(info.getExercise(exerciseName));
102+
printExercise(exercise);
103103
}
104104
} else {
105105
if (stringArgs.length != 1) {
@@ -212,8 +212,8 @@ private int getCompletedExerciseCount(Course course) {
212212
return completed;
213213
}
214214

215-
private String getCurrentExercise(WorkDir workDir) {
216-
List<String> exercises = workDir.getExerciseNames(false, false);
215+
private Exercise getCurrentExercise(WorkDir workDir) {
216+
List<Exercise> exercises = workDir.getExercises(false, false);
217217
if (exercises.size() == 1) {
218218
return exercises.get(0);
219219
}

src/main/java/fi/helsinki/cs/tmc/cli/command/ListExercisesCommand.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ private String getCourseNameFromCurrentDirectory() {
8282
}
8383

8484
private CourseInfo getCourseInfoFromCurrentDirectory() {
85-
WorkDir workDir = ctx.getWorkDir();
86-
workDir.addPath();
8785
return ctx.getCourseInfo();
8886
}
8987

src/main/java/fi/helsinki/cs/tmc/cli/command/PasteCommand.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public void run(CliContext context, CommandLine args) {
5757
return;
5858
}
5959

60-
List<String> exerciseNames = workdir.getExerciseNames();
61-
if (exerciseNames.size() != 1) {
60+
List<Exercise> exercises = workdir.getExercises();
61+
if (exercises.size() != 1) {
6262
io.errorln("Error: Matched too many exercises.");
6363
printUsage(context);
6464
return;
@@ -76,7 +76,7 @@ public void run(CliContext context, CommandLine args) {
7676
true);
7777
}
7878
}
79-
sendPaste(message, exerciseNames.get(0));
79+
sendPaste(message, exercises.get(0));
8080
}
8181

8282
private boolean parseArgs(CommandLine args) {
@@ -103,24 +103,19 @@ private boolean parseArgs(CommandLine args) {
103103
io.errorln("The path '" + stringArgs[0] + "' is not valid exercise.");
104104
return false;
105105
}
106-
} else {
107-
//TODO replace the following call with workdir.getCurrentExercise()
108-
if (!workdir.addPath()) {
109-
io.errorln("You are not in exercise directory.");
110-
return false;
111-
}
106+
}
107+
if (workdir.getExercises().size() != 1) {
108+
io.errorln("You are not in exercise directory.");
109+
return false;
112110
}
113111
return true;
114112
}
115113

116-
private void sendPaste(String message, String exerciseName) {
114+
private void sendPaste(String message, Exercise exercise) {
117115
if (message == null) {
118116
message = "";
119117
}
120118

121-
CourseInfo info = ctx.getCourseInfo();
122-
Exercise exercise = info.getExercise(exerciseName);
123-
124119
URI uri = TmcUtil.sendPaste(ctx, exercise, message);
125120
if (uri == null && exercise.hasDeadlinePassed()) {
126121
io.errorln("Unable to send the paste."

src/main/java/fi/helsinki/cs/tmc/cli/command/RunTestsCommand.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public void run(CliContext context, CommandLine args) {
6161
}
6262
}
6363

64-
List<String> exerciseNames = workDir.getExerciseNames();
65-
if (exerciseNames.isEmpty()) {
66-
io.errorln("You have to be in a course directory to run tests.");
64+
List<Exercise> exercises = workDir.getExercises();
65+
if (exercises.isEmpty()) {
66+
io.errorln("No exercises specified.");
6767
return;
6868
}
6969

@@ -74,11 +74,10 @@ public void run(CliContext context, CommandLine args) {
7474
ResultPrinter resultPrinter = new ResultPrinter(io, showDetails, showPassed,
7575
passedColor, failedColor);
7676

77-
boolean isOnlyExercise = (exerciseNames.size() == 1);
77+
boolean isOnlyExercise = (exercises.size() == 1);
7878

79-
for (String name : exerciseNames) {
80-
io.println(ColorUtil.colorString("Testing: " + name, Color.YELLOW));
81-
Exercise exercise = info.getExercise(name);
79+
for (Exercise exercise : exercises) {
80+
io.println(ColorUtil.colorString("Testing: " + exercise.getName(), Color.YELLOW));
8281

8382
RunResult runResult = TmcUtil.runLocalTests(ctx, exercise);
8483
if (runResult == null) {

src/main/java/fi/helsinki/cs/tmc/cli/command/SubmitCommand.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public void run(CliContext context, CommandLine args) {
7070
}
7171
}
7272

73-
List<String> exerciseNames;
73+
List<Exercise> exercises;
7474
if (filterUncompleted) {
7575
workDir.addPath(workDir.getCourseDirectory());
76-
exerciseNames = workDir.getExerciseNames(true, true);
76+
exercises = workDir.getExercises(true, true);
7777
} else {
78-
exerciseNames = workDir.getExerciseNames();
78+
exercises = workDir.getExercises();
7979
}
8080

81-
if (exerciseNames.isEmpty()) {
81+
if (exercises.isEmpty()) {
8282
if (filterUncompleted && workDir.getCourseDirectory() != null) {
8383
io.println("No locally tested exercises.");
8484
return;
@@ -98,8 +98,8 @@ public void run(CliContext context, CommandLine args) {
9898
ResultPrinter resultPrinter = new ResultPrinter(io, this.showDetails, this.showAll,
9999
color1, color2);
100100

101-
boolean isOnlyExercise = exerciseNames.size() == 1;
102-
List<Exercise> submitExercises = info.getExercises(exerciseNames);
101+
boolean isOnlyExercise = (exercises.size() == 1);
102+
List<Exercise> submitExercises = exercises;
103103
List<List<FeedbackQuestion>> feedbackLists
104104
= new ArrayList<>();
105105
List<String> exercisesWithFeedback = new ArrayList<>();

src/main/java/fi/helsinki/cs/tmc/cli/io/WorkDir.java

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public Path getConfigFile() {
6767
}
6868
}
6969

70-
public List<String> getExerciseNames() {
71-
return getExerciseNames(true, false);
70+
public List<Exercise> getExercises() {
71+
return getExercises(true, false);
7272
}
7373

7474
/**
@@ -77,55 +77,43 @@ public List<String> getExerciseNames() {
7777
* @param onlyTested Return only exercises that are already tested
7878
* @return: return names of exercises as List
7979
*/
80-
public List<String> getExerciseNames(
81-
boolean exists, boolean onlyTested) {
80+
public List<Exercise> getExercises(boolean exists, boolean onlyTested) {
8281
if (this.directories.isEmpty() && getConfigFile() == null) {
8382
return new ArrayList<>();
8483
}
84+
if (this.directories.isEmpty()) {
85+
addPath(workdir);
86+
}
8587
/*TODO somehow use the ctx.getCourseInfo */
8688
CourseInfo courseinfo = CourseInfoIo.load(getConfigFile());
8789
if (courseinfo == null) {
8890
return new ArrayList<>();
8991
}
90-
List<Exercise> allExercises = courseinfo.getExercises();
91-
List<Exercise> exercises = new ArrayList<>();
92-
List<String> filteredExerciseNames = new ArrayList<>();
93-
List<String> locallyTested = courseinfo.getLocalCompletedExercises();
94-
95-
// Conditions for adding all exercises:
96-
// course dir given as parameter
97-
// working dir is course dir and no parameters
98-
if (directories.contains(getCourseDirectory())
99-
|| (getCourseDirectory().equals(workdir) && directories.size() == 0)) {
100-
exercises.addAll(allExercises);
101-
} else if (workdir.startsWith(getCourseDirectory())
102-
&& !workdir.equals(getCourseDirectory())) {
103-
// if workdir is an exercise directory, add it to directories
104-
directories.add(workdir);
105-
} else if (getCourseDirectory() == null) {
92+
if (getCourseDirectory() == null) {
10693
// if we still don't have a course directory, return an empty list
10794
return new ArrayList<>();
10895
}
10996

97+
List<Exercise> allExercises = courseinfo.getExercises();
98+
List<Exercise> exercises = new ArrayList<>();
99+
List<String> locallyTested = courseinfo.getLocalCompletedExercises();
100+
110101
for (Path dir : directories) {
111102
// convert path to a string relative to the course dir
112103
String exDir = getCourseDirectory().relativize(dir).toString();
104+
exDir = exDir.replace(File.separator, "-");
105+
113106
for (Exercise exercise : allExercises) {
114-
if ((exercise.getName().startsWith(exDir.replace(File.separator, "-"))
115-
|| exDir.replace(File.separator, "-").startsWith(exercise.getName()))
116-
&& !exercises.contains(exercise)) {
117-
exercises.add(exercise);
107+
if ((exercise.getName().startsWith(exDir)
108+
|| exDir.startsWith(exercise.getName()))) {
109+
if (filterExercise(exercise, locallyTested, exists, onlyTested)) {
110+
exercises.add(exercise);
111+
}
118112
}
119113
}
120114
}
121115

122-
for (Exercise exercise : exercises) {
123-
if (filterExercise(exercise, locallyTested, exists, onlyTested)) {
124-
filteredExerciseNames.add(exercise.getName());
125-
}
126-
}
127-
128-
return filteredExerciseNames;
116+
return exercises;
129117
}
130118

131119
private boolean filterExercise(Exercise exercise, List<String> tested,
@@ -172,32 +160,19 @@ public boolean addPath(Path path) {
172160
if (this.courseDirectory != null) {
173161
this.configFile = this.courseDirectory.resolve(CourseInfoIo.COURSE_CONFIG);
174162
return true;
175-
} else {
176-
return false;
177163
}
164+
return false;
178165
}
179166
if (!this.directories.contains(path)) {
180167
this.directories.add(path);
181168
}
182-
if (path.startsWith(this.courseDirectory)) {
183-
return true;
184-
}
185-
return false;
169+
return path.startsWith(this.courseDirectory);
186170
}
187171

188172
public boolean addPath(String path) {
189173
return addPath(Paths.get(path));
190174
}
191175

192-
/**
193-
* Same as addPath(Path path), but adds the current working directory.
194-
* Note that workdir should ONLY be overridden in tests
195-
* Actually this is kind of useless. Remove if it remains unused.
196-
*/
197-
public boolean addPath() {
198-
return addPath(workdir);
199-
}
200-
201176
public int directoryCount() {
202177
return this.directories.size();
203178
}

src/test/java/fi/helsinki/cs/tmc/cli/command/PasteCommandTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class PasteCommandTest {
4747
private TestIo io;
4848
private TmcCore mockCore;
4949
private WorkDir workDir;
50-
private ArrayList<String> exerciseNames;
50+
private ArrayList<Exercise> exercises;
5151
private Exercise exercise;
5252

5353
private final URI pasteUri;
@@ -64,10 +64,10 @@ public void setup() throws URISyntaxException {
6464
workDir = mock(WorkDir.class);
6565
when(workDir.getCourseDirectory()).thenReturn(tempDir);
6666
when(workDir.getConfigFile()).thenReturn((tempDir.resolve(CourseInfoIo.COURSE_CONFIG)));
67-
exerciseNames = new ArrayList<>();
68-
exerciseNames.add("paste-exercise");
69-
when(workDir.getExerciseNames()).thenReturn(exerciseNames);
70-
when(workDir.addPath()).thenReturn(true);
67+
exercise = new Exercise("paste-exercise");
68+
exercises = new ArrayList<>();
69+
exercises.add(exercise);
70+
when(workDir.getExercises()).thenReturn(exercises);
7171
when(workDir.addPath(anyString())).thenReturn(true);
7272

7373
mockCore = mock(TmcCore.class);
@@ -76,7 +76,6 @@ public void setup() throws URISyntaxException {
7676
app = new Application(ctx);
7777

7878
CourseInfo mockCourseInfo = mock(CourseInfo.class);
79-
exercise = new Exercise("paste-exercise");
8079
when(mockCourseInfo.getExercise("paste-exercise")).thenReturn(exercise);
8180

8281
mockStatic(TmcUtil.class);
@@ -208,8 +207,7 @@ public void handlesExceptionWhenCallableFails() {
208207

209208
@Test
210209
public void failsWithNoExercise() {
211-
Mockito.when(workDir.getExerciseNames()).thenReturn(new ArrayList<String>());
212-
Mockito.when(workDir.addPath()).thenReturn(false);
210+
Mockito.when(workDir.getExercises()).thenReturn(new ArrayList<Exercise>());
213211
Mockito.when(workDir.addPath(anyString())).thenReturn(false);
214212
app.run(new String[] {"paste", "-m", "This is a message given as an argument"});
215213

src/test/java/fi/helsinki/cs/tmc/cli/command/RunTestsCommandTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void givesAnErrorMessageIfNotInCourseDirectory() {
100100
workDir.setWorkdir(Paths.get(System.getProperty("java.io.tmpdir")));
101101
String[] args = {"test"};
102102
app.run(args);
103-
io.assertContains("You have to be in a course directory");
103+
io.assertContains("No exercises specified");
104104
}
105105

106106
@Test

0 commit comments

Comments
 (0)