rspec-puppet-utils 3.3.0 → 3.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9889ff4f04abf064cb1bb613d08cc43ab297dddc
4
- data.tar.gz: 5962bbdd9291625f70a65b53d3cdb17ccb513efd
3
+ metadata.gz: 4e7ae5ec76add67852ea24ec8732dadd88b46a72
4
+ data.tar.gz: f0d1df43cb66693a55b7e8e22723d86e6b08e3b5
5
5
  SHA512:
6
- metadata.gz: d51415db60d4fcac48d2b1710550e8fc1e7ee7c6251d7645eb6699ba6a2b3eabf2a125b45800692c6d621d1b64d125606d405f1ef93034f68c7719aa7c1d1aa3
7
- data.tar.gz: 0184e3591aeb8c9bb8f4719ec5b731189d1a651492ab32a9f4761b5d681e7215c7ef42d4e5b9d4b24c69f11241dfc17660ce2eeb1378d06728049af582f89b7c
6
+ metadata.gz: 33b15833fe535717c27f94d2589b7ab67295d0c3b65d318167a0846c186e3f834267b0eb3603fd8d557439136e4df6e440970ec0f621cf1ff95e60bfeef76886
7
+ data.tar.gz: 605fa915c909fb127763db9258b61dc523c01dfb18c9be1f6897c0da0a4bfc82a09c22dc8d858bb40b4e80fe7fa4494cb357df42ad374c72a5c26aa7020d5723
@@ -3,46 +3,52 @@ require 'rspec/core/rake_task'
3
3
  require 'fileutils'
4
4
 
5
5
  # ToDo: replace zip cmds with ruby zip lib to avoid shelling out
6
+ # ToDo: What if no lib dir exists?
6
7
 
7
8
  module Rake
8
9
 
9
10
  class Puppet
10
11
 
11
- attr_accessor :module_dirs, :excluded_modules
12
- attr_accessor :package_dir, :package_files, :package_version, :package_versioning
12
+ attr_accessor :excluded_modules, :package_dir, :package_files, :package_version, :package_versioning
13
13
 
14
- @module_dirs # (string[]) The directories containing modules to test
15
- @excluded_dirs # (string[]) Directories excluded from spec search
16
- @excluded_modules # (string[]) Modules excluded from spec testing
17
- @package_dir # (string) Where the puppet zip package will be created
18
- @package_files # (string[]) Files and directories to include in the package
19
- @package_name # (string) Name of the package
20
- @package_version # (string) The version of the package (e.g. 2.1.0)
21
- @package_versioning # (boolean) Is the version included in the package name?
14
+ # @librarian_dir (string) Directory where librarian-puppet installs modules (default "modules")
15
+ # @site_dir (string) Directory for profiles, roles and components (default "site")
16
+ # @lib_dir (string) Directory for custom/internal modules (default "lib")
17
+ # @excluded_dirs (string[]) Directories excluded from spec search
18
+ # @excluded_modules (string[]) Modules excluded from spec testing
19
+ # @package_dir (string) Where the puppet zip package will be created
20
+ # @package_files (string[]) Files and directories to include in the package
21
+ # @package_name (string) Name of the package
22
+ # @package_version (string) The version of the package (e.g. 2.1.0)
23
+ # @package_versioning (boolean) Is the version included in the package name?
22
24
 
23
25
  def initialize
24
26
  extend Rake::DSL # makes 'namespace' and 'task' methods available to instance
25
27
 
26
- @module_dirs = ['lib', 'site'] # Deliberately excludes 'modules' dir
28
+ @librarian_dir = 'modules'
29
+ @site_dir = 'site'
30
+ @lib_dir = 'lib'
27
31
  @excluded_dirs = ['.', '..']
28
32
  @excluded_modules = []
29
33
  @package_dir = 'pkg'
30
- @package_files = @module_dirs + ['modules', 'hieradata', 'environment.conf']
34
+ @package_files = ['hieradata', 'environment.conf']
31
35
  @package_name = 'puppet'
32
36
  @package_version = nil
33
37
  @package_versioning = true
34
38
  end
35
39
 
36
40
  def load_tasks
41
+ validate_unique_module_names
37
42
  load_module_tasks
38
43
  load_build_tasks
39
44
  end
40
45
 
46
+ # private
47
+
41
48
  def testable_modules
42
- raise ArgumentError, 'module_paths must be an array' unless @module_dirs.is_a? Array
43
49
  raise ArgumentError, 'excluded_modules must be an array' unless @excluded_modules.is_a? Array
44
50
  modules = []
