vagrant-triggers 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66df1e94338bdbd5b8ea22fa123abc641dbc9e05
4
- data.tar.gz: 1310d39fe9e67faaac2a2784b6ed841939a7092e
3
+ metadata.gz: 92d93f338ac34daa73bb81b1c67e82a098ed6a3b
4
+ data.tar.gz: 19f64c4f8f7d5379719a126d8e5fdf0fa2db76b3
5
5
  SHA512:
6
- metadata.gz: 3cd9b20a5e2fdb1f15c702502f008fb89afbb91d3685874c4270bde7000bce13cb44506203085c5e651af3d9900dd655c01743e13bed877c6134b7773c39e7cf
7
- data.tar.gz: 600308d8966f0b35fba1c2a17698382ecce0c80054f02103374124144f94345a4e560a301ce47ef97f3bdc85fd90bd99351470dd4c38f7580b3e3fb46b02dab5
6
+ metadata.gz: 0cb4fa497e40a8697e7f86d7276c63a7e38df7c5c82aaa3c804566fa3bc783138547f8d60640f7cde21b7d051bc1abe891fd0f4cd80f24ced622eb7e19c54bb5
7
+ data.tar.gz: ad5b94780e81efe97b693e0460ce1163983a95c171d980d2021ffe99c69b92746ee68125e3ab75c2a398a7de0530965c5d30e126f7028c2cdfd88f7d9d30b00d
data/.travis.yml CHANGED
@@ -1,11 +1,15 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
3
+ - 2.0.0
4
4
  env:
5
- - VAGRANT_VERSION=v1.4.3
5
+ - VAGRANT_VERSION=v1.5.2
6
6
  matrix:
7
7
  include:
8
+ - env: VAGRANT_VERSION=master
9
+ - env: VAGRANT_VERSION=v1.4.3
8
10
  - env: VAGRANT_VERSION=v1.3.5
9
11
  rvm: 1.9.3
10
12
  - env: VAGRANT_VERSION=v1.2.7
11
13
  rvm: 1.9.3
14
+ allow_failures:
15
+ - env: VAGRANT_VERSION=master
data/CHANGELOG.md CHANGED
@@ -1,4 +1,22 @@
1
- ## 0.2.2 (unreleased)
1
+ ## 0.3.0 (April 4, 2014)
2
+
3
+ CHANGES:
4
+
5
+ - Implement a new DSL for running scripts.
6
+
7
+ DEPRECATIONS:
8
+
9
+ - The ```:info``` and ```:execute``` options has been replaced by the new DSL.
10
+
11
+ IMPROVEMENTS:
12
+
13
+ - Plugin messages are now localized.
14
+
15
+ BUG FIXES:
16
+
17
+ - Avoid loops by adding VAGRANT_NO_TRIGGERS to the subprocess shell.
18
+
19
+ ## 0.2.2 (March 1, 2014)
2
20
 
3
21
  NEW FEATURES:
4
22
 
@@ -6,7 +24,7 @@ NEW FEATURES:
6
24
 
7
25
  BUG FIXES:
8
26
 
