From 7ddff513f0ed87c5fd7d3a03ac192ce64106b723 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 27 Aug 2010 23:02:43 -0700 Subject: [PATCH] Shell UI tests. Added option to not translate. Updated non-translated strings. --- bin/vagrant | 5 ++- lib/vagrant/command/box.rb | 4 +- lib/vagrant/command/ssh.rb | 2 +- lib/vagrant/command/ssh_config.rb | 4 +- lib/vagrant/provisioners/chef_server.rb | 2 +- lib/vagrant/provisioners/chef_solo.rb | 2 +- lib/vagrant/ui.rb | 6 +-- test/vagrant/ui_test.rb | 49 +++++++++++++++++++++++++ 8 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 test/vagrant/ui_test.rb diff --git a/bin/vagrant b/bin/vagrant index d79b53ade..435f7e051 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -7,7 +7,8 @@ env = Vagrant::Environment.load! begin Vagrant::CLI.start(ARGV, :env => env) rescue Vagrant::Errors::VagrantError => e - env.ui.error e.message, false - env.ui.error e.backtrace.join("\n"), false if ENV["VAGRANT_DEBUG"] + opts = { :_translate => false, :_prefix => false } + env.ui.error e.message, opts + env.ui.error e.backtrace.join("\n"), opts if ENV["VAGRANT_DEBUG"] exit e.status_code end diff --git a/lib/vagrant/command/box.rb b/lib/vagrant/command/box.rb index 27008a6c7..bed72fc39 100644 --- a/lib/vagrant/command/box.rb +++ b/lib/vagrant/command/box.rb @@ -25,8 +25,8 @@ module Vagrant desc "list", "Lists all installed boxes" def list boxes = Box.all(env).sort - return env.ui.warn("vagrant.commands.box.no_installed_boxes", :_prefix => false)# if boxes.empty? - boxes.each { |b| env.ui.info(b) } + return env.ui.warn("vagrant.commands.box.no_installed_boxes", :_prefix => false) if boxes.empty? + boxes.each { |b| env.ui.info(b, :_translate => false, :_prefix => false) } end end end diff --git a/lib/vagrant/command/ssh.rb b/lib/vagrant/command/ssh.rb index d7978df70..958d26ef8 100644 --- a/lib/vagrant/command/ssh.rb +++ b/lib/vagrant/command/ssh.rb @@ -19,7 +19,7 @@ module Vagrant ssh_vm.ssh.execute do |ssh| ssh_vm.env.ui.info "vagrant.commands.ssh.execute", :command => options[:execute] ssh.exec!(options[:execute]) do |channel, type, data| - ssh_vm.env.ui.info "#{data}" + ssh_vm.env.ui.info "#{data}", :_translate => false end end end diff --git a/lib/vagrant/command/ssh_config.rb b/lib/vagrant/command/ssh_config.rb index 07424dc2e..311b05aac 100644 --- a/lib/vagrant/command/ssh_config.rb +++ b/lib/vagrant/command/ssh_config.rb @@ -10,12 +10,12 @@ module Vagrant vm = target_vms.first raise VMNotCreatedError.new if !vm.created? - env.ui.info Util::TemplateRenderer.render("ssh_config", { + env.ui.info(Util::TemplateRenderer.render("ssh_config", { :host_key => options[:host] || "vagrant", :ssh_user => vm.env.config.ssh.username, :ssh_port => vm.ssh.port, :private_key_path => vm.env.config.ssh.private_key_path - }) + }), :_translate => false, :_prefix => false) end end end diff --git a/lib/vagrant/provisioners/chef_server.rb b/lib/vagrant/provisioners/chef_server.rb index f9f0419db..37d0aea85 100644 --- a/lib/vagrant/provisioners/chef_server.rb +++ b/lib/vagrant/provisioners/chef_server.rb @@ -58,7 +58,7 @@ module Vagrant if type == :exit_status ssh.check_exit_status(data, command) else - env.ui.info("#{data}: #{type}") + env.ui.info("#{data}: #{type}", :_translate => false) end end end diff --git a/lib/vagrant/provisioners/chef_solo.rb b/lib/vagrant/provisioners/chef_solo.rb index 002d1c4d7..73489eb53 100644 --- a/lib/vagrant/provisioners/chef_solo.rb +++ b/lib/vagrant/provisioners/chef_solo.rb @@ -44,7 +44,7 @@ module Vagrant vm.ssh.execute do |ssh| ssh.exec!(command) do |channel, type, data| ssh.check_exit_status(data, command) if type == :exit_status - env.ui.info("#{data}: #{type}") if type != :exit_status + env.ui.info("#{data}: #{type}", :_translate => false) if type != :exit_status end end end diff --git a/lib/vagrant/ui.rb b/lib/vagrant/ui.rb index 7b006f833..2753adc32 100644 --- a/lib/vagrant/ui.rb +++ b/lib/vagrant/ui.rb @@ -26,9 +26,9 @@ module Vagrant end [[:warn, :yellow], [:error, :red], [:info, nil], [:confirm, :green]].each do |method, color| - define_method(method) do |key, opts=nil| - opts = { :_prefix => true }.merge(opts || {}) - message = I18n.t(key, opts) + define_method(method) do |message, opts=nil| + opts = { :_prefix => true, :_translate => true }.merge(opts || {}) + message = I18n.t(message, opts) if opts[:_translate] message = format_message(message) if opts[:_prefix] @shell.say("#{line_reset}#{message}", color) end diff --git a/test/vagrant/ui_test.rb b/test/vagrant/ui_test.rb new file mode 100644 index 000000000..a2b3f404d --- /dev/null +++ b/test/vagrant/ui_test.rb @@ -0,0 +1,49 @@ +require "test_helper" + +class ShellUITest < Test::Unit::TestCase + setup do + @klass = Vagrant::UI::Shell + @shell = mock("shell") + @instance = @klass.new(mock_environment, @shell) + end + + context "prefixing with resource" do + should "prefix message with environment resource" do + @shell.expects(:say).with() do |message, color| + assert message =~ /\[#{@instance.env.resource}\]/ + true + end + + @instance.info("vagrant.errors.test_key") + end + + should "not prefix the message if given false" do + @shell.expects(:say).with() do |message, color| + assert message !~ /\[#{@instance.env.resource}\]/ + true + end + + @instance.info("vagrant.errors.test_key", :_prefix => false) + end + end + + context "translating" do + should "translate the message by default" do + @shell.expects(:say).with() do |message, color| + assert message.include?(I18n.t("vagrant.errors.test_key")) + true + end + + @instance.info("vagrant.errors.test_key") + end + + should "not translate the message if noted" do + @shell.expects(:say).with() do |message, color| + assert message.include?("vagrant.errors.test_key") + true + end + + @instance.info("vagrant.errors.test_key", :_translate => false) + end + end +end