vagrantup 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -0
  3. data/CHANGELOG.md +48 -0
  4. data/Gemfile +16 -0
  5. data/Gemfile.lock +49 -0
  6. data/README.md +2 -2
  7. data/Rakefile +22 -0
  8. data/bin/.gitignore +0 -0
  9. data/lib/vagrant/provisioners/chef_solo.rb +1 -1
  10. data/lib/vagrant/util/template_renderer.rb +2 -2
  11. data/lib/vagrant/version.rb +1 -1
  12. data/templates/Vagrantfile.erb +1 -1
  13. data/templates/nfs/exports.erb +4 -2
  14. data/test/test_helper.rb +128 -0
  15. data/test/vagrant/action/box/destroy_test.rb +30 -0
  16. data/test/vagrant/action/box/download_test.rb +141 -0
  17. data/test/vagrant/action/box/package_test.rb +25 -0
  18. data/test/vagrant/action/box/unpackage_test.rb +103 -0
  19. data/test/vagrant/action/box/verify_test.rb +39 -0
  20. data/test/vagrant/action/builder_test.rb +218 -0
  21. data/test/vagrant/action/env/error_halt_test.rb +21 -0
  22. data/test/vagrant/action/env/set_test.rb +24 -0
  23. data/test/vagrant/action/environment_test.rb +45 -0
  24. data/test/vagrant/action/exception_catcher_test.rb +30 -0
  25. data/test/vagrant/action/general/package_test.rb +254 -0
  26. data/test/vagrant/action/vm/boot_test.rb +83 -0
  27. data/test/vagrant/action/vm/check_box_test.rb +48 -0
  28. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  29. data/test/vagrant/action/vm/clean_machine_folder_test.rb +82 -0
  30. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  31. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  32. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  33. data/test/vagrant/action/vm/customize_test.rb +29 -0
  34. data/test/vagrant/action/vm/destroy_test.rb +26 -0
  35. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +46 -0
  36. data/test/vagrant/action/vm/disable_networks_test.rb +39 -0
  37. data/test/vagrant/action/vm/discard_state_test.rb +36 -0
  38. data/test/vagrant/action/vm/export_test.rb +135 -0
  39. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  40. data/test/vagrant/action/vm/forward_ports_test.rb +191 -0
  41. data/test/vagrant/action/vm/halt_test.rb +69 -0
  42. data/test/vagrant/action/vm/import_test.rb +50 -0
  43. data/test/vagrant/action/vm/match_mac_address_test.rb +28 -0
  44. data/test/vagrant/action/vm/network_test.rb +246 -0
  45. data/test/vagrant/action/vm/nfs_helpers_test.rb +23 -0
  46. data/test/vagrant/action/vm/nfs_test.rb +269 -0
  47. data/test/vagrant/action/vm/package_test.rb +25 -0
  48. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  49. data/test/vagrant/action/vm/persist_test.rb +50 -0
  50. data/test/vagrant/action/vm/provision_test.rb +134 -0
  51. data/test/vagrant/action/vm/resume_test.rb +35 -0
  52. data/test/vagrant/action/vm/share_folders_test.rb +215 -0
  53. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  54. data/test/vagrant/action_test.rb +111 -0
  55. data/test/vagrant/active_list_test.rb +173 -0
  56. data/test/vagrant/box_test.rb +166 -0
  57. data/test/vagrant/command_test.rb +53 -0
  58. data/test/vagrant/commands/base_test.rb +139 -0
  59. data/test/vagrant/commands/box/add_test.rb +34 -0
  60. data/test/vagrant/commands/box/list_test.rb +32 -0
  61. data/test/vagrant/commands/box/remove_test.rb +41 -0
  62. data/test/vagrant/commands/box/repackage_test.rb +52 -0
  63. data/test/vagrant/commands/destroy_test.rb +44 -0
  64. data/test/vagrant/commands/halt_test.rb +50 -0
  65. data/test/vagrant/commands/init_test.rb +71 -0
  66. data/test/vagrant/commands/package_test.rb +97 -0
  67. data/test/vagrant/commands/provision_test.rb +60 -0
  68. data/test/vagrant/commands/reload_test.rb +47 -0
  69. data/test/vagrant/commands/resume_test.rb +44 -0
  70. data/test/vagrant/commands/ssh_config_test.rb +77 -0
  71. data/test/vagrant/commands/ssh_test.rb +129 -0
  72. data/test/vagrant/commands/status_test.rb +40 -0
  73. data/test/vagrant/commands/suspend_test.rb +44 -0
  74. data/test/vagrant/commands/up_test.rb +49 -0
  75. data/test/vagrant/config_test.rb +307 -0
  76. data/test/vagrant/downloaders/base_test.rb +28 -0
  77. data/test/vagrant/downloaders/file_test.rb +33 -0
  78. data/test/vagrant/downloaders/http_test.rb +70 -0
  79. data/test/vagrant/environment_test.rb +770 -0
  80. data/test/vagrant/hosts/base_test.rb +46 -0
  81. data/test/vagrant/hosts/bsd_test.rb +56 -0
  82. data/test/vagrant/hosts/linux_test.rb +56 -0
  83. data/test/vagrant/provisioners/base_test.rb +36 -0
  84. data/test/vagrant/provisioners/chef_server_test.rb +182 -0
  85. data/test/vagrant/provisioners/chef_solo_test.rb +197 -0
  86. data/test/vagrant/provisioners/chef_test.rb +178 -0
  87. data/test/vagrant/ssh_session_test.rb +46 -0
  88. data/test/vagrant/ssh_test.rb +317 -0
  89. data/test/vagrant/systems/linux_test.rb +179 -0
  90. data/test/vagrant/util/busy_test.rb +106 -0
  91. data/test/vagrant/util/plain_logger_test.rb +17 -0
  92. data/test/vagrant/util/platform_test.rb +18 -0
  93. data/test/vagrant/util/resource_logger_test.rb +145 -0
  94. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  95. data/test/vagrant/util/template_renderer_test.rb +145 -0
  96. data/test/vagrant/util/translator_test.rb +61 -0
  97. data/test/vagrant/util_test.rb +27 -0
  98. data/test/vagrant/vm_test.rb +228 -0
  99. data/vagrant.gemspec +34 -0
  100. metadata +130 -9
