Skip to content

Commit 3a8db97

Browse files
committed
Use FileSystemOperations
1 parent e983d7c commit 3a8db97

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.graalvm.internal.tck.utils.FilesUtils;
1414
import org.graalvm.internal.tck.utils.InteractiveTaskUtils;
1515
import org.gradle.api.DefaultTask;
16+
import org.gradle.api.file.FileSystemOperations;
1617
import org.gradle.api.tasks.TaskAction;
1718
import org.gradle.process.ExecOperations;
1819

@@ -35,6 +36,9 @@ public abstract class ContributionTask extends DefaultTask {
3536
@Inject
3637
protected abstract ExecOperations getExecOperations();
3738

39+
@Inject
40+
protected abstract FileSystemOperations getFileSystemOperations();
41+
3842
private Path gradlew;
3943

4044
private final ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).setSerializationInclusion(JsonInclude.Include.NON_NULL);
@@ -156,7 +160,12 @@ private Path getTestsLocation() {
156160

157161
private void checkPackages(Path testsPath) {
158162
List<Path> javaFiles = new ArrayList<>();
159-
FilesUtils.findJavaFiles(testsPath, javaFiles);
163+
try {
164+
FilesUtils.findJavaFiles(testsPath, javaFiles);
165+
} catch (IOException e) {
166+
throw new RuntimeException("Cannot find java files. Reason: " + e);
167+
}
168+
160169
javaFiles.forEach(file -> {
161170
try {
162171
Optional<String> packageLine = Files.readAllLines(file).stream().filter(line -> line.contains("package ")).findFirst();
@@ -325,21 +334,26 @@ private void addTests(Path originalTestsLocation){
325334
}
326335

327336
InteractiveTaskUtils.printUserInfo("Removing dummy test stubs");
328-
invokeCommand("rm -r " + destination, "Cannot delete files from: " + destination);
337+
getFileSystemOperations().delete(deleteSpec -> deleteSpec.delete(destination));
329338

330339
InteractiveTaskUtils.printUserInfo("Copying tests from: " + originalTestsLocation + " to " + destination);
331-
invokeCommand("cp -a " + allTests + " " + destination, "Cannot copy files to: " + destination);
340+
getFileSystemOperations().copy(copySpec -> {
341+
copySpec.from(allTests);
342+
copySpec.into(destination);
343+
});
332344
}
333345

334346
private void addResources(Path originalResourcesDirectory){
335347
if (originalResourcesDirectory == null) {
336348
return;
337349
}
338350

339-
Path destination = testsDirectory.resolve("src").resolve("test");
351+
Path destination = testsDirectory.resolve("src").resolve("test").resolve("resources");
340352
InteractiveTaskUtils.printUserInfo("Copying resources from: " + originalResourcesDirectory + " to " + destination);
341-
342-
invokeCommand("cp -r " + originalResourcesDirectory + " " + destination, "Cannot copy files to: " + destination);
353+
getFileSystemOperations().copy(copySpec -> {
354+
copySpec.from(originalResourcesDirectory);
355+
copySpec.into(destination);
356+
});
343357
}
344358

345359
private void addDockerImages(List<String> images) throws IOException {
@@ -354,7 +368,7 @@ private void addDockerImages(List<String> images) throws IOException {
354368
}
355369

356370
for (var image : images) {
357-
writeToFile(destination, image.concat("\n"), StandardOpenOption.APPEND);
371+
writeToFile(destination, image.concat(System.lineSeparator()), StandardOpenOption.APPEND);
358372
}
359373
}
360374

@@ -431,7 +445,7 @@ private void addAgentConfigBlock() {
431445
throw new RuntimeException("Cannot find template for the graalvm configuration block");
432446
}
433447

434-
String content = "\n" + (new String(stream.readAllBytes(), StandardCharsets.UTF_8));
448+
String content = System.lineSeparator() + (new String(stream.readAllBytes(), StandardCharsets.UTF_8));
435449
writeToFile(buildFilePath, content, StandardOpenOption.APPEND);
436450
} catch (IOException e) {
437451
throw new RuntimeException("Cannot add agent block into: " + buildFilePath);
@@ -510,10 +524,12 @@ private void removeEmptyConfigFiles() throws IOException {
510524
if (Files.exists(agentExtractedPredefinedClasses)) {
511525
File[] extractedPredefinedClasses = new File(agentExtractedPredefinedClasses.toUri()).listFiles();
512526
if (extractedPredefinedClasses == null || extractedPredefinedClasses.length == 0) {
527+
ensureFileBelongsToProject(agentExtractedPredefinedClasses);
528+
513529
boolean canDelete = InteractiveTaskUtils.askForDeletePermission(agentExtractedPredefinedClasses);
514530
if (canDelete) {
515531
InteractiveTaskUtils.printUserInfo("Removing empty: agent-extracted-predefined-classes");
516-
invokeCommand("rm -r " + agentExtractedPredefinedClasses, "Cannot delete empty config file: " + agentExtractedPredefinedClasses);
532+
getFileSystemOperations().delete(deleteSpec -> deleteSpec.delete(agentExtractedPredefinedClasses));
517533
}
518534
}
519535
}
@@ -526,7 +542,7 @@ private void removeConfigFile(Path path, CONFIG_FILES file, List<CONFIG_FILES> r
526542
boolean canDelete = InteractiveTaskUtils.askForDeletePermission(path);
527543
if (canDelete) {
528544
InteractiveTaskUtils.printUserInfo("Removing empty: " + file.get());
529-
invokeCommand("rm " + path, "Cannot delete empty config file: " + path);
545+
getFileSystemOperations().delete(deleteSpec -> deleteSpec.delete(path));
530546
remainingFiles.remove(file);
531547
}
532548
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ConfigurationStringBuilder append(String content) {
2525
}
2626

2727
public ConfigurationStringBuilder newLine() {
28-
this.append("\n");
28+
this.append(System.lineSeparator());
2929
startedNewLine = true;
3030

3131
return this;
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package org.graalvm.internal.tck.utils;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.FileVisitResult;
46
import java.nio.file.Files;
57
import java.nio.file.Path;
8+
import java.nio.file.SimpleFileVisitor;
9+
import java.nio.file.attribute.BasicFileAttributes;
610
import java.util.List;
711

812
public class FilesUtils {
913

10-
public static void findJavaFiles(Path root, List<Path> result) {
11-
if (Files.exists(root) && Files.isRegularFile(root) && root.toString().endsWith(".java")) {
12-
result.add(root);
13-
return;
14-
}
14+
public static void findJavaFiles(Path root, List<Path> result) throws IOException {
15+
Files.walkFileTree(root, new SimpleFileVisitor<>() {
16+
@Override
17+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
18+
if (Files.isRegularFile(file) && file.toString().endsWith(".java")) {
19+
result.add(file);
20+
}
1521

16-
if (Files.isDirectory(root)) {
17-
File[] content = root.toFile().listFiles();
18-
if (content == null) {
19-
return;
22+
return super.visitFile(file, attrs);
2023
}
21-
22-
for (var file : content) {
23-
findJavaFiles(file.toPath(), result);
24-
}
25-
}
24+
});
2625
}
2726
}

0 commit comments

Comments
 (0)