Skip to content

Commit

Permalink
add configuration switch to disable fallback to root directory (#87)
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Purnhagen <[email protected]>
  • Loading branch information
purnhar and purnhar authored Jan 28, 2024
1 parent a137c79 commit bcbbfef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class RuleInvokerService {

private Collection<String> excludedPaths= emptySet();

private boolean fallbackToRootDirectory = true;

public RuleInvokerService(Log log) {
this.log=log;
archUtils =new ArchUtils(log);
Expand All @@ -49,6 +51,14 @@ public RuleInvokerService(Log log, ScopePathProvider scopePathProvider,Collectio
this.excludedPaths=new ExcludedPathsPreProcessor().processExcludedPaths(log, projectBuildDir, excludedPaths);
}

public RuleInvokerService(Log log, ScopePathProvider scopePathProvider,Collection<String> excludedPaths, String projectBuildDir, boolean fallbackToRootDirectory) {
this.log=log;
archUtils =new ArchUtils(log);

this.scopePathProvider=scopePathProvider;
this.excludedPaths=new ExcludedPathsPreProcessor().processExcludedPaths(log, projectBuildDir, excludedPaths);
this.fallbackToRootDirectory = fallbackToRootDirectory;
}

public String invokeRules(Rules rules)
throws InvocationTargetException, InstantiationException, IllegalAccessException {
Expand Down Expand Up @@ -107,7 +117,7 @@ private String invokeConfigurableRules(ConfigurableRule rule) {
String fullPathFromRootTopackage = getPackageNameOnWhichToApplyRules(rule);

log.info("invoking ConfigurableRule "+rule.toString()+" on "+fullPathFromRootTopackage);
JavaClasses classes = archUtils.importAllClassesInPackage(new RootClassFolder(""), fullPathFromRootTopackage,excludedPaths);
JavaClasses classes = archUtils.importAllClassesInPackage(new RootClassFolder(""), fullPathFromRootTopackage, excludedPaths, fallbackToRootDirectory);

InvocationResult result = invokableRules.invokeOn(classes);
return result.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public ArchUtils(Log log) {
}

public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFolder, String packagePath){
return importAllClassesInPackage(rootClassFolder, packagePath,emptyList());
return importAllClassesInPackage(rootClassFolder, packagePath,emptyList(), true);
}

public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFolder, String packagePath, Collection<String> excludedPaths) {
return importAllClassesInPackage(rootClassFolder, packagePath, excludedPaths, true);
}

public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFolder, String packagePath, Collection<String> excludedPaths, boolean fallbackToRootDirectory) {

//not great design, but since all the rules need to call this, it's very convenient to keep this method static
if(log==null){
Expand All @@ -36,7 +40,7 @@ public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFol

Path classesPath = Paths.get(rootClassFolder.getValue() + packagePath);

if (!classesPath.toFile().exists()) {
if (fallbackToRootDirectory && !classesPath.toFile().exists()) {
StringBuilder warnMessage=new StringBuilder("Classpath ").append(classesPath.toFile())
.append(" doesn't exist : loading all classes from root, ie ")
.append(rootClassFolder.getValue())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.societegenerale.commons.plugin.rules;

import java.util.Arrays;
import java.util.Collections;

import com.societegenerale.commons.plugin.SilentLog;
import com.societegenerale.commons.plugin.model.RootClassFolder;
Expand Down Expand Up @@ -39,6 +40,15 @@ public void shouldLoadAllClassesWhenGivenPakageDoesntExist() {
assertThat(noOfClasses).isEqualTo(36);
}

@Test
public void shouldNotLoadAllClassesWhenFallbackIsDisabled() {
JavaClasses classes = ArchUtils.importAllClassesInPackage(new RootClassFolder("./target/classes"), "someNotExistingFolder", Collections.emptyList(), false);

long noOfClasses = classes.stream().filter(it -> !it.isNestedClass()).count();

assertThat(noOfClasses).isZero();
}

@Test
public void shouldIgnoreClassesFromConfiguredPaths() {

Expand Down

0 comments on commit bcbbfef

Please sign in to comment.