mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-15 13:02:19 +00:00
Be more defensive if provided destPath is wrong
The path could already exist, *but* not be a directory. Nice catch James. Associated tests.
This commit is contained in:
1 parent
04641a3da1
commit
4f2e261505
2 files changed
+53
-7
No files matched your search
@@ -1,6 +1,6 @@
|
||||
package hudson.util;
|
||||
|
||||
import org.junit.After;
|
||||
import org.hamcrest.core.StringContains;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -10,15 +10,22 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.core.StringContains.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class AtomicFileWriterTest {
|
||||
@Rule
|
||||
public TemporaryFolder tmp = new TemporaryFolder();
|
||||
File af;
|
||||
AtomicFileWriter afw;
|
||||
String expectedContent = "hello world";
|
||||
@Rule public TemporaryFolder tmp = new TemporaryFolder();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
@@ -26,6 +33,19 @@ public class AtomicFileWriterTest {
|
||||
afw = new AtomicFileWriter(af.toPath(), Charset.defaultCharset());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void symlinkToDirectory() throws Exception {
|
||||
final File folder = tmp.newFolder();
|
||||
final File containingSymlink = tmp.newFolder();
|
||||
final Path zeSymlink = Files.createSymbolicLink(Paths.get(containingSymlink.getAbsolutePath(), "ze_symlink"),
|
||||
folder.toPath());
|
||||
|
||||
|
||||
final Path childFileInSymlinkToDir = Paths.get(zeSymlink.toString(), "childFileInSymlinkToDir");
|
||||
|
||||
new AtomicFileWriter(childFileInSymlinkToDir, Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFile() throws Exception {
|
||||
// Verify the file we created exists
|
||||
@@ -68,4 +88,21 @@ public class AtomicFileWriterTest {
|
||||
// Then
|
||||
assertTrue(Files.notExists(afw.getTemporaryPath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void badPath() throws Exception {
|
||||
final File newFile = tmp.newFile();
|
||||
File parentExistsAndIsAFile = new File(newFile, "badChild");
|
||||
|
||||
assertTrue(newFile.exists());
|
||||
assertFalse(parentExistsAndIsAFile.exists());
|
||||
|
||||
try {
|
||||
new AtomicFileWriter(parentExistsAndIsAFile.toPath(), Charset.forName("UTF-8"));
|
||||
fail("Expected a failure");
|
||||
} catch (IOException e) {
|
||||
assertThat(e.getMessage(),
|
||||
containsString("exists and is neither a directory nor a symlink to a directory"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user