Skip to content

Commit

Permalink
! F allow configuration of project base directory
Browse files Browse the repository at this point in the history
Co-authored-by: Jay Bazuzi <jay@bazuzi.com>
Co-authored-by: Llewellyn Falco <llewellyn.falco@gmail.com>
  • Loading branch information
3 people committed Feb 3, 2025
1 parent 870a0f2 commit 8ad1652
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.approvaltests;

import com.spun.util.ClassUtils;
import org.junit.jupiter.api.Test;
import org.lambda.query.Query;
import org.lambda.query.Queryable;
Expand Down Expand Up @@ -27,7 +28,7 @@ private List<Method> getMethodsWithCheckedExceptions(Class<?> aClass)
}
private Queryable<Class<?>> getAllClasses()
{
return getClasses("..", p -> p.contains("main"),
return getClasses(ClassUtils.getProjectRootPath() + "/..", p -> p.contains("main"),
p1 -> p1.contains(".approvaltests.src.") || p1.contains(".approvaltests-util."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.utils.SourceRoot;
import com.spun.util.ClassUtils;
import com.spun.util.FormattedException;
import org.lambda.query.Query;

Expand All @@ -22,8 +23,8 @@ public class ParserUtilities
public static final List<String> SOURCE_PATHS = new ArrayList<>();
static
{
SOURCE_PATHS.add("src/main/java");
SOURCE_PATHS.add("src/test/java");
SOURCE_PATHS.add(ClassUtils.getProjectRootPath() + "/src/main/java");
SOURCE_PATHS.add(ClassUtils.getProjectRootPath() + "/src/test/java");
}
public static Range getLineNumbersForMethod(Method method)
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public static CompilationUnit getCompilationUnit(Method method)
ParseProblemException parseException = null;
for (String sourceRootPath : SOURCE_PATHS)
{
System.out.println("Trying to parse from: " + sourceRootPath);
SourceRoot sourceRoot = new SourceRoot(Paths.get(sourceRootPath));
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.approvaltests;

import com.github.javaparser.Range;
import com.spun.util.ClassUtils;
import org.approvaltests.core.Options;
import org.junit.jupiter.api.Test;

Expand All @@ -10,7 +11,7 @@ public class ParsingFilesTest
{
public static void addApprovalTestPath()
{
ParserUtilities.SOURCE_PATHS.add("../approvaltests/src/main/java");
ParserUtilities.SOURCE_PATHS.add(ClassUtils.getProjectRootPath() + "/../approvaltests/src/main/java");
}
@Test
public void getLineNumberOfThisMethod() throws Exception
Expand All @@ -23,4 +24,4 @@ public void getLineNumberOfThisMethod() throws Exception
Range r = ParserUtilities.getLineNumbersForMethod(method);
Approvals.verify(r, new Options().inline(expected));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.approvaltests.namer;

import com.spun.util.ClassUtils;
import com.spun.util.tests.TestUtils;
import com.spun.util.tests.TestUtils.SourceDirectoryRestorer;
import org.approvaltests.Approvals;
Expand All @@ -19,7 +20,8 @@ void useAlternativeSourceFileFinder()
@Override
public File call(Class clazz, String fileName)
{
return new File("src/test/java/" + clazz.getPackage().getName().replaceAll("\\.", "/"));
return new File(ClassUtils.getProjectRootPath() + "/src/test/java/"
+ clazz.getPackage().getName().replaceAll("\\.", "/"));
}
};
// end-snippet
Expand Down
15 changes: 14 additions & 1 deletion approvaltests-util/src/main/java/com/spun/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class ClassUtils
{
public static final String APPROVALTESTS_PROJECT_DIRECTORY = "APPROVALTESTS_PROJECT_DIRECTORY";
public static String getClassName(Class<?> clazz)
{
String name = clazz.getName();
Expand Down Expand Up @@ -98,11 +99,23 @@ public static File getSourceDirectory(Class<?> clazz, Function1<String, String>
final String name = clazz.getName();
String[] split = name.split("\\.");
split[split.length - 1] = createLastFileName.call(split[split.length - 1]);
File found = find(new File("."), Arrays.asList(split));
String baseSourcePath = getProjectRootPath();
File found = find(new File(baseSourcePath), Arrays.asList(split));
if (found == null)
{ throw new FormattedException("Didn't find %s under %s", name, FileUtils.getCurrentDirectory()); }
return found.getParentFile();
}
/**
* This returns the {@link com.spun.util.ClassUtils#APPROVALTESTS_PROJECT_DIRECTORY APPROVALTESTS_PROJECT_DIRECTORY} system property if it is set, otherwise it returns the working directory.
*/
public static String getProjectRootPath()
{
if (System.getProperties().containsKey(APPROVALTESTS_PROJECT_DIRECTORY))
{ return System.getProperty(APPROVALTESTS_PROJECT_DIRECTORY); }
if (System.getenv().containsKey(APPROVALTESTS_PROJECT_DIRECTORY))
{ return System.getenv(APPROVALTESTS_PROJECT_DIRECTORY); }
return ".";
}
public static File getSourceDirectory(Class<?> clazz, final String fileName)
{
return getSourceDirectory(clazz, __ -> fileName);
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
<version>3.5.2</version>
<configuration>
<argLine>-Duser.language=en -Duser.region=US</argLine>
<parallel>methods</parallel>
<workingDirectory>target/fork_dir_${surefire.forkNumber}</workingDirectory>
<systemPropertyVariables>
<APPROVALTESTS_PROJECT_DIRECTORY>${project.basedir}</APPROVALTESTS_PROJECT_DIRECTORY>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit 8ad1652

Please sign in to comment.