45
- @module_dirs.each { |module_dir|
51
+ [@lib_dir, @site_dir].each { |module_dir|
46
52
  raise ArgumentError, "Module path #{module_dir} could not be found" unless Dir.exist?(module_dir)
47
53
  entries = Dir.entries(module_dir) - @excluded_dirs - @excluded_modules
48
54
  modules.concat entries.collect { |entry| "#{module_dir}/#{entry}" }
@@ -67,6 +73,15 @@ module Rake
67
73
  false
68
74
  end
69
75
 
76
+ def validate_unique_module_names
77
+ # & == intersection : Returns elements common to the both arrays
78
+ duplicates = Dir.entries(@site_dir) & Dir.entries(@librarian_dir)
79
+ duplicates += Dir.entries(@librarian_dir) & Dir.entries(@lib_dir)
80
+ duplicates += Dir.entries(@lib_dir) & Dir.entries(@site_dir)
81
+ duplicates -= @excluded_dirs
82
+ fail "Duplicate module names: #{duplicates.join ', '}" unless duplicates.empty?
83
+ end
84
+
70
85
  def load_module_tasks
71
86
 
72
87
  modules = testable_modules
@@ -91,40 +106,66 @@ module Rake
91
106
  desc 'Run specs in all modules'
92
107
  task :spec => spec_tasks
93
108
  task :default => :spec
94
-
95
109
  end
96
110
 
97
111
  def load_build_tasks
98
112
 
99
- raise(ArgumentError, 'Please provide a package_version (e.g. "1.0.0")') if @package_version.nil?
113
+ raise ArgumentError, 'Please provide a package_version (e.g. "1.0.0")' if @package_version.nil?
100
114
 
101
115
  full_package_name = @package_versioning ? "puppet-#{@package_version}.zip" : 'puppet.zip'
102
116
  package_desc = @package_versioning ? full_package_name : "#{full_package_name} v#{@package_version}"
103
- package_path = "#{@package_dir}/#{full_package_name}"
117
+ package_path = File.expand_path "#{@package_dir}/#{full_package_name}"
118
+ build_dir = "#{@package_dir}/puppet"
104
119
 
105
120
  namespace :build do
106
121
 
107
122
  # Preps build directory
108
123
  task :prep do
109
124
  puts 'Preparing build'
110
- FileUtils.mkdir_p @package_dir
125
+
111
126
  FileUtils.rm package_path if File.exist?(package_path)
127
+ FileUtils.rm_r build_dir if File.exist?(build_dir)
128
+ FileUtils.mkdir_p build_dir
112
129
  end
113
130
 
114
- task :package => [:prep] do
131
+ task :copy_files => [:prep] do
132
+
133
+ # Copy librarian and site modules into build dir
134
+ puts 'Copying external and site modules'
135
+ FileUtils.cp_r @site_dir, build_dir
136
+ FileUtils.cp_r @librarian_dir, build_dir
137
+
138
+ # Copy lib modules into the librarian build dir
139
+ puts 'Copying lib modules'
140
+ FileUtils.cp_r "#{@lib_dir}/.", "#{build_dir}/#{@librarian_dir}"
141
+
142
+ # Copy other package files
143
+ @package_files.each {|f|
144
+ fail "Could not find package file or directory #{f}" unless File.exist? f
145
+ puts "Copying #{f} to #{build_dir}"
146
+ FileUtils.cp_r f, build_dir
147
+ }
148
+ end
149
+
150
+ task :package => [:copy_files] do
151
+ puts "Creating #{full_package_name}"
115
152
  # Exclude all the spec code as it's not needed once deployed
116
153
  exclude_patterns = ['\*/\*/spec/\*']
117
154
  exclude_string = "-x #{exclude_patterns.join(' ')}"
118
- include_string = @package_files.join(' ')
119
- cmd = "zip -qr #{package_path} #{include_string} #{exclude_string}"
120
- success = system(cmd)
121
- exit 1 unless success
155
+ FileUtils.cd(build_dir) {
156
+ out = `zip -qr '#{package_path}' . #{exclude_string}`
157
+ fail("Error creating package: #{out}") unless $?.exitstatus == 0
158
+ }
122
159
  end
123
160
 
161
+ task :cleanup do
162
+ puts "Cleaning up #{build_dir}"
163
+ FileUtils.rm_r build_dir if File.exist?(build_dir)
164
+ end
124
165
  end
125
166
 
126
167
  desc "Build #{package_desc} without tests"
127
- task :quick_build => 'build:package' do
168
+ task :quick_build => ['build:package', 'build:cleanup'] do
128
169
  puts "Built #{package_desc}"
129
170
  end
130
171
 
@@ -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 = '3.3.0'
4
+ gem.version = '3.4.0'
5
5
  gem.description = 'Helper classes for mock/stub functions, templates and hieradata'
6
6
  gem.summary = ''
7
7
  gem.author = 'Tom Poulton'
@@ -17,6 +17,33 @@ describe Rake::Puppet do
17
17
  expect(puppet.package_files.count).to eq initial_count + 1
18
18
  end
19
19
 
20
+ describe 'load_tasks' do
21
+
22
+ before(:each) do
23
+ puppet.stubs(:validate_unique_module_names)
24
+ puppet.stubs(:load_module_tasks)
25
+ puppet.stubs(:load_build_tasks)
26
+ end
27
+
28
+ it 'validates module names' do
29
+ puppet.expects(:validate_unique_module_names).once
30
+ puppet.load_tasks
31
+ end
32
+
33
+ it 'loads module tasks' do
34
+ puppet.expects(:load_module_tasks).once
35
+ puppet.load_tasks
36
+ end
37
+
38
+ it 'loads build tasks' do
39
+ puppet.expects(:load_build_tasks).once
40
+ puppet.load_tasks
41
+ end
42
+
43
+ end
44
+
45
+ # private
46
+
20
47
  describe 'load_module_tasks' do
21
48
 
22
49
  before(:each) do
@@ -142,12 +169,10 @@ describe Rake::Puppet do
142
169
  Dir.stubs(:exist?).returns true
143
170
  Dir.stubs(:entries).with('lib').returns(['one', 'two'])
144
171
  Dir.stubs(:entries).with('site').returns(['three', 'four'])
145
- Dir.stubs(:entries).with('extra').returns(['five', 'six'])
146
172
  end
147
173
 
148
174
  it 'finds modules in all paths' do
149
- puppet.module_dirs << 'extra'
150
- modules = ['lib/one', 'lib/two', 'site/three', 'site/four', 'extra/five', 'extra/six']
175
+ modules = ['lib/one', 'lib/two', 'site/three', 'site/four']
151
176
  expect(puppet.testable_modules).to match_array modules
152
177
  end
153
178
 
@@ -163,11 +188,6 @@ describe Rake::Puppet do
163
188
  expect(puppet.testable_modules).to match_array modules
164
189
  end
165
190
 
166
- it 'raises an error if module_paths is not an array' do
167
- puppet.module_dirs = 'not an array'
168
- expect { puppet.testable_modules }.to raise_error(ArgumentError, /must be an array/)
169
- end
170
-
171
191
  it 'raises an error if excluded modules is not an array' do
172
192
  puppet.excluded_modules = 'not an array'
173
193
  expect { puppet.testable_modules }.to raise_error(ArgumentError, /must be an array/)
@@ -182,4 +202,45 @@ describe Rake::Puppet do
182
202
 
183
203
  end
184
204
 
205
+ describe 'validate_unique_module_names' do
206
+
207
+ let(:dir_list_a) { ['.', '..', 'foo', 'bar'] }
208
+ let(:dir_list_b) { ['.', '..', 'baz', 'bam'] }
209
+ let(:dir_list_c) { ['.', '..', 'fuzzy', 'bazzy'] }
210
+
211
+ before(:each) do
212
+ Dir.stubs(:entries).with('site').returns dir_list_a
213
+ Dir.stubs(:entries).with('modules').returns dir_list_b
214
+ Dir.stubs(:entries).with('lib').returns dir_list_c
215
+ end
216
+
217
+ it 'does not fail if all module names are unique' do
218
+ expect {
219
+ puppet.validate_unique_module_names
220
+ }.to_not raise_error
221
+ end
222
+
223
+ it 'fails if site and modules dirs have conflicting module names' do
224
+ Dir.stubs(:entries).with('modules').returns dir_list_a
225
+ expect {
226
+ puppet.validate_unique_module_names
227
+ }.to raise_error /Duplicate module names/
228
+ end
229
+
230
+ it 'fails if modules and lib dirs have conflicting module names' do
231
+ Dir.stubs(:entries).with('lib').returns dir_list_b
232
+ expect {
233
+ puppet.validate_unique_module_names
234
+ }.to raise_error /Duplicate module names/
235
+ end
236
+
237
+ it 'fails if lib and site dirs have conflicting module names' do
238
+ Dir.stubs(:entries).with('site').returns dir_list_c
239
+ expect {
240
+ puppet.validate_unique_module_names
241
+ }.to raise_error /Duplicate module names/
242
+ end
243
+
244
+ end
245
+
185
246
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-puppet-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Poulton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-24 00:00:00.000000000 Z
11
+ date: 2017-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet