rspec-puppet-utils 2.0.1 → 2.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- data.tar.gz: 1cee9e4a75b93b9b3d2bbdd4d9c18d6f224c5448
4
- metadata.gz: da8f2e5c38ad50874bfcdba758d6e239922654da
3
+ data.tar.gz: b0dfaaec1638d3f1d372730e2e988c64e5d9223a
4
+ metadata.gz: 49631403a559b447615d123c159513b69bea340d
5
5
  SHA512:
6
- data.tar.gz: d75f54a18d91e8ca31bf1ed170ec118e35b1253cf9f91ac3dd9e972d4234e91cefcd8a9ec912324d7e92a4683e9d9438a02b28210123caab03e27f3b503a7d71
7
- metadata.gz: 8135746b0dfe552fd456f5b5eb747a1180f4c8c943d1918ee8b1beb0b46004fd3dcce66fed644a4c78748279f6dfa7069e952956ef4d28c21783c3976f460983
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.0](../../wiki/Release-Notes#v200)
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.stubs(:call).with([1, 2]).returns(3)
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.stubs(:call).with([]).returns(nil)
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.stubs(:call).with(['non-ex']).raises(Puppet::ParseError.new('Key not found'))
40
- f.stubs(:call).with(['db-password']).returns('password1')
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
- it 'should not contain syntax errors' do
110
- # Use true to ignore empty files (default false)
111
- expect { validator.load true }.to_not raise_error
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
- context 'with valid yaml' do
115
-
116
- validator.load true
117
-
118
- # Check types
119
- it 'should use arrays for api host lists' do
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
- if options[:type] == :rvalue
10
- this = self
11
- Puppet::Parser::Functions.newfunction(name.to_sym, options) { |args| this.call args}
12
- yield self if block_given?
13
- else
14
- # Even though the puppet function does not return a value,
15
- # this mock still needs to do something, what it returns doesn't really matter.
16
- Puppet::Parser::Functions.newfunction(name.to_sym, options) { |args| args }
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)
@@ -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.1'
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.1
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-24 00:00:00 Z
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