9
- - Remove Vagrant specific environment variables when executing commands [(#5)](https://github.com/emyl/vagrant-triggers/issues/2)
27
+ - Remove Vagrant specific environment variables when executing commands [(#5)](https://github.com/emyl/vagrant-triggers/issues/5)
10
28
 
11
29
  ## 0.2.1 (November 19, 2013)
12
30
 
data/Gemfile CHANGED
@@ -2,8 +2,13 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
+ # Warning: Hack below.
6
+ #
7
+ # Add the current project gem to the "plugins" group
8
+ dependencies.find { |dep| dep.name == "vagrant-triggers" }.instance_variable_set(:@groups, [:default, :plugins])
9
+
5
10
  group :development do
6
- gem "vagrant", :github => "mitchellh/vagrant", :ref => ENV.fetch("VAGRANT_VERSION", "v1.4.3")
11
+ gem "vagrant", :github => "mitchellh/vagrant", :ref => ENV.fetch("VAGRANT_VERSION", "master")
7
12
  end
8
13
 
9
14
  group :test do
data/README.md CHANGED
@@ -17,32 +17,42 @@ Installation is performed in the prescribed manner for Vagrant plugins:
17
17
 
18
18
  ### Basic usage
19
19
 
20
- The following ```Vagrantfile``` configuration options are added:
20
+ ```ruby
21
+ Vagrant.configure("2") do |config|
22
+ # Your existing Vagrant configuration
23
+ ...
21
24
 
22
- ```
23
- trigger.before :command, { :option => "value", ... }
24
- ```
25
+ config.trigger.before :command, :option => "value" do
26
+ run "script"
27
+ ...
28
+ end
25
29
 
26
- ```
27
- trigger.after :command, { :option => "value", ... }
30
+ config.trigger.after :command, :option => "value" do
31
+ run "script"
32
+ ...
33
+ end
34
+ end
28
35
  ```
29
36
 
30
37
  The first argument is the command in which the trigger will be tied. It could be an array (e.g. ```[:up, :resume]```) in case of multiple commands.
31
38
 
32
39
  ### Options
33
40
 
34
- * ```:execute => "script"```: the script to execute
35
- * ```:info => "string"```: an informational message to be displayed, instead of executing a command. This is only displayed if :execute is not set.
36
-
37
- * ```:append_to_path => ["dir", "dir"]```: additional places where looking for the script. See [this wiki page](https://github.com/emyl/vagrant-triggers/wiki/The-:append_to_path-option) for details.
38
- * ```:force => true```: continue even if the script fails (exits with non-zero code)
41
+ * ```:append_to_path => ["dir", "dir"]```: additional places where looking for scripts. See [this wiki page](https://github.com/emyl/vagrant-triggers/wiki/The-:append_to_path-option) for details.
42
+ * ```:force => true```: continue even one of the scripts fails (exits with non-zero code)
39
43
  * ```:stdout => true```: display script output
40
44
 
45
+ ### Trigger block DSL
46
+
47
+ The given block will be evaluated by an instance of the [VagrantPlugins::Triggers::DSL](https://github.com/emyl/vagrant-triggers/blob/master/lib/vagrant-triggers/dsl.rb) class. This class defines a very simple DSL for running scripts on the host machine. Basically only one method (`run`) is directly defined, all the other calls will be forwarded to Vagrant's [ui](https://github.com/mitchellh/vagrant/blob/master/lib/vagrant/ui.rb) instance. This allows the definition of custom messages along with scripts.
48
+
49
+ For additional details you can take a look to the [VagrantPlugins::Triggers::DSL](https://github.com/emyl/vagrant-triggers/blob/master/lib/vagrant-triggers/dsl.rb) definition.
50
+
41
51
  ### Skipping execution
42
52
 
43
53
  Triggers won't run if ```VAGRANT_NO_TRIGGERS``` environment variable is set.
44
54
 
45
- ## Example
55
+ ## A more detailed example
46
56
 
47
57
  In the following example a VirtualBox VM (not managed by Vagrant) will be tied to the machine defined in ```Vagrantfile```, to make so that it follows its lifecycle:
48
58
 
@@ -55,12 +65,17 @@ Vagrant.configure("2") do |config|
55
65
  :suspend => "controlvm 22aed8b3-d246-40d5-8ad4-176c17552c43 savestate",
56
66
  :halt => "controlvm 22aed8b3-d246-40d5-8ad4-176c17552c43 acpipowerbutton",
57
67
  }.each do |command, trigger|
58
- config.trigger.before command, :execute => "vboxmanage #{trigger}", :stdout => true
68
+ config.trigger.before command, :stdout => true do
69
+ info "Executing #{command} action on the VirtualBox tied VM..."
70
+ run "vboxmanage #{trigger}"
71
+ end
59
72
  end
60
73
 
61
74
  end
62
75
  ```
63
76
 
77
+ For additional examples, see the [trigger recipes](https://github.com/emyl/vagrant-triggers/wiki/Trigger-recipes) wiki page.
78
+
64
79
  ## Contributing
65
80
 
66
81
  To contribute, clone the repository, and use [Bundler](http://bundler.io/)
@@ -76,4 +91,3 @@ You can now fork this repository, make your changes and send a pull request.
76
91
 
77
92
 
78
93
  [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/emyl/vagrant-triggers/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
79
-
@@ -5,9 +5,10 @@ module VagrantPlugins
5
5
  module Triggers
6
6
  lib_path = Pathname.new(File.expand_path("../vagrant-triggers", __FILE__))
7
7
  autoload :Action, lib_path.join("action")
8
+ autoload :DSL, lib_path.join("dsl")
8
9
  autoload :Config, lib_path.join("config")
9
10
  autoload :Errors, lib_path.join("errors")
10
-
11
+
11
12
  # This returns the path to the source of this plugin.
12
13
  #
13
14
  # @return [Pathname]
@@ -1,6 +1,4 @@
1
1
  require "log4r"
2
- require "shellwords"
3
- require "vagrant/util/subprocess"
4
2
 
5
3
  module VagrantPlugins
6
4
  module Triggers
@@ -22,41 +20,6 @@ module VagrantPlugins
22
20
 
23
21
  private
24
22
 
25
- def build_environment
26
- @logger.debug("Original environment: #{ENV.inspect}")
27
-
28
- # Remove GEM_ environment variables
29
- ["GEM_HOME", "GEM_PATH", "GEMRC"].each { |gem_var| ENV.delete(gem_var) }
30
-
31
- # Create the new PATH removing Vagrant bin directory
32
- # and appending directories specified through the
33
- # :append_to_path option
34
- new_path = ENV["VAGRANT_INSTALLER_ENV"] ? ENV["PATH"].gsub(/#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}.*?#{File::PATH_SEPARATOR}/, "") : ENV["PATH"]
35
- new_path += Array(@options[:append_to_path]).map { |dir| "#{File::PATH_SEPARATOR}#{dir}" }.join
36
- ENV["PATH"] = new_path
37
- @logger.debug("PATH modifed: #{ENV["PATH"]}")
38
- end
39
-
40
- def execute(raw_command)
41
- @env[:ui].info 'Executing command "' + raw_command + '"...'
42
- command = Shellwords.shellsplit(raw_command)
43
- env_backup = ENV.to_hash
44
- begin
45
- build_environment
46
- result = Vagrant::Util::Subprocess.execute(command[0], *command[1..-1])
47
- rescue Vagrant::Errors::CommandUnavailable, Vagrant::Errors::CommandUnavailableWindows
48
- raise Errors::CommandUnavailable, :command => command[0]
49
- ensure
50
- ENV.replace(env_backup)
51
- end
52
- if result.exit_code != 0 && !@options[:force]
53
- raise Errors::CommandFailed, :command => raw_command, :stderr => result.stderr
54
- end
55
- if @options[:stdout]
56
- @env[:ui].info "Command output:\n\n#{result.stdout}\n"
57
- end
58
- end
59
-
60
23
  def fire_triggers
61
24
  # Triggers don't fire on environment load and unload.
62
25
  return if [:environment_load, :environment_unload].include?(@env[:action_name])
@@ -66,13 +29,24 @@ module VagrantPlugins
66
29
  @logger.debug("Looking for triggers #{@condition} action #{current_action}.")
67
30
  triggers_to_fire = @env[:machine].config.trigger.triggers.find_all { |t| t[:action] == current_action && t[:condition] == @condition }
68
31
  unless triggers_to_fire.empty?
69
- @env[:ui].info "Running triggers #{@condition} action..."
32
+ # Emit a warning message if the old syntax is found
33
+ if @env[:machine].config.trigger.deprecation_warning
34
+ @env[:ui].warn I18n.t("vagrant_triggers.action.trigger.deprecated_syntax")
35
+ end
36
+ @env[:ui].info I18n.t("vagrant_triggers.action.trigger.running_triggers", :condition => @condition)
70
37
  triggers_to_fire.each do |trigger|
38
+ # Ugly block, will change in v0.4
71
39
  @options = trigger[:options]
72
- if @options[:execute]
73
- execute(@options[:execute])
74
- elsif @options[:info]
75
- @env[:ui].info @options[:info]
40
+ case
41
+ when trigger[:proc]
42
+ dsl = DSL.new(@env[:ui], @options)
43
+ dsl.instance_eval &trigger[:proc]
44
+ when @options[:execute]
45
+ dsl = DSL.new(@env[:ui], @options)
46
+ dsl.execute @options[:execute]
47
+ when @options[:info]
48
+ dsl = DSL.new(@env[:ui], @options)
49
+ dsl.info @options[:info]
76
50
  else
77
51
  @logger.debug("Trigger command not found.")
78
52
  end
@@ -1,19 +1,21 @@
1
1
  module VagrantPlugins
2
2
  module Triggers
3
3
  class Config < Vagrant.plugin("2", :config)
4
+ attr_reader :deprecation_warning
4
5
  attr_reader :triggers
5
-
6
+
6
7
  def initialize
7
- @triggers = []
8
+ @deprecation_warning = false
9
+ @triggers = []
8
10
  end
9
11
 
10
- def after(actions, options = {})
11
- add_trigger(actions, :after, options)
12
+ def after(actions, options = {}, &block)
13
+ add_trigger(actions, :after, options, block)
12
14
  end
13
15
 
14
- def before(actions, options = {})
15
- add_trigger(actions, :before, options)
16
- end
16
+ def before(actions, options = {}, &block)
17
+ add_trigger(actions, :before, options, block)
18
+ end
17
19
 
18
20
  def validate(machine)
19
21
  errors = []
@@ -27,9 +29,10 @@ module VagrantPlugins
27
29
 
28
30
  private
29
31
 
30
- def add_trigger(actions, condition, options)
32
+ def add_trigger(actions, condition, options, proc)
31
33
  Array(actions).each do |action|
32
- @triggers << { :action => action, :condition => condition, :options => options }
34
+ @triggers << { :action => action, :condition => condition, :options => options, :proc => proc }
35
+ @deprecation_warning = true if options[:execute] || options[:info]
33
36
  end
34
37
  end
35
38
  end
@@ -0,0 +1,65 @@
1
+ require "log4r"
2
+ require "shellwords"
3
+ require "vagrant/util/subprocess"
4
+
5
+ module VagrantPlugins
6
+ module Triggers
7
+ class DSL
8
+ def initialize(ui, options = {})
9
+ @logger = Log4r::Logger.new("vagrant::plugins::triggers::dsl")
10
+ @options = options
11
+ @ui = ui
12
+ end
13
+
14
+ def run(raw_command, options = {})
15
+ @ui.info I18n.t("vagrant_triggers.action.trigger.executing_command", :command => raw_command)
16
+ command = Shellwords.shellsplit(raw_command)
17
+ env_backup = ENV.to_hash
18
+ begin
19
+ build_environment
20
+ result = Vagrant::Util::Subprocess.execute(command[0], *command[1..-1])
21
+ rescue Vagrant::Errors::CommandUnavailable, Vagrant::Errors::CommandUnavailableWindows
22
+ raise Errors::CommandUnavailable, :command => command[0]
23
+ ensure
24
+ ENV.replace(env_backup)
25
+ end
26
+ if result.exit_code != 0 && !@options[:force]
27
+ raise Errors::CommandFailed, :command => raw_command, :stderr => result.stderr
28
+ end
29
+ if @options[:stdout]
30
+ @ui.info I18n.t("vagrant_triggers.action.trigger.command_output", :output => result.stdout)
31
+ end
32
+ end
33
+ alias_method :execute, :run
34
+
35
+ def method_missing(method, *args, &block)
36
+ # If the @ui object responds to the given method, call it
37
+ if @ui.respond_to?(method)
38
+ @ui.send(method, *args, *block)
39
+ else
40
+ super(method, *args, &block)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def build_environment
47
+ @logger.debug("Original environment: #{ENV.inspect}")
48
+
49
+ # Remove GEM_ environment variables
50
+ ["GEM_HOME", "GEM_PATH", "GEMRC"].each { |gem_var| ENV.delete(gem_var) }
51
+
52
+ # Create the new PATH removing Vagrant bin directory
53
+ # and appending directories specified through the
54
+ # :append_to_path option
55
+ new_path = ENV["VAGRANT_INSTALLER_ENV"] ? ENV["PATH"].gsub(/#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}.*?#{File::PATH_SEPARATOR}/, "") : ENV["PATH"]
56
+ new_path += Array(@options[:append_to_path]).map { |dir| "#{File::PATH_SEPARATOR}#{dir}" }.join
57
+ ENV["PATH"] = new_path
58
+ @logger.debug("PATH modifed: #{ENV["PATH"]}")
59
+
60
+ # Add the VAGRANT_NO_TRIGGERS variable to avoid loops
61
+ ENV["VAGRANT_NO_TRIGGERS"] = "1"
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,7 +1,7 @@
1
1
  # This is a sanity check to make sure no one is attempting to install
2
2
  # this into an early Vagrant version.
3
3
  if Vagrant::VERSION < "1.2.0"
4
- raise "The Vagrant Triggers plugin is only compatible with Vagrant 1.1+"
4
+ raise "The Vagrant Triggers plugin is only compatible with Vagrant 1.2+"
5
5
  end
6
6
 
7
7
  module VagrantPlugins
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module Triggers
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
data/locales/en.yml CHANGED
@@ -1,5 +1,26 @@
1
1
  en:
2
2
  vagrant_triggers:
3
+ action:
4
+ trigger:
5
+ command_output: |-
6
+ Command output:
7
+ ---------------
8
+ %{output}
9
+ ---------------
10
+ deprecated_syntax: |-
11
+ --------------------
12
+ DEPRECATION WARNING:
13
+ --------------------
14
+ The `:execute` and `:info` trigger options are deprecated
15
+ and will be removed in vagrant-triggers 0.4. Please upgrade
16
+ your Vagrantfile according to the new trigger DSL.
17
+
18
+ More information on the project home page:
19
+ https://github.com/emyl/vagrant-triggers
20
+ executing_command: |-
21
+ Executing command "%{command}"...
22
+ running_triggers: |-
23
+ Running triggers %{condition} action...
3
24
  errors:
4
25
  command_failed: |-
5
26
  The command "%{command}" returned a failed exit code. The
data/spec/spec_helper.rb CHANGED
@@ -3,3 +3,5 @@ SimpleCov.start
3
3
 
4
4
  require "vagrant"
5
5
  require_relative "../lib/vagrant-triggers"
6
+
7
+ VagrantPlugins::Triggers::Plugin.init_i18n
@@ -1,4 +1,3 @@
1
- require "fileutils"
2
1
  require "spec_helper"
3
2
 
4
3
  describe VagrantPlugins::Triggers::Action::Trigger do
@@ -12,171 +11,62 @@ describe VagrantPlugins::Triggers::Action::Trigger do
12
11
  let(:ui) { double("ui", :info => info) }
13
12
  let(:info) { double("info") }
14
13
 
15
- let(:result) { double("result", :exit_code => 0, :stderr => stderr) }
16
- let(:stderr) { double("stderr") }
17
-
18
14
  before do
19
- @triggers = [ { :action => machine_action, :condition => condition, :options => { :execute => "foo" } } ]
15
+ trigger_block = Proc.new { nil }
16
+ @triggers = [ { :action => machine_action, :condition => condition, :options => { }, :proc => trigger_block } ]
17
+ machine.stub_chain(:config, :trigger, :deprecation_warning)
20
18
  machine.stub_chain(:config, :trigger, :triggers).and_return(@triggers)
21
19
  end
22
20
 
23
- context "with a regular command" do
24
- before do
25
- Vagrant::Util::Subprocess.stub(:execute => result)
26
- end
27
-
28
- it "should skip :environment_load and :environment_unload actions" do
29
- [:environment_load, :environment_unload].each do |action|
30
- env.stub(:[]).with(:action_name).and_return(action)
31
- env.should_not_receive(:[]).with(:machine_action)
32
- described_class.new(app, env, condition).call(env)
33
- end
34
- end
35
-
36
- it "shouldn't fire if machine action is not defined" do
37
- env.stub(:[]).with(:action_name)
38
- env.stub(:[]).with(:machine_action).and_return(nil)
39
- env.should_not_receive(:[]).with(:machine)
40
- described_class.new(app, env, condition).call(env)
41
- end
42
-
43
- it "should fire trigger when all conditions are satisfied" do
44
- Vagrant::Util::Subprocess.should_receive(:execute).with("foo")
45
- described_class.new(app, env, condition).call(env)
46
- end
47
-
48
- it "should fire all defined triggers" do
49
- @triggers << { :action => machine_action, :condition => condition, :options => { :execute => "bar" } }
50
- Vagrant::Util::Subprocess.should_receive(:execute).twice
51
- described_class.new(app, env, condition).call(env)
52
- end
53
-
54
- it "shouldn't execute trigger with no command" do
55
- @triggers[0][:options] = {}
56
- Vagrant::Util::Subprocess.should_not_receive(:execute)
57
- described_class.new(app, env, condition).call(env)
58
- end
59
-
60
- it "shouldn't fire trigger when condition doesn't match" do
61
- @triggers[0][:condition] = "blah"
62
- Vagrant::Util::Subprocess.should_not_receive(:execute)
63
- described_class.new(app, env, condition).call(env)
64
- end
65
-
66
- it "shouldn't fire trigger when action doesn't match" do
67
- @triggers[0][:action] = "blah"
68
- Vagrant::Util::Subprocess.should_not_receive(:execute)
69
- described_class.new(app, env, condition).call(env)
70
- end
71
-
72
- it "should raise an error if executed command exits with non-zero code" do
73
- result.stub(:exit_code => 1)
74
- expect { described_class.new(app, env, condition).call(env) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandFailed)
75
- end
76
-
77
- it "shouldn't raise an error if executed command exits with non-zero code but :force option was specified" do
78
- @triggers[0][:options][:force] = true
79
- result.stub(:exit_code => 1)
80
- expect { described_class.new(app, env, condition).call(env) }.not_to raise_error()
81
- end
82
-
83
- it "should display output if :stdout option was specified" do
84
- @triggers[0][:options][:stdout] = true
85
- result.stub(:stdout => "Some output")
86
- ui.should_receive(:info).with("Command output:\n\nSome output\n")
21
+ it "should skip :environment_load and :environment_unload actions" do
22
+ [:environment_load, :environment_unload].each do |action|
23
+ env.stub(:[]).with(:action_name).and_return(action)
24
+ VagrantPlugins::Triggers::DSL.should_not_receive(:new)
87
25
  described_class.new(app, env, condition).call(env)
88
26
  end
89
27
  end
90
28
 
91
- context "with a command not in the PATH" do
92
- before do
93
- @tmp_dir = Vagrant::Util::Platform.windows? ? ENV["USERPROFILE"] : ENV["HOME"]
94
- File.open("#{@tmp_dir}/foo", "w+", 0700) { |file| }
95
- File.stub(:executable? => false)
96
- File.stub(:executable?).with("#{@tmp_dir}/foo").and_return(true)
97
- end
98
-
99
- after do
100
- File.delete("#{@tmp_dir}/foo")
101
- end
102
-
103
- it "should raise a CommandUnavailable error by default" do
104
- expect { described_class.new(app, env, condition).call(env) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandUnavailable)
105
- end
106
-
107
- it "should raise a CommandUnavailable error on Windows" do
108
- Vagrant::Util::Platform.stub(:windows? => true)
109
- expect { described_class.new(app, env, condition).call(env) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandUnavailable)
110
- end
111
-
112
- it "should honor the :append_to_path option and restore original path after execution" do
113
- @triggers[0][:options][:append_to_path] = @tmp_dir
114
- original_path = ENV["PATH"]
115
- described_class.new(app, env, condition).call(env)
116
- expect(ENV["PATH"]).to eq(original_path)
117
- end
118
-
119
- it "should accept an array for the :append_to_path option" do
120
- @triggers[0][:options][:append_to_path] = [@tmp_dir, @tmp_dir]
121
- expect { described_class.new(app, env, condition).call(env) }.not_to raise_error()
122
- end
29
+ it "shouldn't fire if machine action is not defined" do
30
+ env.stub(:[]).with(:action_name)
31
+ env.stub(:[]).with(:machine_action).and_return(nil)
32
+ VagrantPlugins::Triggers::DSL.should_not_receive(:new)
33
+ described_class.new(app, env, condition).call(env)
123
34
  end
124
35
 
125
- context "within the Vagrant environment" do
126
- before do
127
- @original_path = ENV["PATH"]
128
- ENV["EMBEDDED_DIR"] = Vagrant::Util::Platform.windows? ? ENV["USERPROFILE"] : ENV["HOME"]
129
- ENV["GEM_HOME"] = "#{ENV["EMBEDDED_DIR"]}/gems"
130
- ENV["GEM_PATH"] = ENV["GEM_HOME"]
131
- ENV["GEMRC"] = "#{ENV["EMBEDDED_DIR"]}/etc/gemrc"
132
- ENV["PATH"] = "#{ENV["EMBEDDED_DIR"]}/bin:#{ENV["PATH"]}"
133
- end
134
-
135
- context "with a command which is present into the Vagrant embedded dir" do
136
- before do
137
- Dir.mkdir("#{ENV["EMBEDDED_DIR"]}/bin")
138
- File.open("#{ENV["EMBEDDED_DIR"]}/bin/foo", "w+", 0700) { |file| }
139
- end
140
-
141
- it "should raise a CommandUnavailable error" do
142
- expect { described_class.new(app, env, condition).call(env) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandUnavailable)
143
- end
36
+ it "should fire trigger when all conditions are satisfied" do
37
+ dsl = double("dsl")
38
+ VagrantPlugins::Triggers::DSL.stub(:new).with(ui, @triggers.first[:options]).and_return(dsl)
39
+ dsl.should_receive(:instance_eval).and_yield
40
+ described_class.new(app, env, condition).call(env)
41
+ end
144
42
 
145
- after do
146
- FileUtils.rm_rf("#{ENV["EMBEDDED_DIR"]}/bin")
147
- end
148
- end
43
+ it "should fire all defined triggers" do
44
+ @triggers << @triggers.first
45
+ VagrantPlugins::Triggers::DSL.should_receive(:new).twice
46
+ described_class.new(app, env, condition).call(env)
47
+ end
149
48
 
150
- it "should not pass GEM_HOME to the executed command" do
151
- Vagrant::Util::Subprocess.should_receive(:execute) do |command|
152
- expect(ENV).not_to have_key("GEM_HOME")
153
- result
154
- end
155
- described_class.new(app, env, condition).call(env)
156
- end
49
+ it "shouldn't execute trigger with no command or block" do
50
+ @triggers[0][:proc] = nil
51
+ VagrantPlugins::Triggers::DSL.should_not_receive(:new)
52
+ described_class.new(app, env, condition).call(env)
53
+ end
157
54
 
158
- it "should not pass GEM_PATH to the executed command" do
159
- Vagrant::Util::Subprocess.should_receive(:execute) do |command|
160
- expect(ENV).not_to have_key("GEM_PATH")
161
- result
162
- end
163
- described_class.new(app, env, condition).call(env)
164
- end
55
+ it "shouldn't fire trigger when condition doesn't match" do
56
+ @triggers[0][:condition] = "blah"
57
+ VagrantPlugins::Triggers::DSL.should_not_receive(:new)
58
+ described_class.new(app, env, condition).call(env)
59
+ end
165
60
 
166
- it "should not pass GEMRC to the executed command" do
167
- Vagrant::Util::Subprocess.should_receive(:execute) do |command|
168
- expect(ENV).not_to have_key("GEMRC")
169
- result
170
- end
171
- described_class.new(app, env, condition).call(env)
172
- end
61
+ it "shouldn't fire trigger when action doesn't match" do
62
+ @triggers[0][:action] = "blah"
63
+ VagrantPlugins::Triggers::DSL.should_not_receive(:new)
64
+ described_class.new(app, env, condition).call(env)
65
+ end
173
66
 
174
- after do
175
- ENV["EMBEDDED_DIR"] = nil
176
- ENV["GEM_HOME"] = nil
177
- ENV["GEM_PATH"] = nil
178
- ENV["GEMRC"] = nil
179
- ENV["PATH"] = @original_path
180
- end
67
+ it "should emit a warning message if the deprecation warning flag is set" do
68
+ machine.stub_chain(:config, :trigger, :deprecation_warning).and_return(true)
69
+ ui.should_receive(:warn).with(/DEPRECATION WARNING/)
70
+ described_class.new(app, env, condition).call(env)
181
71
  end
182
72
  end
@@ -16,25 +16,32 @@ describe VagrantPlugins::Triggers::Config do
16
16
 
17
17
  describe "add triggers" do
18
18
  it "should add before triggers" do
19
- config.before :up, :exec => "echo ls"
20
- expect(config.triggers.first).to eq({ :action => :up, :condition => :before, :options => { :exec => "echo ls" } })
19
+ config.before(:up) { run "ls" }
20
+ expect(config.triggers).to have(1).item
21
21
  end
22
22
 
23
23
  it "should add after triggers" do
24
- config.after :up, :exec => "echo ls"
25
- expect(config.triggers.first).to eq({ :action => :up, :condition => :after, :options => { :exec => "echo ls" } })
24
+ config.after(:up) { run "ls" }
25
+ expect(config.triggers).to have(1).item
26
+ end
27
+
28
+ [:execute, :info].each do |option|
29
+ it "should set the deprecation warning flag if the old #{option.to_s} option is passed" do
30
+ config.before(:up, option => true)
31
+ expect(config.deprecation_warning).to be_true
32
+ end
26
33
  end
27
34
  end
28
35
 
29
36
  describe "accept multiple entries" do
30
37
  it "should record multiple entries" do
31
- config.before :up, :exec => "echo ls"
32
- config.after :up, :exec => "echo ls"
38
+ config.before(:up) { run "ls" }
39
+ config.after(:up) { run "ls" }
33
40
  expect(config.triggers).to have(2).items
34
41
  end
35
42
 
36
43
  it "should record multiple entries if the action is an array" do
37
- config.before [:up, :halt], :exec => "echo ls"
44
+ config.before([:up, :halt]) { run "ls" }
38
45
  expect(config.triggers).to have(2).items
39
46
  end
40
47
  end
@@ -0,0 +1,133 @@
1
+ require "fileutils"
2
+ require "spec_helper"
3
+
4
+ describe VagrantPlugins::Triggers::DSL do
5
+ let(:result) { double("result", :exit_code => 0, :stderr => stderr) }
6
+ let(:stderr) { double("stderr") }
7
+
8
+ let(:ui) { double("ui", :info => info) }
9
+ let(:info) { double("info") }
10
+
11
+ before do
12
+ @command = "foo"
13
+ @dsl = described_class.new(ui, {})
14
+ end
15
+
16
+ context "method missing" do
17
+ it "acts as proxy if the ui object respond to the called method" do
18
+ ui.stub(:foo).and_return("bar")
19
+ expect(@dsl.foo).to eq("bar")
20
+ end
21
+ end
22
+
23
+ context "run a regular command" do
24
+ before do
25
+ Vagrant::Util::Subprocess.stub(:execute => result)
26
+ end
27
+
28
+ it "should raise an error if executed command exits with non-zero code" do
29
+ result.stub(:exit_code => 1)
30
+ expect { @dsl.run(@command) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandFailed)
31
+ end
32
+
33
+ it "shouldn't raise an error if executed command exits with non-zero code but :force option was specified" do
34
+ dsl = described_class.new(ui, :force => true)
35
+ result.stub(:exit_code => 1)
36
+ expect { dsl.run(@command) }.not_to raise_error()
37
+ end
38
+
39
+ it "should display output if :stdout option was specified" do
40
+ dsl = described_class.new(ui, :stdout => true)
41
+ result.stub(:stdout => "Some output")
42
+ ui.should_receive(:info).with(/Some output/)
43
+ dsl.run(@command)
44
+ end
45
+
46
+ it "should pass VAGRANT_NO_TRIGGERS environment variable to the command" do
47
+ Vagrant::Util::Subprocess.should_receive(:execute) do |command|
48
+ expect(ENV).to have_key("VAGRANT_NO_TRIGGERS")
49
+ result
50
+ end
51
+ @dsl.run(@command)
52
+ end
53
+ end
54
+
55
+ context "run a command not in the PATH" do
56
+ before do
57
+ @tmp_dir = Vagrant::Util::Platform.windows? ? ENV["USERPROFILE"] : ENV["HOME"]
58
+ File.open("#{@tmp_dir}/#{@command}", "w+", 0700) { |file| }
59
+ File.stub(:executable? => false)
60
+ File.stub(:executable?).with("#{@tmp_dir}/#{@command}").and_return(true)
61
+ end
62
+
63
+ after do
64
+ File.delete("#{@tmp_dir}/#{@command}")
65
+ end
66
+
67
+ it "should raise a CommandUnavailable error by default" do
68
+ expect { @dsl.run(@command) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandUnavailable)
69
+ end
70
+
71
+ it "should raise a CommandUnavailable error on Windows" do
72
+ Vagrant::Util::Platform.stub(:windows? => true)
73
+ expect { @dsl.run(@command) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandUnavailable)
74
+ end
75
+
76
+ it "should honor the :append_to_path option and restore original path after execution" do
77
+ dsl = described_class.new(ui, :append_to_path => @tmp_dir)
78
+ original_path = ENV["PATH"]
79
+ dsl.run(@command)
80
+ expect(ENV["PATH"]).to eq(original_path)
81
+ end
82
+
83
+ it "should accept an array for the :append_to_path option" do
84
+ dsl = described_class.new(ui, :append_to_path => [@tmp_dir, @tmp_dir])
85
+ expect { dsl.run(@command) }.not_to raise_error()
86
+ end
87
+ end
88
+
89
+ context "run a command simulating the Vagrant environment" do
90
+ before do
91
+ @original_path = ENV["PATH"]
92
+ ENV["VAGRANT_INSTALLER_ENV"] = "1"
93
+ ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"] = Vagrant::Util::Platform.windows? ? ENV["USERPROFILE"] : ENV["HOME"]
94
+ ENV["GEM_HOME"] = "#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}/gems"
95
+ ENV["GEM_PATH"] = ENV["GEM_HOME"]
96
+ ENV["GEMRC"] = "#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}/etc/gemrc"
97
+ ENV["PATH"] = "#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}/bin:#{ENV["PATH"]}"
98
+ end
99
+
100
+ context "with a command which is present into the Vagrant embedded dir" do
101
+ before do
102
+ Dir.mkdir("#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}/bin")
103
+ File.open("#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}/bin/#{@command}", "w+", 0700) { |file| }
104
+ end
105
+
106
+ it "should raise a CommandUnavailable error" do
107
+ expect { @dsl.run(@command) }.to raise_error(VagrantPlugins::Triggers::Errors::CommandUnavailable)
108
+ end
109
+
110
+ after do
111
+ FileUtils.rm_rf("#{ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]}/bin")
112
+ end
113
+ end
114
+
115
+ ["GEM_HOME", "GEM_PATH", "GEMRC"].each do |env_var|
116
+ it "should not pass #{env_var} to the executed command" do
117
+ Vagrant::Util::Subprocess.should_receive(:execute) do |command|
118
+ expect(ENV).not_to have_key(env_var)
119
+ result
120
+ end
121
+ @dsl.run(@command)
122
+ end
123
+ end
124
+
125
+ after do
126
+ ENV["EMBEDDED_DIR"] = nil
127
+ ENV["GEM_HOME"] = nil
128
+ ENV["GEM_PATH"] = nil
129
+ ENV["GEMRC"] = nil
130
+ ENV["PATH"] = @original_path
131
+ end
132
+ end
133
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-triggers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emiliano Ticci
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-01 00:00:00.000000000 Z
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -64,6 +64,7 @@ files:
64
64
  - lib/vagrant-triggers/action/trigger.rb
65
65
  - lib/vagrant-triggers/action.rb
66
66
  - lib/vagrant-triggers/config.rb
67
+ - lib/vagrant-triggers/dsl.rb
67
68
  - lib/vagrant-triggers/errors.rb
68
69
  - lib/vagrant-triggers/plugin.rb
69
70
  - lib/vagrant-triggers/version.rb
@@ -75,6 +76,7 @@ files:
75
76
  - spec/spec_helper.rb
76
77
  - spec/vagrant-triggers/action/trigger_spec.rb
77
78
  - spec/vagrant-triggers/config_spec.rb
79
+ - spec/vagrant-triggers/dsl_spec.rb
78
80
  - spec/vagrant-triggers/vagrant_spec.rb
79
81
  - vagrant-triggers.gemspec
80
82
  - .gitignore
@@ -106,4 +108,5 @@ test_files:
106
108
  - spec/spec_helper.rb
107
109
  - spec/vagrant-triggers/action/trigger_spec.rb
108
110
  - spec/vagrant-triggers/config_spec.rb
111
+ - spec/vagrant-triggers/dsl_spec.rb
109
112
  - spec/vagrant-triggers/vagrant_spec.rb