3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.scanner.scan.filesystem;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.FilenameUtils;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.TemporaryFolder;
30 import org.sonar.api.batch.bootstrap.ProjectDefinition;
31 import org.sonar.api.batch.fs.internal.DefaultInputModule;
32 import org.sonar.api.utils.log.LogTester;
33 import org.sonar.api.utils.log.LoggerLevel;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class ModuleFileSystemInitializerTest {
40 public TemporaryFolder temp = new TemporaryFolder();
43 public LogTester logTester = new LogTester();
46 public void test_default_directories() throws Exception {
47 File baseDir = temp.newFolder("base");
48 File workDir = temp.newFolder("work");
49 ProjectDefinition module = ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(workDir);
51 ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(new DefaultInputModule(module), module);
53 assertThat(logTester.logs(LoggerLevel.INFO)).contains("Base dir: " + baseDir.toPath().toAbsolutePath().toString());
54 assertThat(logTester.logs(LoggerLevel.INFO)).contains("Working dir: " + workDir.toPath().toAbsolutePath().toString());
55 assertThat(initializer.sources()).isEmpty();
56 assertThat(initializer.tests()).isEmpty();
60 public void should_init_directories() throws IOException {
61 File baseDir = temp.newFolder("base");
62 File workDir = temp.newFolder("work");
63 File sourceDir = new File(baseDir, "src/main/java");
64 FileUtils.forceMkdir(sourceDir);
65 File testDir = new File(baseDir, "src/test/java");
66 FileUtils.forceMkdir(testDir);
67 File binaryDir = new File(baseDir, "target/classes");
68 FileUtils.forceMkdir(binaryDir);
70 ProjectDefinition project = ProjectDefinition.create()
73 .addSources("src/main/java", "src/main/unknown")
74 .addTests("src/test/java", "src/test/unknown");
76 ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(new DefaultInputModule(project), project);
78 assertThat(initializer.sources()).hasSize(1);
79 assertThat(path(initializer.sources().get(0))).endsWith("src/main/java");
80 assertThat(initializer.tests()).hasSize(1);
81 assertThat(path(initializer.tests().get(0))).endsWith("src/test/java");
85 public void supportFilenamesWithComma() throws IOException {
86 File baseDir = temp.newFolder("base");
87 File workDir = temp.newFolder("work");
88 File sourceFile = new File(baseDir, "my,File.cs");
89 sourceFile.createNewFile();
90 File testFile = new File(baseDir, "my,TestFile.cs");
91 testFile.createNewFile();
93 ProjectDefinition project = ProjectDefinition.create()
96 .addSources("\"my,File.cs\"")
97 .addTests("\"my,TestFile.cs\"");
99 ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(new DefaultInputModule(project), project);
101 assertThat(initializer.sources()).hasSize(1);
102 assertThat(initializer.sources().get(0)).isEqualTo(sourceFile.toPath());
103 assertThat(initializer.tests()).hasSize(1);
104 assertThat(initializer.tests().get(0)).isEqualTo(testFile.toPath());
107 private String path(Path f) throws IOException {
108 return FilenameUtils.separatorsToUnix(f.toString());