Skip to content

Commit

Permalink
Merge pull request #222 from dhunnapotha/fix_proper_escaping_of_dolla…
Browse files Browse the repository at this point in the history
…r_v2

Fix for #207, Some escaped '$' characters are not unescaped
  • Loading branch information
bkeepers committed Dec 2, 2015
2 parents 648d1bc + ad8f07a commit 80e8a2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/dotenv/substitutions/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ module Substitutions
module Variable
class << self
VARIABLE = /
(\\)? # is it escaped with a backslash?
(\$) # literal $
\{? # allow brace wrapping
([A-Z0-9_]+) # match the variable
\}? # closing brace
(\\)? # is it escaped with a backslash?
(\$) # literal $
(?!\() # shouldnt be followed by paranthesis
\{? # allow brace wrapping
([A-Z0-9_]+)? # optional alpha nums
\}? # closing brace
/xi

def call(value, env)
Expand All @@ -23,8 +24,10 @@ def call(value, env)

if match[1] == '\\'
variable[1..-1]
else
elsif match[3]
env.fetch(match[3]) { ENV[match[3]] }
else
variable
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/dotenv/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ def env(string)
expect(env('FOO="bar\rbaz"')).to eql("FOO" => "bar\rbaz")
end

it "escape $ properly when no alphabets/numbers/_ are followed by it" do
expect(env("FOO=\"bar\\$ \\$\\$\"")).to eql("FOO" => "bar$ $$")
end

# echo bar $ -> prints bar $ in the shell
it "ignore $ when it is not escaped and no variable is followed by it" do
expect(env("FOO=\"bar $ \"")).to eql("FOO" => "bar $ ")
end

# This functionality is not supported on JRuby or Rubinius
if (!defined?(RUBY_ENGINE) || RUBY_ENGINE != "jruby") &&
!defined?(Rubinius)
Expand Down

0 comments on commit 80e8a2d

Please sign in to comment.