8144009: ToolBox should have a cleanDirectory method
Added cleanDirectory method to ToolBox. Reviewed-by: jjg
This commit is contained in:
parent
68126c8ee9
commit
ea92f6bf06
@ -273,6 +273,34 @@ public class ToolBox {
|
||||
Files.delete(Paths.get(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all content of a directory (but not the directory itself).
|
||||
* @param root the directory to be cleaned
|
||||
*/
|
||||
public void cleanDirectory(Path root) throws IOException {
|
||||
if (!Files.isDirectory(root)) {
|
||||
throw new IOException(root + " is not a directory");
|
||||
}
|
||||
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes a) throws IOException {
|
||||
Files.delete(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
|
||||
if (e != null) {
|
||||
throw e;
|
||||
}
|
||||
if (!dir.equals(root)) {
|
||||
Files.delete(dir);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves a file.
|
||||
* If the given destination exists and is a directory, the file will be moved
|
||||
|
@ -36,6 +36,7 @@
|
||||
* @run main Wrapper CompileCircularSources
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
@ -46,13 +47,11 @@ public class CompileCircularSources extends SJavacTester {
|
||||
}
|
||||
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(BIN);
|
||||
clean(GENSRC, BIN);
|
||||
Files.createDirectories(GENSRC);
|
||||
|
||||
Map<String,Long> previous_bin_state = collectState(BIN);
|
||||
|
||||
ToolBox tb = new ToolBox();
|
||||
tb.writeFile(GENSRC.resolve("alfa/omega/A.java"),
|
||||
"package alfa.omega; public class A { beta.B b; }");
|
||||
tb.writeFile(GENSRC.resolve("beta/B.java"),
|
||||
@ -74,6 +73,5 @@ public class CompileCircularSources extends SJavacTester {
|
||||
BIN + "/beta/B.class",
|
||||
BIN + "/gamma/C.class",
|
||||
BIN + "/javac_state");
|
||||
clean(GENSRC, BIN);
|
||||
}
|
||||
}
|
||||
|
@ -47,9 +47,7 @@ public class CompileExcludingDependency extends SJavacTester {
|
||||
|
||||
// Verify that excluding classes from compilation but not from linking works
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(BIN);
|
||||
clean(GENSRC,BIN);
|
||||
Map<String,Long> previous_bin_state = collectState(BIN);
|
||||
ToolBox tb = new ToolBox();
|
||||
tb.writeFile(GENSRC.resolve("alfa/omega/A.java"),
|
||||
@ -69,6 +67,5 @@ public class CompileExcludingDependency extends SJavacTester {
|
||||
verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,
|
||||
BIN + "/alfa/omega/A.class",
|
||||
BIN + "/javac_state");
|
||||
clean(GENSRC, BIN);
|
||||
}
|
||||
}
|
||||
|
@ -46,8 +46,6 @@ public class CompileWithAtFile extends SJavacTester {
|
||||
}
|
||||
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
ToolBox tb = new ToolBox();
|
||||
tb.writeFile(GENSRC.resolve("list.txt"),
|
||||
"-if */alfa/omega/A.java\n" +
|
||||
"-if */beta/B.java\n" +
|
||||
@ -55,11 +53,11 @@ public class CompileWithAtFile extends SJavacTester {
|
||||
"-d " + BIN + "\n" +
|
||||
"--state-dir=" + BIN + "\n");
|
||||
tb.writeFile(GENSRC.resolve("alfa/omega/A.java"),
|
||||
"package alfa.omega; import beta.B; public class A { B b; }");
|
||||
"package alfa.omega; import beta.B; public class A { B b; }");
|
||||
tb.writeFile(GENSRC.resolve("beta/B.java"),
|
||||
"package beta; public class B { }");
|
||||
"package beta; public class B { }");
|
||||
tb.writeFile(GENSRC.resolve("beta/C.java"),
|
||||
"broken");
|
||||
"broken");
|
||||
|
||||
Files.createDirectory(BIN);
|
||||
Map<String,Long> previous_bin_state = collectState(BIN);
|
||||
@ -71,6 +69,5 @@ public class CompileWithAtFile extends SJavacTester {
|
||||
BIN + "/javac_state",
|
||||
BIN + "/alfa/omega/A.class",
|
||||
BIN + "/beta/B.class");
|
||||
clean(GENSRC, BIN);
|
||||
}
|
||||
}
|
||||
|
@ -49,9 +49,7 @@ public class CompileWithInvisibleSources extends SJavacTester {
|
||||
// gensrc2 contains broken code in beta.B, thus exclude that package
|
||||
// gensrc3 contains a proper beta.B
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(BIN);
|
||||
clean(GENSRC, GENSRC2, GENSRC3, BIN);
|
||||
|
||||
Map<String,Long> previous_bin_state = collectState(BIN);
|
||||
|
||||
@ -82,7 +80,7 @@ public class CompileWithInvisibleSources extends SJavacTester {
|
||||
BIN + "/javac_state");
|
||||
|
||||
System.out.println("----- Compile with exluded beta went well!");
|
||||
clean(BIN);
|
||||
tb.cleanDirectory(BIN);
|
||||
compileExpectFailure(GENSRC.toString(),
|
||||
"-sourcepath", GENSRC2.toString(),
|
||||
"-sourcepath", GENSRC3.toString(),
|
||||
@ -93,6 +91,5 @@ public class CompileWithInvisibleSources extends SJavacTester {
|
||||
SERVER_ARG);
|
||||
|
||||
System.out.println("----- Compile without exluded beta failed, as expected! Good!");
|
||||
clean(GENSRC, BIN);
|
||||
}
|
||||
}
|
||||
|
@ -48,9 +48,7 @@ public class CompileWithOverrideSources extends SJavacTester {
|
||||
// Compile gensrc and gensrc2. However do not compile broken beta.B in gensrc,
|
||||
// only compile ok beta.B in gensrc2
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(BIN);
|
||||
clean(GENSRC, GENSRC2, GENSRC3, BIN);
|
||||
|
||||
Map<String,Long> previous_bin_state = collectState(BIN);
|
||||
ToolBox tb = new ToolBox();
|
||||
@ -80,7 +78,7 @@ public class CompileWithOverrideSources extends SJavacTester {
|
||||
BIN + "/javac_state");
|
||||
|
||||
System.out.println("----- Compile with exluded beta went well!");
|
||||
clean(BIN);
|
||||
tb.cleanDirectory(BIN);
|
||||
compileExpectFailure(GENSRC.toString(),
|
||||
GENSRC2.toString(),
|
||||
"-d", BIN.toString(),
|
||||
@ -90,6 +88,5 @@ public class CompileWithOverrideSources extends SJavacTester {
|
||||
SERVER_ARG);
|
||||
|
||||
System.out.println("----- Compile without exluded beta failed, as expected! Good!");
|
||||
clean(GENSRC, GENSRC2, BIN);
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,6 @@ public class IncCompileChangeNative extends SJavacTester {
|
||||
ToolBox tb = new ToolBox();
|
||||
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(GENSRC);
|
||||
Files.createDirectories(BIN);
|
||||
Files.createDirectories(HEADERS);
|
||||
@ -59,8 +58,6 @@ public class IncCompileChangeNative extends SJavacTester {
|
||||
initialCompile();
|
||||
incrementalCompileDropAllNatives();
|
||||
incrementalCompileAddNative();
|
||||
|
||||
clean(GENSRC, BIN, HEADERS);
|
||||
}
|
||||
|
||||
// Update B.java with one less native method i.e. it has no longer any methods
|
||||
|
@ -51,15 +51,12 @@ public class IncCompileDropClasses extends SJavacTester {
|
||||
ToolBox tb = new ToolBox();
|
||||
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(GENSRC);
|
||||
Files.createDirectories(BIN);
|
||||
Files.createDirectories(HEADERS);
|
||||
|
||||
initialCompile();
|
||||
incrementalCompileDroppingClasses();
|
||||
|
||||
clean(GENSRC, BIN, HEADERS);
|
||||
}
|
||||
|
||||
// Testing that deleting AA.java deletes all generated inner class including AA.class
|
||||
|
@ -50,15 +50,12 @@ public class IncCompileNoChanges extends SJavacTester {
|
||||
Map<String,Long> previous_headers_state;
|
||||
|
||||
void test() throws Exception {
|
||||
clean(Paths.get(getClass().getSimpleName()));
|
||||
Files.createDirectories(GENSRC);
|
||||
Files.createDirectories(BIN);
|
||||
Files.createDirectories(HEADERS);
|
||||
|
||||
initialCompile();
|
||||
incrementalCompileNoChanges();
|
||||
|
||||
clean(GENSRC, BIN, HEADERS);
|
||||
}
|
||||
|
||||
// Testing that no change in sources implies no change in binaries
|
||||
|
@ -51,15 +51,12 @@ public class IncCompileUpdateNative extends SJavacTester {
|
||||
ToolBox tb = new ToolBox();
|
||||
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(GENSRC);
|
||||
Files.createDirectories(BIN);
|
||||
Files.createDirectories(HEADERS);
|
||||
|
||||
initialCompile();
|
||||
incrementalCompileChangeNative();
|
||||
|
||||
clean(GENSRC, BIN, HEADERS);
|
||||
}
|
||||
|
||||
// Update B.java with a new value for the final static annotated with @Native
|
||||
|
@ -46,8 +46,6 @@ public class NoState extends SJavacTester {
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
ToolBox tb = new ToolBox();
|
||||
tb.writeFile(GENSRC.resolve("pkg/A.java"), "package pkg; class A {}");
|
||||
Files.createDirectory(BIN);
|
||||
compile("-d", BIN.toString(),
|
||||
|
@ -47,9 +47,7 @@ public class PermittedArtifact extends SJavacTester {
|
||||
|
||||
//Verify that --permit-artifact=bin works
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Files.createDirectories(BIN);
|
||||
clean(GENSRC, BIN);
|
||||
|
||||
Map<String,Long> previous_bin_state = collectState(BIN);
|
||||
|
||||
@ -73,6 +71,5 @@ public class PermittedArtifact extends SJavacTester {
|
||||
BIN + "/alfa/omega/A.class",
|
||||
BIN + "/alfa/omega/AA.class",
|
||||
BIN + "/javac_state");
|
||||
clean(GENSRC, BIN);
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ public class SJavacTester {
|
||||
+ "portfile=testportfile,"
|
||||
+ "background=false";
|
||||
|
||||
final ToolBox tb = new ToolBox();
|
||||
final Path TEST_ROOT = Paths.get(getClass().getSimpleName());
|
||||
|
||||
// Generated sources that will test aspects of sjavac
|
||||
@ -53,7 +54,6 @@ public class SJavacTester {
|
||||
|
||||
void initialCompile() throws Exception {
|
||||
System.out.println("\nInitial compile of gensrc.");
|
||||
ToolBox tb = new ToolBox();
|
||||
tb.writeFile(GENSRC.resolve("alfa/omega/AINT.java"),
|
||||
"package alfa.omega; public interface AINT { void aint(); }");
|
||||
tb.writeFile(GENSRC.resolve("alfa/omega/A.java"),
|
||||
@ -101,30 +101,6 @@ public class SJavacTester {
|
||||
}
|
||||
}
|
||||
|
||||
void delete(final Path root) throws IOException {
|
||||
if (!Files.exists(root)) return;
|
||||
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
|
||||
{
|
||||
Files.delete(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException
|
||||
{
|
||||
if (e == null) {
|
||||
if (!dir.equals(root)) Files.delete(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
} else {
|
||||
// directory iteration failed
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void compile(String... args) throws Exception {
|
||||
int rc = Main.go(args);
|
||||
if (rc != 0) throw new Exception("Error during compile!");
|
||||
@ -265,10 +241,4 @@ public class SJavacTester {
|
||||
throw new Exception("The dir should not differ! But it does!");
|
||||
}
|
||||
}
|
||||
|
||||
void clean(Path... listOfDirs) throws Exception {
|
||||
for (Path dir : listOfDirs) {
|
||||
delete(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,6 @@ public class StateDir extends SJavacTester {
|
||||
}
|
||||
|
||||
void test() throws Exception {
|
||||
clean(TEST_ROOT);
|
||||
Path BAR = TEST_ROOT.resolve("bar");
|
||||
Files.createDirectories(BAR);
|
||||
Files.createDirectories(BIN);
|
||||
@ -69,6 +68,5 @@ public class StateDir extends SJavacTester {
|
||||
Map<String,Long> new_bar_state = collectState(BAR);
|
||||
verifyThatFilesHaveBeenAdded(previous_bar_state, new_bar_state,
|
||||
BAR + "/javac_state");
|
||||
clean(GENSRC, BIN, BAR);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user