Skip to content

Commit

Permalink
Handle multiline strings in .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
szimek committed Aug 14, 2012
1 parent 6042783 commit 7d6de5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/foreman/env.rb
Expand Up @@ -9,8 +9,10 @@ def initialize(filename)
if line =~ /\A([A-Za-z_0-9]+)=(.*)\z/
key = $1
case val = $2
# Remove single quotes
when /\A'(.*)'\z/ then ax[key] = $1
when /\A"(.*)"\z/ then ax[key] = $1.gsub(/\\(.)/, '\1')
# Remove double quotes and unescape string preserving newline characters
when /\A"(.*)"\z/ then ax[key] = $1.gsub('\n', "\n").gsub(/\\(.)/, '\1')
else ax[key] = val
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/foreman/engine_spec.rb
Expand Up @@ -90,6 +90,14 @@ def shutdown
subject.env["OTHER"].should == 'escaped"quote'
end

it "should handle multiline strings" do
File.open("/tmp/env", "w") do |f|
f.puts 'FOO="bar\nbaz"'
end
subject.load_env "/tmp/env"
subject.env["FOO"].should == "bar\nbaz"
end

it "should fail if specified and doesnt exist" do
lambda { subject.load_env "/tmp/env" }.should raise_error(Errno::ENOENT)
end
Expand Down

0 comments on commit 7d6de5b

Please sign in to comment.