puppetlabs_spec_helper 1.1.1 → 2.0.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.
@@ -1,5 +1,7 @@
1
+ require 'fileutils'
1
2
  require 'rake'
2
3
  require 'rspec/core/rake_task'
4
+ require 'tmpdir'
3
5
  require 'yaml'
4
6
 
5
7
  # optional gems
@@ -11,10 +13,33 @@ end
11
13
 
12
14
  task :default => [:help]
13
15
 
16
+ pattern = 'spec/{aliases,classes,defines,unit,functions,hosts,integration,type_aliases,types}/**/*_spec.rb'
17
+
18
+ spec = Gem::Specification.find_by_name 'gettext-setup'
19
+ load "#{spec.gem_dir}/lib/tasks/gettext.rake"
20
+ locales_dir = File.absolute_path('locales', File.dirname(__FILE__))
21
+ # Initialization requires a valid locales directory
22
+ if File.exist? locales_dir
23
+ GettextSetup.initialize(locales_dir)
24
+ else
25
+ puts "No 'locales' directory found in #{File.dirname(__FILE__)}, skipping gettext initialization"
26
+ end
27
+
14
28
  desc "Run spec tests on an existing fixtures directory"
15
29
  RSpec::Core::RakeTask.new(:spec_standalone) do |t|
16
30
  t.rspec_opts = ['--color']
17
- t.pattern = 'spec/{classes,defines,unit,functions,hosts,integration,types}/**/*_spec.rb'
31
+
32
+ if ENV['CI_NODE_TOTAL'] && ENV['CI_NODE_INDEX']
33
+ ci_total = ENV['CI_NODE_TOTAL'].to_i
34
+ ci_index = ENV['CI_NODE_INDEX'].to_i
35
+ raise "CI_NODE_INDEX must be between 1-#{ci_total}" unless ci_index >= 1 && ci_index <= ci_total
36
+
37
+ files = Rake::FileList[pattern].to_a
38
+ per_node = (files.size / ci_total.to_f).ceil
39
+ t.pattern = files.each_slice(per_node).to_a[ci_index - 1] || files.first
40
+ else
41
+ t.pattern = pattern
42
+ end
18
43
  end
19
44
 
20
45
  desc "Run beaker acceptance tests"
@@ -23,12 +48,6 @@ RSpec::Core::RakeTask.new(:beaker) do |t|
23
48
  t.pattern = 'spec/acceptance'
24
49
  end
25
50
 
26
- desc "Generate code coverage information"
27
- RSpec::Core::RakeTask.new(:coverage) do |t|
28
- t.rcov = true
29
- t.rcov_opts = ['--exclude', 'spec']
30
- end
31
-
32
51
  # This is a helper for the self-symlink entry of fixtures.yml
33
52
  def source_dir
34
53
  Dir.pwd
@@ -47,6 +66,43 @@ def repositories
47
66
  @repositories
48
67
  end
49
68
 
69
+ # get the array of Beaker set names
70
+ # @return [Array<String>]
71
+ def beaker_node_sets
72
+ return @beaker_nodes if @beaker_nodes
73
+ @beaker_nodes = Dir['spec/acceptance/nodesets/*.yml'].sort.map do |node_set|
74
+ node_set.slice!('.yml')
75
+ File.basename(node_set)
76
+ end
77
+ end
78
+
79
+ # Use "vagrant ssh" to login to the given node in the node set
80
+ # @param set [String] The name of the node set (yml file)
81
+ # @param node [String] The name of the node in the set. For multi-node sets.
82
+ def vagrant_ssh(set, node = nil)
83
+ vagrant_yml_dir = File.join '.vagrant', 'beaker_vagrant_files', "#{set}.yml"
84
+ vagrant_file = File.join vagrant_yml_dir, 'Vagrantfile'
85
+ unless File.file? vagrant_file
86
+ puts "There is no Vagrantfile at: '#{vagrant_file}'. Perhaps, the node is not created or is destroyed."
87
+ exit 1
88
+ end
89
+ Dir.chdir(vagrant_yml_dir) do
90
+ command = 'vagrant ssh'
91
+ command += " #{node}" if node
92
+ # Vagrant is not distributed as a normal gem
93
+ # and we should protect it from the current Ruby environment
94
+ env = {
95
+ 'RUBYLIB' => nil,
96
+ 'GEM_PATH' => nil,
97
+ 'BUNDLE_BIN_PATH' => nil,
98
+ }
99
+ system env, command
100
+ end
101
+ end
102
+
103
+ def auto_symlink
104
+ { Dir.pwd.split('/').last.split('-').last => '#{source_dir}' }
105
+ end
50
106
 
