Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix template (-t) handling of blank lines and comments #413

Merged
merged 1 commit into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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