private SensorContextTester(Path moduleBaseDir) { this.settings = new MapSettings(); this.fs = new DefaultFileSystem(moduleBaseDir).setEncoding(Charset.defaultCharset()); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.project = new DefaultInputProject(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.runtime = SonarRuntimeImpl.forSonarQube(ApiVersion.load(System2.INSTANCE), SonarQubeSide.SCANNER); }
private void addFileToFs(DefaultInputFile inputFile) { context.fileSystem().add(inputFile); }
@Test public void testFs() throws Exception { DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder()); tester.setFileSystem(fs); assertThat(tester.fileSystem().baseDir()).isNotEqualTo(baseDir); }
@Test public void test_encoding() throws Exception { fs.setEncoding(Charset.forName("ISO-8859-1")); assertThat(fs.encoding()).isEqualTo(Charset.forName("ISO-8859-1")); }
@Test public void test_directories() throws Exception { assertThat(fs.baseDir()).isAbsolute().isDirectory().exists(); assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath()); File workdir = temp.newFolder(); fs.setWorkDir(workdir.toPath()); assertThat(fs.workDir()).isAbsolute().isDirectory().exists(); assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workdir.getCanonicalPath()); }
@Before public void prepare() throws Exception { basedir = temp.newFolder(); fs = new DefaultFileSystem(basedir.toPath()); }
@Test public void files() { assertThat(fs.inputFiles(fs.predicates().all())).isEmpty(); fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build()); fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build()); // no language fs.add(new TestInputFileBuilder("foo", "src/readme.txt").build()); assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull(); assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull(); assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "src/Bar.java").getAbsolutePath()))).isNotNull(); assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "does/not/exist").getAbsolutePath()))).isNull(); assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "../src/Bar.java").getAbsolutePath()))).isNull(); assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "src/Bar.java").toURI()))).isNotNull(); assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "does/not/exist").toURI()))).isNull(); assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "../src/Bar.java").toURI()))).isNull(); assertThat(fs.files(fs.predicates().all())).hasSize(4); assertThat(fs.files(fs.predicates().hasLanguage("java"))).hasSize(2); assertThat(fs.files(fs.predicates().hasLanguage("cobol"))).isEmpty(); assertThat(fs.hasFiles(fs.predicates().all())).isTrue(); assertThat(fs.hasFiles(fs.predicates().hasLanguage("java"))).isTrue(); assertThat(fs.hasFiles(fs.predicates().hasLanguage("cobol"))).isFalse(); assertThat(fs.inputFiles(fs.predicates().all())).hasSize(4); assertThat(fs.inputFiles(fs.predicates().hasLanguage("php"))).hasSize(1); assertThat(fs.inputFiles(fs.predicates().hasLanguage("java"))).hasSize(2); assertThat(fs.inputFiles(fs.predicates().hasLanguage("cobol"))).isEmpty(); assertThat(fs.languages()).containsOnly("java", "php"); }
@Test public void filesWithDefaultPredicate() { DefaultInputFile file1 = new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build(); fs.add(file1); fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build()); fs.setDefaultPredicate(p -> f -> f.relativePath().endsWith("Foo.php")); Iterator<File> iterator = fs.files(fs.predicates().all()).iterator(); assertThat(iterator.hasNext()).isTrue(); assertThat(iterator.next()).isEqualTo(file1.file()); assertThat(iterator.hasNext()).isFalse(); }
@Test public void input_file_supports_non_indexed_predicates() { fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); // it would fail if more than one java file assertThat(fs.inputFile(fs.predicates().hasLanguage("java"))).isNotNull(); } }
@Override public boolean hasFiles(FilePredicate predicate) { return inputFiles(predicate).iterator().hasNext(); }
static SensorContextTester createContext(int majorVersion, int minorVersion) throws IOException { Path workDir = Files.createTempDirectory("gotemp"); workDir.toFile().deleteOnExit(); Path projectDir = Files.createTempDirectory("goproject"); projectDir.toFile().deleteOnExit(); SensorContextTester context = SensorContextTester.create(workDir); context.fileSystem().setWorkDir(workDir); Path filePath = projectDir.resolve("main.go"); InputFile mainInputFile = TestInputFileBuilder.create("module", projectDir.toFile(), filePath.toFile()) .setCharset(StandardCharsets.UTF_8) .setLanguage(GoLanguage.KEY) .setContents("package main\n" + "import \"fmt\"\n" + "func main() {\n" + " fmt.Println(\"Hello\")\n" + "}\n") .setType(InputFile.Type.MAIN) .build(); context.fileSystem().add(mainInputFile); context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.create(majorVersion, minorVersion), SonarQubeSide.SERVER)); return context; }
@Override public File resolvePath(String path) { File file = new File(path); if (!file.isAbsolute()) { try { file = new File(baseDir(), path).getCanonicalFile(); } catch (IOException e) { throw new IllegalArgumentException("Unable to resolve path '" + path + "'", e); } } return file; } }
@Test public void should_not_export_by_default() throws IOException { File workDir = temp.newFolder("sonar"); fs.setWorkDir(workDir.toPath()); jsonReport.execute(); verifyZeroInteractions(issueCache); }
@Override public Iterable<File> files(FilePredicate predicate) { doPreloadFiles(); return Iterables.transform(inputFiles(predicate), new Function<InputFile, File>() { @Override public File apply(InputFile input) { return input.file(); } }); }
@Override public DefaultFileSystem setEncoding(Charset c) { LOG.debug("Setting filesystem encoding: " + c); return super.setEncoding(c); }
@Override public void start() { if (settings.hasKey(CoreProperties.PROJECT_LANGUAGE_PROPERTY)) { String languageKey = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY); LOG.info("Language is forced to {}", languageKey); Language language = languages.get(languageKey); if (language == null) { throw MessageException.of("You must install a plugin that supports the language '" + languageKey + "'"); } // force the registration of the language, even if there are no related source files fs.addLanguages(languageKey); } }
@Before public void prepare() throws IOException { baseDir = temp.newFolder(); fs = new DefaultFileSystem(baseDir.toPath()); input = mock(BlameInput.class); when(input.fileSystem()).thenReturn(fs); }
@Test public void input_file_fails_if_too_many_results() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("expected one element"); fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build()); fs.inputFile(fs.predicates().all()); }
@Override public Iterable<File> files(FilePredicate predicate) { return () -> StreamSupport.stream(inputFiles(predicate).spliterator(), false) .map(InputFile::file) .iterator(); }