Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quoted values check #25

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,16 @@ func parseLine(line string) (key string, value string, err error) {
value = strings.Trim(value, " ")

// check if we've got quoted values
if strings.Count(value, "\"") == 2 || strings.Count(value, "'") == 2 {
first := string(value[0:1])
last := string(value[len(value)-1:])
if first == last && strings.ContainsAny(first, `"'`) {
// pull the quotes off the edges
value = strings.Trim(value, "\"'")
value = strings.Trim(value, `"'`)

// expand quotes
value = strings.Replace(value, "\\\"", "\"", -1)
value = strings.Replace(value, `\"`, `"`, -1)
// expand newlines
value = strings.Replace(value, "\\n", "\n", -1)
value = strings.Replace(value, `\n`, "\n", -1)
}

return
Expand Down
16 changes: 8 additions & 8 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,24 @@ func TestParsing(t *testing.T) {
parseAndCompare(t, "FOO= bar", "FOO", "bar")

// parses double quoted values
parseAndCompare(t, "FOO=\"bar\"", "FOO", "bar")
parseAndCompare(t, `FOO="bar"`, "FOO", "bar")

// parses single quoted values
parseAndCompare(t, "FOO='bar'", "FOO", "bar")

// parses escaped double quotes
parseAndCompare(t, "FOO=escaped\\\"bar\"", "FOO", "escaped\"bar")
parseAndCompare(t, `FOO="escaped\"bar"`, "FOO", `escaped"bar`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case was corrected + code fixed.


// parses yaml style options
parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1")

// parses export keyword
parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2")
parseAndCompare(t, "export OPTION_B='\\n'", "OPTION_B", "\n")
parseAndCompare(t, `export OPTION_B='\n'`, "OPTION_B", "\n")

// it 'expands newlines in quoted strings' do
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
parseAndCompare(t, "FOO=\"bar\\nbaz\"", "FOO", "bar\nbaz")
parseAndCompare(t, `FOO="bar\nbaz"`, "FOO", "bar\nbaz")

// it 'parses varibales with "." in the name' do
// expect(env('FOO.BAR=foobar')).to eql('FOO.BAR' => 'foobar')
Expand All @@ -224,14 +224,14 @@ func TestParsing(t *testing.T) {

// it 'allows # in quoted value' do
// expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
parseAndCompare(t, "FOO=\"bar#baz\" # comment", "FOO", "bar#baz")
parseAndCompare(t, `FOO="bar#baz" # comment`, "FOO", "bar#baz")
parseAndCompare(t, "FOO='bar#baz' # comment", "FOO", "bar#baz")
parseAndCompare(t, "FOO=\"bar#baz#bang\" # comment", "FOO", "bar#baz#bang")
parseAndCompare(t, `FOO="bar#baz#bang" # comment`, "FOO", "bar#baz#bang")

// it 'parses # in quoted values' do
// expect(env('foo="ba#r"')).to eql('foo' => 'ba#r')
// expect(env("foo='ba#r'")).to eql('foo' => 'ba#r')
parseAndCompare(t, "FOO=\"ba#r\"", "FOO", "ba#r")
parseAndCompare(t, `FOO="ba#r"`, "FOO", "ba#r")
parseAndCompare(t, "FOO='ba#r'", "FOO", "ba#r")

// it 'throws an error if line format is incorrect' do
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestLinesToIgnore(t *testing.T) {
}

// make sure we're not getting false positives
if isIgnoredLine("export OPTION_B='\\n'") {
if isIgnoredLine(`export OPTION_B='\n'`) {
t.Error("ignoring a perfectly valid line to parse")
}
}
Expand Down