mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-10-26 19:15:09 +00:00
Add tests for empty directory in tar and zip archives (#9809)
This commit is contained in:
1 parent
fbad19eecf
commit
2cbbfe784e
2 files changed
+61
No files matched your search
@@ -36,9 +36,17 @@ import hudson.model.TaskListener;
|
||||
import hudson.util.StreamTaskListener;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.apache.tools.tar.TarEntry;
|
||||
import org.apache.tools.tar.TarInputStream;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
@@ -121,6 +129,28 @@ public class TarArchiverTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Ignore("TODO fails to add empty directories to archive")
|
||||
@Issue("JENKINS-73837")
|
||||
@Test
|
||||
public void emptyDirectory() throws Exception {
|
||||
Path tar = tmp.newFile("test.tar").toPath();
|
||||
Path root = tmp.newFolder().toPath();
|
||||
Files.createDirectory(root.resolve("foo"));
|
||||
Files.createDirectory(root.resolve("bar"));
|
||||
Files.writeString(root.resolve("bar/file.txt"), "foobar", StandardCharsets.UTF_8);
|
||||
try (OutputStream out = Files.newOutputStream(tar)) {
|
||||
new FilePath(root.toFile()).tar(out, "**");
|
||||
}
|
||||
Set<String> names = new HashSet<>();
|
||||
try (InputStream is = Files.newInputStream(tar);
|
||||
TarInputStream tis = new TarInputStream(is, StandardCharsets.UTF_8.name())) {
|
||||
TarEntry te;
|
||||
while ((te = tis.getNextEntry()) != null) {
|
||||
names.add(te.getName());
|
||||
}
|
||||
}
|
||||
assertEquals(Set.of("foo/", "bar/", "bar/file.txt"), names);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test backing up an open file
|
||||
|
||||
@@ -2,13 +2,21 @@ package hudson.util.io;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import hudson.FilePath;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
@@ -72,4 +80,27 @@ public class ZipArchiverTest {
|
||||
assertEquals(length, zipEntry.getSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Ignore("TODO fails to add empty directories to archive")
|
||||
@Issue("JENKINS-49296")
|
||||
@Test
|
||||
public void emptyDirectory() throws Exception {
|
||||
Path zip = tmp.newFile("test.zip").toPath();
|
||||
Path root = tmp.newFolder().toPath();
|
||||
Files.createDirectory(root.resolve("foo"));
|
||||
Files.createDirectory(root.resolve("bar"));
|
||||
Files.writeString(root.resolve("bar/file.txt"), "foobar", StandardCharsets.UTF_8);
|
||||
try (OutputStream out = Files.newOutputStream(zip)) {
|
||||
new FilePath(root.toFile()).zip(out, "**");
|
||||
}
|
||||
Set<String> names = new HashSet<>();
|
||||
try (InputStream is = Files.newInputStream(zip);
|
||||
ZipInputStream zis = new ZipInputStream(is, StandardCharsets.UTF_8)) {
|
||||
ZipEntry ze;
|
||||
while ((ze = zis.getNextEntry()) != null) {
|
||||
names.add(ze.getName());
|
||||
}
|
||||
}
|
||||
assertEquals(Set.of("foo/", "bar/", "bar/file.txt"), names);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user