FileFilter.java revision 3c8ccb384513dd9bae0f98ac516ea36fbaa3173b
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.dumprendertree2; 18 19import android.util.Log; 20 21import java.io.BufferedReader; 22import java.io.File; 23import java.io.FileNotFoundException; 24import java.io.FileReader; 25import java.io.IOException; 26import java.util.ArrayList; 27import java.util.Arrays; 28import java.util.HashSet; 29import java.util.List; 30import java.util.Set; 31 32/** 33 * A utility to filter out some files/directories from the views and tests that run. 34 */ 35public class FileFilter { 36 private static final String LOG_TAG = "FileFilter"; 37 38 private static final String TEST_EXPECTATIONS_TXT_PATH = 39 "platform/android/test_expectations.txt"; 40 41 private static final String TOKEN_SKIP = "SKIP"; 42 private static final String TOKEN_IGNORE_RESULT = "IGNORE_RESULT"; 43 private static final String TOKEN_SLOW = "SLOW"; 44 45 private final Set<String> mSkipList = new HashSet<String>(); 46 private final Set<String> mIgnoreResultList = new HashSet<String>(); 47 private final Set<String> mSlowList = new HashSet<String>(); 48 49 private final String mRootDirPath; 50 51 public FileFilter(String rootDirPath) { 52 /** It may or may not contain a trailing slash */ 53 this.mRootDirPath = rootDirPath; 54 55 reloadConfiguration(); 56 } 57 58 private static final String trimTrailingSlashIfPresent(String path) { 59 File file = new File(path); 60 return file.getPath(); 61 } 62 63 public void reloadConfiguration() { 64 File txt_exp = new File(mRootDirPath, TEST_EXPECTATIONS_TXT_PATH); 65 66 BufferedReader bufferedReader; 67 try { 68 bufferedReader = 69 new BufferedReader(new FileReader(txt_exp)); 70 71 String line; 72 String entry; 73 String[] parts; 74 String path; 75 Set<String> tokens; 76 Boolean skipped; 77 while (true) { 78 line = bufferedReader.readLine(); 79 if (line == null) { 80 break; 81 } 82 83 /** Remove the comment and trim */ 84 entry = line.split("//", 2)[0].trim(); 85 86 /** Omit empty lines, advance to next line */ 87 if (entry.isEmpty()) { 88 continue; 89 } 90 91 /** Split on whitespace into path part and the rest */ 92 parts = entry.split("\\s", 2); 93 94 /** At this point parts.length >= 1 */ 95 if (parts.length == 1) { 96 Log.w(LOG_TAG + "::reloadConfiguration", 97 "There are no options specified for the test!"); 98 continue; 99 } 100 101 path = trimTrailingSlashIfPresent(parts[0]); 102 103 /** Split on whitespace */ 104 tokens = new HashSet<String>(Arrays.asList(parts[1].split("\\s", 0))); 105 106 /** Chose the right collections to add to */ 107 skipped = false; 108 if (tokens.contains(TOKEN_SKIP)) { 109 mSkipList.add(path); 110 skipped = true; 111 } 112 113 /** If test is on skip list we ignore any further options */ 114 if (skipped) { 115 continue; 116 } 117 118 if (tokens.contains(TOKEN_IGNORE_RESULT)) { 119 mIgnoreResultList.add(path); 120 } 121 122 if (tokens.contains(TOKEN_SLOW)) { 123 mSlowList.add(path); 124 } 125 } 126 } catch (FileNotFoundException e) { 127 Log.w(LOG_TAG + "::reloadConfiguration", "File not found: " + txt_exp.getPath()); 128 } catch (IOException e) { 129 Log.e(LOG_TAG + "::reloadConfiguration", "IOException: " + e.getMessage()); 130 } 131 } 132 133 /** 134 * Checks if test is supposed to be skipped. 135 * 136 * <p> 137 * Path given should relative within LayoutTests folder, e.g. fast/dom/foo.html 138 * 139 * @param testPath 140 * - a relative path within LayoutTests folder 141 * @return if the test is supposed to be skipped 142 */ 143 public boolean isSkip(String testPath) { 144 for (String prefix : getPrefixes(testPath)) { 145 if (mSkipList.contains(prefix)) { 146 return true; 147 } 148 } 149 150 return false; 151 } 152 153 /** 154 * Checks if test result is supposed to be ignored. 155 * 156 * <p> 157 * Path given should relative within LayoutTests folder, e.g. fast/dom/foo.html 158 * 159 * @param testPath 160 * - a relative path within LayoutTests folder 161 * @return if the test result is supposed to be ignored 162 */ 163 public boolean isIgnoreRes(String testPath) { 164 for (String prefix : getPrefixes(testPath)) { 165 if (mIgnoreResultList.contains(prefix)) { 166 return true; 167 } 168 } 169 170 return false; 171 } 172 173 /** 174 * Checks if test is slow and should have timeout increased. 175 * 176 * <p> 177 * Path given should relative within LayoutTests folder, e.g. fast/dom/foo.html 178 * 179 * @param testPath 180 * - a relative path within LayoutTests folder 181 * @return if the test is slow and should have timeout increased. 182 */ 183 public boolean isSlow(String testPath) { 184 for (String prefix : getPrefixes(testPath)) { 185 if (mSlowList.contains(prefix)) { 186 return true; 187 } 188 } 189 190 return false; 191 } 192 193 /** 194 * Returns the list of all path prefixes of the given path. 195 * 196 * <p> 197 * e.g. this/is/a/path returns the list: this this/is this/is/a this/is/a/path 198 * 199 * @param path 200 * @return the list of all path prefixes of the given path. 201 */ 202 private static List<String> getPrefixes(String path) { 203 File file = new File(path); 204 List<String> prefixes = new ArrayList<String>(8); 205 206 do { 207 prefixes.add(file.getPath()); 208 file = file.getParentFile(); 209 } while (file != null); 210 211 return prefixes; 212 } 213 214 /** 215 * Checks if the directory may contain tests or contains just helper files. 216 * 217 * @param dirName 218 * @return 219 * if the directory may contain tests 220 */ 221 public static boolean isTestDir(String dirName) { 222 return (!dirName.equals("script-tests") 223 && !dirName.equals("resources") && !dirName.startsWith(".")); 224 } 225 226 /** 227 * Checks if the file is a test. 228 * Currently we run .html and .xhtml tests. 229 * 230 * @param testName 231 * @return 232 * if the file is a test 233 */ 234 public static boolean isTestFile(String testName) { 235 return testName.endsWith(".html") || testName.endsWith(".xhtml"); 236 } 237 238 /** 239 * Return the path to the file relative to the tests root dir 240 * 241 * @param filePath 242 * @return 243 * the path relative to the tests root dir 244 */ 245 public String getRelativePath(String filePath) { 246 File rootDir = new File(mRootDirPath); 247 return filePath.replaceFirst(rootDir.getPath() + File.separator, ""); 248 } 249 250 /** 251 * Return the path to the file relative to the tests root dir 252 * 253 * @param filePath 254 * @return 255 * the path relative to the tests root dir 256 */ 257 public String getRelativePath(File file) { 258 return getRelativePath(file.getAbsolutePath()); 259 } 260 261 public File getAbsoluteFile(String relativePath) { 262 return new File(mRootDirPath, relativePath); 263 } 264 265 public String getAboslutePath(String relativePath) { 266 return getAbsoluteFile(relativePath).getAbsolutePath(); 267 } 268 269 /** 270 * If the path contains extension (e.g .foo at the end of the file) then it changes 271 * this (.foo) into newEnding (so it has to contain the dot if we want to preserve it). 272 * 273 * <p>If the path doesn't contain an extension, it adds the ending to the path. 274 * 275 * @param relativePath 276 * @param newEnding 277 * @return 278 * a new path, containing the newExtension 279 */ 280 public static String setPathEnding(String relativePath, String newEnding) { 281 int dotPos = relativePath.lastIndexOf('.'); 282 if (dotPos == -1) { 283 return relativePath + newEnding; 284 } 285 286 return relativePath.substring(0, dotPos) + newEnding; 287 } 288}