Skip to content

Commit

Permalink
JENKINS-10599 Publish over ssh with flatten creates empty directory
Browse files Browse the repository at this point in the history
Only create the target directory when the first file is copied.
  • Loading branch information
oldelvet committed Aug 5, 2011
1 parent 4e3a5a3 commit ad1c610
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/jenkins/plugins/publish_over/BPTransfer.java
Expand Up @@ -185,20 +185,24 @@ private class DirectoryMaker {
private final BPBuildInfo buildInfo;
private final BPClient client;
private final Set<String> flattenedFileNames = new LinkedHashSet<String>();
private boolean flattenResetCompleted;
private String previousPath;
private String relativeRemoteSubDirectory;

DirectoryMaker(final BPBuildInfo buildInfo, final BPClient client) throws IOException {
this.buildInfo = buildInfo;
this.client = client;
if (flatten) {
resetToSubDirectory();
}
}

public void changeAndMakeDirs(final FilePath filePath) throws IOException, InterruptedException {
if (flatten)
if (flatten) {
assertNotDuplicateFileName(filePath);
if (!flattenResetCompleted) {
// Only create target directory when there is a file to store
resetToSubDirectory();
flattenResetCompleted = true;
}
}
final String relPath = buildInfo.getRelativePath(filePath, removePrefix);
if (LOG.isDebugEnabled())
LOG.debug(Messages.log_pathToFile(filePath.getName(), relPath));
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/jenkins/plugins/publish_over/BPTransferTest.java
Expand Up @@ -148,6 +148,13 @@ private void assertSingleFileInRoot(final String filename, final String pattern)
assertTransfer(transfer, expectedFileCount);
}

@Test public void testNoFiles() throws Exception {
final BPTransfer transfer = new BPTransfer("*.log", "", "", false, false);
// Should not change to/create initial directory on zero file transfer
final int expectedFileCount = 0;
assertTransfer(transfer, expectedFileCount);
}

@Test public void testEnvVarInPattern() throws Exception {
final RandomFile toTransfer = new RandomFile(baseDir.getRoot(), "hello_123.txt");
final BPTransfer transfer = new BPTransfer("hello_${BUILD_NUMBER}.*", "", "", false, false);
Expand Down Expand Up @@ -299,6 +306,14 @@ private void assertCreateDirectories(final RandomFile srcFile, final String remo
assertTransfer(transfer, 2);
}

@Test public void testCreateNoFilesWithDirectories() throws Exception {
final String remoteDir = "remote/root";
final BPTransfer transfer = new BPTransfer("**/*", remoteDir, "", false, false);

// Should not change to/create initial directory on zero file transfer
assertTransfer(transfer, 0);
}

@Test public void testFlatten() throws Exception {
final String srcPath1 = "bit/of/a/trek/to";
final String srcPath2 = "file/somewhere";
Expand All @@ -317,6 +332,14 @@ private void assertCreateDirectories(final RandomFile srcFile, final String remo
assertTransfer(transfer, 2);
}

@Test public void testFlattenWithNoFiles() throws Exception {
final String remoteDir = "remote/root";
final BPTransfer transfer = new BPTransfer("**/*", remoteDir, "", false, true);

// Should not change to/create initial directory on zero file transfer
assertTransfer(transfer, 0);
}

@Test(expected = BapPublisherException.class)
public void testFlattenThrowsExceptionIfTwoFilesHaveSameName() throws Exception {
final String srcPath1 = "bit/of/a/trek/to";
Expand Down Expand Up @@ -353,6 +376,15 @@ public void testFlattenThrowsExceptionIfTwoFilesHaveSameName() throws Exception
assertTransfer(transfer, 2);
}

@Test public void testRemovePrefixNoFiles() throws Exception {
final String prefix = "gonna/remove";

final BPTransfer transfer = new BPTransfer("**/*", "", prefix, false, false);

// Should not change to/create initial directory on zero file transfer
assertTransfer(transfer, 0);
}

@Test public void testRemovePrefixTrailingSlash() throws Exception {
final String prefix = "gonna/remove/";
final String srcPath1 = "but/not/this";
Expand Down

0 comments on commit ad1c610

Please sign in to comment.