diff --git a/lib/vagrant/alias.rb b/lib/vagrant/alias.rb index 660ec001f..29b924976 100644 --- a/lib/vagrant/alias.rb +++ b/lib/vagrant/alias.rb @@ -9,13 +9,12 @@ module Vagrant aliases_file = env.home_path.join("aliases") if aliases_file.file? aliases_file.readlines.each do |line| - # skip comments - next if line.strip.start_with?("#") - # separate keyword-command pairs - keyword, command = line.split("=").collect(&:strip) + keyword, command = interpret(line) - register(keyword, command) + if keyword && command + register(keyword, command) + end end end end @@ -25,6 +24,16 @@ module Vagrant @aliases end + # This interprets a raw line from the aliases file. + def interpret(line) + # is it a comment? + return nil if line.strip.start_with?("#") + + keyword, command = line.split("=", 2).collect(&:strip) + + [keyword, command] + end + # This registers an alias. def register(keyword, command) @aliases.register(keyword.to_sym) do diff --git a/test/unit/vagrant/alias_test.rb b/test/unit/vagrant/alias_test.rb new file mode 100644 index 000000000..b15eac33d --- /dev/null +++ b/test/unit/vagrant/alias_test.rb @@ -0,0 +1,49 @@ +require_relative "../base" + +require "vagrant/alias" + +describe Vagrant::Alias do + include_context "unit" + include_context "command plugin helpers" + + let(:iso_env) { isolated_environment } + let(:env) { iso_env.create_vagrant_env } + + describe "#interpret" do + let(:interpreter) { described_class.new(env) } + + it "returns nil for comments" do + comments = [ + "# this is a comment", + "# so is this ", + " # and this", + " # this too " + ] + + comments.each do |comment| + expect(interpreter.interpret(comment)).to be_nil + end + end + + it "properly interprets a simple alias" do + keyword, command = interpreter.interpret("keyword=command") + + expect(keyword).to eq("keyword") + expect(command).to eq("command") + end + + it "properly interprets an alias with excess whitespace" do + keyword, command = interpreter.interpret(" keyword = command ") + + expect(keyword).to eq("keyword") + expect(command).to eq("command") + end + + it "properly interprets an alias with an equals sign in the command" do + keyword, command = interpreter.interpret(" keyword = command = command ") + + expect(keyword).to eq("keyword") + expect(command).to eq("command = command") + end + end +end