2 * Copyright 2000-2011 JetBrains s.r.o.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package jetbrains.buildServer.nuget.tests.util.fsScanner;
\r
19 import com.intellij.openapi.util.SystemInfo;
\r
20 import jetbrains.buildServer.BaseTestCase;
\r
21 import jetbrains.buildServer.nuget.agent.runner.publish.fsScanner.DirectoryScanner;
\r
22 import jetbrains.buildServer.util.FileUtil;
\r
23 import org.testng.Assert;
\r
24 import org.testng.annotations.Test;
\r
26 import java.io.File;
\r
27 import java.io.IOException;
\r
28 import java.util.Collection;
\r
29 import java.util.Iterator;
\r
30 import java.util.Set;
\r
31 import java.util.TreeSet;
\r
33 public class TestDirectoryScanner extends BaseTestCase {
\r
34 private void AssertScannerResult(String[] fsDescription, String[] includePatterns, String[] excludePatterns, String[] expectedResult) throws IOException {
\r
35 File fsp = createTempDir();
\r
37 CreateDirectories(fsDescription, fsp);
\r
38 Collection<File> findFiles = DirectoryScanner.FindFiles(fsp, includePatterns, excludePatterns);
\r
40 Set<File> expected = new TreeSet<File>();
\r
41 for (String s : expectedResult) {
\r
42 expected.add(new File(fsp, s));
\r
44 Set<File> actual = new TreeSet<File>();
\r
46 System.out.println("Found: ");
\r
47 for (File file : findFiles) {
\r
49 System.out.println(" " + file);
\r
52 Assert.assertEquals(expected.size(), actual.size());
\r
53 final Iterator<File> eIt = expected.iterator();
\r
54 final Iterator<File> aIt = actual.iterator();
\r
56 for (int i = 0; i < expected.size(); i++)
\r
59 final File eNext = eIt.next();
\r
60 final File aNext = aIt.next();
\r
61 Assert.assertEquals(PreparePath(eNext), PreparePath(aNext));
\r
66 private static void CreateDirectories(String[] fsDescription, File fsp) {
\r
67 for (String f : fsDescription) {
\r
68 File path = new File(fsp, f.substring(2));
\r
69 if (f.startsWith("f:")) {
\r
70 FileUtil.createParentDirs(path);
\r
71 FileUtil.writeFile(path, "text");
\r
72 } else if (f.startsWith("d:"))
\r
75 Assert.fail("Wrong fsDescription: " + f);
\r
81 public void TestSmoke() throws IOException {
\r
82 AssertScannerResult(
\r
91 new String[]{"**/*.exe"},
\r
92 new String[]{"a/a.ca", "a/a.dll"});
\r
96 public void TestEmptyExclude() throws IOException {
\r
97 AssertScannerResult(
\r
105 new String[]{"**"},
\r
107 new String[]{"a/a.ca", "a/a.dll", "a/a.exe"});
\r
111 public void TestEmptyInclude() throws IOException {
\r
112 AssertScannerResult(
\r
121 new String[]{"**"},
\r
126 public void TestBothEmpty() throws IOException {
\r
127 AssertScannerResult(
\r
141 public void TestShouldNotMatchDirectory() throws IOException {
\r
142 AssertScannerResult(
\r
145 "f:a/b/c/d/e/f/g/h/i/p/g/aaa.txt",
\r
146 "d:a/r/c/d/e/f/g/h/i/p/g/aaa.txt",
\r
148 new String[]{"**/*.txt"},
\r
150 new String[]{"a/b/c/d/e/f/g/h/i/p/g/aaa.txt"});
\r
154 public void TestCaseSensitive() throws IOException {
\r
155 if (SystemInfo.isWindows) return;
\r
157 AssertScannerResult(
\r
160 "f:a/b/c/d/e/f/g/h/i/p/g/aAa.txt",
\r
161 "f:a/r/e/a/l/f/g/h/i/p/g/aaa.txt",
\r
163 new String[]{"**/*A*.txt"},
\r
165 new String[]{"a/b/c/d/e/f/g/h/i/p/g/aAa.txt"});
\r
169 public void TestCaseInSensitive() throws IOException {
\r
170 if (SystemInfo.isWindows) return;
\r
172 AssertScannerResult(
\r
175 "f:a/b/c/d/e/f/g/h/i/p/g/aAa.txt",
\r
176 "f:a/r/e/a/l/f/g/h/i/p/g/aaa.txt",
\r
178 new String[]{"**/*A*.txt"},
\r
182 "a/b/c/d/e/f/g/h/i/p/g/aAa.txt",
\r
183 "a/r/e/a/l/f/g/h/i/p/g/aaa.txt",
\r
189 public void AbsoluteIncludePath() throws IOException {
\r
191 File fsp = createTempDir();
\r
192 File atxtFsp = new File(fsp, "a.txt");
\r
193 FileUtil.writeFile(atxtFsp, "text");
\r
195 Collection<File> files = DirectoryScanner.FindFiles(fsp, new String[]{atxtFsp.getPath()}, new String[0]);
\r
196 Assert.assertEquals(1, files.size());
\r
197 Assert.assertEquals(PreparePath(atxtFsp), PreparePath(files.iterator().next()));
\r
200 private static String PreparePath(File f) {
\r
201 String path = f.getPath();
\r
202 path = path.replace('\\', '/');
\r
203 if (!SystemInfo.isWindows) {
\r
206 return path.toLowerCase();
\r