51
107
  def fixtures(category)
52
108
  if File.exists?('.fixtures.yml')
@@ -58,13 +114,17 @@ def fixtures(category)
58
114
  end
59
115
 
60
116
  begin
61
- fixtures = YAML.load_file(fixtures_yaml)["fixtures"]
117
+ fixtures = YAML.load_file(ENV['FIXTURES_YML'] || fixtures_yaml)["fixtures"]
62
118
  rescue Errno::ENOENT
63
- return {}
119
+ fixtures = {}
64
120
  rescue Psych::SyntaxError => e
65
121
  abort("Found malformed YAML in #{fixtures_yaml} on line #{e.line} column #{e.column}: #{e.problem}")
66
122
  end
67
123
 
124
+ if fixtures['symlinks'].nil?
125
+ fixtures['symlinks'] = auto_symlink
126
+ end
127
+
68
128
  result = {}
69
129
  if fixtures.include? category and fixtures[category] != nil
70
130
  fixtures[category].each do |fixture, opts|
@@ -76,14 +136,14 @@ def fixtures(category)
76
136
  elsif opts.instance_of?(Hash)
77
137
  target = "spec/fixtures/modules/#{fixture}"
78
138
  real_source = eval('"'+opts["repo"]+'"')
79
- result[real_source] = { "target" => target, "ref" => opts["ref"], "branch" => opts["branch"], "scm" => opts["scm"], "flags" => opts["flags"]}
139
+ result[real_source] = { "target" => target, "ref" => opts["ref"], "branch" => opts["branch"], "scm" => opts["scm"], "flags" => opts["flags"], "subdir" => opts["subdir"]}
80
140
  end
81
141
  end
82
142
  end
83
143
  return result
84
144
  end
85
145
 
86
- def clone_repo(scm, remote, target, ref=nil, branch=nil, flags = nil)
146
+ def clone_repo(scm, remote, target, subdir=nil, ref=nil, branch=nil, flags = nil)
87
147
  args = []
88
148
  case scm
89
149
  when 'hg'
@@ -120,6 +180,16 @@ def revision(scm, target, ref)
120
180
  system("cd #{target} && #{scm} #{args.flatten.join ' '}")
121
181
  end
122
182
 
183
+ def remove_subdirectory(target, subdir)
184
+ unless subdir.nil?
185
+ Dir.mktmpdir {|tmpdir|
186
+ FileUtils.mv(Dir.glob("#{target}/#{subdir}/{.[^\.]*,*}"), tmpdir)
187
+ FileUtils.rm_rf("#{target}/#{subdir}")
188
+ FileUtils.mv(Dir.glob("#{tmpdir}/{.[^\.]*,*}"), "#{target}")
189
+ }
190
+ end
191
+ end
192
+
123
193
  # creates a logger so we can log events with certain levels
124
194
  def logger
125
195
  unless @logger
@@ -135,6 +205,13 @@ def logger
135
205
  @logger
136
206
  end
137
207
 
208
+ def module_working_directory
209
+ # The problem with the relative path is that PMT doesn't expand the path properly and so passing in a relative path here
210
+ # becomes something like C:\somewhere\backslashes/spec/fixtures/work-dir on Windows, and then PMT barfs itself.
211
+ # This has been reported as https://tickets.puppetlabs.com/browse/PUP-4884
212
+ File.expand_path(ENV['MODULE_WORKING_DIR'] ? ENV['MODULE_WORKING_DIR'] : 'spec/fixtures/work-dir')
213
+ end
214
+
138
215
  # returns the current thread count that is currently active
139
216
  # a status of false or nil means the thread completed
140
217
  # so when anything else we count that as a active thread
@@ -177,9 +254,13 @@ task :spec_prep do
177
254
  rescue
178
255
  end
179
256
 
257
+ # git has a race condition creating that directory, that would lead to aborted clone operations
258
+ FileUtils::mkdir_p("spec/fixtures/modules")
259
+
180
260
  repositories.each do |remote, opts|
181
261
  scm = 'git'
182
262
  target = opts["target"]
263
+ subdir = opts["subdir"]
183
264
  ref = opts["ref"]
184
265
  scm = opts["scm"] if opts["scm"]
185
266
  branch = opts["branch"] if opts["branch"]
@@ -190,8 +271,9 @@ task :spec_prep do
190
271
  logger.debug "New Thread started for #{remote}"
191
272
  # start up a new thread and store it in the opts hash
192
273
  opts[:thread] = Thread.new do
