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 org.apache.commons.io.FileUtils;
25 import org.apache.commons.io.FilenameUtils;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.sonar.api.batch.bootstrap.ProjectDefinition;
30 import org.sonar.api.scan.filesystem.PathResolver;
31 import org.sonar.api.utils.TempFolder;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
36 public class ModuleFileSystemInitializerTest {
39 public TemporaryFolder temp = new TemporaryFolder();
41 PathResolver pathResolver = new PathResolver();
44 public void test_default_directories() throws Exception {
45 File baseDir = temp.newFolder("base");
46 File workDir = temp.newFolder("work");
47 ProjectDefinition module = ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(workDir);
49 ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(module, mock(TempFolder.class), pathResolver);
51 assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath());
52 assertThat(initializer.workingDir().getCanonicalPath()).isEqualTo(workDir.getCanonicalPath());
53 assertThat(initializer.sources()).isEmpty();
54 assertThat(initializer.tests()).isEmpty();
58 public void should_init_directories() throws IOException {
59 File baseDir = temp.newFolder("base");
60 File sourceDir = new File(baseDir, "src/main/java");
61 FileUtils.forceMkdir(sourceDir);
62 File testDir = new File(baseDir, "src/test/java");
63 FileUtils.forceMkdir(testDir);
64 File binaryDir = new File(baseDir, "target/classes");
65 FileUtils.forceMkdir(binaryDir);
67 ProjectDefinition project = ProjectDefinition.create()
69 .addSources("src/main/java", "src/main/unknown")
70 .addTests("src/test/java", "src/test/unknown");
72 ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(project, mock(TempFolder.class), pathResolver);
74 assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath());
75 assertThat(initializer.sources()).hasSize(1);
76 assertThat(path(initializer.sources().get(0))).endsWith("src/main/java");
77 assertThat(initializer.tests()).hasSize(1);
78 assertThat(path(initializer.tests().get(0))).endsWith("src/test/java");
82 public void supportFilenamesWithComma() throws IOException {
83 File baseDir = temp.newFolder("base");
84 File sourceFile = new File(baseDir, "my,File.cs");
85 sourceFile.createNewFile();
86 File testFile = new File(baseDir, "my,TestFile.cs");
87 testFile.createNewFile();
89 ProjectDefinition project = ProjectDefinition.create()
91 .addSources("\"my,File.cs\"")
92 .addTests("\"my,TestFile.cs\"");
94 ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(project, mock(TempFolder.class), pathResolver);
96 assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath());
97 assertThat(initializer.sources()).hasSize(1);
98 assertThat(initializer.sources().get(0)).isEqualTo(sourceFile);
99 assertThat(initializer.tests()).hasSize(1);
100 assertThat(initializer.tests().get(0)).isEqualTo(testFile);
103 private String path(File f) throws IOException {
104 return FilenameUtils.separatorsToUnix(f.getCanonicalPath());