TestFinder.java revision 2:da1e581c933b
1/* 2 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.test.framework; 27 28import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_CHECK_COMPILE_MSG; 29import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_COMPARE; 30import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_COMPILE_FAIL; 31import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_RUN_FAIL; 32import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_IGNORE_STD_ERROR; 33import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_RUN; 34import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ENABLE_STRICT_MODE; 35import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDES_FILE; 36import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_DIR; 37import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_LIST; 38import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_FRAMEWORK; 39import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_INCLUDES; 40import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_LIST; 41import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ROOTS; 42import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_UNCHECKED_DIR; 43 44import java.io.File; 45import java.io.IOException; 46import java.nio.file.FileSystem; 47import java.nio.file.FileSystems; 48import java.nio.file.FileVisitOption; 49import java.nio.file.FileVisitResult; 50import java.nio.file.Files; 51import java.nio.file.Path; 52import java.nio.file.SimpleFileVisitor; 53import java.nio.file.attribute.BasicFileAttributes; 54import java.util.ArrayList; 55import java.util.Collections; 56import java.util.EnumSet; 57import java.util.HashMap; 58import java.util.HashSet; 59import java.util.List; 60import java.util.Map; 61import java.util.Scanner; 62import java.util.Set; 63import javax.xml.xpath.XPath; 64import javax.xml.xpath.XPathConstants; 65import javax.xml.xpath.XPathExpressionException; 66import javax.xml.xpath.XPathFactory; 67import org.w3c.dom.NodeList; 68import org.xml.sax.InputSource; 69 70/** 71 * Utility class to find/parse script test files and to create 'test' instances. 72 * Actual 'test' object type is decided by clients of this class. 73 */ 74final class TestFinder { 75 private TestFinder() {} 76 77 interface TestFactory<T> { 78 // 'test' instance type is decided by the client. 79 T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments); 80 // place to log messages from TestFinder 81 void log(String mg); 82 } 83 84 85 // finds all tests from configuration and calls TestFactory to create 'test' instance for each script test found 86 static <T> void findAllTests(final List<T> tests, final Set<String> orphans, final TestFactory<T> testFactory) throws Exception { 87 final String framework = System.getProperty(TEST_JS_FRAMEWORK); 88 final String testList = System.getProperty(TEST_JS_LIST); 89 if (testList == null || testList.length() == 0) { 90 // Run the tests under the test roots dir, selected by the 91 // TEST_JS_INCLUDES patterns 92 final String testRootsString = System.getProperty(TEST_JS_ROOTS, "test/script"); 93 if (testRootsString == null || testRootsString.length() == 0) { 94 throw new Exception("Error: " + TEST_JS_ROOTS + " must be set"); 95 } 96 final String testRoots[] = testRootsString.split(" "); 97 final FileSystem fileSystem = FileSystems.getDefault(); 98 final Set<String> testExcludeSet = getExcludeSet(); 99 final Path[] excludePaths = getExcludeDirs(); 100 for (final String root : testRoots) { 101 final Path dir = fileSystem.getPath(root); 102 findTests(framework, dir, tests, orphans, excludePaths, testExcludeSet, testFactory); 103 } 104 } else { 105 // TEST_JS_LIST contains a blank speparated list of test file names. 106 final String strArray[] = testList.split(" "); 107 for (final String ss : strArray) { 108 handleOneTest(framework, new File(ss).toPath(), tests, orphans, testFactory); 109 } 110 } 111 } 112 113 private static boolean inExcludePath(final Path file, final Path[] excludePaths) { 114 if (excludePaths == null) { 115 return false; 116 } 117 118 for (final Path excludePath : excludePaths) { 119 if (file.startsWith(excludePath)) { 120 return true; 121 } 122 } 123 return false; 124 } 125 126 private static <T> void findTests(final String framework, final Path dir, final List<T> tests, final Set<String> orphanFiles, final Path[] excludePaths, final Set<String> excludedTests, final TestFactory<T> factory) throws Exception { 127 final String pattern = System.getProperty(TEST_JS_INCLUDES); 128 final String extension = pattern == null ? "js" : pattern; 129 final Exception[] exceptions = new Exception[1]; 130 final List<String> excludedActualTests = new ArrayList<>(); 131 132 if (! dir.toFile().isDirectory()) { 133 factory.log("WARNING: " + dir + " not found or not a directory"); 134 } 135 136 Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { 137 @Override 138 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { 139 final String fileName = file.getName(file.getNameCount() - 1).toString(); 140 if (fileName.endsWith(extension)) { 141 final String namex = file.toString().replace('\\', '/'); 142 if (!inExcludePath(file, excludePaths) && !excludedTests.contains(file.getFileName().toString())) { 143 try { 144 handleOneTest(framework, file, tests, orphanFiles, factory); 145 } catch (final Exception ex) { 146 exceptions[0] = ex; 147 return FileVisitResult.TERMINATE; 148 } 149 } else { 150 excludedActualTests.add(namex); 151 } 152 } 153 return FileVisitResult.CONTINUE; 154 } 155 }); 156 Collections.sort(excludedActualTests); 157 158 for (final String excluded : excludedActualTests) { 159 factory.log("Excluding " + excluded); 160 } 161 162 if (exceptions[0] != null) { 163 throw exceptions[0]; 164 } 165 } 166 167 private static final String uncheckedDirs[] = System.getProperty(TEST_JS_UNCHECKED_DIR, "test/script/external/test262/").split(" "); 168 169 private static boolean isUnchecked(final Path testFile) { 170 for (final String uncheckedDir : uncheckedDirs) { 171 if (testFile.startsWith(uncheckedDir)) { 172 return true; 173 } 174 } 175 return false; 176 } 177 178 private static <T> void handleOneTest(final String framework, final Path testFile, final List<T> tests, final Set<String> orphans, TestFactory<T> factory) throws Exception { 179 final String name = testFile.getFileName().toString(); 180 181 assert name.lastIndexOf(".js") > 0 : "not a JavaScript: " + name; 182 183 // defaults: testFile is a test and should be run 184 boolean isTest = isUnchecked(testFile); 185 boolean isNotTest = false; 186 boolean shouldRun = true; 187 boolean compileFailure = false; 188 boolean runFailure = false; 189 boolean checkCompilerMsg = false; 190 boolean noCompare = false; 191 boolean ignoreStdError = false; 192 193 final List<String> engineOptions = new ArrayList<>(); 194 final List<String> scriptArguments = new ArrayList<>(); 195 boolean inComment = false; 196 197 try (Scanner scanner = new Scanner(testFile)) { 198 while (scanner.hasNext()) { 199 // TODO: Scan for /ref=file qualifiers, etc, to determine run 200 // behavior 201 String token = scanner.next(); 202 if (token.startsWith("/*")) { 203 inComment = true; 204 } else if (token.endsWith(("*/"))) { 205 inComment = false; 206 } else if (!inComment) { 207 continue; 208 } 209 210 // remove whitespace and trailing semicolons, if any 211 // (trailing semicolons are found in some sputnik tests) 212 token = token.trim(); 213 final int semicolon = token.indexOf(';'); 214 if (semicolon > 0) { 215 token = token.substring(0, semicolon); 216 } 217 switch (token) { 218 case "@test": 219 isTest = true; 220 break; 221 case "@test/fail": 222 isTest = true; 223 compileFailure = true; 224 break; 225 case "@test/compile-error": 226 isTest = true; 227 compileFailure = true; 228 checkCompilerMsg = true; 229 shouldRun = false; 230 break; 231 case "@test/warning": 232 isTest = true; 233 checkCompilerMsg = true; 234 break; 235 case "@test/nocompare": 236 isTest = true; 237 noCompare = true; 238 break; 239 case "@subtest": 240 isTest = false; 241 isNotTest = true; 242 break; 243 case "@runif": 244 if (System.getProperty(scanner.next()) != null) { 245 shouldRun = true; 246 } else { 247 isTest = false; 248 isNotTest = true; 249 } 250 break; 251 case "@run": 252 shouldRun = true; 253 break; 254 case "@run/fail": 255 shouldRun = true; 256 runFailure = true; 257 break; 258 case "@run/ignore-std-error": 259 shouldRun = true; 260 ignoreStdError = true; 261 break; 262 case "@argument": 263 scriptArguments.add(scanner.next()); 264 break; 265 case "@option": 266 engineOptions.add(scanner.next()); 267 break; 268 } 269 270 // negative tests are expected to fail at runtime only 271 // for those tests that are expected to fail at compile time, 272 // add @test/compile-error 273 if (token.equals("@negative") || token.equals("@strict_mode_negative")) { 274 shouldRun = true; 275 runFailure = true; 276 } 277 278 if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) { 279 if (!strictModeEnabled()) { 280 return; 281 } 282 } 283 } 284 } catch (final Exception ignored) { 285 return; 286 } 287 288 if (isTest) { 289 final Map<String, String> testOptions = new HashMap<>(); 290 if (compileFailure) { 291 testOptions.put(OPTIONS_EXPECT_COMPILE_FAIL, "true"); 292 } 293 if (shouldRun) { 294 testOptions.put(OPTIONS_RUN, "true"); 295 } 296 if (runFailure) { 297 testOptions.put(OPTIONS_EXPECT_RUN_FAIL, "true"); 298 } 299 if (checkCompilerMsg) { 300 testOptions.put(OPTIONS_CHECK_COMPILE_MSG, "true"); 301 } 302 if (!noCompare) { 303 testOptions.put(OPTIONS_COMPARE, "true"); 304 } 305 if (ignoreStdError) { 306 testOptions.put(OPTIONS_IGNORE_STD_ERROR, "true"); 307 } 308 309 tests.add(factory.createTest(framework, testFile.toFile(), engineOptions, testOptions, scriptArguments)); 310 } else if (!isNotTest) { 311 orphans.add(name); 312 } 313 } 314 315 private static boolean strictModeEnabled() { 316 return Boolean.getBoolean(TEST_JS_ENABLE_STRICT_MODE); 317 } 318 319 private static Set<String> getExcludeSet() throws XPathExpressionException { 320 final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST); 321 322 String[] testExcludeArray = {}; 323 if (testExcludeList != null) { 324 testExcludeArray = testExcludeList.split(" "); 325 } 326 final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length); 327 for (final String test : testExcludeArray) { 328 testExcludeSet.add(test); 329 } 330 331 final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE); 332 if (testExcludesFile != null && !testExcludesFile.isEmpty()) { 333 try { 334 loadExcludesFile(testExcludesFile, testExcludeSet); 335 } catch (final XPathExpressionException e) { 336 System.err.println("Error: unable to load test excludes from " + testExcludesFile); 337 e.printStackTrace(); 338 throw e; 339 } 340 } 341 return testExcludeSet; 342 } 343 344 private static void loadExcludesFile(final String testExcludesFile, final Set<String> testExcludeSet) throws XPathExpressionException { 345 final XPath xpath = XPathFactory.newInstance().newXPath(); 346 final NodeList testIds = (NodeList)xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET); 347 for (int i = testIds.getLength() - 1; i >= 0; i--) { 348 testExcludeSet.add(testIds.item(i).getNodeValue()); 349 } 350 } 351 352 private static Path[] getExcludeDirs() { 353 final String excludeDirs[] = System.getProperty(TEST_JS_EXCLUDE_DIR, "test/script/currently-failing").split(" "); 354 final Path[] excludePaths = new Path[excludeDirs.length]; 355 final FileSystem fileSystem = FileSystems.getDefault(); 356 int i = 0; 357 for (final String excludeDir : excludeDirs) { 358 excludePaths[i++] = fileSystem.getPath(excludeDir); 359 } 360 return excludePaths; 361 } 362} 363