193
- clone_repo(scm, remote, target, ref, branch, flags)
274
+ clone_repo(scm, remote, target, subdir, ref, branch, flags)
194
275
  revision(scm, target, ref) if ref
276
+ remove_subdirectory(target, subdir) if subdir
195
277
  end
196
278
  else
197
279
  # the last thread started should be the longest wait
@@ -206,7 +288,6 @@ task :spec_prep do
206
288
  # wait for all the threads to finish
207
289
  repositories.each {|remote, opts| opts[:thread].join }
208
290
 
209
- FileUtils::mkdir_p("spec/fixtures/modules")
210
291
  fixtures("symlinks").each do |source, target|
211
292
  if is_windows
212
293
  fail "Cannot symlink on Windows unless using at least Puppet 3.5" if !puppet_symlink_available
@@ -228,10 +309,14 @@ task :spec_prep do
228
309
  end
229
310
  next if File::exists?(target)
230
311
 
312
+ working_dir = module_working_directory
313
+ target_dir = File.expand_path('spec/fixtures/modules')
314
+
231
315
  command = "puppet module install" + ref + flags + \
232
316
  " --ignore-dependencies" \
233
317
  " --force" \
234
- " --target-dir spec/fixtures/modules #{remote}"
318
+ " --module_working_dir #{working_dir}" \
319
+ " --target-dir #{target_dir} #{remote}"
235
320
 
236
321
  unless system(command)
237
322
  fail "Failed to install module #{remote} to #{target}"
@@ -262,6 +347,8 @@ task :spec_clean do
262
347
  FileUtils::rm_rf(target)
263
348
  end
264
349
 
350
+ FileUtils::rm_rf(module_working_directory)
351
+
265
352
  fixtures("symlinks").each do |source, target|
266
353
  FileUtils::rm_f(target)
267
354
  end
@@ -272,19 +359,59 @@ task :spec_clean do
272
359
 
273
360
  end
274
361
 
275
- desc "Run spec tests in a clean fixtures directory"
362
+ desc "Run spec tests and clean the fixtures directory if successful"
276
363
  task :spec do
277
364
  Rake::Task[:spec_prep].invoke
278
365
  Rake::Task[:spec_standalone].invoke
279
366
  Rake::Task[:spec_clean].invoke
280
367
  end
281
368
 
369
+ desc "Parallel spec tests"
370
+ task :parallel_spec do
371
+ begin
372
+ require 'parallel_tests'
373
+
374
+ args = ['-t', 'rspec'].
375
+ concat(Rake::FileList[pattern].to_a)
376
+
377
+ Rake::Task[:spec_prep].invoke
378
+ ParallelTests::CLI.new.run(args)
379
+ Rake::Task[:spec_clean].invoke
380
+ rescue LoadError
381
+ fail 'Add the parallel_tests gem to Gemfile to enable this task'
382
+ end
383
+ end
384
+
282
385
  desc "List available beaker nodesets"
283
- task :beaker_nodes do
284
- Dir['spec/acceptance/nodesets/*.yml'].sort!.select { |node|
285
- node.slice!('.yml')
286
- puts File.basename(node)
287
- }
386
+ task 'beaker:sets' do
387
+ beaker_node_sets.each do |set|
388
+ puts set
389
+ end
390
+ end
391
+
392
+ # alias for compatibility
393
+ task 'beaker_nodes' => 'beaker:sets'
394
+
395
+ desc 'Try to use vagrant to login to the Beaker node'
396
+ task 'beaker:ssh', [:set, :node] do |_task, args|
397
+ set = args[:set] || ENV['BEAKER_set'] || ENV['RS_SET'] || 'default'
398
+ node = args[:node]
399
+ vagrant_ssh set, node
400
+ end
401
+
402
+ beaker_node_sets.each do |set|
403
+ desc "Run the Beaker acceptance tests for the node set '#{set}'"
404
+ task "beaker:#{set}" do
405
+ ENV['BEAKER_set'] = set
406
+ Rake::Task['beaker'].reenable
407
+ Rake::Task['beaker'].invoke
408
+ end
409
+
410
+ desc "Use vagrant to login to a node from the set '#{set}'"
411
+ task "beaker:ssh:#{set}", [:node] do |_task, args|
412
+ node = args[:node]
413
+ vagrant_ssh set, node
414
+ end
288
415
  end
289
416
 
290
417
  desc "Build puppet module package"
@@ -314,11 +441,19 @@ PuppetLint::RakeTask.new(:lint) do |config|
314
441
  config.fail_on_warnings = true
