Skip to content

Commit 6dc4c52

Browse files
MooninautClement Cherlin
and
Clement Cherlin
authored
Fix classpath relative-to-absolute conversion in DotenvReader (#27)
* Fix classpath relative-to-absolute conversion in DotenvReader Escape "." to prevent removing the trailing character from a simple directory name with no leading "/" or "./" Anchor the regex to prevents stripping a trailing literal "." from a directory name, which is unlikely but possible Add tests for simple resource directory and resource directory with trailing "." Examples of bug: .directory("envdir") would try to load "resources/envdi/.env" and fail. .directory("/trailingdot./envdir") would try to load "resources/trailingdot/envdir/.env") and fail. * Rename dirTest to assertDirectory to satisfy Codacy Static Code Analysis Co-authored-by: Clement Cherlin <[email protected]>
1 parent 5ffffe3 commit 6dc4c52

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/main/java/io/github/cdimascio/dotenv/internal/DotenvReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public List<String> read() throws DotenvException, IOException {
5454

5555
try {
5656
return ClasspathHelper
57-
.loadFileFromClasspath(location.replaceFirst("./", "/"))
57+
.loadFileFromClasspath(location.replaceFirst("^\\./", "/"))
5858
.collect(Collectors.toList());
5959
} catch (DotenvException e) {
6060
Path cwd = FileSystems.getDefault().getPath(".").toAbsolutePath().normalize();

src/test/java/tests/BasicTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ public void resourceRelative() {
6666
assertHostEnvVar(dotenv);
6767
}
6868

69+
@Test
70+
public void resourceAbsoluteDir() {
71+
assertDirectory("/envdir","Simple Subdirectory");
72+
}
73+
74+
@Test
75+
public void resourceRelativeDir() {
76+
assertDirectory("./envdir", "Simple Subdirectory");
77+
}
78+
79+
@Test
80+
public void resourceUnanchoredDir() {
81+
assertDirectory("envdir", "Simple Subdirectory");
82+
}
83+
84+
@Test
85+
public void resourceAbsoluteTrailingDotDir() {
86+
assertDirectory("/trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
87+
}
88+
89+
@Test
90+
public void resourceRelativeTrailingDotDir() {
91+
assertDirectory("./trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
92+
}
93+
94+
@Test
95+
public void resourceUnanchoredTrailingDotDir() {
96+
assertDirectory("trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
97+
}
98+
6999
@Test
70100
public void resourceCurrent() {
71101
var dotenv = Dotenv.configure()
@@ -130,6 +160,13 @@ public void dotenvIgnoreMissing() {
130160
assertNull(dotenv.get("MY_TEST_EV1"));
131161
}
132162

163+
private void assertDirectory(String directory, String expected) {
164+
var dotenv = Dotenv.configure()
165+
.directory(directory)
166+
.load();
167+
assertEquals(expected, dotenv.get("MY_TEST_EV1"));
168+
}
169+
133170
private void assertHostEnvVar(Dotenv env) {
134171
var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
135172
if (isWindows) {

src/test/resources/envdir/.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Good test EV
2+
MY_TEST_EV1=Simple Subdirectory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Good test EV
2+
MY_TEST_EV1=Trailing Dot Directory With Subdirectory

0 commit comments

Comments
 (0)