-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-34122] Exclude output file from itself #19
Changes from all commits
1294911
1e61f00
fda998a
46717d2
a6ee28a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,12 +120,23 @@ public ZipItFileCallable(FilePath zipFile, String glob) { | |
|
||
@Override | ||
public Integer invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException { | ||
String canonicalZip = new File(zipFile.getRemote()).getCanonicalPath(); | ||
System.out.println("zipFileRemote is: " + zipFile.getRemote()); | ||
System.out.println("canonicalZip is: " + canonicalZip); | ||
|
||
Archiver archiver = ArchiverFactory.ZIP.create(zipFile.write()); | ||
FileSet fs = Util.createFileSet(dir, glob); | ||
DirectoryScanner scanner = fs.getDirectoryScanner(new org.apache.tools.ant.Project()); | ||
try { | ||
for (String path : scanner.getIncludedFiles()) { | ||
archiver.visit(new File(dir, path), path); | ||
File toArchive = new File(dir, path).getCanonicalFile(); | ||
if (toArchive.getPath().equals(canonicalZip)) { | ||
System.out.println("Not archiving " + toArchive + " as this is the output zip itself"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
else { | ||
System.out.println("archiving " + toArchive + " as this is not excluded"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
archiver.visit(toArchive, path); | ||
} | ||
} | ||
} finally { | ||
archiver.close(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,10 +42,7 @@ | |
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Tests for {@link ZipStep}. | ||
|
@@ -91,6 +88,59 @@ public void simpleArchivedZip() throws Exception { | |
|
||
} | ||
|
||
@Test | ||
public void shouldNotPutOutputArchiveIntoItself() throws Exception { | ||
|
||
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition( | ||
"node {" + | ||
" writeFile file: 'hello.txt', text: 'Hello world'\n" + | ||
" zip zipFile: 'output.zip', dir: '', glob: '', archive: true\n" + | ||
"}", false)); | ||
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
run = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
j.assertLogContains("Writing zip file", run); | ||
verifyArchivedNotContainingItself(run); | ||
} | ||
|
||
@Test | ||
public void canArchiveFileWithSameName() throws Exception { | ||
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition( | ||
"node {\n" + | ||
" dir ('src') {\n" + | ||
" writeFile file: 'hello.txt', text: 'Hello world'\n" + | ||
" writeFile file: 'output.zip', text: 'not really a zip'\n" + | ||
" }\n" + | ||
" dir ('out') {\n" + | ||
" zip zipFile: 'output.zip', dir: '../src', glob: '', archive: true\n" + | ||
" }\n" + | ||
"}\n", | ||
false)); | ||
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
run = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
|
||
j.assertLogContains("Writing zip file", run); | ||
assertTrue("Build should have artifacts", run.getHasArtifacts()); | ||
Run<WorkflowJob, WorkflowRun>.Artifact artifact = run.getArtifacts().get(0); | ||
assertEquals("output.zip", artifact.getFileName()); | ||
VirtualFile file = run.getArtifactManager().root().child(artifact.relativePath); | ||
ZipInputStream zip = new ZipInputStream(file.open()); | ||
ZipEntry entry = zip.getNextEntry(); | ||
while (entry != null && !entry.getName().equals("output.zip")) { | ||
System.out.println("zip entry name is: " + entry.getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can live with it in a test, though I don't like it much. |
||
entry = zip.getNextEntry(); | ||
} | ||
assertNotNull("output.zip should be included in the zip", entry); | ||
// we should have the the zip - but double check | ||
assertEquals("output.zip", entry.getName()); | ||
Scanner scanner = new Scanner(zip); | ||
assertTrue(scanner.hasNextLine()); | ||
// the file that was not a zip should be included. | ||
assertEquals("not really a zip", scanner.nextLine()); | ||
zip.close(); | ||
} | ||
|
||
@Test | ||
public void globbedArchivedZip() throws Exception { | ||
|
||
|
@@ -153,4 +203,14 @@ private void verifyArchivedHello(WorkflowRun run, String basePath) throws IOExce | |
assertNull("There should be no more entries", zip.getNextEntry()); | ||
zip.close(); | ||
} | ||
|
||
private void verifyArchivedNotContainingItself(WorkflowRun run) throws IOException { | ||
assertTrue("Build should have artifacts", run.getHasArtifacts()); | ||
Run<WorkflowJob, WorkflowRun>.Artifact artifact = run.getArtifacts().get(0); | ||
VirtualFile file = run.getArtifactManager().root().child(artifact.relativePath); | ||
ZipInputStream zip = new ZipInputStream(file.open()); | ||
for(ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { | ||
assertNotEquals("The zip output file shouldn't contain itself", entry.getName(), artifact.getFileName()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this normal to
sysout
instead of logging?