Skip to content

Commit

Permalink
Fix template (-t) handling of blank lines and comments
Browse files Browse the repository at this point in the history
Template treats every line as a variable declaration. Add conditions
to ensure only variable declarations are templated. Blank lines and
comments are left as-is.
  • Loading branch information
benforeva committed Jun 23, 2020
1 parent 9e16a42 commit fae8519
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/dotenv/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def create_template
File.open(@env_file, "r") do |env_file|
File.open("#{@env_file}.template", "w") do |env_template|
env_file.each do |line|
variable = line.split("=").first
env_template.puts "#{variable}=#{variable}"
var, value = line.split("=")
is_a_comment = var.strip[0].eql?("#")
line_transform = value.nil? || is_a_comment ? line : "#{var}=#{var}"
env_template.puts line_transform
end
end
end
Expand Down
69 changes: 53 additions & 16 deletions spec/dotenv/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,59 @@ def run(*args)
expect(cli.argv).to eql(["foo something"])
end

it "templates a file specified by -t" do
@buffer = StringIO.new
@input = StringIO.new("FOO=BAR\nFOO2=BAR2")
@origin_filename = "plain.env"
@template_filename = "plain.env.template"
@content = "the content fo the file"
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
# rubocop:disable LineLength
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)

# call the function that writes to the file
cli = Dotenv::CLI.new(["-t", "plain.env"])
cli.send(:parse_argv!, cli.argv)

# reading the buffer and checking its content.
expect(@buffer.string).to eq("FOO=FOO\nFOO2=FOO2\n")
describe "templates a file specified by -t" do
before do
@buffer = StringIO.new
@origin_filename = "plain.env"
@template_filename = "plain.env.template"
end
it "templates variables" do
@input = StringIO.new("FOO=BAR\nFOO2=BAR2")
# rubocop:disable LineLength
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
# call the function that writes to the file
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
# reading the buffer and checking its content.
expect(@buffer.string).to eq("FOO=FOO\nFOO2=FOO2\n")
end

it "ignores blank lines" do
@input = StringIO.new("\nFOO=BAR\nFOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
expect(@buffer.string).to eq("\nFOO=FOO\nFOO2=FOO2\n")
end

it "ignores comments" do
@comment_input = StringIO.new("#Heading comment\nFOO=BAR\nFOO2=BAR2\n")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@comment_input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
expect(@buffer.string).to eq("#Heading comment\nFOO=FOO\nFOO2=FOO2\n")
end

it "ignores comments with =" do
@comment_with_equal_input = StringIO.new("#Heading=comment\nFOO=BAR\nFOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@comment_with_equal_input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
expect(@buffer.string).to eq("#Heading=comment\nFOO=FOO\nFOO2=FOO2\n")
end

it "ignores comments with leading spaces" do
@comment_leading_spaces_input = StringIO.new(" #Heading comment\nFOO=BAR\nFOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@comment_leading_spaces_input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
expect(@buffer.string).to eq(" #Heading comment\nFOO=FOO\nFOO2=FOO2\n")
end
end

# Capture output to $stdout and $stderr
Expand Down

0 comments on commit fae8519

Please sign in to comment.