From fae85193a947b4e1a351ae99d09db0cc75be6484 Mon Sep 17 00:00:00 2001 From: Andre Dickson Date: Tue, 23 Jun 2020 02:23:27 +0000 Subject: [PATCH] Fix template (-t) handling of blank lines and comments 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. --- lib/dotenv/template.rb | 6 ++-- spec/dotenv/cli_spec.rb | 69 +++++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/lib/dotenv/template.rb b/lib/dotenv/template.rb index a872cef2..1e4bd5e1 100644 --- a/lib/dotenv/template.rb +++ b/lib/dotenv/template.rb @@ -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 diff --git a/spec/dotenv/cli_spec.rb b/spec/dotenv/cli_spec.rb index 70ace472..ba3732ca 100644 --- a/spec/dotenv/cli_spec.rb +++ b/spec/dotenv/cli_spec.rb @@ -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