Skip to content

Commit

Permalink
Revert #318
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmagic committed Apr 23, 2018
1 parent 294ab7b commit 7366df8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 49 deletions.
22 changes: 11 additions & 11 deletions lib/dotenv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class Parser
[Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command]

LINE = /
\A
\s*
(?:export\s+)? # optional export
([\w\.]+) # key
(?:\s*=\s*|:\s+?) # separator
( # optional value begin
'(?:\\'|[^'])*' # single quoted value
'(?:\'|[^'])*' # single quoted value
| # or
"(?:\\"|[^"])*" # double quoted value
"(?:\"|[^"])*" # double quoted value
| # or
[^#\r\n]+ # unquoted value
[^#\n]+ # unquoted value
)? # value end
\s*
(?:\#.*)? # optional comment
\z
/x

class << self
Expand All @@ -42,12 +44,7 @@ def initialize(string, is_load)
end

def call
# Process matches
@string.scan(LINE).each do |key, value|
@hash[key] = parse_value(value || "")
end
# Process non-matches
@string.gsub(LINE, "").split(/[\n\r]+/).each do |line|
@string.split(/[\n\r]+/).each do |line|
parse_line(line)
end
@hash
Expand All @@ -56,7 +53,10 @@ def call
private

def parse_line(line)
if line.split.first == "export"
if (match = line.match(LINE))
key, value = match.captures
@hash[key] = parse_value(value || "")
elsif line.split.first == "export"
if variable_not_set?(line)
raise FormatError, "Line #{line.inspect} has an unset variable"
end
Expand All @@ -65,7 +65,7 @@ def parse_line(line)

def parse_value(value)
# Remove surrounding quotes
value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')
value = value.strip.sub(/\A(['"])(.*)\1\z/, '\2')

if Regexp.last_match(1) == '"'
value = unescape_characters(expand_newlines(value))
Expand Down
38 changes: 0 additions & 38 deletions spec/dotenv/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,44 +169,6 @@ def env(string)
expect(env("foo=")).to eql("foo" => "")
end

it "allows multi-line values in single quotes" do
env_file = %(OPTION_A=first line
export OPTION_B='line 1
line 2
line 3'
OPTION_C="last line"
OPTION_ESCAPED='line one
this is \\'quoted\\'
one more line')

expected_result = {
"OPTION_A" => "first line",
"OPTION_B" => "line 1\nline 2\nline 3",
"OPTION_C" => "last line",
"OPTION_ESCAPED" => "line one\nthis is \\'quoted\\'\none more line"
}
expect(env(env_file)).to eql(expected_result)
end

it "allows multi-line values in double quotes" do
env_file = %(OPTION_A=first line
export OPTION_B="line 1
line 2
line 3"
OPTION_C="last line"
OPTION_ESCAPED="line one
this is \\"quoted\\"
one more line")

expected_result = {
"OPTION_A" => "first line",
"OPTION_B" => "line 1\nline 2\nline 3",
"OPTION_C" => "last line",
"OPTION_ESCAPED" => "line one\nthis is \"quoted\"\none more line"
}
expect(env(env_file)).to eql(expected_result)
end

if RUBY_VERSION > "1.8.7"
it "parses shell commands interpolated in $()" do
expect(env("echo=$(echo hello)")).to eql("echo" => "hello")
Expand Down

0 comments on commit 7366df8

Please sign in to comment.