diff --git a/lib/dotenv/parser.rb b/lib/dotenv/parser.rb index 0d95dfa4..e3f54c02 100644 --- a/lib/dotenv/parser.rb +++ b/lib/dotenv/parser.rb @@ -12,21 +12,21 @@ class Parser [Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command] LINE = / - (?:^|\A) # beginning of line - \s* # leading whitespace - (?:export\s+)? # optional export - ([\w\.]+) # key - (?:\s*=\s*?|:\s+?) # separator - ( # optional value begin - '(?:\\'|[^'])*' # single quoted value - | # or - "(?:\\"|[^"])*" # double quoted value - | # or - [^\#\r\n]+ # unquoted value - )? # value end - \s* # trailing whitespace - (?:\#.*)? # optional comment - (?:$|\z) # end of line + (?:^|\A) # beginning of line + \s* # leading whitespace + (?:export\s+)? # optional export + ([\w\.]+) # key + (?:\s*=\s*?|:\s+?) # separator + ( # optional value begin + \s*'(?:\\'|[^'])*' # single quoted value + | # or + \s*"(?:\\"|[^"])*" # double quoted value + | # or + [^\#\r\n]+ # unquoted value + )? # value end + \s* # trailing whitespace + (?:\#.*)? # optional comment + (?:$|\z) # end of line /x class << self diff --git a/spec/dotenv/parser_spec.rb b/spec/dotenv/parser_spec.rb index b2ae94d3..508b8acc 100644 --- a/spec/dotenv/parser_spec.rb +++ b/spec/dotenv/parser_spec.rb @@ -9,6 +9,10 @@ def env(string) expect(env("FOO=bar")).to eql("FOO" => "bar") end + it "parses unquoted values with spaces after seperator" do + expect(env("FOO= bar")).to eql("FOO" => "bar") + end + it "parses values with spaces around equal sign" do expect(env("FOO =bar")).to eql("FOO" => "bar") expect(env("FOO= bar")).to eql("FOO" => "bar") @@ -143,6 +147,10 @@ def env(string) expect(env('foo="bar#baz" # comment')).to eql("foo" => "bar#baz") end + it "allows # in quoted value with spaces after seperator" do + expect(env('foo= "bar#baz" # comment')).to eql("foo" => "bar#baz") + end + it "ignores comment lines" do expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql("foo" => "bar") end