bkoski-better_backticks 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 5
@@ -0,0 +1,2 @@
1
+ require 'kernel'
2
+ require 'open4'
@@ -0,0 +1,30 @@
1
+ module Kernel
2
+
3
+ # Override raises an error if _cmd_ returns a non-zero exit status.
4
+ # Returns stdout + stderr if _cmd_ succeeds. Note that these are simply concatenated; STDERR is not inline.
5
+ def ` cmd
6
+ stdout, stderr = ''
7
+
8
+ begin
9
+ status = Open4::popen4(cmd) do |pid, stdin_stream, stdout_stream, stderr_stream|
10
+ stdout = stdout_stream.read
11
+ stderr = stderr_stream.read
12
+ end
13
+ raise stderr.strip if !status.success?
14
+ rescue Exception => e
15
+ raise "'#{cmd}' failed with: '#{e.message}'"
16
+ end
17
+
18
+ return stdout + stderr
19
+ end
20
+
21
+ alias_method :system!, :system
22
+
23
+ # Override raises an error if _cmd_ returns non-zero exit status.
24
+ # Returns true if _cmd_ succeeds. system!() preserves standard subshell handling (no exceptions; returns false on fail)
25
+ def system cmd
26
+ `#{cmd}`
27
+ return true
28
+ end
29
+
30
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'better_backticks'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,69 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestKernel < Test::Unit::TestCase
4
+
5
+ context "backticks" do
6
+ should "raise a runtime error with contents of stderr if command does not succeed" do
7
+ exception = assert_raises(RuntimeError) do
8
+ `echo "error text on stderr" >&2; exit -1;`
9
+ end
10
+
11
+ assert_match "failed with: 'error text on stderr'", exception.message
12
+ end
13
+
14
+ should "raise runtime error with contents of stderr if command is not found" do
15
+ exception = assert_raises(RuntimeError) do
16
+ `bad-cmd`
17
+ end
18
+
19
+ assert_match "failed with: 'No such file or directory - bad-cmd'", exception.message
20
+ end
21
+
22
+ should "return the contents of stdout + stderr if command succeeds" do
23
+ assert_match "std out\nstd err output\n", `echo "std out"; echo "std err output" >&2; exit 0;`
24
+ end
25
+ end
26
+
27
+ context "%x{} syntax" do
28
+ should "raise a runtime error with contents of stderr if command does not succeed" do
29
+ exception = assert_raises(RuntimeError) do
30
+ %x{echo "error text on stderr" >&2; exit -1;}
31
+ end
32
+
33
+ assert_match "failed with: 'error text on stderr'", exception.message
34
+ end
35
+
36
+ should "return the contents of stdout + stderr if command succeeds" do
37
+ assert_match "std out\nstd err output\n", %x{echo "std out"; echo "std err output" >&2; exit 0;}
38
+ end
39
+ end
40
+
41
+ context "system" do
42
+ should "raise a runtime error with contents of stderr if command does not succeed" do
43
+ exception = assert_raises(RuntimeError) do
44
+ system %{echo "error text on stderr" >&2; exit -1;}
45
+ end
46
+
47
+ assert_match "failed with: 'error text on stderr'", exception.message
48
+ end
49
+
50
+ should "return true if command succeeds" do
51
+ assert_kind_of TrueClass, system("echo 'hi!';")
52
+ end
53
+ end
54
+
55
+ context "system!" do
56
+ should "not raise an exception, even when command fails" do
57
+ assert_nothing_raised {system!("exit -1;")}
58
+ end
59
+
60
+ should "return false when command fails" do
61
+ assert_kind_of FalseClass, system!("exit -1;")
62
+ end
63
+
64
+ should "return true if command succeeds" do
65
+ assert_kind_of TrueClass, system!("echo 'hi!';")
66
+ end
67
+ end
68
+
69
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bkoski-better_backticks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Koski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-28 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: gems@benkoski.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/better_backticks.rb
27
+ - lib/kernel.rb
28
+ - test/test_helper.rb
29
+ - test/test_kernel.rb
30
+ has_rdoc: true
31
+ homepage: http://benkoski.com/better_backticks
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --inline-source
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: TODO
57
+ test_files: []
58
+