Skip to content

Commit 991aa17

Browse files
committed
Use object mapper instead of ConfigurationStringBuilder
1 parent f26cf18 commit 991aa17

File tree

3 files changed

+32
-56
lines changed

3 files changed

+32
-56
lines changed

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

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

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.core.util.DefaultIndenter;
6+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
57
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.fasterxml.jackson.databind.SerializationFeature;
79
import org.graalvm.internal.tck.model.MetadataIndexEntry;
@@ -33,6 +35,7 @@ public abstract class ContributionTask extends DefaultTask {
3335
private static final String BUILD_FILE = "build.gradle";
3436
private static final String USER_CODE_FILTER_FILE = "user-code-filter.json";
3537
private static final String REQUIRED_DOCKER_IMAGES_FILE = "required-docker-images.txt";
38+
3639
@Inject
3740
protected abstract ExecOperations getExecOperations();
3841

@@ -322,18 +325,16 @@ private void updateAllowedPackages(List<String> allowedPackages, boolean isAlrea
322325
}
323326

324327
private void addTests(Path originalTestsLocation){
325-
Path destination = testsDirectory.resolve("src")
326-
.resolve("test")
327-
.resolve("java");
328+
Path destination = testsDirectory.resolve("src").resolve("test").resolve("java");
328329
Path allTests = originalTestsLocation.resolve(".");
329330

330331
ensureFileBelongsToProject(destination);
332+
InteractiveTaskUtils.printUserInfo("Removing dummy test stubs");
331333
boolean shouldDelete = InteractiveTaskUtils.askForDeletePermission(destination);
332334
if (!shouldDelete) {
333335
throw new RuntimeException("The task didn't get permission to delete dummy stubs. Cannot proceed with the task execution");
334336
}
335337

336-
InteractiveTaskUtils.printUserInfo("Removing dummy test stubs");
337338
getFileSystemOperations().delete(deleteSpec -> deleteSpec.delete(destination));
338339

339340
InteractiveTaskUtils.printUserInfo("Copying tests from: " + originalTestsLocation + " to " + destination);
@@ -367,36 +368,24 @@ private void addDockerImages(List<String> images) throws IOException {
367368
Files.createFile(destination);
368369
}
369370

370-
for (var image : images) {
371+
for (String image : images) {
371372
writeToFile(destination, image.concat(System.lineSeparator()), StandardOpenOption.APPEND);
372373
}
373374
}
374375

375376
private void addUserCodeFilterFile(List<String> packages) throws IOException {
376377
InteractiveTaskUtils.printUserInfo("Generating " + USER_CODE_FILTER_FILE);
378+
List<Map<String, String>> filterFileRules = new ArrayList<>();
377379

378-
ConfigurationStringBuilder sb = new ConfigurationStringBuilder();
379-
sb.openObject().newLine();
380-
sb.indent();
381-
sb.quote("rules").separateWithSemicolon().openArray().newLine();
382-
sb.indent();
383-
sb.openObject().appendStringProperty("excludeClasses", "**").closeObject().concat().newLine();
384-
for (int i = 0; i < packages.size(); i++) {
385-
String nextPackage = packages.get(i) + ".**";
386-
sb.openObject().appendStringProperty("includeClasses", nextPackage).closeObject();
387-
if (i < packages.size() - 1) {
388-
sb.concat();
389-
}
380+
// add exclude classes
381+
filterFileRules.add(Map.of("excludeClasses", "**"));
390382

391-
sb.newLine();
392-
}
393-
394-
sb.unindent();
395-
sb.closeArray().newLine();
396-
sb.unindent();
397-
sb.closeObject();
383+
// add include classes
384+
packages.forEach(p -> filterFileRules.add(Map.of("includeClasses", p + ".**")));
398385

399-
writeToFile(testsDirectory.resolve(USER_CODE_FILTER_FILE), sb.toString(), StandardOpenOption.CREATE);
386+
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
387+
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
388+
objectMapper.writer(prettyPrinter).writeValue(testsDirectory.resolve(USER_CODE_FILTER_FILE).toFile(), Map.of("rules", filterFileRules));
400389
}
401390

402391
private void addAdditionalDependencies(List<Coordinates> dependencies) {
@@ -436,7 +425,7 @@ private void addAgentConfigBlock() {
436425
InteractiveTaskUtils.printUserInfo("Configuring agent block in: " + BUILD_FILE);
437426

438427
if (!Files.exists(buildFilePath) || !Files.isRegularFile(buildFilePath)) {
439-
throw new RuntimeException("Cannot add additional dependencies to " + buildFilePath + ". Please check if a " + BUILD_FILE + " exists on that location.");
428+
throw new RuntimeException("Cannot add agent block to " + buildFilePath + ". Please check if a " + BUILD_FILE + " exists on that location.");
440429
}
441430

442431

@@ -483,37 +472,37 @@ private void removeEmptyConfigFiles() throws IOException {
483472
List<CONFIG_FILES> remainingFiles = new LinkedList<>(Arrays.asList(CONFIG_FILES.values()));
484473

485474
Path resourceConfigPath = metadataDirectory.resolve(CONFIG_FILES.RESOURCE.get());
486-
ResourceConfigModel resourceConfig = objectMapper.readValue(new File(resourceConfigPath.toUri()), new TypeReference<>() {});
475+
ResourceConfigModel resourceConfig = objectMapper.readValue(resourceConfigPath.toFile(), new TypeReference<>() {});
487476
if (resourceConfig.isEmpty()) {
488477
removeConfigFile(resourceConfigPath, CONFIG_FILES.RESOURCE, remainingFiles);
489478
}
490479

491480
Path serializationConfigPath = metadataDirectory.resolve(CONFIG_FILES.SERIALIZATION.get());
492-
SerializationConfigModel serializationConfig = objectMapper.readValue(new File(serializationConfigPath.toUri()), new TypeReference<>() {});
481+
SerializationConfigModel serializationConfig = objectMapper.readValue(serializationConfigPath.toFile(), new TypeReference<>() {});
493482
if (serializationConfig.isEmpty()) {
494483
removeConfigFile(serializationConfigPath, CONFIG_FILES.SERIALIZATION, remainingFiles);
495484
}
496485

497486
Path jniConfigPath = metadataDirectory.resolve(CONFIG_FILES.JNI.get());
498-
List<Object> jniConfig = objectMapper.readValue(new File(jniConfigPath.toUri()), new TypeReference<>() {});
487+
List<Object> jniConfig = objectMapper.readValue(jniConfigPath.toFile(), new TypeReference<>() {});
499488
if (jniConfig.isEmpty()) {
500489
removeConfigFile(jniConfigPath, CONFIG_FILES.JNI, remainingFiles);
501490
}
502491

503492
Path proxyConfigPath = metadataDirectory.resolve(CONFIG_FILES.PROXY.get());
504-
List<Object> proxyConfig = objectMapper.readValue(new File(proxyConfigPath.toUri()), new TypeReference<>() {});
493+
List<Object> proxyConfig = objectMapper.readValue(proxyConfigPath.toFile(), new TypeReference<>() {});
505494
if (proxyConfig.isEmpty()) {
506495
removeConfigFile(proxyConfigPath, CONFIG_FILES.PROXY, remainingFiles);
507496
}
508497

509498
Path reflectConfigPath = metadataDirectory.resolve(CONFIG_FILES.REFLECTION.get());
510-
List<Object> reflectConfig = objectMapper.readValue(new File(reflectConfigPath.toUri()), new TypeReference<>() {});
499+
List<Object> reflectConfig = objectMapper.readValue(reflectConfigPath.toFile(), new TypeReference<>() {});
511500
if (reflectConfig.isEmpty()) {
512501
removeConfigFile(reflectConfigPath, CONFIG_FILES.REFLECTION, remainingFiles);
513502
}
514503

515504
Path predefinedClassesConfigPath = metadataDirectory.resolve(CONFIG_FILES.PREDEFINED_CLASSES.get());
516-
List<PredefinedClassesConfigModel> predefinedClassesConfig = objectMapper.readValue(new File(predefinedClassesConfigPath.toUri()), new TypeReference<>() {});
505+
List<PredefinedClassesConfigModel> predefinedClassesConfig = objectMapper.readValue(predefinedClassesConfigPath.toFile(), new TypeReference<>() {});
517506
if (predefinedClassesConfig.size() == 1) {
518507
if (predefinedClassesConfig.get(0).isEmpty()) {
519508
removeConfigFile(predefinedClassesConfigPath, CONFIG_FILES.PREDEFINED_CLASSES, remainingFiles);
@@ -522,13 +511,13 @@ private void removeEmptyConfigFiles() throws IOException {
522511

523512
Path agentExtractedPredefinedClasses = metadataDirectory.resolve("agent-extracted-predefined-classes");
524513
if (Files.exists(agentExtractedPredefinedClasses)) {
525-
File[] extractedPredefinedClasses = new File(agentExtractedPredefinedClasses.toUri()).listFiles();
514+
File[] extractedPredefinedClasses = agentExtractedPredefinedClasses.toFile().listFiles();
526515
if (extractedPredefinedClasses == null || extractedPredefinedClasses.length == 0) {
527516
ensureFileBelongsToProject(agentExtractedPredefinedClasses);
528517

518+
InteractiveTaskUtils.printUserInfo("Removing empty: agent-extracted-predefined-classes");
529519
boolean canDelete = InteractiveTaskUtils.askForDeletePermission(agentExtractedPredefinedClasses);
530520
if (canDelete) {
531-
InteractiveTaskUtils.printUserInfo("Removing empty: agent-extracted-predefined-classes");
532521
getFileSystemOperations().delete(deleteSpec -> deleteSpec.delete(agentExtractedPredefinedClasses));
533522
}
534523
}
@@ -539,33 +528,20 @@ private void removeEmptyConfigFiles() throws IOException {
539528

540529
private void removeConfigFile(Path path, CONFIG_FILES file, List<CONFIG_FILES> remainingFiles) {
541530
ensureFileBelongsToProject(path);
531+
532+
InteractiveTaskUtils.printUserInfo("Removing empty: " + file.get());
542533
boolean canDelete = InteractiveTaskUtils.askForDeletePermission(path);
543534
if (canDelete) {
544-
InteractiveTaskUtils.printUserInfo("Removing empty: " + file.get());
545535
getFileSystemOperations().delete(deleteSpec -> deleteSpec.delete(path));
546536
remainingFiles.remove(file);
547537
}
548538
}
549539

550540
private void trimIndexFile(Path index, List<CONFIG_FILES> remainingFiles) throws IOException {
551541
InteractiveTaskUtils.printUserInfo("Removing sufficient entries from: " + index);
552-
ConfigurationStringBuilder sb = new ConfigurationStringBuilder();
553-
sb.openArray().newLine();
554-
sb.indent();
555-
for (int i = 0; i < remainingFiles.size(); i++) {
556-
sb.quote(remainingFiles.get(i).get());
557-
558-
if (i != remainingFiles.size() - 1) {
559-
sb.concat();
560-
}
561-
562-
sb.newLine();
563-
}
564-
565-
sb.unindent();
566-
sb.closeArray();
567-
568-
writeToFile(index, sb.toString(), StandardOpenOption.TRUNCATE_EXISTING);
542+
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
543+
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
544+
objectMapper.writer(prettyPrinter).writeValue(index.toFile(), remainingFiles.stream().map(CONFIG_FILES::get).toList());
569545
}
570546

571547
private boolean shouldCreatePullRequest() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public static boolean askYesNoQuestion(String question, String helpMessage, bool
5454
}
5555

5656
public static boolean askForDeletePermission(Path file) {
57-
String question = "Please double check that the task can remove the following file: " + file +
57+
String question = "The contributing task would like to delete following file: " + file +
5858
". If this file is not generated by the task, please report the error on the metadata repository. " +
59-
"Can the task delete this file? [y/N]";
59+
"Do you confirm that contributing task can delete this file? [y/N]";
6060
String help = "The task should only remove dummy files generated in the previous step. Please verify that the following file can be removed: " + file;
6161

6262
return askYesNoQuestion(question, help, false);

tests/tck-build-logic/src/main/resources/contributing/questions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
},
2727
{
2828
"question-key": "additionalDependencies",
29-
"question": "What additional testImplementation dependencies you want to include? Enter the next dependency (to stop type \"-\")",
30-
"help": "Enter the testImplementation dependencies (press enter after each dependency) you want to include use in tests. Provide dependencies in form of Maven coordinates. Maven coordinates consist of three parts in the following format \"groupId:artifactId:version\". For more information visit: https://maven.apache.org/repositories/artifacts.html When you finish adding dependencies, type \"-\" to terminate the inclusion process"
29+
"question": "What are the additional dependencies you want to include? Enter the next dependency (to stop type \"-\")",
30+
"help": "Enter the additional dependencies (press enter after each dependency) you want to include use in tests. Provide dependencies in form of Maven coordinates. Maven coordinates consist of three parts in the following format \"groupId:artifactId:version\". For more information visit: https://maven.apache.org/repositories/artifacts.html When you finish adding dependencies, type \"-\" to terminate the inclusion process"
3131
},
3232
{
3333
"question-key": "shouldCreatePullRequest",

0 commit comments

Comments
 (0)