315
442
  config.disable_checks = [
316
443
  '80chars',
444
+ '140chars',
317
445
  'class_inherits_from_params_class',
318
446
  'class_parameter_defaults',
319
447
  'documentation',
320
448
  'single_quote_string_with_variables']
321
- config.ignore_paths = ["tests/**/*.pp", "vendor/**/*.pp","examples/**/*.pp", "spec/**/*.pp", "pkg/**/*.pp"]
449
+ config.ignore_paths = [
450
+ "bundle/**/*.pp",
451
+ "pkg/**/*.pp",
452
+ "spec/**/*.pp",
453
+ "tests/**/*.pp",
454
+ "types/**/*.pp",
455
+ "vendor/**/*.pp",
456
+ ]
322
457
  end
323
458
 
324
459
  require 'puppet-syntax/tasks/puppet-syntax'
@@ -326,6 +461,9 @@ PuppetSyntax.exclude_paths ||= []
326
461
  PuppetSyntax.exclude_paths << "spec/fixtures/**/*"
327
462
  PuppetSyntax.exclude_paths << "pkg/**/*"
328
463
  PuppetSyntax.exclude_paths << "vendor/**/*"
464
+ if Puppet.version.to_f < 4.0
465
+ PuppetSyntax.exclude_paths << "types/**/*"
466
+ end
329
467
  PuppetSyntax.future_parser = true if ENV['FUTURE_PARSER'] == 'yes'
330
468
 
331
469
  desc "Check syntax of Ruby files and call :syntax and :metadata_lint"
@@ -380,11 +518,11 @@ task :compute_dev_version do
380
518
  print new_version
381
519
  end
382
520
 
383
- desc "Runs all nessesary checks on a module in preparation for a release"
521
+ desc "Runs all necessary checks on a module in preparation for a release"
384
522
  task :release_checks do
385
523
  Rake::Task[:lint].invoke
386
524
  Rake::Task[:validate].invoke
387
- Rake::Task[:spec].invoke
525
+ Rake::Task[:parallel_spec].invoke
388
526
  Rake::Task["check:symlinks"].invoke
389
527
  Rake::Task["check:test_file"].invoke
390
528
  Rake::Task["check:dot_underscore"].invoke
@@ -394,7 +532,7 @@ end
394
532
  namespace :check do
395
533
  desc "Fails if symlinks are present in directory"
396
534
  task :symlinks do
397
- symlink = `find . -type l -ls`
535
+ symlink = `find . -path ./.git -prune -o -type l -ls`
398
536
  unless symlink == ""
399
537
  puts symlink
400
538
  fail "A symlink exists within this directory"
@@ -436,3 +574,16 @@ desc "Display the list of available rake tasks"
436
574
  task :help do
437
575
  system("rake -T")
438
576
  end
577
+
578
+ begin
579
+ require 'rubocop/rake_task'
580
+ RuboCop::RakeTask.new(:rubocop) do |task|
581
+ # These make the rubocop experience maybe slightly less terrible
582
+ task.options = ['-D', '-S', '-E']
583
+ end
584
+ rescue LoadError
585
+ desc "rubocop is not available in this installation"
586
+ task :rubocop do
587
+ fail "rubocop is not available in this installation"
588
+ end
589
+ end
@@ -1,5 +1,8 @@
1
1
  module PuppetlabsSpecHelper
2
+ VERSION = "2.0.0"
3
+
4
+ # compat for pre-1.2.0 users; deprecated
2
5
  module Version
3
- STRING = '1.1.1'
6
+ STRING = PuppetlabsSpecHelper::VERSION
4
7
  end
5
8
  end
@@ -1,88 +1,33 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
- # stub: puppetlabs_spec_helper 1.1.1 ruby lib
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'puppetlabs_spec_helper/version'
6
5
 
7
- Gem::Specification.new do |s|
8
- s.name = "puppetlabs_spec_helper"
9
- s.version = "1.1.1"
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "puppetlabs_spec_helper"
8
+ spec.version = PuppetlabsSpecHelper::VERSION
9
+ spec.authors = ["Puppet, Inc.", "Community Contributors"]
10
+ spec.email = ["modules-team@puppet.com"]
10
11
 
