Skip to content

Commit e17f44c

Browse files
committed
Extract recuring questions mechanism into a function
1 parent a58d23f commit e17f44c

File tree

2 files changed

+40
-57
lines changed

2 files changed

+40
-57
lines changed

tests/tck-build-logic/src/main/java/org/graalvm/internal/tck/ContributionTask.java

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ public abstract class ContributionTask extends DefaultTask {
4949
@Inject
5050
protected abstract FileSystemOperations getFileSystemOperations();
5151

52-
private Path gradlew;
53-
5452
private final ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).setSerializationInclusion(JsonInclude.Include.NON_NULL);
5553

54+
private Path gradlew;
5655
private Path testsDirectory;
5756
private Path metadataDirectory;
5857

@@ -166,7 +165,7 @@ private void checkPackages(Path testsPath) throws ContributingException {
166165
try {
167166
FilesUtils.findJavaFiles(testsPath, javaFiles);
168167
} catch (IOException e) {
169-
throw new ContributingException("Cannot find java files. Reason: " + e);
168+
throw new ContributingException("Cannot find Java files. Reason: " + e);
170169
}
171170

172171
for (Path file : javaFiles) {
@@ -211,71 +210,29 @@ private Path getResourcesLocation(){
211210

212211
private List<String> getDockerImages() {
213212
ContributingQuestion question = questions.get("docker");
214-
215-
List<String> images = new ArrayList<>();
216-
while (true) {
217-
String nextImage = InteractiveTaskUtils.askQuestion(question.question(), question.help(), answer -> {
218-
if (!answer.equalsIgnoreCase("-") && answer.split(":").length != 2) {
219-
throw new ContributingException("Docker image name not provided in the correct format. Type help for explanation.");
220-
}
221-
222-
return answer;
223-
});
224-
225-
if (nextImage.trim().equalsIgnoreCase("-")) {
226-
break;
213+
return InteractiveTaskUtils.askRecurringQuestions(question.question(), question.help(), 0, answer -> {
214+
if (answer.split(":").length != 2) {
215+
throw new ContributingException("Docker image name not provided in the correct format. Type help for explanation.");
227216
}
228217

229-
images.add(nextImage);
230-
}
231-
232-
return images;
218+
return answer;
219+
});
233220
}
234221

235222
private List<String> getAllowedPackages() {
236223
ContributingQuestion question = questions.get("allowedPackages");
237-
List<String> packages = new ArrayList<>();
238-
while (true) {
239-
String nextPackage = InteractiveTaskUtils.askQuestion(question.question(), question.help(), answer -> answer);
240-
if (nextPackage.trim().equalsIgnoreCase("-")) {
241-
if (packages.isEmpty()) {
242-
InteractiveTaskUtils.printErrorMessage("At least one package must be provided. Type help for explanation.");
243-
continue;
244-
}
245-
246-
break;
247-
}
248-
249-
packages.add(nextPackage);
250-
}
251-
252-
return packages;
224+
return InteractiveTaskUtils.askRecurringQuestions(question.question(), question.help(), 1, answer -> answer);
253225
}
254226

255227
private List<Coordinates> getAdditionalDependencies() {
256228
ContributingQuestion question = questions.get("additionalDependencies");
257-
List<Coordinates> dependencies = new ArrayList<>();
258-
while (true) {
259-
Coordinates dependency = InteractiveTaskUtils.askQuestion(question.question(), question.help(), answer -> {
260-
if (answer.equalsIgnoreCase("-")) {
261-
return null;
262-
}
263-
264-
try {
265-
return CoordinateUtils.fromString(answer);
266-
} catch (IllegalArgumentException ex) {
267-
throw new ContributingException(ex.getMessage());
268-
}
269-
});
270-
271-
if (dependency == null) {
272-
break;
229+
return InteractiveTaskUtils.askRecurringQuestions(question.question(), question.help(), 0, answer -> {
230+
try {
231+
return CoordinateUtils.fromString(answer);
232+
} catch (IllegalArgumentException ex) {
233+
throw new ContributingException(ex.getMessage());
273234
}
274-
275-
dependencies.add(dependency);
276-
}
277-
278-
return dependencies;
235+
});
279236
}
280237

281238
private void createStubs(boolean shouldUpdate){

tests/tck-build-logic/src/main/java/org/graalvm/internal/tck/utils/InteractiveTaskUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.graalvm.internal.tck.exceptions.ContributingException;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
57
import java.util.Scanner;
68

79
public class InteractiveTaskUtils {
@@ -31,6 +33,30 @@ public static <R> R askQuestion(String question, String helpMessage, Contributin
3133
}
3234
}
3335

36+
public static <R> List<R> askRecurringQuestions(String question, String help, int minimalNumberOfAnswers, ContributingHandler<String, R> handleAnswer) {
37+
List<R> answers = new ArrayList<>();
38+
while (true) {
39+
String answer = InteractiveTaskUtils.askQuestion(question, help, a -> a);
40+
if (answer.equalsIgnoreCase("-")) {
41+
if (answers.size() < minimalNumberOfAnswers) {
42+
printErrorMessage("At least " + minimalNumberOfAnswers + " should be provided. Currently provided: " + answers.size());
43+
continue;
44+
}
45+
46+
break;
47+
}
48+
49+
try {
50+
R item = handleAnswer.apply(answer);
51+
answers.add(item);
52+
} catch (ContributingException e) {
53+
printErrorMessage(e.getMessage());
54+
}
55+
}
56+
57+
return answers;
58+
}
59+
3460
public static boolean askYesNoQuestion(String question, String helpMessage, boolean defaultAnswer) {
3561
Scanner scanner = new Scanner(System.in);
3662

0 commit comments

Comments
 (0)