Skip to content

Commit

Permalink
Add tests for empty directory in tar and zip archives (#9809)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Oct 13, 2024
1 parent fbad19e commit 2cbbfe7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
30 changes: 30 additions & 0 deletions core/src/test/java/hudson/util/io/TarArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@
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;
Expand Down Expand Up @@ -121,6 +129,28 @@ private static void run(FilePath dir, String... cmds) throws InterruptedExceptio
}
}

@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
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/hudson/util/io/ZipArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

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;
Expand Down Expand Up @@ -72,4 +80,27 @@ public void huge64bitFile() throws IOException {
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);
}
}

0 comments on commit 2cbbfe7

Please sign in to comment.