chefspec-bootstrap 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,34 +1,9 @@
1
+ Gemfile.lock
1
2
  *.gem
2
3
  *.rbc
3
- /.config
4
- /coverage/
5
- /InstalledFiles
6
- /pkg/
7
4
  /spec/reports/
8
- /test/tmp/
9
- /test/version_tmp/
10
- /tmp/
11
-
12
- ## Specific to RubyMotion:
13
- .dat*
14
- .repl_history
15
- build/
16
-
17
- ## Documentation cache and generated files:
18
- /.yardoc/
19
- /_yardoc/
20
- /doc/
21
- /rdoc/
22
-
23
- ## Environment normalisation:
24
5
  /.bundle/
25
6
  /lib/bundler/man/
26
-
27
- # for a library or gem, you might want to ignore these files since the code is
28
- # intended to run in multiple environments; otherwise, check them in:
29
- # Gemfile.lock
30
- # .ruby-version
31
- # .ruby-gemset
32
-
33
- # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
7
+ .ruby-version
8
+ .ruby-gemset
34
9
  .rvmrc
@@ -1,13 +1,25 @@
1
- Encoding:
2
- Enabled: false
3
-
4
1
  LineLength:
5
2
  Max: 120
6
3
 
4
+ Style/Next:
5
+ Enabled: false
6
+
7
7
  Documentation:
8
8
  Enabled: false
9
9
 
10
10
  MethodLength:
11
11
  Enabled: false
12
12
 
13
- inherit_from: .rubocop_todo.yml
13
+ Metrics/AbcSize:
14
+ Max: 25
15
+
16
+ Metrics/ClassLength:
17
+ Max: 150
18
+
19
+ Style/FileName:
20
+ Exclude:
21
+ - bin/chefspec-bootstrap
22
+
23
+ Lint/UselessAssignment:
24
+ Exclude:
25
+ - lib/chefspec_bootstrap.rb
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1
6
+
7
+ addons:
8
+ code_climate:
9
+ repo_token: 05908e3abbf8623c35e780470ccc7883031c7dc06b993cf916f0cb898f49a164
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
- gem 'rubocop'
5
4
 
5
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  chefspec-bootstrap
2
2
  ==================
3
3
 
