mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-15 13:02:19 +00:00
[JENKINS-48405] Use NIO in tryOnceDeleteFile and makeWritable (#3169)
* Use NIO in tryOnceDeleteFile and makeWritable * Don't try to set PosixFileAttributes on Windows * Do not create arbitrary exceptions in makeWritable to fix test failures on Windows * Remove unhelpful layer of exception wrapping * Add test exercising Util#makeWritable in Util#tryOnceDeleteFile * Add test for deleting a non-existant file * Return early if changing permissions with NIO succeeds
This commit is contained in:
1 parent
3a529636cf
commit
1270ba3b40
2 files changed
+62
-57
No files matched your search
@@ -36,6 +36,9 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.FileSystemException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
@@ -287,12 +290,6 @@ public class UtilTest {
|
||||
@Test
|
||||
public void testDeleteFile_onWindows() throws Exception {
|
||||
Assume.assumeTrue(Functions.isWindows());
|
||||
Class<?> c;
|
||||
try {
|
||||
c = Class.forName("java.nio.file.FileSystemException");
|
||||
} catch (ClassNotFoundException x) {
|
||||
throw new AssumptionViolatedException("prior to JDK 7", x);
|
||||
}
|
||||
final int defaultDeletionMax = Util.DELETION_MAX;
|
||||
try {
|
||||
File f = tmp.newFile();
|
||||
@@ -304,7 +301,7 @@ public class UtilTest {
|
||||
Util.deleteFile(f);
|
||||
fail("should not have been deletable");
|
||||
} catch (IOException x) {
|
||||
assertThat(calcExceptionHierarchy(x), hasItem(c));
|
||||
assertThat(calcExceptionHierarchy(x), hasItem(FileSystemException.class));
|
||||
assertThat(x.getMessage(), containsString(f.getPath()));
|
||||
}
|
||||
} finally {
|
||||
@@ -313,6 +310,27 @@ public class UtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFileReadOnly() throws Exception {
|
||||
// Removing the calls to Util#makeWritable in Util#tryOnceDeleteFile should cause this test to fail.
|
||||
Path file = tmp.newFolder().toPath().resolve("file.tmp");
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.createFile(file);
|
||||
// Using old IO so the test can run on Windows.
|
||||
file.getParent().toFile().setWritable(false);
|
||||
file.toFile().setWritable(false);
|
||||
Util.deleteFile(file.toFile());
|
||||
assertFalse(Files.exists(file));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFileDoesNotExist() throws Exception {
|
||||
Path file = tmp.newFolder().toPath().resolve("file.tmp");
|
||||
assertFalse(Files.exists(file));
|
||||
// Should not throw an exception.
|
||||
Util.deleteFile(file.toFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteContentsRecursive() throws Exception {
|
||||
final File dir = tmp.newFolder();
|
||||
|
||||
Reference in new issue
Block a user