public ProjectDefinition addSourceFiles(String... paths) {
// Hack for visual studio project builder that used to add baseDir first as source dir
List<String> sourceDirs = getSourceDirs();
- if (sourceDirs.size() == 1 && new File(sourceDirs.get(0)).isDirectory()) {
+ if (sourceDirs.size() == 1 && isDirectory(sourceDirs.get(0))) {
resetSources();
}
return addSources(paths);
public ProjectDefinition addSourceFiles(File... files) {
// Hack for visual studio project builder that used to add baseDir first as source dir
List<String> sourceDirs = getSourceDirs();
- if (sourceDirs.size() == 1 && new File(sourceDirs.get(0)).isDirectory()) {
+ if (sourceDirs.size() == 1 && isDirectory(sourceDirs.get(0))) {
resetSources();
}
return addSources(files);
public ProjectDefinition addTestFiles(String... paths) {
// Hack for visual studio project builder that used to add baseDir first as test dir
List<String> testDirs = getTestDirs();
- if (testDirs.size() == 1 && new File(testDirs.get(0)).isDirectory()) {
+ if (testDirs.size() == 1 && isDirectory(testDirs.get(0))) {
resetTests();
}
return addTests(paths);
}
+ private boolean isDirectory(String relativeOrAbsoluteDir) {
+ File file = new File(relativeOrAbsoluteDir);
+ if (!file.isAbsolute()) {
+ file = new File(baseDir, relativeOrAbsoluteDir);
+ }
+ return file.isDirectory();
+ }
+
/**
* @deprecated since 4.5 use {@link #addTests(File...)}
*/
@Deprecated
public ProjectDefinition addTestFiles(File... files) {
+ // Hack for visual studio project builder that used to add baseDir first as test dir
+ List<String> testDirs = getTestDirs();
+ if (testDirs.size() == 1 && isDirectory(testDirs.get(0))) {
+ resetTests();
+ }
return addTests(files);
}
*/
package org.sonar.api.batch.bootstrap;
-import static org.hamcrest.Matchers.nullValue;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.CoreProperties;
import java.io.File;
+import java.io.IOException;
import java.util.List;
import java.util.Properties;
-import org.junit.Test;
-import org.sonar.api.CoreProperties;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
public class ProjectDefinitionTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
@Test
public void shouldSetKey() {
ProjectDefinition def = ProjectDefinition.create();
assertThat(root.getTestDirs().size(), is(0));
}
+ @Test
+ public void shouldResetSourceDirsWhenUsindDeprecatedMethod() throws IOException {
+ File baseDir = temp.newFolder();
+ ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir);
+ File src = new File(baseDir, "src");
+ src.mkdir();
+ root.addSourceDirs("src");
+ assertThat(root.getSourceDirs()).containsOnly("src");
+
+ root.addSourceFiles("foo.java");
+ assertThat(root.getSourceDirs()).containsOnly("foo.java");
+ }
+
+ @Test
+ public void shouldResetTestDirsWhenUsindDeprecatedMethod() throws IOException {
+ File baseDir = temp.newFolder();
+ ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir);
+ File test = new File(baseDir, "test");
+ test.mkdir();
+ root.addTestDirs("test");
+ assertThat(root.getTestDirs()).containsOnly("test");
+
+ root.addTestFiles("fooTest.java");
+ assertThat(root.getTestDirs()).containsOnly("fooTest.java");
+ }
+
+ @Test
+ public void shouldResetSourceDirsWhenUsindDeprecatedFileMethod() throws IOException {
+ File baseDir = temp.newFolder();
+ ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir);
+ File src = new File(baseDir, "src");
+ src.mkdir();
+ root.addSourceDirs("src");
+ assertThat(root.getSourceDirs()).containsOnly("src");
+
+ File file = new File(src, "foo.java");
+ root.addSourceFiles(file);
+ assertThat(root.getSourceDirs()).containsOnly(file.getAbsolutePath());
+ }
+
+ @Test
+ public void shouldResetTestDirsWhenUsindDeprecatedFileMethod() throws IOException {
+ File baseDir = temp.newFolder();
+ ProjectDefinition root = ProjectDefinition.create().setBaseDir(baseDir);
+ File test = new File(baseDir, "test");
+ test.mkdir();
+ root.addTestDirs("test");
+ assertThat(root.getTestDirs()).containsOnly("test");
+
+ File file = new File(test, "fooTest.java");
+ root.addTestFiles(file);
+ assertThat(root.getTestDirs()).containsOnly(file.getAbsolutePath());
+ }
+
private static void assertFiles(List<String> paths, String... values) {
assertThat(paths.size(), is(values.length));
for (int i = 0; i < values.length; i++) {