guard-process 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -4,6 +4,7 @@ notifications:
4
4
  - andre@socialreferral.com
5
5
  rvm:
6
6
  - 1.9.2
7
+ - 1.9.3
7
8
  - jruby
8
9
  script: "bundle exec rake test"
9
10
  env: "JRUBY_OPTS='--1.9' "
data/README.md CHANGED
@@ -2,7 +2,7 @@ Guard::Process [![Build Status](https://secure.travis-ci.org/socialreferral/guar
2
2
  ==============
3
3
  Guard to run continues processes.
4
4
 
5
- This gem requires Ruby 1.9.2 or JRuby in 1.9 mode.
5
+ This gem requires Ruby 1.9.2, 1.9.3 or JRuby in 1.9 mode.
6
6
 
7
7
  Usage
8
8
  -----
@@ -10,6 +10,8 @@ Please read the [Guard documentation](https://github.com/guard/guard#readme) to
10
10
 
11
11
  There is also an exellent screencast available on [Railscasts](http://railscasts.com/episodes/264-guard)
12
12
 
13
+ Additionally there is a great introductory article on [Intridea Blog](http://intridea.com/2011/8/25/hire-a-guard-for-your-project) mentioning Guard::Process.
14
+
13
15
  Guardfile
14
16
  ---------
15
17
  You can add as many process Guards as you want, an example Guardfile:
@@ -4,7 +4,7 @@ require 'rbconfig' unless defined?(RbConfig)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "guard-process"
7
- s.version = "1.0.1"
7
+ s.version = "1.0.2"
8
8
  s.authors = ["Mark Kremer"]
9
9
  s.email = ["mark@socialreferral.com"]
10
10
  s.homepage = ""
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency('guard', '>= 0.4.2')
22
+ s.add_dependency('spoon', '~> 0.0.1')
23
+ s.add_dependency('ffi', '~> 1.0.9')
22
24
  s.add_development_dependency('minitest')
23
25
  s.add_development_dependency('mocha')
24
26
  s.add_development_dependency('guard-minitest')
data/lib/guard/process.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  require 'guard'
2
2
  require 'guard/guard'
3
+ require 'spoon'
3
4
 
4
5
  module Guard
5
6
  class Process < Guard
6
7
  def initialize(watchers = [], options = {})
7
- @process = nil
8
8
  @pid = nil
9
- @command = options.fetch(:command).split
10
- @env = options[:env]
9
+ @command = options.fetch(:command).split(" ")
10
+ @env = options[:env] || {}
11
11
  @name = options[:name]
12
12
  @stop_signal = options[:stop_signal] || "TERM"
13
13
  super
@@ -23,18 +23,23 @@ module Guard
23
23
 
24
24
  def start
25
25
  UI.info("Starting process #{@name}")
26
- @command.unshift(@env) if @env
27
- @process = IO.popen(@command)
26
+ original_env = {}
27
+ @env.each_pair do |key, value|
28
+ original_env[key] = ENV[key]
29
+ ENV[key] = value
30
+ end
31
+ @pid = Spoon.spawnp(*@command)
32
+ original_env.each_pair do |key, value|
33
+ ENV[key] = value
34
+ end
28
35
  UI.info("Started process #{@name}")
29
- @pid = @process.pid
30
36
  end
31
37
 
32
38
  def stop
33
- if @process
39
+ if @pid
34
40
  UI.info("Stopping process #{@name}")
35
- ::Process.kill(@stop_signal, @process.pid)
41
+ ::Process.kill(@stop_signal, @pid)
36
42
  ::Process.waitpid(@pid) rescue Errno::ESRCH
37
- @process.close
38
43
  @pid = nil
39
44
  UI.info("Stopped process #{@name}")
40
45
  end
@@ -1,9 +1,10 @@
1
1
  require_relative '../test_helper'
2
+ require 'timeout'
2
3
 
3
4
  class GuardProcessTest < MiniTest::Unit::TestCase
4
5
  def setup
5
6
  ENV['GUARD_ENV'] = 'test'
6
- @command = "#{File.expand_path(File.dirname(__FILE__) + '/../run_me.rb')}"
7
+ @command = "ruby #{TEST_ROOT}/run_me.rb"
7
8
  @name = "RunMe"
8
9
  @options = {:command => @command, :name => @name}
9
10
  @guard = Guard::Process.new([], @options)
@@ -23,13 +24,6 @@ class GuardProcessTest < MiniTest::Unit::TestCase
23
24
  @guard.run_on_change("")
24
25
  end
25
26
 
26
- def test_env_is_passed_to_io_popen_if_given
27
- @options[:env] = {'VAR1' => 'VALUE 1', 'VAR2' => 'VALUE 2'}
28
- IO.expects(:popen).with([@options[:env], @command]).returns(stub_everything)
29
- @guard = Guard::Process.new([], @options)
30
- @guard.start
31
- end
32
-
33
27
  def test_start_runs_command_and_stop_stops_it
34
28
  Guard::UI.expects(:info).with("Starting process #{@name}")
35
29
  Guard::UI.expects(:info).with("Started process #{@name}")
@@ -48,17 +42,43 @@ class GuardProcessTest < MiniTest::Unit::TestCase
48
42
  assert @guard.process_running?
49
43
  end
50
44
 
51
- def test_commands_are_formatted_correctly_with_and_without_env
52
- @options = {:command => 'echo test test', :name => 'EchoProcess'}
53
- @env = {'VAR3' => 'VALUE 3'}
45
+ def test_start_sets_env_properly
46
+ ENV['VAR1'] = 'VALUE A'
47
+ @options[:env] = {'VAR1' => 'VALUE 1', 'VAR3' => 'VALUE 2'}
54
48
 
55
- IO.expects(:popen).with(["echo", "test", "test"]).returns(stub_everything)
56
- IO.expects(:popen).with([@env, "echo", "test", "test"]).returns(stub_everything)
49
+ environment_file = "#{TEST_ROOT}/test_environment.txt"
50
+ File.delete(environment_file) if File.exists?(environment_file)
57
51
 
58
52
  @guard = Guard::Process.new([], @options)
59
- @guard.start and @guard.stop
53
+ @guard.start
54
+
55
+ # Check the environment
56
+ assert_equal ENV['VAR1'], 'VALUE A'
57
+ assert_nil ENV['VAR2']
58
+ assert_nil ENV['VAR3']
59
+
60
+ # Wait for run_me.rb to write the environment details to file
61
+ Timeout.timeout(30) do
62
+ until File.exist?("#{TEST_ROOT}/test_environment.txt") do
63
+ sleep 0.5
64
+ end
65
+ end
66
+
67
+ # Read the written environment and convert it to a Hash, and get rid of the file
68
+ written_env = Hash[File.read("#{TEST_ROOT}/test_environment.txt").split("\n").map {|l| l.split(" = ")}]
69
+ File.delete("#{TEST_ROOT}/test_environment.txt")
70
+
71
+ # Verify that the written environment matches what was set in options[:env]
72
+ assert_equal 'VALUE 1', written_env['VAR1']
73
+ assert_equal nil, written_env['VAR2']
74
+ assert_equal 'VALUE 2', written_env['VAR3']
75
+ end
76
+
77
+ def test_commands_are_formatted_properly_for_spoon
78
+ @options = {:command => 'echo test test', :name => 'EchoProcess', :env => {"VAR1" => "VALUE 1"}}
79
+ ::Process.stubs(:kill).returns(true)
60
80
 
61
- @options[:env] = @env
81
+ Spoon.expects(:spawnp).with("echo", "test", "test").returns(stub_everything)
62
82
 
63
83
  @guard = Guard::Process.new([], @options)
64
84
  @guard.start and @guard.stop
data/test/run_me.rb CHANGED
@@ -1,5 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
+
3
+ file_name = "#{File.expand_path(File.dirname(__FILE__))}/test_environment.txt"
4
+
5
+ File.open(file_name, 'w') do |file|
6
+ file.write("VAR1 = #{ENV['VAR1']}\n")
7
+ file.write("VAR2 = #{ENV['VAR2']}\n")
8
+ file.write("VAR3 = #{ENV['VAR3']}\n")
9
+ end
10
+
2
11
  loop do
3
- puts "#{Time.now}: running..."
4
12
  sleep 1
5
13
  end
14
+
data/test/test_helper.rb CHANGED
@@ -2,3 +2,5 @@ require 'minitest/unit'
2
2
  require 'minitest/autorun'
3
3
  require 'mocha'
4
4
  require 'guard/process'
5
+
6
+ TEST_ROOT = File.expand_path(File.dirname(__FILE__))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-process
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-08 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2011-10-13 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: guard
17
- requirement: &11464980 !ruby/object:Gem::Requirement
16
+ requirement: &8618500 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,32 @@ dependencies:
22
21
  version: 0.4.2
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *11464980
24
+ version_requirements: *8618500
25
+ - !ruby/object:Gem::Dependency
26
+ name: spoon
27
+ requirement: &8617860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *8617860
36
+ - !ruby/object:Gem::Dependency
37
+ name: ffi
38
+ requirement: &8617120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.9
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *8617120
26
47
  - !ruby/object:Gem::Dependency
27
48
  name: minitest
28
- requirement: &11464440 !ruby/object:Gem::Requirement
49
+ requirement: &8616540 !ruby/object:Gem::Requirement
29
50
  none: false
30
51
  requirements:
31
52
  - - ! '>='
@@ -33,10 +54,10 @@ dependencies:
33
54
  version: '0'
34
55
  type: :development
35
56
  prerelease: false
36
- version_requirements: *11464440
57
+ version_requirements: *8616540
37
58
  - !ruby/object:Gem::Dependency
38
59
  name: mocha
39
- requirement: &11463840 !ruby/object:Gem::Requirement
60
+ requirement: &8615820 !ruby/object:Gem::Requirement
40
61
  none: false
41
62
  requirements:
42
63
  - - ! '>='
@@ -44,10 +65,10 @@ dependencies:
44
65
  version: '0'
45
66
  type: :development
46
67
  prerelease: false
47
- version_requirements: *11463840
68
+ version_requirements: *8615820
48
69
  - !ruby/object:Gem::Dependency
49
70
  name: guard-minitest
50
- requirement: &11463300 !ruby/object:Gem::Requirement
71
+ requirement: &8615140 !ruby/object:Gem::Requirement
51
72
  none: false
52
73
  requirements:
53
74
  - - ! '>='
@@ -55,10 +76,10 @@ dependencies:
55
76
  version: '0'
56
77
  type: :development
57
78
  prerelease: false
58
- version_requirements: *11463300
79
+ version_requirements: *8615140
59
80
  - !ruby/object:Gem::Dependency
60
81
  name: guard-bundler
61
- requirement: &11432120 !ruby/object:Gem::Requirement
82
+ requirement: &8614520 !ruby/object:Gem::Requirement
62
83
  none: false
63
84
  requirements:
64
85
  - - ! '>='
@@ -66,10 +87,10 @@ dependencies:
66
87
  version: '0'
67
88
  type: :development
68
89
  prerelease: false
69
- version_requirements: *11432120
90
+ version_requirements: *8614520
70
91
  - !ruby/object:Gem::Dependency
71
92
  name: rake
72
- requirement: &11431540 !ruby/object:Gem::Requirement
93
+ requirement: &8613900 !ruby/object:Gem::Requirement
73
94
  none: false
74
95
  requirements:
75
96
  - - ! '>='
@@ -77,10 +98,10 @@ dependencies:
77
98
  version: '0'
78
99
  type: :development
79
100
  prerelease: false
80
- version_requirements: *11431540
101
+ version_requirements: *8613900
81
102
  - !ruby/object:Gem::Dependency
82
103
  name: rb-inotify
83
- requirement: &11430540 !ruby/object:Gem::Requirement
104
+ requirement: &8613120 !ruby/object:Gem::Requirement
84
105
  none: false
85
106
  requirements:
86
107
  - - ! '>='
@@ -88,7 +109,7 @@ dependencies:
88
109
  version: '0'
89
110
  type: :development
90
111
  prerelease: false
91
- version_requirements: *11430540
112
+ version_requirements: *8613120
92
113
  description: Guard extension to run cli processes
93
114
  email:
94
115
  - mark@socialreferral.com
@@ -109,7 +130,6 @@ files:
109
130
  - test/guard/process_test.rb
110
131
  - test/run_me.rb
111
132
  - test/test_helper.rb
112
- has_rdoc: true
113
133
  homepage: ''
114
134
  licenses: []
115
135
  post_install_message:
@@ -130,10 +150,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
150
  version: '0'
131
151
  segments:
132
152
  - 0
133
- hash: -228258594788711827
153
+ hash: 3990963759899305213
134
154
  requirements: []
135
155
  rubyforge_project:
136
- rubygems_version: 1.6.2
156
+ rubygems_version: 1.8.10
137
157
  signing_key:
138
158
  specification_version: 3
139
159
  summary: Guard extension to run cli processes