11
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
- s.require_paths = ["lib"]
13
- s.authors = ["Puppet Labs"]
14
- s.date = "2016-03-02"
15
- s.description = "Contains rake tasks and a standard spec_helper for running spec tests on puppet modules"
16
- s.email = ["modules-dept@puppetlabs.com"]
17
- s.extra_rdoc_files = [
18
- "LICENSE",
19
- "README.markdown"
20
- ]
21
- s.files = [
22
- ".noexec.yaml",
23
- ".travis.yml",
24
- "CHANGELOG.md",
25
- "CONTRIBUTING.md",
26
- "Gemfile",
27
- "LICENSE",
28
- "README.markdown",
29
- "Rakefile",
30
- "lib/puppetlabs_spec_helper/module_spec_helper.rb",
31
- "lib/puppetlabs_spec_helper/puppet_spec_helper.rb",
32
- "lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb",
33
- "lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb",
34
- "lib/puppetlabs_spec_helper/puppetlabs_spec/matchers.rb",
35
- "lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb",
36
- "lib/puppetlabs_spec_helper/puppetlabs_spec_helper.rb",
37
- "lib/puppetlabs_spec_helper/rake_tasks.rb",
38
- "lib/puppetlabs_spec_helper/version.rb",
39
- "puppet_spec_helper.rb",
40
- "puppetlabs_spec_helper.gemspec",
41
- "puppetlabs_spec_helper.rb",
42
- "spec/lib/puppet/type/spechelper.rb",
43
- "spec/spec_helper.rb",
44
- "spec/unit/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals_spec.rb",
45
- "spec/watchr.rb"
46
- ]
47
- s.homepage = "http://github.com/puppetlabs/puppetlabs_spec_helper"
48
- s.licenses = ["Apache-2.0"]
49
- s.rubygems_version = "2.4.5.1"
50
- s.summary = "Standard tasks and configuration for module spec tests"
12
+ spec.summary = %q{Standard tasks and configuration for module spec tests.}
13
+ spec.description = %q{Contains rake tasks and a standard spec_helper for running spec tests on puppet modules.}
14
+ spec.homepage = "http://github.com/puppetlabs/puppetlabs_spec_helper"
51
15
 
52
- if s.respond_to? :specification_version then
53
- s.specification_version = 4
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
54
20
 
55
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
- s.add_runtime_dependency(%q<rake>, [">= 0"])
57
- s.add_runtime_dependency(%q<rspec-puppet>, [">= 0"])
58
- s.add_runtime_dependency(%q<puppet-lint>, [">= 0"])
59
- s.add_runtime_dependency(%q<puppet-syntax>, [">= 0"])
60
- s.add_runtime_dependency(%q<mocha>, [">= 0"])
61
- s.add_development_dependency(%q<yard>, [">= 0"])
62
- s.add_development_dependency(%q<pry>, [">= 0"])
63
- s.add_development_dependency(%q<jeweler>, [">= 0"])
64
- s.add_development_dependency(%q<puppet>, ["~> 3.8.3"])
65
- else
66
- s.add_dependency(%q<rake>, [">= 0"])
67
- s.add_dependency(%q<rspec-puppet>, [">= 0"])
68
- s.add_dependency(%q<puppet-lint>, [">= 0"])
69
- s.add_dependency(%q<puppet-syntax>, [">= 0"])
70
- s.add_dependency(%q<mocha>, [">= 0"])
71
- s.add_dependency(%q<yard>, [">= 0"])
72
- s.add_dependency(%q<pry>, [">= 0"])
73
- s.add_dependency(%q<jeweler>, [">= 0"])
74
- s.add_dependency(%q<puppet>, ["~> 3.8.3"])
75
- end
76
- else
77
- s.add_dependency(%q<rake>, [">= 0"])
78
- s.add_dependency(%q<rspec-puppet>, [">= 0"])
79
- s.add_dependency(%q<puppet-lint>, [">= 0"])
80
- s.add_dependency(%q<puppet-syntax>, [">= 0"])
81
- s.add_dependency(%q<mocha>, [">= 0"])
82
- s.add_dependency(%q<yard>, [">= 0"])
83
- s.add_dependency(%q<pry>, [">= 0"])
84
- s.add_dependency(%q<jeweler>, [">= 0"])
85
- s.add_dependency(%q<puppet>, ["~> 3.8.3"])
86
- end
87
- end
21
+ spec.add_runtime_dependency "mocha", "~> 1.0"
22
+ spec.add_runtime_dependency "puppet-lint", "~> 2.0"
23
+ spec.add_runtime_dependency "puppet-syntax", "~> 2.0"
24
+ spec.add_runtime_dependency "rspec-puppet", "~> 2.0"
88
25
 
26
+ spec.add_development_dependency "bundler", "~> 1.12"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "puppet", "~> 3"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "yard"
32
+ spec.add_development_dependency "gettext-setup", "~> 0.13"
33
+ end