Skip to content

Commit

Permalink
[detekt] Load default configuration on detekt config file missing. (#182
Browse files Browse the repository at this point in the history
)

Resolves #179
  • Loading branch information
pjagielski authored Mar 15, 2018
1 parent eae1f56 commit 536680f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PathFilter>(), false, false, new ArrayList<Path>());

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

Expand Down Expand Up @@ -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<ReviewFile> files = new ArrayList<>();
for (String filePath : filePaths) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
detekt.enabled=true

0 comments on commit 536680f

Please sign in to comment.