Skip to content

Commit

Permalink
Quore regex replacent that contains filename
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Aug 11, 2014
1 parent e924b00 commit e716b89
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/hudson/plugins/ws_cleanup/Cleanup.java
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.tools.ant.DirectoryScanner;
Expand Down Expand Up @@ -117,7 +118,7 @@ public Object invoke(File f, VirtualChannel channel) throws IOException, Interru
private List<String> fixQuotesAndExpand(String fullPath) {
String tempCommand = null;
if (deleteCommand.contains("%s")) {
tempCommand = deleteCommand.replaceAll("%s", "\"" + StringEscapeUtils.escapeJava(fullPath) + "\"");
tempCommand = deleteCommand.replaceAll("%s", "\"" + Matcher.quoteReplacement(fullPath) + "\"");
} else {
tempCommand = deleteCommand + " " + fullPath;
}
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.ws_cleanup;

import static org.junit.Assert.assertTrue;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.tasks.Shell;

import java.util.Collections;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class CleanupTest {

@Rule public JenkinsRule j = new JenkinsRule();

// "IllegalArgumentException: Illegal group reference" observed when filename contained '$';
@Test
public void doNotTreatFilenameAsRegexReplaceWhenUsingCustomCommand() throws Exception {
final String filename = "\\s! Dozen for 5$ only!";

FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut");
p.getBuildersList().add(new Shell("touch '" + filename + "'"));
j.buildAndAssertSuccess(p); // Populate workspace

p.getBuildWrappersList().add(new PreBuildCleanup(Collections.<Pattern>emptyList(), false, null, "rm %s"));
FreeStyleBuild b = j.buildAndAssertSuccess(p);

final String log = b.getLog();
assertTrue(log, log.contains(
"Using command: rm " + b.getWorkspace().getRemote() + "/" + filename
));
}
}

0 comments on commit e716b89

Please sign in to comment.