-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unicode handling in .env files (fixes #32)
- Loading branch information
Showing
3 changed files
with
85 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
project/modules/core/src/test/java/net/ashald/envfile/parsers/DotEnvFileParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.ashald.envfile.parsers; | ||
import net.ashald.envfile.EnvFileErrorException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
|
||
public class DotEnvFileParserTest { | ||
|
||
private DotEnvFileParser parser = new DotEnvFileParser(); | ||
|
||
private String createFile(String content) throws IOException { | ||
File file = File.createTempFile("test", ".env"); | ||
file.deleteOnExit(); | ||
|
||
String filePath = file.getAbsolutePath(); | ||
Files.write(Paths.get(filePath), content.getBytes()); | ||
return filePath; | ||
} | ||
|
||
@Test | ||
public void testMalformedEncoding() throws IOException, EnvFileErrorException { | ||
parser.readFile(createFile("\\usr")); // should not fail | ||
} | ||
|
||
@Test | ||
public void testLinesStartingWithHasAreIgnored() throws IOException, EnvFileErrorException { | ||
StringWriter stringWriter = new StringWriter(); | ||
PrintWriter writer = new PrintWriter(stringWriter, true); | ||
writer.println("# ignored"); | ||
writer.println(" # also ignored"); | ||
writer.println("key=value"); | ||
|
||
Map<String, String> result = parser.readFile(createFile(stringWriter.toString())); | ||
Assert.assertEquals(1, result.size()); | ||
Assert.assertEquals("value", result.get("key")); | ||
} | ||
|
||
@Test | ||
public void testInlineComments() throws IOException, EnvFileErrorException { | ||
StringWriter stringWriter = new StringWriter(); | ||
PrintWriter writer = new PrintWriter(stringWriter, true); | ||
writer.println("key1=foo#bar"); // not a comment | ||
writer.println("key2=foo #bar"); // a comment | ||
writer.println("key3='foo #bar'"); // a comment | ||
writer.println("key4=\"foo #bar\""); // a comment | ||
writer.println("key5=foo \\#bar"); // a comment | ||
|
||
Map<String, String> result = parser.readFile(createFile(stringWriter.toString())); | ||
Assert.assertEquals("foo#bar", result.get("key1")); | ||
Assert.assertEquals("foo", result.get("key2")); | ||
Assert.assertEquals("foo #bar", result.get("key3")); | ||
Assert.assertEquals("foo #bar", result.get("key4")); | ||
Assert.assertEquals("foo #bar", result.get("key5")); | ||
} | ||
|
||
} |