diff --git a/build.gradle b/build.gradle index 39b7e482..0b6ce368 100644 --- a/build.gradle +++ b/build.gradle @@ -130,6 +130,8 @@ dependencies { // detekt compile 'io.gitlab.arturbosch.detekt:detekt-core:1.0.0.RC3' compile 'io.gitlab.arturbosch.detekt:detekt-rules:1.0.0.RC3' + // dependency only for obtaining default configuration + compile 'io.gitlab.arturbosch.detekt:detekt-cli:1.0.0.RC3' // Test dependencies testCompile 'junit:junit:4.11' diff --git a/src/main/java/pl/touk/sputnik/processor/detekt/DetektProcessor.java b/src/main/java/pl/touk/sputnik/processor/detekt/DetektProcessor.java index e4809766..99007352 100644 --- a/src/main/java/pl/touk/sputnik/processor/detekt/DetektProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/detekt/DetektProcessor.java @@ -3,6 +3,7 @@ import io.gitlab.arturbosch.detekt.api.Config; import io.gitlab.arturbosch.detekt.api.Detektion; import io.gitlab.arturbosch.detekt.api.YamlConfig; +import io.gitlab.arturbosch.detekt.cli.ClasspathResourceConverter; import io.gitlab.arturbosch.detekt.core.DetektFacade; import io.gitlab.arturbosch.detekt.core.Detektor; import io.gitlab.arturbosch.detekt.core.PathFilter; @@ -70,13 +71,24 @@ private String buildCommonPathAsFilePrefix(String commonPath) { @NotNull private Detektor buildDetector(String commonPath) { - Path configPath = FileSystems.getDefault().getPath(configuration.getProperty(GeneralOption.DETEKT_CONFIG_FILE)); - Config config = YamlConfig.Companion.load(configPath); + String configFilename = configuration.getProperty(GeneralOption.DETEKT_CONFIG_FILE); + Config config; + if (configFilename != null) { + Path configPath = FileSystems.getDefault().getPath(configFilename); + config = YamlConfig.Companion.load(configPath); + } else { + config = loadDefaultConfig(); + } ProcessingSettings processingSettings = new ProcessingSettings(FileSystems.getDefault().getPath(commonPath), config, new ArrayList(), false, false, new ArrayList()); return DetektFacade.INSTANCE.instance(processingSettings, new RuleSetLocator(processingSettings).load(), Arrays.asList(new LoggingFileProcessor())); } + @NotNull + private Config loadDefaultConfig() { + return YamlConfig.Companion.loadResource(new ClasspathResourceConverter().convert("default-detekt-config.yml")); + } + @NotNull @Override public String getName() { diff --git a/src/test/java/pl/touk/sputnik/processor/detekt/DetektProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/detekt/DetektProcessorTest.java index 6989d9d2..09b6de41 100644 --- a/src/test/java/pl/touk/sputnik/processor/detekt/DetektProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/detekt/DetektProcessorTest.java @@ -17,7 +17,8 @@ import static org.assertj.core.api.Assertions.assertThat; public class DetektProcessorTest { - private static final String CONFIGURATION_WITH_KTLINT_ENABLED = "detekt/configuration/configurationWithEnabledDetekt.properties"; + private static final String CONFIGURATION_WITH_KTLINT_ENABLED_AND_WITH_DETEKT_CONFIG_FILE = "detekt/configuration/configurationWithEnabledDetektAndDetektConfigFile.properties"; + private static final String CONFIGURATION_WITH_KTLINT_ENABLED_AND_WITHOUT_DETEKT_CONFIG_FILE = "detekt/configuration/configurationWithEnabledDetektAndWithoutDetektConfigFile.properties"; private static final String VIOLATIONS_1 = "src/test/resources/detekt/testFiles/Violations1.kt"; private static final String VIOLATIONS_2 = "src/test/resources/detekt/testFiles/sub/Violations2.kt"; @@ -29,7 +30,7 @@ public class DetektProcessorTest { @Before public void setUp() throws Exception { - config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_KTLINT_ENABLED); + config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_KTLINT_ENABLED_AND_WITH_DETEKT_CONFIG_FILE); sut = new DetektProcessor(config); } @@ -87,6 +88,18 @@ public void shouldReturnNoViolationsForEmptyReview() { assertThat(result.getViolations()).isEmpty(); } + @Test + public void shouldProcessReviewsOnDefaultConfig() { + Configuration configWithoutDetektConfigFile = + ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_KTLINT_ENABLED_AND_WITHOUT_DETEKT_CONFIG_FILE); + Review review = getReview(VIOLATIONS_1, VIOLATIONS_2, VIOLATIONS_3, REVIEW_GROOVY_FILE); + + DetektProcessor detektProcessor = new DetektProcessor(configWithoutDetektConfigFile); + ReviewResult result = detektProcessor.process(review); + + assertThat(result).isNotNull(); + } + private Review getReview(String... filePaths) { List files = new ArrayList<>(); for (String filePath : filePaths) { diff --git a/src/test/resources/detekt/configuration/configurationWithEnabledDetekt.properties b/src/test/resources/detekt/configuration/configurationWithEnabledDetektAndDetektConfigFile.properties similarity index 100% rename from src/test/resources/detekt/configuration/configurationWithEnabledDetekt.properties rename to src/test/resources/detekt/configuration/configurationWithEnabledDetektAndDetektConfigFile.properties diff --git a/src/test/resources/detekt/configuration/configurationWithEnabledDetektAndWithoutDetektConfigFile.properties b/src/test/resources/detekt/configuration/configurationWithEnabledDetektAndWithoutDetektConfigFile.properties new file mode 100644 index 00000000..02d4c41c --- /dev/null +++ b/src/test/resources/detekt/configuration/configurationWithEnabledDetektAndWithoutDetektConfigFile.properties @@ -0,0 +1 @@ +detekt.enabled=true