From 5cbe17973bd54c4e433e93ed1c16b5f3d58a142e Mon Sep 17 00:00:00 2001 From: bap2000 Date: Sun, 10 Apr 2011 14:29:55 +0100 Subject: [PATCH] Add method to list with excludes --- core/src/main/java/hudson/FilePath.java | 19 +++++++-- core/src/test/java/hudson/FilePathTest.java | 46 ++++++++++++++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 8b84f09000..f18f560a7c 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -1199,9 +1199,22 @@ public final class FilePath implements Serializable { * can be empty but always non-null. */ public FilePath[] list(final String includes) throws IOException, InterruptedException { + return list(includes, null); + } + + /** + * List up files in this directory that matches the given Ant-style filter. + * + * @param includes + * @param excludes + * See {@link FileSet} for the syntax. String like "foo/*.zip" or "foo/**/*.xml" + * @return + * can be empty but always non-null. + */ + public FilePath[] list(final String includes, final String excludes) throws IOException, InterruptedException { return act(new FileCallable() { public FilePath[] invoke(File f, VirtualChannel channel) throws IOException { - String[] files = glob(f,includes); + String[] files = glob(f,includes,excludes); FilePath[] r = new FilePath[files.length]; for( int i=0; i expected = new HashSet(); + expected.add(createFilePath(baseDir, "top", "sub", "app.log")); + expected.add(createFilePath(baseDir, "top", "sub", "trace.log")); + expected.add(createFilePath(baseDir, "top", "db", "db.log")); + expected.add(createFilePath(baseDir, "top", "db", "trace.log")); + final FilePath[] result = new FilePath(baseDir).list("**"); + assertEquals(expected, new HashSet(Arrays.asList(result))); + } finally { + Util.deleteRecursive(baseDir); + } + } + + public void testListWithExcludes() throws Exception { + File baseDir = Util.createTempDir(); + try { + final Set expected = new HashSet(); + expected.add(createFilePath(baseDir, "top", "sub", "app.log")); + createFilePath(baseDir, "top", "sub", "trace.log"); + expected.add(createFilePath(baseDir, "top", "db", "db.log")); + createFilePath(baseDir, "top", "db", "trace.log"); + final FilePath[] result = new FilePath(baseDir).list("**", "**/trace.log"); + assertEquals(expected, new HashSet(Arrays.asList(result))); + } finally { + Util.deleteRecursive(baseDir); + } + } + }