fakecmd 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +108 -3
- data/lib/fakecmd.rb +25 -25
- data/lib/fakecmd/version.rb +3 -3
- metadata +23 -29
data/README.md
CHANGED
@@ -1,7 +1,112 @@
|
|
1
1
|
fakecmd
|
2
2
|
=======
|
3
3
|
|
4
|
-
|
5
|
-
[
|
4
|
+
* [Homepage](http://github.com/blom/fakecmd)
|
5
|
+
* [Documentation](http://rdoc.info/github/blom/fakecmd)
|
6
6
|
|
7
|
-
|
7
|
+
Fakes system commands. Intended for use in tests. If your code relies heavily
|
8
|
+
on system commands and verifies their exit status, you may not want to rely on
|
9
|
+
the OS to be able to run your tests. That's the basic idea.
|
10
|
+
|
11
|
+
Originally inspired by [FakeFS](http://github.com/defunkt/fakefs).
|
12
|
+
|
13
|
+
Installation
|
14
|
+
------------
|
15
|
+
|
16
|
+
gem install fakecmd
|
17
|
+
|
18
|
+
Usage
|
19
|
+
-----
|
20
|
+
|
21
|
+
### Require
|
22
|
+
|
23
|
+
require "fakecmd"
|
24
|
+
|
25
|
+
### Add some commands
|
26
|
+
|
27
|
+
FakeCmd.add "bar", 0, "pork"
|
28
|
+
FakeCmd.add :foo, 1, "chop"
|
29
|
+
FakeCmd.add /hat/, 2, "good"
|
30
|
+
|
31
|
+
*Command*, *exit status*, and *output*.
|
32
|
+
|
33
|
+
#### Everything is a regular expression internally
|
34
|
+
|
35
|
+
"hi"
|
36
|
+
:hi
|
37
|
+
/hi/
|
38
|
+
|
39
|
+
All equal.
|
40
|
+
|
41
|
+
### Enable
|
42
|
+
|
43
|
+
Faking only happens between `on!` and `off!`, or in a block.
|
44
|
+
|
45
|
+
#### `on!` and `off!`
|
46
|
+
|
47
|
+
FakeCmd.on!
|
48
|
+
# ...
|
49
|
+
FakeCmd.off!
|
50
|
+
|
51
|
+
#### Block
|
52
|
+
|
53
|
+
FakeCmd do
|
54
|
+
# ...
|
55
|
+
end
|
56
|
+
|
57
|
+
### Run
|
58
|
+
|
59
|
+
#### Using the examples above
|
60
|
+
|
61
|
+
`bar mitzva` # => "pork"
|
62
|
+
$?.exitstatus # => 0
|
63
|
+
|
64
|
+
%x(fool) # => "chop"
|
65
|
+
$?.exitstatus # => 1
|
66
|
+
|
67
|
+
%x(nice hat) # => "good"
|
68
|
+
$?.exitstatus # => 2
|
69
|
+
|
70
|
+
#### No match
|
71
|
+
|
72
|
+
`nope` # => false
|
73
|
+
$?.exitstatus # => 127
|
74
|
+
|
75
|
+
### Which calls are faked
|
76
|
+
|
77
|
+
``
|
78
|
+
%x
|
79
|
+
|
80
|
+
For now.
|
81
|
+
|
82
|
+
### Clear the collection
|
83
|
+
|
84
|
+
FakeCmd.clear!
|
85
|
+
|
86
|
+
A simple example
|
87
|
+
----------------
|
88
|
+
|
89
|
+
module Users
|
90
|
+
def self.count
|
91
|
+
c = %x(users).split.size
|
92
|
+
c if $?.exitstatus == 0
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class UsersTest < Test::Unit::TestCase
|
97
|
+
def setup
|
98
|
+
FakeCmd.clear!
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_count_success
|
102
|
+
FakeCmd.add :users, 0, "a b c"
|
103
|
+
assert_equal 3, FakeCmd { Users.count }
|
104
|
+
assert_equal 0, $?.exitstatus
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_count_failure
|
108
|
+
FakeCmd.add :users, 1
|
109
|
+
assert_nil FakeCmd { Users.count }
|
110
|
+
assert_equal 1, $?.exitstatus
|
111
|
+
end
|
112
|
+
end
|
data/lib/fakecmd.rb
CHANGED
@@ -5,16 +5,21 @@ module FakeCmd
|
|
5
5
|
module_function
|
6
6
|
@@enabled = false
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
8
|
+
def add(cmd, status = 0, output = "", &block)
|
9
|
+
cmd = cmd.to_s if cmd.is_a?(Symbol)
|
10
|
+
commands << {
|
11
|
+
:regexp => Regexp.new(cmd),
|
12
|
+
:output => block ? block.call : output,
|
13
|
+
:status => status
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear!
|
18
|
+
commands.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
def commands
|
22
|
+
@_commands ||= Set.new
|
18
23
|
end
|
19
24
|
|
20
25
|
def off!
|
@@ -27,12 +32,16 @@ module FakeCmd
|
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
35
|
+
def on!
|
36
|
+
unless @@enabled
|
37
|
+
Kernel.class_eval do
|
38
|
+
alias_method :fakecmd_backquote, :`
|
39
|
+
def `(cmd)
|
40
|
+
FakeCmd.process_command(cmd)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
@@enabled = true
|
44
|
+
end
|
36
45
|
end
|
37
46
|
|
38
47
|
def process_command(cmd)
|
@@ -44,15 +53,6 @@ module FakeCmd
|
|
44
53
|
end
|
45
54
|
system ""
|
46
55
|
end
|
47
|
-
|
48
|
-
def add(cmd, status = 0, output = "", &block)
|
49
|
-
cmd = cmd.to_s if cmd.is_a?(Symbol)
|
50
|
-
commands << {
|
51
|
-
:regexp => Regexp.new(cmd),
|
52
|
-
:output => block ? block.call : output,
|
53
|
-
:status => status
|
54
|
-
}
|
55
|
-
end
|
56
56
|
end
|
57
57
|
|
58
58
|
def FakeCmd
|
data/lib/fakecmd/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakecmd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 29
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- "\xC3\x98rjan Blom"
|
@@ -15,79 +14,74 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-11-06 00:00:00 +01:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
21
|
+
name: bluecloth
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
23
|
none: false
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
hash: 3
|
28
27
|
segments:
|
29
28
|
- 0
|
30
29
|
version: "0"
|
31
|
-
requirement: *id001
|
32
30
|
type: :development
|
33
|
-
name: bluecloth
|
34
31
|
prerelease: false
|
32
|
+
version_requirements: *id001
|
35
33
|
- !ruby/object:Gem::Dependency
|
36
|
-
|
34
|
+
name: mg
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
36
|
none: false
|
38
37
|
requirements:
|
39
38
|
- - ">="
|
40
39
|
- !ruby/object:Gem::Version
|
41
|
-
hash: 3
|
42
40
|
segments:
|
43
41
|
- 0
|
44
42
|
version: "0"
|
45
|
-
requirement: *id002
|
46
43
|
type: :development
|
47
|
-
name: mg
|
48
44
|
prerelease: false
|
45
|
+
version_requirements: *id002
|
49
46
|
- !ruby/object:Gem::Dependency
|
50
|
-
|
47
|
+
name: rcov
|
48
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
49
|
none: false
|
52
50
|
requirements:
|
53
51
|
- - ">="
|
54
52
|
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
53
|
segments:
|
57
54
|
- 0
|
58
55
|
version: "0"
|
59
|
-
requirement: *id003
|
60
56
|
type: :development
|
61
|
-
name: rcov
|
62
57
|
prerelease: false
|
58
|
+
version_requirements: *id003
|
63
59
|
- !ruby/object:Gem::Dependency
|
64
|
-
|
60
|
+
name: rspec
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
62
|
none: false
|
66
63
|
requirements:
|
67
|
-
- -
|
64
|
+
- - ~>
|
68
65
|
- !ruby/object:Gem::Version
|
69
|
-
hash: 3
|
70
66
|
segments:
|
71
|
-
-
|
72
|
-
version: "
|
73
|
-
requirement: *id004
|
67
|
+
- 2
|
68
|
+
version: "2"
|
74
69
|
type: :development
|
75
|
-
name: rspec
|
76
70
|
prerelease: false
|
71
|
+
version_requirements: *id004
|
77
72
|
- !ruby/object:Gem::Dependency
|
78
|
-
|
73
|
+
name: yard
|
74
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
79
75
|
none: false
|
80
76
|
requirements:
|
81
77
|
- - ">="
|
82
78
|
- !ruby/object:Gem::Version
|
83
|
-
hash: 3
|
84
79
|
segments:
|
85
80
|
- 0
|
86
81
|
version: "0"
|
87
|
-
requirement: *id005
|
88
82
|
type: :development
|
89
|
-
name: yard
|
90
83
|
prerelease: false
|
84
|
+
version_requirements: *id005
|
91
85
|
description: Fakes system commands. Intended for use in tests.
|
92
86
|
email: blom@blom.tv
|
93
87
|
executables: []
|
@@ -115,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
109
|
requirements:
|
116
110
|
- - ">="
|
117
111
|
- !ruby/object:Gem::Version
|
118
|
-
hash:
|
112
|
+
hash: 2149922095191616200
|
119
113
|
segments:
|
120
114
|
- 0
|
121
115
|
version: "0"
|
@@ -124,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
118
|
requirements:
|
125
119
|
- - ">="
|
126
120
|
- !ruby/object:Gem::Version
|
127
|
-
hash:
|
121
|
+
hash: 2149922095191616200
|
128
122
|
segments:
|
129
123
|
- 0
|
130
124
|
version: "0"
|