Skip to content

Commit fa82612

Browse files
committed
SCMSourceRetrieverTest.lease highlighted that dir2Jar was improperly deleting its source; anyway it needed to be refactored to be simpler and more efficient by directly archiving the desired files
1 parent 6ba9d43 commit fa82612

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryRetriever.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
import edu.umd.cs.findbugs.annotations.CheckForNull;
3737
import edu.umd.cs.findbugs.annotations.NonNull;
3838
import hudson.slaves.WorkspaceList;
39+
import hudson.util.DirScanner;
40+
import hudson.util.FileVisitor;
3941
import hudson.util.io.ArchiverFactory;
42+
import java.io.File;
4043
import java.io.IOException;
4144
import java.io.OutputStream;
4245
import java.nio.file.Path;
@@ -84,35 +87,28 @@ public void retrieveJar(@NonNull String name, @NonNull String version, boolean c
8487
* into a JAR file with Groovy in classpath orientation and {@code resources/} as a ZIP folder.
8588
*/
8689
static void dir2Jar(@NonNull String name, @NonNull FilePath dir, @NonNull FilePath jar) throws IOException, InterruptedException {
87-
// TODO do this more efficiently by packing JAR directly
88-
FilePath tmp = jar.sibling(jar.getBaseName() + "-repack");
89-
tmp.mkdirs();
90+
lookForBadSymlinks(dir, dir);
91+
FilePath mf = jar.withSuffix(".mf");
9092
try {
91-
FilePath src = dir.child("src");
92-
if (src.isDirectory()) {
93-
src.moveAllChildrenTo(tmp);
94-
}
95-
FilePath vars = dir.child("vars");
96-
if (vars.isDirectory()) {
97-
vars.moveAllChildrenTo(tmp);
98-
}
99-
FilePath resources = dir.child("resources");
100-
if (resources.isDirectory()) {
101-
resources.renameTo(tmp.child("resources"));
102-
}
103-
lookForBadSymlinks(tmp, tmp);
104-
try (OutputStream os = tmp.child(JarFile.MANIFEST_NAME).write()) {
93+
try (OutputStream os = mf.write()) {
10594
Manifest m = new Manifest();
10695
m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
10796
// Informational debugging aid, since the hex JAR basename will be meaningless:
10897
m.getMainAttributes().putValue(ATTR_LIBRARY_NAME, name);
10998
m.write(os);
11099
}
111100
try (OutputStream os = jar.write()) {
112-
tmp.archive(ArchiverFactory.ZIP, os, "**");
101+
dir.archive(ArchiverFactory.ZIP, os, new DirScanner() {
102+
@Override public void scan(File dir, FileVisitor visitor) throws IOException {
103+
scanSingle(new File(mf.getRemote()), JarFile.MANIFEST_NAME, visitor);
104+
new DirScanner.Glob("**/*.groovy", null).scan(new File(dir, "src"), visitor);
105+
new DirScanner.Glob("*.groovy,*.txt", null).scan(new File(dir, "vars"), visitor);
106+
new DirScanner.Glob("resources/", null).scan(dir, visitor);
107+
}
108+
});
113109
}
114110
} finally {
115-
tmp.deleteRecursive();
111+
mf.delete();
116112
}
117113
}
118114

src/test/java/org/jenkinsci/plugins/workflow/libs/LibraryRetrieverTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.jar.JarFile;
3434
import org.apache.commons.io.IOUtils;
3535
import static org.hamcrest.MatcherAssert.assertThat;
36+
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
3637
import static org.hamcrest.Matchers.containsInAnyOrder;
3738
import static org.hamcrest.Matchers.is;
3839
import static org.junit.Assert.assertThrows;
@@ -56,6 +57,10 @@ public class LibraryRetrieverTest {
5657
assertDir2Jar(Set.of("src/p1/xxx.groovy", "vars/yyy.groovy", "resources/a.txt", "resources/b/c.txt"), Set.of("p1/xxx.groovy", "yyy.groovy", "resources/a.txt", "resources/b/c.txt"));
5758
}
5859

60+
@Test public void otherFiles() throws Exception {
61+
assertDir2Jar(Set.of("vars/v.groovy", "vars/v.txt", "vars/README.md", "README.md", "docs/usage.png", "src/p1/C.groovy", "src/p1/README.md"), Set.of("v.groovy", "v.txt", "p1/C.groovy"));
62+
}
63+
5964
@Test public void safeSymlinks() throws Exception {
6065
FilePath work = new FilePath(tmp.newFolder());
6166
FilePath dir = work.child("dir");
@@ -79,16 +84,16 @@ public class LibraryRetrieverTest {
7984
assertThrows(SecurityException.class, () -> LibraryRetriever.dir2Jar("mylib", dir, jar));
8085
}
8186

82-
// TODO assert that other files are not copied
83-
8487
private void assertDir2Jar(Set<String> inputs, Set<String> outputs) throws Exception {
8588
FilePath work = new FilePath(tmp.newFolder());
8689
FilePath dir = work.child("dir");
8790
for (String input : inputs) {
8891
dir.child(input).write("xxx", null);
8992
}
9093
FilePath jar = work.child("x.jar");
94+
var before = dir.list("**");
9195
LibraryRetriever.dir2Jar("mylib", dir, jar);
96+
assertThat(dir.list("**"), arrayContainingInAnyOrder(before));
9297
Set<String> actualOutputs = new TreeSet<>();
9398
try (JarFile jf = new JarFile(jar.getRemote())) {
9499
assertThat(jf.getManifest().getMainAttributes().getValue(LibraryRetriever.ATTR_LIBRARY_NAME), is("mylib"));

0 commit comments

Comments
 (0)