Skip to content

Commit c0c248c

Browse files
committed
B!! solving obscure bug with package settings and package that use text that is earlier in the path
1 parent aea011c commit c0c248c

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

approvaltests-tests/src/test/java/org/approvaltests/MethodVerification.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@ public static Queryable<Class<?>> getClasses(String startingDirectory, Function1
3535
}
3636
public static Class<?> getJavaClass(String path)
3737
{
38-
String className = path.substring(0, path.length() - ".java".length());
39-
if (className.contains(".com."))
40-
{
41-
className = className.substring(className.indexOf(".com.") + 1);
42-
}
43-
if (className.contains(".org."))
44-
{
45-
className = className.substring(className.indexOf(".org.") + 1);
46-
}
38+
String className = getJavaClassNameFromPath(path);
4739
try
4840
{
4941
Class<?> aClass = Class.forName(className);
@@ -52,7 +44,20 @@ public static Class<?> getJavaClass(String path)
5244
catch (Throwable e)
5345
{
5446
SimpleLogger.variable("Path", path);
47+
SimpleLogger.variable("className", className);
5548
throw ObjectUtils.throwAsError(e);
5649
}
5750
}
51+
public static String getJavaClassNameFromPath(String path)
52+
{
53+
String className = path.substring(0, path.length() - ".java".length());
54+
String[] top = {"com", "org", "tests"};
55+
for (String head : top)
56+
{
57+
head = "." + head + ".";
58+
if (className.contains(head))
59+
{ return className.substring(className.indexOf(head) + 1); }
60+
}
61+
return className;
62+
}
5863
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package tests;
2+
3+
public class PackageSettings
4+
{
5+
public static String ApprovalBaseDirectory = "../resources";
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tests;
2+
3+
import org.approvaltests.namer.StackTraceNamer;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.File;
7+
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public class StackTraceNamerTest
11+
{
12+
@Test
13+
void testNameCollision()
14+
{
15+
StackTraceNamer approvalNamer = new StackTraceNamer();
16+
String actual = approvalNamer.getApprovedFile(".txt").toString();
17+
assertTrue(actual.contains(File.separator + "src" + File.separator),
18+
"actual " + actual + " does not contain /src/");
19+
}
20+
}

approvaltests/src/main/java/org/approvaltests/namer/StackTraceNamer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public String getBaseDirectory()
3636
if (!StringUtils.isEmpty(NamerFactory.getApprovalBaseDirectory()))
3737
{
3838
String packageName = info.getFullClassName().substring(0, info.getFullClassName().lastIndexOf("."));
39-
String packagepath = packageName.replace('.', File.separatorChar);
40-
String currentBase = baseDir.substring(0, baseDir.indexOf(packagepath));
39+
String packagepath = File.separator + packageName.replace('.', File.separatorChar);
40+
String currentBase = baseDir.substring(0, baseDir.indexOf(packagepath) + 1);
4141
String newBase = currentBase + NamerFactory.getApprovalBaseDirectory() + File.separator + packagepath;
4242
baseDir = ObjectUtils.throwAsError(() -> new File(newBase).getCanonicalPath().toString());
4343
}

0 commit comments

Comments
 (0)