Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add configuration switch to disable fallback to root directory (3.x) #87

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading