Skip to content

Commit

Permalink
Fix classpath relative-to-absolute conversion in DotenvReader (#27)
Browse files Browse the repository at this point in the history
* 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 <ccherlin@dminc.com>
  • Loading branch information
Mooninaut and Clement Cherlin authored May 29, 2022
1 parent 5ffffe3 commit 6dc4c52
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<String> read() throws DotenvException, IOException {

try {
return ClasspathHelper
.loadFileFromClasspath(location.replaceFirst("./", "/"))
.loadFileFromClasspath(location.replaceFirst("^\\./", "/"))
.collect(Collectors.toList());
} catch (DotenvException e) {
Path cwd = FileSystems.getDefault().getPath(".").toAbsolutePath().normalize();
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/tests/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ public void resourceRelative() {
assertHostEnvVar(dotenv);
}

@Test
public void resourceAbsoluteDir() {
assertDirectory("/envdir","Simple Subdirectory");
}

@Test
public void resourceRelativeDir() {
assertDirectory("./envdir", "Simple Subdirectory");
}

@Test
public void resourceUnanchoredDir() {
assertDirectory("envdir", "Simple Subdirectory");
}

@Test
public void resourceAbsoluteTrailingDotDir() {
assertDirectory("/trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}

@Test
public void resourceRelativeTrailingDotDir() {
assertDirectory("./trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}

@Test
public void resourceUnanchoredTrailingDotDir() {
assertDirectory("trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}

@Test
public void resourceCurrent() {
var dotenv = Dotenv.configure()
Expand Down Expand Up @@ -130,6 +160,13 @@ public void dotenvIgnoreMissing() {
assertNull(dotenv.get("MY_TEST_EV1"));
}

private void assertDirectory(String directory, String expected) {
var dotenv = Dotenv.configure()
.directory(directory)
.load();
assertEquals(expected, dotenv.get("MY_TEST_EV1"));
}

private void assertHostEnvVar(Dotenv env) {
var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
if (isWindows) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/envdir/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Good test EV
MY_TEST_EV1=Simple Subdirectory
2 changes: 2 additions & 0 deletions src/test/resources/trailingdot./envdir/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Good test EV
MY_TEST_EV1=Trailing Dot Directory With Subdirectory

0 comments on commit 6dc4c52

Please sign in to comment.