scmd 2.3.1 → 3.0.0
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.
- data/README.md +27 -0
- data/lib/scmd/command.rb +12 -6
- data/lib/scmd/version.rb +1 -1
- data/scmd.gemspec +2 -2
- data/test/system/command_tests.rb +22 -1
- data/test/unit/command_tests.rb +6 -2
- metadata +15 -15
data/README.md
CHANGED
@@ -94,6 +94,33 @@ Raise an exception if not successful with `run!`:
|
|
94
94
|
Scmd.new("cd /path/that/does/not/exist").run! #=> Scmd::Command::Failure
|
95
95
|
```
|
96
96
|
|
97
|
+
### Environment variables
|
98
|
+
|
99
|
+
Pass environment variables:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
cmd = Scmd.new("echo $TEST_VAR", {
|
103
|
+
:env => {
|
104
|
+
'TEST_VAR' => 'hi'
|
105
|
+
}
|
106
|
+
})
|
107
|
+
```
|
108
|
+
|
109
|
+
### Process spawn options
|
110
|
+
|
111
|
+
Pass options:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
reader, writer = IO.pipe
|
115
|
+
# this is an example that uses file descriptor redirection options
|
116
|
+
cmd = Scmd.new("echo test 1>&#{writer.fileno}", {
|
117
|
+
:options => { writer => writer }
|
118
|
+
})
|
119
|
+
reader.gets # => "test\n"
|
120
|
+
```
|
121
|
+
|
122
|
+
For all the possible options see [posix-spawn](https://github.com/rtomayko/posix-spawn#status).
|
123
|
+
|
97
124
|
## Installation
|
98
125
|
|
99
126
|
Add this line to your application's Gemfile:
|
data/lib/scmd/command.rb
CHANGED
@@ -14,12 +14,14 @@ module Scmd
|
|
14
14
|
READ_CHECK_TIMEOUT = 0.001 # seconds
|
15
15
|
DEFAULT_STOP_TIMEOUT = 3 # seconds
|
16
16
|
|
17
|
-
attr_reader :cmd_str, :env
|
17
|
+
attr_reader :cmd_str, :env, :options
|
18
18
|
attr_reader :pid, :exitstatus, :stdout, :stderr
|
19
19
|
|
20
|
-
def initialize(cmd_str,
|
20
|
+
def initialize(cmd_str, opts = nil)
|
21
|
+
opts ||= {}
|
21
22
|
@cmd_str = cmd_str
|
22
|
-
@env = stringify_hash(env || {})
|
23
|
+
@env = stringify_hash(opts[:env] || {})
|
24
|
+
@options = opts[:options] || {}
|
23
25
|
reset_attrs
|
24
26
|
end
|
25
27
|
|
@@ -131,7 +133,7 @@ module Scmd
|
|
131
133
|
reset_attrs
|
132
134
|
@stop_r, @stop_w = IO.pipe
|
133
135
|
@read_output_thread = nil
|
134
|
-
@child_process = ChildProcess.new(@cmd_str, @env)
|
136
|
+
@child_process = ChildProcess.new(@cmd_str, @env, @options)
|
135
137
|
end
|
136
138
|
|
137
139
|
def teardown_run
|
@@ -165,8 +167,12 @@ module Scmd
|
|
165
167
|
|
166
168
|
attr_reader :pid, :stdin, :stdout, :stderr
|
167
169
|
|
168
|
-
def initialize(cmd_str, env)
|
169
|
-
@pid, @stdin, @stdout, @stderr = *::POSIX::Spawn::popen4(
|
170
|
+
def initialize(cmd_str, env, options)
|
171
|
+
@pid, @stdin, @stdout, @stderr = *::POSIX::Spawn::popen4(
|
172
|
+
env,
|
173
|
+
cmd_str,
|
174
|
+
options
|
175
|
+
)
|
170
176
|
@wait_pid, @wait_status = nil, nil
|
171
177
|
end
|
172
178
|
|
data/lib/scmd/version.rb
CHANGED
data/scmd.gemspec
CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
|
-
gem.add_development_dependency("assert", ["~> 2.
|
22
|
-
gem.add_dependency("posix-spawn", ["
|
21
|
+
gem.add_development_dependency("assert", ["~> 2.15"])
|
22
|
+
gem.add_dependency("posix-spawn", ["~> 0.3.11"])
|
23
23
|
end
|
@@ -159,15 +159,36 @@ class Scmd::Command
|
|
159
159
|
desc "with environment variables"
|
160
160
|
setup do
|
161
161
|
@cmd = Scmd::Command.new("echo $SCMD_TEST_VAR", {
|
162
|
-
'SCMD_TEST_VAR' => 'test'
|
162
|
+
:env => { 'SCMD_TEST_VAR' => 'test' }
|
163
163
|
})
|
164
164
|
end
|
165
165
|
|
166
166
|
should "use them when running the command" do
|
167
167
|
@cmd.run
|
168
|
+
assert @cmd.success?
|
168
169
|
assert_equal "test\n", @cmd.stdout
|
169
170
|
end
|
170
171
|
|
171
172
|
end
|
172
173
|
|
174
|
+
class WithOptionsTests < SystemTests
|
175
|
+
desc "with options"
|
176
|
+
setup do
|
177
|
+
@path = "/"
|
178
|
+
# `chdir` is the only one that reliably worked
|
179
|
+
@cmd = Scmd::Command.new("pwd", {
|
180
|
+
:options => { :chdir => @path }
|
181
|
+
})
|
182
|
+
end
|
183
|
+
|
184
|
+
should "use them when running the command" do
|
185
|
+
@cmd.run
|
186
|
+
assert @cmd.success?
|
187
|
+
# if the option didn't work or was ignored it would use this process' dir
|
188
|
+
assert_not_equal "#{Dir.pwd}\n", @cmd.stdout
|
189
|
+
assert_equal "#{@path}\n", @cmd.stdout
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
193
|
+
|
173
194
|
end
|
data/test/unit/command_tests.rb
CHANGED
@@ -10,7 +10,7 @@ class Scmd::Command
|
|
10
10
|
end
|
11
11
|
subject { @cmd }
|
12
12
|
|
13
|
-
should have_readers :cmd_str, :env
|
13
|
+
should have_readers :cmd_str, :env, :options
|
14
14
|
should have_readers :pid, :exitstatus, :stdout, :stderr
|
15
15
|
should have_imeths :run, :run!
|
16
16
|
should have_imeths :start, :wait, :stop, :kill
|
@@ -27,12 +27,16 @@ class Scmd::Command
|
|
27
27
|
|
28
28
|
should "stringify its env hash" do
|
29
29
|
cmd = Scmd::Command.new("echo $SCMD_TEST_VAR", {
|
30
|
-
:SCMD_TEST_VAR => 1
|
30
|
+
:env => { :SCMD_TEST_VAR => 1 }
|
31
31
|
})
|
32
32
|
expected = { 'SCMD_TEST_VAR' => '1' }
|
33
33
|
assert_equal expected, cmd.env
|
34
34
|
end
|
35
35
|
|
36
|
+
should "default its options to an empty hash" do
|
37
|
+
assert_equal({}, subject.options)
|
38
|
+
end
|
39
|
+
|
36
40
|
should "default its result values" do
|
37
41
|
assert_nil subject.pid
|
38
42
|
assert_nil subject.exitstatus
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scmd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
- 2
|
8
7
|
- 3
|
9
|
-
-
|
10
|
-
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 3.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
19
|
+
date: 2015-10-12 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
requirement: &id001 !ruby/object:Gem::Requirement
|
@@ -24,30 +24,30 @@ dependencies:
|
|
24
24
|
requirements:
|
25
25
|
- - ~>
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
hash:
|
27
|
+
hash: 29
|
28
28
|
segments:
|
29
29
|
- 2
|
30
|
-
-
|
31
|
-
version: "2.
|
32
|
-
version_requirements: *id001
|
30
|
+
- 15
|
31
|
+
version: "2.15"
|
33
32
|
type: :development
|
34
33
|
name: assert
|
34
|
+
version_requirements: *id001
|
35
35
|
prerelease: false
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
38
|
none: false
|
39
39
|
requirements:
|
40
|
-
- -
|
40
|
+
- - ~>
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
hash:
|
42
|
+
hash: 5
|
43
43
|
segments:
|
44
44
|
- 0
|
45
45
|
- 3
|
46
|
-
-
|
47
|
-
version: 0.3.
|
48
|
-
version_requirements: *id002
|
46
|
+
- 11
|
47
|
+
version: 0.3.11
|
49
48
|
type: :runtime
|
50
49
|
name: posix-spawn
|
50
|
+
version_requirements: *id002
|
51
51
|
prerelease: false
|
52
52
|
description: Build and run system commands.
|
53
53
|
email:
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
requirements: []
|
110
110
|
|
111
111
|
rubyforge_project:
|
112
|
-
rubygems_version: 1.8.
|
112
|
+
rubygems_version: 1.8.25
|
113
113
|
signing_key:
|
114
114
|
specification_version: 3
|
115
115
|
summary: Build and run system commands.
|