@Test public void should_exclude_huge_files() { MapSettings mapSettings = new MapSettings(); final int MAX_SIZE_SETTING = 128; mapSettings.setProperty("sonar.javascript.maxFileSize", "" + MAX_SIZE_SETTING); mapSettings.setProperty(JavaScriptLanguage.FILE_SUFFIXES_KEY, JavaScriptLanguage.FILE_SUFFIXES_DEFVALUE); mapSettings.setProperty(TypeScriptLanguage.FILE_SUFFIXES_KEY, TypeScriptLanguage.FILE_SUFFIXES_DEFVALUE); JavaScriptExclusionsFileFilter filter = new JavaScriptExclusionsFileFilter(mapSettings.asConfig()); final long K = 1000L; long[] sizes = { 10, 10 * K, 100 * K, 150 * K, 200 * K, 800 * K, 2000 * K }; assertThat(sizes[sizes.length - 1]) .withFailMessage("All example sizes are below threshold, the test must be adjusted.") .isGreaterThan(MAX_SIZE_SETTING); for (long size: sizes) { for (String ending : new String[]{"js", "jsx", "ts", "tsx"}) { assertThat(filter.accept(inputFile("name." + ending, syntheticJsFileContent(size)))) .withFailMessage("Wrong result for size " + size + " for file with ending " + ending) .isEqualTo(size < MAX_SIZE_SETTING * K); } assertThat(filter.accept(inputFile("name.java", syntheticJavaFileContent(size, "Foo")))) .withFailMessage("Wrong result for size " + size) .isTrue(); } }
@Override public boolean accept(InputFile inputFile) { boolean isJsTs = JavaScriptLanguage.KEY.equals(inputFile.language()) || TypeScriptLanguage.KEY.equals(inputFile.language()); if (isJsTs && SizeAssessor.hasExcessiveSize(inputFile, maxFileSizeKb * 1000)) { LOG.debug("File {} was excluded because of excessive size", inputFile); return false; } String relativePath = inputFile.uri().toString(); if (WildcardPattern.match(excludedPatterns, relativePath)) { LOG.debug("File {} was excluded by {} or {}", inputFile, JavaScriptPlugin.JS_EXCLUSIONS_KEY, JavaScriptPlugin.TS_EXCLUSIONS_KEY); return false; } boolean isMinified = new MinificationAssessor().isMinified(inputFile); if (isMinified) { LOG.debug("File [" + inputFile.uri() + "] looks like a minified file and will not be analyzed"); return false; } return true; }
JavaScriptExclusionsFileFilter implements InputFileFilter { @Override public boolean accept(InputFile inputFile) { boolean isJsTs = JavaScriptLanguage.KEY.equals(inputFile.language()) || TypeScriptLanguage.KEY.equals(inputFile.language()); if (isJsTs && SizeAssessor.hasExcessiveSize(inputFile, maxFileSizeKb * 1000)) { LOG.debug("File {} was excluded because of excessive size", inputFile); return false; } String relativePath = inputFile.uri().toString(); if (WildcardPattern.match(excludedPatterns, relativePath)) { LOG.debug("File {} was excluded by {} or {}", inputFile, JavaScriptPlugin.JS_EXCLUSIONS_KEY, JavaScriptPlugin.TS_EXCLUSIONS_KEY); return false; } boolean isMinified = new MinificationAssessor().isMinified(inputFile); if (isMinified) { LOG.debug("File [" + inputFile.uri() + "] looks like a minified file and will not be analyzed"); return false; } return true; } }
JavaScriptExclusionsFileFilter implements InputFileFilter { @Override public boolean accept(InputFile inputFile) { boolean isJsTs = JavaScriptLanguage.KEY.equals(inputFile.language()) || TypeScriptLanguage.KEY.equals(inputFile.language()); if (isJsTs && SizeAssessor.hasExcessiveSize(inputFile, maxFileSizeKb * 1000)) { LOG.debug("File {} was excluded because of excessive size", inputFile); return false; } String relativePath = inputFile.uri().toString(); if (WildcardPattern.match(excludedPatterns, relativePath)) { LOG.debug("File {} was excluded by {} or {}", inputFile, JavaScriptPlugin.JS_EXCLUSIONS_KEY, JavaScriptPlugin.TS_EXCLUSIONS_KEY); return false; } boolean isMinified = new MinificationAssessor().isMinified(inputFile); if (isMinified) { LOG.debug("File [" + inputFile.uri() + "] looks like a minified file and will not be analyzed"); return false; } return true; } JavaScriptExclusionsFileFilter(Configuration configuration); }
JavaScriptExclusionsFileFilter implements InputFileFilter { @Override public boolean accept(InputFile inputFile) { boolean isJsTs = JavaScriptLanguage.KEY.equals(inputFile.language()) || TypeScriptLanguage.KEY.equals(inputFile.language()); if (isJsTs && SizeAssessor.hasExcessiveSize(inputFile, maxFileSizeKb * 1000)) { LOG.debug("File {} was excluded because of excessive size", inputFile); return false; } String relativePath = inputFile.uri().toString(); if (WildcardPattern.match(excludedPatterns, relativePath)) { LOG.debug("File {} was excluded by {} or {}", inputFile, JavaScriptPlugin.JS_EXCLUSIONS_KEY, JavaScriptPlugin.TS_EXCLUSIONS_KEY); return false; } boolean isMinified = new MinificationAssessor().isMinified(inputFile); if (isMinified) { LOG.debug("File [" + inputFile.uri() + "] looks like a minified file and will not be analyzed"); return false; } return true; } JavaScriptExclusionsFileFilter(Configuration configuration); @Override boolean accept(InputFile inputFile); }
JavaScriptExclusionsFileFilter implements InputFileFilter { @Override public boolean accept(InputFile inputFile) { boolean isJsTs = JavaScriptLanguage.KEY.equals(inputFile.language()) || TypeScriptLanguage.KEY.equals(inputFile.language()); if (isJsTs && SizeAssessor.hasExcessiveSize(inputFile, maxFileSizeKb * 1000)) { LOG.debug("File {} was excluded because of excessive size", inputFile); return false; } String relativePath = inputFile.uri().toString(); if (WildcardPattern.match(excludedPatterns, relativePath)) { LOG.debug("File {} was excluded by {} or {}", inputFile, JavaScriptPlugin.JS_EXCLUSIONS_KEY, JavaScriptPlugin.TS_EXCLUSIONS_KEY); return false; } boolean isMinified = new MinificationAssessor().isMinified(inputFile); if (isMinified) { LOG.debug("File [" + inputFile.uri() + "] looks like a minified file and will not be analyzed"); return false; } return true; } JavaScriptExclusionsFileFilter(Configuration configuration); @Override boolean accept(InputFile inputFile);  }