4
+ [![Gem Version](http://img.shields.io/gem/v/chefspec-bootstrap.svg)][gem]
5
+ [![Build Status](http://img.shields.io/travis/AMeng/chefspec-bootstrap.svg)][travis]
6
+ [![Code Climate](http://img.shields.io/codeclimate/github/AMeng/chefspec-bootstrap.svg)][codeclimate]
7
+
8
+ [gem]: https://rubygems.org/gems/chefspec-bootstrap
9
+ [travis]: http://travis-ci.org/AMeng/chefspec-bootstrap
10
+ [codeclimate]: https://codeclimate.com/github/AMeng/chefspec-bootstrap
11
+
4
12
  A command line tool to get started with ChefSpec. Generates spec files for your untested recipes.
5
13
 
6
14
  Given a cookbook called `my_cookbook` with a recipe called `my_recipe.rb`:
@@ -16,7 +24,7 @@ template "/etc/apache2/sites-available/mysite" do
16
24
  end
17
25
  ```
18
26
 
19
- The command line tool will generate output the following to stdout:
27
+ The command line tool will output the following to stdout:
20
28
  ```ruby
21
29
  require 'chefspec'
22
30
 
@@ -37,7 +45,6 @@ describe 'my_cookbook::my_recipe' do
37
45
  end
38
46
  ```
39
47
 
40
-
41
48
  Getting Started
42
49
  ---
43
50
  Install the gem
@@ -45,7 +52,7 @@ Install the gem
45
52
  gem install chefspec-bootstrap
46
53
  ```
47
54
 
48
- Run the command-line tool
55
+ Run the command-line tool, pointing to a recipe:
49
56
  ```
50
57
  chefspec-bootstrap my_cookbook/recipes/my_recipe.rb
51
58
  ```
@@ -54,9 +61,15 @@ Options
54
61
  ---
55
62
  ```
56
63
  Usage: chefspec-bootstrap <recipe.rb> [options]
57
- -t, --template TEMPLATEFILE ERB template file used to generate specs
64
+ -t, --template <file> ERB template file used to generate specs
65
+ -s, --spechelper <file> spec_helper.rb file. By default, looks in spec/spec_helper.rb
66
+ -o, --output <file> File to output spec. Prints to stdout if not specified.
58
67
  ```
59
68
 
60
69
  Creating a custom template
61
70
  ---
62
71
  A custom erb template can be passsed using the `-t` flag. See the included default template for an example.
72
+
73
+ What this project is NOT:
74
+ ---
75
+ This is not a replacement for writing ChefSpec tests. Rather, this is a way to get started when you have an entirely untested recipe.
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'rubocop/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ RuboCop::RakeTask.new(:rubocop)
6
+
7
+ task default: [:rubocop, :spec]
@@ -1,28 +1,31 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rspec/expectations'
4
- require 'chefspec-bootstrap'
3
+ require_relative '../lib/chefspec_bootstrap'
5
4
  require 'optparse'
6
5
 
7
- options = {:template => nil}
6
+ options = { template: nil }
8
7
 
9
8
  optparse = OptionParser.new do |opts|
10
- opts.banner = "Usage: chefspec-bootstrap <recipe.rb> [options]"
9
+ opts.banner = 'Usage: chefspec-bootstrap <recipe.rb> [options]'
11
10
 
12
- opts.on('-t', '--template <file>', "ERB template file used to generate specs") do |t|
11
+ opts.on('-t', '--template <file>', 'ERB template file used to generate specs') do |t|
13
12
  options[:template] = t
14
13
  end
15
14
 
16
- opts.on('-s', '--spechelper <file>', "spec_helper.rb file. By default, looks in spec/spec_helper.rb") do |s|
15
+ opts.on('-s', '--spec-helper <file>', 'spec_helper.rb file. By default, looks in spec/spec_helper.rb') do |s|
17
16
  options[:spec_helper] = s
18
17
  end
19
18
 
20
- opts.on('-o', '--output <file>', "File to output spec. Prints to stdout if not specified.") do |o|
19
+ opts.on('-o', '--output <file>', 'File to output spec. Prints to stdout if not specified.') do |o|
21
20
  options[:output_file] = o
22
21
  end
22
+
23
+ opts.on('-c', '--cookbook-path <dir>', 'Cookbook path (directory). Your spec_helper file can override this.') do |c|
24
+ options[:cookbook_path] = c
25
+ end
23
26
  end
24
27
 
25
- abort "Recipe not specified!" if ARGV.empty?
28
+ abort 'Recipe not specified!' if ARGV.empty?
26
29
 
27
30
  optparse.parse!
28
31
 
@@ -31,6 +34,7 @@ bootstrap = ChefSpec::Bootstrap.new(
31
34
  recipe,
32
35
  options[:template],
33
36
  options[:spec_helper],
34
- options[:output_file]
37
+ options[:output_file],
38
+ options[:cookbook_path]
35
39
  )
36
40
  bootstrap.generate
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'chefspec-bootstrap'
3
- s.version = '0.0.4'
3
+ s.version = '0.0.5'
4
4
  s.date = '2014-12-01'
5
5
  s.summary = 'Bootstrap your ChefSpec tests.'
6
6
  s.description = 'Automatically generate ChefSpec tests based on your recipes.'
@@ -9,8 +9,12 @@ Gem::Specification.new do |s|
9
9
  s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
10
10
  s.homepage = 'http://rubygems.org/gems/chefspec-bootstrap'
11
11
  s.license = 'Apache'
12
- s.executables = ['chefspec-bootstrap']
12
+ s.executables = ['chefspec-bootstrap']
13
13
  s.required_ruby_version = '>= 1.9'
14
14
 
15
- s.add_dependency 'chefspec', '~> 3.4.0'
15
+ s.add_dependency 'chefspec', '~> 4.0.0'
16
+
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'rspec', '~> 3.0'
19
+ s.add_development_dependency 'rubocop'
16
20
  end
@@ -6,11 +6,12 @@ require_relative 'api_map'
6
6
 
7
7
  module ChefSpec
8
8
  class Bootstrap
9
- def initialize(recipe, template_file, spec_helper_file, output_file)
9
+ def initialize(recipe, template_file, spec_helper_file, output_file, cookbook_path)
10
10
  @template_file = template_file
11
11
  @recipe = recipe
12
12
  @spec_helper_file = spec_helper_file || 'spec/spec_helper.rb'
13
13
  @output_file = output_file
14
+ @cookbook_path = cookbook_path || 'cookbooks'
14
15
  end
15
16
 
16
17
  def setup
@@ -34,7 +35,7 @@ module ChefSpec
34
35
  rescue LoadError
35
36
  @spec_helper = false
36
37
  ::RSpec.configure do |config|
37
- config.cookbook_path = ['cookbooks']
38
+ config.cookbook_path = [@cookbook_path]
38
39
  end
39
40
  end
40
41
  end
@@ -42,17 +43,15 @@ module ChefSpec
42
43
  def generate
43
44
  setup
44
45
 
45
- abort 'Output file already exists. Refusing to override.' if @output_file and File.exist?(@output_file)
46
+ abort 'Output file already exists. Refusing to override.' if @output_file && File.exist?(@output_file)
46
47
 
47
48
  erb = ERB.new(File.read(@template_file))
48
49
 
49
50
  path, recipe_file = File.split(@recipe)
50
51
  recipe = recipe_file.split('.')[0]
51
- cookbook = path.split(File::SEPARATOR)[1]
52
+ cookbook = path.split(File::SEPARATOR)[-2]
52
53
  chef_run = get_chef_run(cookbook, recipe)
53
54
 
54
- puts 'Chefspec execution failed. Generating generic spec.' unless chef_run
55
-
56
55
  resources = get_resources(chef_run, cookbook, recipe)
57
56
  test_cases = generate_test_cases(resources)
58
57
  spec_helper = @spec_helper
@@ -60,9 +59,7 @@ module ChefSpec
60
59
  spec_output = erb.result(binding)
61
60
 
62
61
  if @output_file
63
- File.open(@output_file, 'w') do |spec_file|
64
- spec_file.write(spec_output)
65
- end
62
+ generate_spec_file(spec_output)
66
63
  else
67
64
  puts spec_output
68
65
  end
@@ -72,9 +69,20 @@ module ChefSpec
72
69
  @root ||= Pathname.new(File.expand_path('../../', __FILE__))
73
70
  end
74
71
 
72
+ def generate_spec_file(output)
73
+ output_path = @output_file.split(File::SEPARATOR)
74
+ output_path.pop
75
+
76
+ FileUtils.mkpath(output_path.join(File::SEPARATOR)) if output_path
77
+
78
+ File.open(@output_file, 'w') do |spec_file|
79
+ spec_file.write(output)
80
+ end
81
+ end
82
+
75
83
  def get_chef_run(cookbook, recipe)
76
84
  return ChefSpec::Runner.new.converge("#{cookbook}::#{recipe}")
77
- rescue Exception => e
85
+ rescue StandardError
78
86
  return nil
79
87
  end
80
88
 
@@ -105,21 +113,19 @@ module ChefSpec
105
113
  test_cases = []
106
114
  resources.each do |resource|
107
115
  verbs = resource.action
108
- unless verbs.respond_to?(:each)
109
- verbs = [verbs]
110
- end
116
+ verbs = [verbs] unless verbs.respond_to?(:each)
111
117
 
112
118
  noun = resource.resource_name
113
119
  adjective = resource.name
114
120
 
115
121
  verbs.each do |verb|
116
- unless verb == :nothing
117
- test_cases.push(
118
- it: get_it_block(noun, verb, adjective),
119
- expect: get_expect_block(noun, verb),
120
- name: adjective
121
- )
122
- end
122
+ next if verb == :nothing
123
+
124
+ test_cases.push(
125
+ it: get_it_block(noun, verb, adjective),
126
+ expect: get_expect_block(noun, verb),
127
+ name: adjective
128
+ )
123
129
  end
124
130
  end
125
131
  test_cases
@@ -0,0 +1,13 @@
1
+ require_relative '../lib/chefspec_bootstrap'
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'Bootstrap' do
5
+ %w(failure directory package).each do |resource|
6
+ it "creates the expected spec file for #{resource}" do
7
+ bootstrap = ChefSpec::Bootstrap.new("spec/cookbooks/spec/recipes/#{resource}.rb", nil, nil, nil, nil)
8
+ template = File.open("spec/templates/#{resource}.txt", 'rb').read
9
+
10
+ expect { bootstrap.generate }.to output(template).to_stdout
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ directory 'defaultable'
2
+
3
+ directory 'identifiable' do
4
+ path 'nameable'
5
+ end
6
+
7
+ directory 'creatable' do
8
+ action :create
9
+ end
10
+
11
+ directory 'deletable' do
12
+ action :delete
13
+ end
@@ -0,0 +1 @@
1
+ failure
@@ -0,0 +1,25 @@
1
+ package 'defaultable'
2
+
3
+ package 'identifiable' do
4
+ package_name 'nameable'
5
+ end
6
+
7
+ package 'installable' do
8
+ action :install
9
+ end
10
+
11
+ package 'purgeable' do
12
+ action :purge
13
+ end
14
+
15
+ package 'reconfigurable' do
16
+ action :reconfig
17
+ end
18
+
19
+ package 'removeable' do
20
+ action :remove
21
+ end
22
+
23
+ package 'upgradable' do
24
+ action :upgrade
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ RSpec.configure do |config|
5
+ config.cookbook_path = ['spec/cookbooks']
6
+ config.raise_errors_for_deprecations!
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'chefspec'
2
+ require_relative '../spec_helper'
3
+
4
+ describe 'spec::directory' do
5
+ let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }
6
+
7
+ it "creates the defaultable directory" do
8
+ expect(chef_run).to create_directory("defaultable")
9
+ end
10
+
11
+ it "creates the identifiable directory" do
12
+ expect(chef_run).to create_directory("identifiable")
13
+ end
14
+
15
+ it "creates the creatable directory" do
16
+ expect(chef_run).to create_directory("creatable")
17
+ end
18
+
19
+ it "deletes the deletable directory" do
20
+ expect(chef_run).to delete_directory("deletable")
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ require 'chefspec'
2
+ require_relative '../spec_helper'
3
+
4
+ describe 'spec::failure' do
5
+ let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }
6
+ it 'runs successfully' do
7
+ expect{chef_run}.not_to raise_error
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ require 'chefspec'
2
+ require_relative '../spec_helper'
3
+
4
+ describe 'spec::package' do
5
+ let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }
6
+
7
+ it "installs the defaultable package" do
8
+ expect(chef_run).to install_package("defaultable")
9
+ end
10
+
11
+ it "installs the identifiable package" do
12
+ expect(chef_run).to install_package("identifiable")
13
+ end
14
+
15
+ it "installs the installable package" do
16
+ expect(chef_run).to install_package("installable")
17
+ end
18
+
19
+ it "purges the purgeable package" do
20
+ expect(chef_run).to purge_package("purgeable")
21
+ end
22
+
23
+ it "reconfigs the reconfigurable package" do
24
+ expect(chef_run).to reconfig_package("reconfigurable")
25
+ end
26
+
27
+ it "removes the removeable package" do
28
+ expect(chef_run).to remove_package("removeable")
29
+ end
30
+
31
+ it "upgrades the upgradable package" do
32
+ expect(chef_run).to upgrade_package("upgradable")
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chefspec-bootstrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Alexander Meng
@@ -13,17 +14,67 @@ dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: chefspec
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
19
- version: 3.4.0
21
+ version: 4.0.0
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
26
- version: 3.4.0
29
+ version: 4.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rubocop
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
27
78
  description: Automatically generate ChefSpec tests based on your recipes.
28
79
  email: alexbmeng@gmail.com
29
80
  executables:
@@ -33,37 +84,48 @@ extra_rdoc_files: []
33
84
  files:
34
85
  - .gitignore
35
86
  - .rubocop.yml
36
- - .rubocop_todo.yml
87
+ - .travis.yml
37
88
  - Gemfile
38
89
  - LICENSE
39
90
  - README.md
91
+ - Rakefile
40
92
  - bin/chefspec-bootstrap
41
93
  - chefspec-bootstrap.gemspec
42
94
  - lib/api_map.rb
43
- - lib/chefspec-bootstrap.rb
95
+ - lib/chefspec_bootstrap.rb
96
+ - spec/chefspec_bootstrap_spec.rb
97
+ - spec/cookbooks/spec/recipes/directory.rb
98
+ - spec/cookbooks/spec/recipes/failure.rb
99
+ - spec/cookbooks/spec/recipes/package.rb
100
+ - spec/spec_helper.rb
101
+ - spec/templates/directory.txt
102
+ - spec/templates/failure.txt
103
+ - spec/templates/package.txt
44
104
  - templates/default.erb
45
105
  homepage: http://rubygems.org/gems/chefspec-bootstrap
46
106
  licenses:
47
107
  - Apache
48
- metadata: {}
49
108
  post_install_message:
50
109
  rdoc_options: []
51
110
  require_paths:
52
111
  - lib
53
112
  required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
54
114
  requirements:
55
115
  - - ! '>='
56
116
  - !ruby/object:Gem::Version
57
117
  version: '1.9'
58
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
59
120
  requirements:
60
121
  - - ! '>='
61
122
  - !ruby/object:Gem::Version
62
123
  version: '0'
63
124
  requirements: []
64
125
  rubyforge_project:
65
- rubygems_version: 2.2.2
126
+ rubygems_version: 1.8.25
66
127
  signing_key:
67
- specification_version: 4
128
+ specification_version: 3
68
129
  summary: Bootstrap your ChefSpec tests.
69
130
  test_files: []
131
+ has_rdoc:
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YTA2YjJjMWJkYTNmNDU4OGNlOWFmMGVlNDBjMzdiYTRlMjk5OTRkMg==
5
- data.tar.gz: !binary |-
6
- Y2I2OTdlNmNkZDYzMTA4MThhMjY3M2U2OGNlOWVjZGRlZTBiZjExMA==
7
- SHA512:
8
- metadata.gz: !binary |-
9
- NDE1Y2NjYTljNjJlMTA4NjkzZGM2ODU4ZTNjNmFlMTZlMzMyZTFhZWJkNTU4
10
- NzM2MDE0N2NiMTI1NGYwZTM0MWQwZDZjN2RhY2EyMGQxZTQ1NDA3YjE2OWI5
11
- M2JkMjlhYjExZGQ4ZGVhM2VjNmU1MWE1OGEzNzIxZGE5MzE4N2U=
12
- data.tar.gz: !binary |-
13
- MTNkYzA3ZmY3MDQxYjk3NmMzZGI5NWJkYWVkZWIwYzI1OWRjMzgxNGQwYjk3
14
- NzQxYmE2YWFmZjFkZDEyMTIwNDM3NDQyYzFiMDRkODA1MzZkOWU1N2I5Nzg2
15
- ODQyY2FmNGVlNDMxYzcwODc3Zjc5MGNhMmI5MGFmZjc0OGM4NDM=
@@ -1,35 +0,0 @@
1
- # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2014-06-09 15:47:50 -0400 using RuboCop version 0.23.0.
3
- # The point is for the user to remove these configuration records
4
- # one by one as the offenses are removed from the code base.
5
- # Note that changes in the inspected code, or installation of new
6
- # versions of RuboCop, may require this file to be generated again.
7
-
8
- # Offense count: 1
9
- # Cop supports --auto-correct.
10
- Lint/RescueException:
11
- Enabled: false
12
-
13
- # Offense count: 2
14
- Lint/UselessAssignment:
15
- Enabled: false
16
-
17
- # Offense count: 1
18
- # Configuration parameters: CountComments.
19
- Style/ClassLength:
20
- Max: 134
21
-
22
- # Offense count: 2
23
- # Configuration parameters: Exclude.
24
- Style/FileName:
25
- Enabled: false
26
-
27
- # Offense count: 1
28
- # Configuration parameters: MaxLineLength.
29
- Style/IfUnlessModifier:
30
- Enabled: false
31
-
32
- # Offense count: 1
33
- # Configuration parameters: EnforcedStyle, SupportedStyles.
34
- Style/Next:
35
- Enabled: false