@@ -0,0 +1,179 @@
1
+ require "test_helper"
2
+
3
+ class LinuxSystemTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Systems::Linux
6
+ @ssh = mock("ssh")
7
+ @mock_env = mock_environment
8
+ @vm = mock("vm")
9
+ @vm.stubs(:env).returns(@mock_env)
10
+ @instance = @klass.new(@vm)
11
+ end
12
+
13
+ context "halting" do
14
+ setup do
15
+ @ssh_session = mock("ssh_session")
16
+ @ssh.stubs(:execute).yields(@ssh_session)
17
+ @vm.stubs(:ssh).returns(@ssh)
18
+
19
+ @real_vm = mock("real_vm")
20
+ @real_vm.stubs(:state).returns(:powered_off)
21
+ @vm.stubs(:vm).returns(@real_vm)
22
+ end
23
+
24
+ should "execute halt via SSH" do
25
+ @ssh_session.expects(:exec!).with("sudo halt").once
26
+ @instance.halt
27
+ end
28
+ end
29
+
30
+ context "mounting shared folders" do
31
+ setup do
32
+ @name = "foo"
33
+ @guestpath = "/bar"
34
+ end
35
+
36
+ should "create the dir, mount the folder, then set permissions" do
37
+ mount_seq = sequence("mount_seq")
38
+ @ssh.expects(:exec!).with("sudo mkdir -p #{@guestpath}").in_sequence(mount_seq)
39
+ @instance.expects(:mount_folder).with(@ssh, @name, @guestpath).in_sequence(mount_seq)
40
+ @ssh.expects(:exec!).with("sudo chown #{@vm.env.config.ssh.username} #{@guestpath}").in_sequence(mount_seq)
41
+
42
+ @instance.mount_shared_folder(@ssh, @name, @guestpath)
43
+ end
44
+ end
45
+
46
+ context "preparing unison" do
47
+ setup do
48
+ @ssh.stubs(:exec!)
49
+ @ssh.stubs(:upload!)
50
+ @vm.stubs(:ssh).returns(@ssh)
51
+ end
52
+
53
+ should "upload the script" do
54
+ @vm.ssh.expects(:upload!).with do |script, path|
55
+ assert_equal @mock_env.config.unison.script, path
56
+ true
57
+ end
58
+
59
+ @instance.prepare_unison(@ssh)
60
+ end
61
+
62
+ should "make the script executable" do
63
+ @ssh.expects(:exec!).with("sudo chmod +x #{@mock_env.config.unison.script}").once
64
+ @instance.prepare_unison(@ssh)
65
+ end
66
+
67
+ should "remove old crontab entry file" do
68
+ @ssh.expects(:exec!).with("sudo rm #{@mock_env.config.unison.crontab_entry_file}", :error_check => false).once
69
+ @instance.prepare_unison(@ssh)
70
+ end
71
+ end
72
+
73
+ context "creating unison entry" do
74
+ setup do
75
+ @ssh.stubs(:exec!)
76
+ @options = {
77
+ :guestpath => "foo",
78
+ :original => { :guestpath => "bar!" }
79
+ }
80
+ end
81
+
82
+ should "render the crontab entry with proper variables" do
83
+ variables = {
84
+ :from => @options[:guestpath],
85
+ :to => @options[:original][:guestpath],
86
+ :options => @mock_env.config.unison.options,
87
+ :script => @mock_env.config.unison.script,
88
+ :log_file => @mock_env.config.unison.log_file % "bar-"
89
+ }
90
+ Vagrant::Util::TemplateRenderer.expects(:render).with('/unison/crontab_entry',
91
+ variables).once
92
+ @instance.create_unison(@ssh, @options)
93
+ end
94
+
95
+ should "remove the .unison directory" do
96
+ @ssh.expects(:exec!).with("sudo rm -rf ~/.unison")
97
+ @instance.create_unison(@ssh, @options)
98
+ end
99
+
100
+ should "remove the original guestpath" do
101
+ @ssh.expects(:exec!).with("sudo rm -rf #{@options[:original][:guestpath]}")
102
+ @instance.create_unison(@ssh, @options)
103
+ end
104
+
105
+ should "enable the crontab file" do
106
+ @ssh.expects(:exec!).with("crontab #{@mock_env.config.unison.crontab_entry_file}")
107
+ @instance.create_unison(@ssh, @options)
108
+ end
109
+ end
110
+
111
+ #-------------------------------------------------------------------
112
+ # "Private" methods tests
113
+ #-------------------------------------------------------------------
114
+ context "mounting the main folder" do
115
+ setup do
116
+ @name = "foo"
117
+ @guestpath = "bar"
118
+ @sleeptime = 0
119
+ @limit = 10
120
+
121
+ @success_return = false
122
+ end
123
+
124
+ def mount_folder
125
+ @instance.mount_folder(@ssh, @name, @guestpath, @sleeptime)
126
+ end
127
+
128
+ should "execute the proper mount command" do
129
+ @ssh.expects(:exec!).with("sudo mount -t vboxsf -o uid=#{@vm.env.config.ssh.username},gid=#{@vm.env.config.ssh.username} #{@name} #{@guestpath}").returns(@success_return)
130
+ mount_folder
131
+ end
132
+
133
+ should "test type of text and text string to detect error" do
134
+ data = mock("data")
135
+ data.expects(:[]=).with(:result, !@success_return)
136
+
137
+ @ssh.expects(:exec!).yields(data, :stderr, "No such device").returns(@success_return)
138
+ mount_folder
139
+ end
140
+
141
+ should "test type of text and test string to detect success" do
142
+ data = mock("data")
143
+ data.expects(:[]=).with(:result, @success_return)
144
+
145
+ @ssh.expects(:exec!).yields(data, :stdout, "Nothing such device").returns(@success_return)
146
+ mount_folder
147
+ end
148
+
149
+ should "raise an ActionException if the command fails constantly" do
150
+ @ssh.expects(:exec!).times(@limit).returns(!@success_return)
151
+
152
+ assert_raises(Vagrant::Action::ActionException) {
153
+ mount_folder
154
+ }
155
+ end
156
+
157
+ should "not raise any exception if the command succeeded" do
158
+ @ssh.expects(:exec!).once.returns(@success_return)
159
+
160
+ assert_nothing_raised {
161
+ mount_folder
162
+ }
163
+ end
164
+
165
+ should "add uid AND gid to mount" do
166
+ uid = "foo"
167
+ gid = "bar"
168
+ env = mock_environment do |config|
169
+ config.vm.shared_folder_uid = uid
170
+ config.vm.shared_folder_gid = gid
171
+ end
172
+
173
+ @vm.stubs(:env).returns(env)
174
+
175
+ @ssh.expects(:exec!).with("sudo mount -t vboxsf -o uid=#{uid},gid=#{gid} #{@name} #{@guestpath}").returns(@success_return)
176
+ mount_folder
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,106 @@
1
+ require "test_helper"
2
+
3
+ class BusyUtilTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Util::Busy
6
+ end
7
+
8
+ context "registering" do
9
+ setup do
10
+ @callback = lambda { puts "FOO" }
11
+ Signal.stubs(:trap)
12
+ end
13
+
14
+ teardown do
15
+ @klass.registered.clear
16
+ end
17
+
18
+ should "trap the signal on the first registration" do
19
+ Signal.expects(:trap).with("INT").once
20
+ @klass.register(@callback)
21
+ @klass.register(lambda { puts "BAR" })
22
+ end
23
+
24
+ should "not register the same callback multiple times" do
25
+ @klass.register(@callback)
26
+ @klass.register(@callback)
27
+ @klass.register(@callback)
28
+ assert_equal 1, @klass.registered.length
29
+ assert_equal @callback, @klass.registered.first
30
+ end
31
+ end
32
+
33
+ context "unregistering" do
34
+ setup do
35
+ Signal.stubs(:trap)
36
+
37
+ @callback = lambda { puts "FOO" }
38
+ end
39
+
40
+ teardown do
41
+ @klass.registered.clear
42
+ end
43
+
44
+ should "remove the callback and set the trap to DEFAULT when removing final" do
45
+ @klass.register(@callback)
46
+ Signal.expects(:trap).with("INT", "DEFAULT").once
47
+ @klass.unregister(@callback)
48
+ assert @klass.registered.empty?
49
+ end
50
+
51
+ should "not reset signal trap if not final callback" do
52
+ @klass.register(@callback)
53
+ @klass.register(lambda { puts "BAR" })
54
+ Signal.expects(:trap).never
55
+ @klass.unregister(@callback)
56
+ end
57
+ end
58
+
59
+ context "marking for busy" do
60
+ setup do
61
+ @callback = lambda { "foo" }
62
+ end
63
+
64
+ should "register, call the block, then unregister" do
65
+ waiter = mock("waiting")
66
+ proc = lambda { waiter.ping! }
67
+
68
+ seq = sequence('seq')
69
+ @klass.expects(:register).with(@callback).in_sequence(seq)
70
+ waiter.expects(:ping!).in_sequence(seq)
71
+ @klass.expects(:unregister).with(@callback).in_sequence(seq)
72
+
73
+ @klass.busy(@callback, &proc)
74
+ end
75
+
76
+ should "unregister callback even if block raises exception" do
77
+ waiter = mock("waiting")
78
+ proc = lambda { waiter.ping! }
79
+
80
+ seq = sequence('seq')
81
+ @klass.expects(:register).with(@callback).in_sequence(seq)
82
+ waiter.expects(:ping!).raises(Exception.new("uh oh!")).in_sequence(seq)
83
+ @klass.expects(:unregister).with(@callback).in_sequence(seq)
84
+
85
+ assert_raises(Exception) { @klass.busy(@callback, &proc) }
86
+ end
87
+ end
88
+
89
+ context "firing callbacks" do
90
+ setup do
91
+ Signal.stubs(:trap)
92
+ end
93
+
94
+ teardown do
95
+ @klass.registered.clear
96
+ end
97
+
98
+ should "just call the registered callbacks" do
99
+ waiting = mock("waiting")
100
+ waiting.expects(:ping!).once
101
+
102
+ @klass.register(lambda { waiting.ping! })
103
+ @klass.fire_callbacks
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,17 @@
1
+ require "test_helper"
2
+
3
+ class PlainLoggerUtilTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Util::PlainLogger
6
+ @instance = @klass.new(nil)
7
+ end
8
+
9
+ should "inherit from the standard logger" do
10
+ assert @instance.is_a?(::Logger)
11
+ end
12
+
13
+ should "just add a newline to the message" do
14
+ msg = "foo bar baz"
15
+ assert_equal "#{msg}\n", @instance.format_message("1", "2", "3", msg)
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require "test_helper"
2
+
3
+ class PlatformTest < Test::Unit::TestCase
4
+ context "file options" do
5
+ should "include add binary bit to options on windows platform" do
6
+ # This constant is not defined on non-windows platforms, so define it here
7
+ File::BINARY = 4096 unless defined?(File::BINARY)
8
+
9
+ Mario::Platform.expects(:windows?).returns(true)
10
+ assert_equal Vagrant::Util::Platform.tar_file_options, File::CREAT|File::EXCL|File::WRONLY|File::BINARY
11
+ end
12
+
13
+ should "not include binary bit on other platforms" do
14
+ Mario::Platform.expects(:windows?).returns(false)
15
+ assert_equal Vagrant::Util::Platform.tar_file_options, File::CREAT|File::EXCL|File::WRONLY
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,145 @@
1
+ require "test_helper"
2
+
3
+ class ResourceLoggerUtilTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Util::ResourceLogger
6
+ end
7
+
8
+ context "singleton logger" do
9
+ setup do
10
+ @klass.reset_singleton_logger!
11
+
12
+ @result = mock("result")
13
+ end
14
+
15
+ should "return a nil plain logger if no environment is given" do
16
+ Vagrant::Util::PlainLogger.expects(:new).with(nil).returns(@result)
17
+ assert_equal @result, @klass.singleton_logger
18
+ end
19
+
20
+ should "return a nil plain logger if the config is not loaded" do
21
+ env = mock_environment
22
+ env.config.stubs(:loaded?).returns(false)
23
+
24
+ Vagrant::Util::PlainLogger.expects(:new).with(nil).returns(@result)
25
+ assert_equal @result, @klass.singleton_logger(env)
26
+ end
27
+
28
+ should "return a logger with the specified output if environment is ready" do
29
+ output = mock("output")
30
+ env = mock_environment
31
+ env.config.vagrant.log_output = output
32
+
33
+ Vagrant::Util::PlainLogger.expects(:new).with(output).returns(@result)
34
+ assert_equal @result, @klass.singleton_logger(env)
35
+ end
36
+
37
+ should "only load the logger once" do
38
+ output = mock("output")
39
+ env = mock_environment
40
+ env.config.vagrant.log_output = output
41
+
42
+ Vagrant::Util::PlainLogger.expects(:new).with(output).returns(@result)
43
+ assert_equal @result, @klass.singleton_logger(env)
44
+ assert_equal @result, @klass.singleton_logger(env)
45
+ assert_equal @result, @klass.singleton_logger(env)
46
+ end
47
+ end
48
+
49
+ context "initialization" do
50
+ should "setup the logger and attributes" do
51
+ env = mock_environment
52
+ resource = mock("resource")
53
+ result = mock("result")
54
+
55
+ @klass.expects(:singleton_logger).with(env).returns(result)
56
+ instance = @klass.new(resource, env)
57
+ assert_equal resource, instance.resource
58
+ assert_equal env, instance.env
59
+ assert_equal result, instance.logger
60
+ end
61
+ end
62
+
63
+ context "with an instance" do
64
+ setup do
65
+ @resource = "foo"
66
+ @env = mock_environment
67
+ @logger = mock("logger")
68
+
69
+ @klass.stubs(:singleton_logger).returns(@logger)
70
+ @instance = @klass.new(@resource, @env)
71
+ end
72
+
73
+ context "logging methods" do
74
+ setup do
75
+ @instance.stubs(:flush_progress)
76
+ @instance.stubs(:cl_reset).returns("")
77
+ end
78
+
79
+ [:debug, :info, :error, :fatal].each do |method|
80
+ should "log with the proper format on #{method}" do
81
+ message = "bar"
82
+ @logger.expects(method).with("[#{@resource}] #{message}").once
83
+ @instance.send(method, message)
84
+ end
85
+ end
86
+ end
87
+
88
+ context "reporting progress" do
89
+ setup do
90
+ @instance.stubs(:flush_progress)
91
+ end
92
+
93
+ should "flush progress" do
94
+ @instance.expects(:flush_progress).once
95
+ @instance.report_progress(72, 100)
96
+ end
97
+
98
+ should "add the reporter to the progress reporters" do
99
+ @instance.report_progress(72, 100)
100
+ assert @klass.progress_reporters.has_key?(@instance.resource)
101
+ end
102
+ end
103
+
104
+ context "clearing progress" do
105
+ setup do
106
+ @instance.stubs(:flush_progress)
107
+
108
+ @klass.progress_reporters.clear
109
+ @instance.report_progress(72, 100)
110
+ end
111
+
112
+ should "remove the key from the reporters" do
113
+ assert @klass.progress_reporters.has_key?(@instance.resource)
114
+ @instance.clear_progress
115
+ assert !@klass.progress_reporters.has_key?(@instance.resource)
116
+ end
117
+ end
118
+
119
+ context "command line reset" do
120
+ setup do
121
+ Mario::Platform.logger(nil)
122
+ end
123
+
124
+ context "on windows" do
125
+ setup do
126
+ Mario::Platform.forced = Mario::Platform::Windows7
127
+ end
128
+
129
+ should "just return \\r for the clear screen" do
130
+ assert_equal "\r", @instance.cl_reset
131
+ end
132
+ end
133
+
134
+ context "on other platforms" do
135
+ setup do
136
+ Mario::Platform.forced = Mario::Platform::Linux
137
+ end
138
+
139
+ should "return the full clear screen" do
140
+ assert_equal "\r\e[0K", @instance.cl_reset
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,43 @@
1
+ require "test_helper"
2
+
3
+ class StackedProcRunnerUtilTest < Test::Unit::TestCase
4
+ class TestClass
5
+ include Vagrant::Util::StackedProcRunner
6
+ end
7
+
8
+ setup do
9
+ @instance = TestClass.new
10
+ @instance.proc_stack.clear
11
+ end
12
+
13
+ should "not run the procs right away" do
14
+ obj = mock("obj")
15
+ obj.expects(:foo).never
16
+ @instance.push_proc { |config| obj.foo }
17
+ @instance.push_proc { |config| obj.foo }
18
+ @instance.push_proc { |config| obj.foo }
19
+ end
20
+
21
+ should "run the blocks when run_procs! is ran" do
22
+ obj = mock("obj")
23
+ obj.expects(:foo).times(2)
24
+ @instance.push_proc { obj.foo }
25
+ @instance.push_proc { obj.foo }
26
+ @instance.run_procs!
27
+ end
28
+
29
+ should "run the blocks with the same arguments" do
30
+ passed_config = mock("config")
31
+ @instance.push_proc { |config| assert passed_config.equal?(config) }
32
+ @instance.push_proc { |config| assert passed_config.equal?(config) }
33
+ @instance.run_procs!(passed_config)
34
+ end
35
+
36
+ should "not clear the blocks after running" do
37
+ obj = mock("obj")
38
+ obj.expects(:foo).times(2)
39
+ @instance.push_proc { obj.foo }
40
+ @instance.run_procs!
41
+ @instance.run_procs!
42
+ end
43
+ end
@@ -0,0 +1,145 @@
1
+ require "test_helper"
2
+
3
+ class TemplateRendererUtilTest < Test::Unit::TestCase
4
+ context "initializing" do
5
+ should "set the template to the given argument" do
6
+ r = Vagrant::Util::TemplateRenderer.new("foo")
7
+ assert_equal "foo", r.template
8
+ end
9
+
10
+ should "set any additional variables" do
11
+ r = Vagrant::Util::TemplateRenderer.new("foo", {:bar => :baz})
12
+ assert_equal :baz, r.bar
13
+ end
14
+ end
15
+
16
+ context "rendering" do
17
+ setup do
18
+ @template = "foo"
19
+ @r = Vagrant::Util::TemplateRenderer.new(@template)
20
+ @r.stubs(:full_template_path).returns(@template + "!")
21
+
22
+ @contents = "bar"
23
+
24
+ @file = mock("file")
25
+ @file.stubs(:read).returns(@contents)
26
+ File.stubs(:open).yields(@file)
27
+ end
28
+
29
+ should "open the template file for reading" do
30
+ File.expects(:open).with(@r.full_template_path, 'r').once
31
+ @r.render
32
+ end
33
+
34
+ should "set the template to the file contents, render, then set it back" do
35
+ result = "bar"
36
+
37
+ template_seq = sequence("template_seq")
38
+ @r.expects(:template=).with(@file.read).in_sequence(template_seq)
39
+ @r.expects(:render_string).returns(result).in_sequence(template_seq)
40
+ @r.expects(:template=).with(@template).in_sequence(template_seq)
41
+ assert_equal result, @r.render
42
+ end
43
+
44
+ should "render the ERB file in the context of the renderer" do
45
+ result = "bar"
46
+ template = "<%= foo %>"
47
+ @r.foo = result
48
+ @file.expects(:read).returns(template)
49
+ assert_equal result, @r.render
50
+ end
51
+ end
52
+
53
+ context "rendering as string" do
54
+ setup do
55
+ @result = "foo"
56
+ @erb = mock("erb")
57
+ @erb.stubs(:result).returns(@result)
58
+
59
+ @r = Vagrant::Util::TemplateRenderer.new("foo")
60
+ end
61
+
62
+ should "simply render the template as a string" do
63
+ Erubis::Eruby.expects(:new).with(@r.template, :trim => true).returns(@erb)
64
+ @erb.expects(:result).returns(@result)
65
+ assert_equal @result, @r.render_string
66
+ end
67
+ end
68
+
69
+ context "the full template path" do
70
+ setup do
71
+ @template = "foo"
72
+ @r = Vagrant::Util::TemplateRenderer.new(@template)
73
+ end
74
+
75
+ should "be the ERB file in the templates directory" do
76
+ result = File.join(PROJECT_ROOT, "templates", "#{@template}.erb")
77
+ assert_equal result, @r.full_template_path
78
+ end
79
+
80
+ should "remove duplicate path separators" do
81
+ @r.template = "foo///bar"
82
+ result = File.join(PROJECT_ROOT, "templates", "foo", "bar.erb")
83
+ assert_equal result, @r.full_template_path
84
+ end
85
+ end
86
+
87
+ context "class methods" do
88
+ context "render_with method" do
89
+ setup do
90
+ @template = "foo"
91
+ @r = Vagrant::Util::TemplateRenderer.new(@template)
92
+ @r.stubs(:render)
93
+
94
+ @method = :rawr
95
+
96
+ Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
97
+ end
98
+
99
+ should "use the second argument as the template" do
100
+ Vagrant::Util::TemplateRenderer.expects(:new).with(@template, {}).returns(@r)
101
+ Vagrant::Util::TemplateRenderer.render_with(@method, @template)
102
+ end
103
+
104
+ should "send in additional argument to the renderer" do
105
+ data = {:hey => :foo}
106
+ Vagrant::Util::TemplateRenderer.expects(:new).with(@template, data).returns(@r)
107
+ Vagrant::Util::TemplateRenderer.render_with(@method, @template, data)
108
+ end
109
+
110
+ should "yield a block if given with the renderer as the argument" do
111
+ @r.expects(:yielded=).with(true).once
112
+ Vagrant::Util::TemplateRenderer.render_with(@method, @template) do |r|
113
+ r.yielded = true
114
+ end
115
+ end
116
+
117
+ should "render the result using the given method" do
118
+ result = mock('result')
119
+ @r.expects(@method).returns(result)
120
+ assert_equal result, Vagrant::Util::TemplateRenderer.render_with(@method, @template)
121
+ end
122
+
123
+ should "convert the given method to a sym prior to calling" do
124
+ @r.expects(@method.to_sym).returns(nil)
125
+ Vagrant::Util::TemplateRenderer.render_with(@method.to_s, @template)
126
+ end
127
+ end
128
+
129
+ context "render method" do
130
+ should "call render_with the render! method" do
131
+ args = ["foo", "bar", "baz"]
132
+ Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render, *args)
133
+ Vagrant::Util::TemplateRenderer.render(*args)
134
+ end
135
+ end
136
+
137
+ context "render_string method" do
138
+ should "call render_with the render! method" do
139
+ args = ["foo", "bar", "baz"]
140
+ Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render_string, *args)
141
+ Vagrant::Util::TemplateRenderer.render_string(*args)
142
+ end
143
+ end
144
+ end
145
+ end