rspec-puppet-utils 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -34
- data/lib/mock_function.rb +19 -8
- data/rspec-puppet-utils.gemspec +1 -1
- data/spec/classes/mock_function_spec.rb +31 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
data.tar.gz:
|
4
|
-
metadata.gz:
|
3
|
+
data.tar.gz: b0dfaaec1638d3f1d372730e2e988c64e5d9223a
|
4
|
+
metadata.gz: 49631403a559b447615d123c159513b69bea340d
|
5
5
|
SHA512:
|
6
|
-
data.tar.gz:
|
7
|
-
metadata.gz:
|
6
|
+
data.tar.gz: d268e8294f419de6ef69486b865b55e3fcfa1bf11d6b5716213154bf306f4f4bc102f0852e84804ae64f453593ce0a3c307704fbf218003fe5bd2735890a8467
|
7
|
+
metadata.gz: a9c3ec8679ca46e3cf44153a62fe5881883247f92af38fe473da8b42bf227722971c8223d3bf9da95e56f3c9c8d01e39e0ab989f0103851026412988bde8e8dd
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
This is a more refined version of a previous project about [rspec-puppet unit testing](https://github.com/TomPoulton/rspec-puppet-unit-testing), it provides a class for mocking functions, a harness for testing templates, and a simple tool for testing hiera data files. The motivation for mocking functions etc is provided in that project so I won't go over it here.
|
4
4
|
|
5
|
-
See [release notes for v2.0.
|
5
|
+
See [release notes for v2.0.1](../../wiki/Release-Notes#v201)
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
@@ -16,13 +16,14 @@ require 'spec_helper'
|
|
16
16
|
describe 'foo::bar' do
|
17
17
|
|
18
18
|
let!(:add_stuff) { MockFunction.new('add_stuff') { |f|
|
19
|
-
f.
|
19
|
+
f.stub.with([1, 2]).returns(3)
|
20
20
|
}
|
21
21
|
}
|
22
22
|
|
23
23
|
it 'should do something with add_stuff' do
|
24
24
|
# Specific stub for this test
|
25
|
-
add_stuff.
|
25
|
+
add_stuff.stub.with([]).returns(nil)
|
26
|
+
...
|
26
27
|
...
|
27
28
|
end
|
28
29
|
end
|
@@ -36,8 +37,8 @@ MockFunction.new('func', {:type => :statement})
|
|
36
37
|
You can mock Hiera:
|
37
38
|
```ruby
|
38
39
|
MockFunction.new('hiera') { |f|
|
39
|
-
f.
|
40
|
-
f.
|
40
|
+
f.stub.with(['non-ex']).raises(Puppet::ParseError.new('Key not found'))
|
41
|
+
f.stub.with(['db-password']).returns('password1')
|
41
42
|
}
|
42
43
|
```
|
43
44
|
You handle when the functions are created yourself, e.g. you can assign it to a local variable `func = MockFunction...` create it in a before block `before(:each) do MockFunction... end` or use let `let!(:func) { MockFunction... }`
|
@@ -47,6 +48,7 @@ If you use let, **use `let!()` and not `let()`**, this is because lets are lazy-
|
|
47
48
|
Also if you use `let` when mocking hiera, **you can't use `:hiera` as the name due to conflicts** so you have to do something like `let!(:mock_hiera) { MockFunction.new('hiera') }`
|
48
49
|
|
49
50
|
Notes:
|
51
|
+
- `f.stub` and `f.expect` are helper methods for `f.stubs(:call)` and `f.expects(:call)`
|
50
52
|
- You always stub the `call` method as that gets called internally
|
51
53
|
- The `call` method takes an array of arguments
|
52
54
|
|
@@ -105,39 +107,30 @@ describe 'YAML hieradata' do
|
|
105
107
|
|
106
108
|
# Files are loaded recursively
|
107
109
|
validator = HieraData::YamlValidator.new('spec/fixtures/hieradata')
|
110
|
+
validator.load_data :ignore_empty
|
111
|
+
# Use load_data without args to catch empty files
|
108
112
|
|
109
|
-
|
110
|
-
|
111
|
-
|
113
|
+
# Check types
|
114
|
+
it 'should use arrays for api host lists' do
|
115
|
+
validator.validate('my-api-hosts') { |v|
|
116
|
+
expect(v).to be_an Array
|
117
|
+
}
|
112
118
|
end
|
113
119
|
|
114
|
-
|
115
|
-
|
116
|
-
validator.
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
validator.validate('my-api-hosts') { |v|
|
121
|
-
expect(v).to be_an Array
|
122
|
-
}
|
123
|
-
end
|
124
|
-
|
125
|
-
# Use regex to match keys
|
126
|
-
it 'ports should only contain digits' do
|
127
|
-
validator.validate(/-port$/) { |v|
|
128
|
-
expect(v).to match /^[0-9]+$/
|
129
|
-
}
|
130
|
-
end
|
131
|
-
|
132
|
-
# Supply a list of files that the key must be in
|
133
|
-
# (all matches in all other files are still validated)
|
134
|
-
# :live and :qa correspond to live.yaml and qa.yaml
|
135
|
-
it 'should override password in live and qa' do
|
136
|
-
validator.validate('password', [:live, :qa]) { |v|
|
137
|
-
expect ...
|
138
|
-
}
|
139
|
-
end
|
120
|
+
# Use regex to match keys
|
121
|
+
it 'ports should only contain digits' do
|
122
|
+
validator.validate(/-port$/) { |v|
|
123
|
+
expect(v).to match /^[0-9]+$/
|
124
|
+
}
|
125
|
+
end
|
140
126
|
|
127
|
+
# Supply a list of files that the key must be in
|
128
|
+
# (all matches in all other files are still validated)
|
129
|
+
# :live and :qa correspond to live.yaml and qa.yaml
|
130
|
+
it 'should override password in live and qa' do
|
131
|
+
validator.validate('password', [:live, :qa]) { |v|
|
132
|
+
expect ...
|
133
|
+
}
|
141
134
|
end
|
142
135
|
|
143
136
|
end
|
@@ -155,6 +148,8 @@ Diff:
|
|
155
148
|
+"TwoFive"
|
156
149
|
```
|
157
150
|
|
151
|
+
For more about usage see the [wiki page](../../wiki/Hiera-Data-Validator)
|
152
|
+
|
158
153
|
## Setup
|
159
154
|
- Add `rspec-puppet-utils` to your Gemfile (or use `gem install rspec-puppet-utils`)
|
160
155
|
- Add `require 'rspec-puppet-utils'` to the top of your `spec_helper`
|
data/lib/mock_function.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'puppet'
|
2
|
+
require 'mocha'
|
2
3
|
|
3
4
|
module RSpecPuppetUtils
|
4
5
|
|
@@ -6,17 +7,27 @@ module RSpecPuppetUtils
|
|
6
7
|
|
7
8
|
def initialize(name, options = {})
|
8
9
|
parse_options! options
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
#
|
16
|
-
|
10
|
+
this = self
|
11
|
+
Puppet::Parser::Functions.newfunction(name.to_sym, options) { |args| this.call args}
|
12
|
+
yield self if block_given?
|
13
|
+
|
14
|
+
if options[:type] == :statement
|
15
|
+
# call is called on statement function incase expects(:call) is needed
|
16
|
+
# The method is defined incase expects(:call) isn't used
|
17
|
+
def this.call args
|
18
|
+
args
|
19
|
+
end
|
17
20
|
end
|
18
21
|
end
|
19
22
|
|
23
|
+
def stub
|
24
|
+
self.stubs(:call)
|
25
|
+
end
|
26
|
+
|
27
|
+
def expect
|
28
|
+
self.expects(:call)
|
29
|
+
end
|
30
|
+
|
20
31
|
private
|
21
32
|
|
22
33
|
def parse_options!(options)
|
data/rspec-puppet-utils.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |gem|
|
3
3
|
gem.name = 'rspec-puppet-utils'
|
4
|
-
gem.version = '2.0.
|
4
|
+
gem.version = '2.0.2'
|
5
5
|
gem.description = 'Helper classes for mock/stub functions, templates and hierdata'
|
6
6
|
gem.summary = ''
|
7
7
|
gem.author = 'Tom Poulton'
|
@@ -80,15 +80,45 @@ describe MockFunction do
|
|
80
80
|
|
81
81
|
end
|
82
82
|
|
83
|
+
describe '#stub' do
|
84
|
+
|
85
|
+
let(:func) { MockFunction.new('func') }
|
86
|
+
|
87
|
+
it 'should stub #call' do
|
88
|
+
expectation = func.stub
|
89
|
+
expect(expectation).to be_a Mocha::Expectation
|
90
|
+
expect(expectation.matches_method? :call).to eq true
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#expect' do
|
96
|
+
|
97
|
+
let(:func) { MockFunction.new('func') }
|
98
|
+
|
99
|
+
it 'should register expect on #call' do
|
100
|
+
expectation = func.expect
|
101
|
+
expect(expectation).to be_a Mocha::Expectation
|
102
|
+
expect(expectation.matches_method? :call).to eq true
|
103
|
+
func.call [nil] # satisfy the expect we just created!
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
83
108
|
context 'when :type => :statement' do
|
84
109
|
|
110
|
+
let!(:statement) { MockFunction.new 'statement', {:type => :statement} }
|
111
|
+
|
85
112
|
it 'should not raise error' do
|
86
|
-
MockFunction.new 'statement', {:type => :statement}
|
87
113
|
expect {
|
88
114
|
scope.function_statement []
|
89
115
|
}.to_not raise_error
|
90
116
|
end
|
91
117
|
|
118
|
+
it 'should respond to #call' do
|
119
|
+
expect(statement.respond_to? :call).to eq true
|
120
|
+
end
|
121
|
+
|
92
122
|
end
|
93
123
|
|
94
124
|
context 'when :type => :rvalue' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-puppet-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Poulton
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-27 00:00:00 Z
|
13
13
|
dependencies: []
|
14
14
|
|
15
15
|
description: Helper classes for mock/stub functions, templates and hierdata
|