Permalink
Browse files

Update cookbook to latest standards

-  Refactor providers to support whyrun mode
-  Package custom ChefSpec matchers
-  Add Test Kitchen integration test
-  Update ChefSpec tests
-  Expand environment variables (like `$PATH`) (Fixes #6)
-  Add Rakefile for running tests
-  Use the latest and greatest testing strategies
  • Loading branch information...
1 parent 447b4b6 commit 15979e73e51067e9f73b19a5261ba58b04d0b21d @sethvargo sethvargo committed Jan 19, 2014
Showing with 411 additions and 282 deletions.
  1. +22 −16 .gitignore
  2. +27 −0 .kitchen.yml
  3. +0 −1 .rspec
  4. +4 −0 .rubocop.yml
  5. +3 −6 .travis.yml
  6. +8 −0 Berksfile
  7. +11 −1 CHANGELOG.md
  8. +8 −4 Gemfile
  9. +20 −12 README.md
  10. +38 −0 Rakefile
  11. +0 −3 Strainerfile
  12. +0 −1 attributes/default.rb
  13. +0 −7 chefignore
  14. +17 −0 libraries/matchers.rb
  15. +1 −1 metadata.rb
  16. +62 −21 providers/alias.rb
  17. +61 −23 providers/environment.rb
  18. +25 −14 resources/alias.rb
  19. +25 −14 resources/environment.rb
  20. +0 −22 spec/alias_spec.rb
  21. +0 −9 spec/cookbooks/alias/metadata.rb
  22. +0 −4 spec/cookbooks/alias/recipes/add_explicit.rb
  23. +0 −9 spec/cookbooks/environment/metadata.rb
  24. +0 −4 spec/cookbooks/environment/recipes/add_explicit.rb
  25. +0 −22 spec/environment_spec.rb
  26. +15 −0 spec/providers/alias_spec.rb
  27. +16 −0 spec/providers/environment_spec.rb
  28. +3 −10 spec/spec_helper.rb
  29. +0 −39 spec/support/alias.rb
  30. +0 −39 spec/support/environment.rb
  31. +2 −0 test/fixtures/cookbooks/magic_shell_alias/metadata.rb
  32. 0 ...ookbooks/alias/recipes/add_implicit.rb → test/fixtures/cookbooks/magic_shell_alias/recipes/add.rb
  33. +5 −0 test/fixtures/cookbooks/magic_shell_alias/recipes/remove.rb
  34. +2 −0 test/fixtures/cookbooks/magic_shell_environment/metadata.rb
  35. 0 ...ironment/recipes/add_implicit.rb → test/fixtures/cookbooks/magic_shell_environment/recipes/add.rb
  36. +5 −0 test/fixtures/cookbooks/magic_shell_environment/recipes/remove.rb
  37. +6 −0 test/integration/magic_shell_alias_add/serverspec/assert_added_spec.rb
  38. +5 −0 test/integration/magic_shell_alias_remove/serverspec/assert_removed_spec.rb
  39. +6 −0 test/integration/magic_shell_environment_add/serverspec/assert_added_spec.rb
  40. +5 −0 test/integration/magic_shell_environment_remove/serverspec/assert_removed_spec.rb
  41. +9 −0 test/shared/spec_helper.rb
View
@@ -1,17 +1,23 @@
-*.swp
-.rvmrc
-
-.idea
-.yardoc
-*.gem
-*.html
-*.rbc
-*.swp
-coverage
-doc
-tmp
+*~
+*#
+.#*
+\#*#
+.*.sw[a-z]
+*.un~
+*.tmp
+*.bk
+*.bkup
+.kitchen.local.yml
+Berksfile.lock
Gemfile.lock
-gemfiles/*.lock
-.bundle
-bin
-vendor
+
+.bundle/
+.cache/
+.kitchen/
+.vagrant/
+.vagrant.d/
+.yardoc/
+bin/
+doc/
+tmp/
+vendor/
View
@@ -0,0 +1,27 @@
+driver:
+ name: vagrant
+
+provisioner:
+ name: chef_solo
+ data_path: test/shared
+
+platforms:
+ - name: ubuntu-12.04
+ - name: centos-6.4
+
+suites:
+ #
+ # Alias suites
+ #
+ - name: magic_shell_alias_add
+ run_list: magic_shell_alias::add
+ - name: magic_shell_alias_remove
+ run_list: magic_shell_alias::remove
+
+ #
+ # Environment suites
+ #
+ - name: magic_shell_environment_add
+ run_list: magic_shell_environment::add
+ - name: magic_shell_environment_remove
+ run_list: magic_shell_environment::remove
View
1 .rspec
@@ -1 +0,0 @@
---color
View
@@ -0,0 +1,4 @@
+Encoding:
+ Enabled: false
+LineLength:
+ Enabled: false
View
@@ -1,9 +1,6 @@
-language: ruby
rvm:
- - 1.9.2
- 1.9.3
-env:
- - CHEF_VERSION='~> 10.0'
- - CHEF_VERSION='~> 11.0'
+ - 2.0.0
+ - 2.1.0
script:
- - bundle exec strainer test
+ - bundle exec rake travis:ci
View
@@ -0,0 +1,8 @@
+source 'https://api.berkshelf.com'
+
+group :integration do
+ cookbook 'magic_shell_alias', path: 'test/fixtures/cookbooks/magic_shell_alias'
+ cookbook 'magic_shell_environment', path: 'test/fixtures/cookbooks/magic_shell_environment'
+end
+
+metadata
View
@@ -1,8 +1,18 @@
magic_shell CHANGELOG
=====================
+1.0.0
+-----
+* Refactor providers to support whyrun mode
+* Package custom ChefSpec matchers
+* Add Test Kitchen integration test
+* Update ChefSpec tests
+* Expand environment variables (like `$PATH`)
+* Add Rakefile for running tests
+* Use the latest and greatest testing strategies
+
0.3.1, 0.3.2
------------
+------------
* **No changes** - chefigonre with `knife cookbook site share` was being ignored, so unnecessary artifacts were uploaded
View
12 Gemfile
@@ -1,9 +1,13 @@
source 'https://rubygems.org'
-gem 'chef', ENV['CHEF_VERSION'] || '>= 10.0'
+gem 'rake'
group :test do
- gem 'chefspec', '~> 1.0.0'
- gem 'foodcritic', '~> 2.1.0'
- gem 'strainer', '~> 2.0.0'
+ gem 'berkshelf', '~> 3.0.0.beta'
+ gem 'chefspec', '~> 3.0'
+ gem 'foodcritic', '~> 3.0'
+ gem 'rubocop', '~> 0.16'
+
+ gem 'test-kitchen', '~> 1.1'
+ gem 'kitchen-vagrant', '~> 0.14'
end
View
@@ -4,14 +4,6 @@ magic_shell Cookbook
Provides utility for adding some syntactic sugar to your shell.
-Requirements
-------------
-None
-
-Attributes
-----------
-None
-
Usage
-----
Update the `metadata.rb` for your cookbook to depend on magic_shell
@@ -63,8 +55,24 @@ Contributing
3. Code, document, write specs, test
4. Submit a PR
-License and Authors
--------------------
-Author: [Nathen Harvey](https://github.com/nathenharvey) ([@nathenharvey](https://twitter.com/nathenharvey))
-Copyright 2012, CustomInk, LLC
+License & Authors
+-----------------
+- Author: Nathen Harvey <nharvey@customink.com>
+- Author: Seth Vargo <sethvargo@gmail.com>
+
+```text
+Copyright 2012-2014 CustomInk, LLC.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+```
View
@@ -0,0 +1,38 @@
+require 'bundler/setup'
+
+namespace :style do
+ require 'rubocop/rake_task'
+ desc 'Run Ruby style checks'
+ Rubocop::RakeTask.new(:ruby)
+
+ require 'foodcritic'
+ desc 'Run Chef style checks'
+ FoodCritic::Rake::LintTask.new(:chef)
+end
+
+desc 'Run all style checks'
+task style: ['style:chef', 'style:ruby']
+
+require 'rspec/core/rake_task'
+desc 'Run ChefSpec unit tests'
+RSpec::Core::RakeTask.new(:unit) do |t|
+ t.rspec_opts = '--color --format progress'
+end
+
+require 'kitchen'
+desc 'Run Test Kitchen integration tests'
+task :integration do
+ Kitchen.logger = Kitchen.default_file_logger
+ Kitchen::Config.new.instances.each do |instance|
+ instance.test(:always)
+ end
+end
+
+# We cannot run Test Kitchen on Travis CI yet...
+namespace :travis do
+ desc 'Run tests on Travis'
+ task ci: ['style', 'unit']
+end
+
+# The default rake task should just run it all
+task default: ['style', 'unit', 'integration']
View
@@ -1,3 +0,0 @@
-knife: bundle exec knife cookbook test $COOKBOOK
-foodcritic: bundle exec foodcritic $SANDBOX/$COOKBOOK
-rspec: (cd $COOKBOOK && bundle exec rspec --color --format documentation)
View
@@ -1 +0,0 @@
-default['magic_shell']['environment'] = {}
View
@@ -1,7 +0,0 @@
-spec/
-.gitignore
-.rspec
-.travis.yml
-Gemfile
-Gemfile.lock
-Strainerfile
View
@@ -0,0 +1,17 @@
+if defined?(ChefSpec)
+ def add_magic_shell_alias(name)
+ ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_alias, :add, name)
+ end
+
+ def remove_magic_shell_alias(name)
+ ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_alias, :remove, name)
+ end
+
+ def add_magic_shell_environment(name)
+ ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_environment, :add, name)
+ end
+
+ def remove_magic_shell_environment(name)
+ ChefSpec::Matchers::ResourceMatcher.new(:magic_shell_environment, :remove, name)
+ end
+end
View
@@ -4,4 +4,4 @@
license 'Apache 2.0'
description 'Installs/Configures command_alias'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
-version '0.3.2'
+version '1.0.0'
View
@@ -1,27 +1,68 @@
+#
+# Cookbook Name:: magic_shell
+# Provider:: alias
+#
+# Author:: Nathen Harvey <nharvey@customink.com>
+# Author:: Seth Vargo <sethvargo@gmail.com>
+#
+# Copyright 2012-2014, CustomInk, LLC.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+# Delegate update actions to inline resources
+#
+use_inline_resources
+
+#
+# This provider supports why-run mode.
+#
+def whyrun_supported?
+ true
+end
+
+#
+# Create a file entry for the given alias.
+#
action :add do
- command_name = new_resource.alias_name.gsub(/ /,"_")
- if !new_resource.command.nil?
- Chef::Log.info("Adding #{command_name}.sh to /etc/profile.d/")
- file_contents = "# This alias was generated by Chef for #{node["fqdn"]}\n"
- file_contents += "alias #{command_name}='#{new_resource.command}'"
- resource = file "/etc/profile.d/#{command_name}.sh" do
- owner "root"
- group "root"
- mode "0755"
- content file_contents
- action :nothing
- end
- resource.run_action(:create)
- new_resource.updated_by_last_action(true) if resource.updated_by_last_action?
- end
+ file = Chef::Resource::File.new(destination, run_context)
+ file.owner('root')
+ file.group('root')
+ file.mode('0755')
+ file.content(<<-EOH.gsub(/^ {8}/, ''))
+ #
+ # This file was generated by Chef for #{node['fqdn']}
+ # Do NOT modify this file by hand!
+ #
+
+ alias #{new_resource.alias_name}="#{new_resource.command}"
+ EOH
+ file.run_action(:create)
end
+#
+# Delete the file entry for the given alias.
+#
action :remove do
- command_name = new_resource.alias_name.gsub(/ /,"_")
- resource = file "/etc/profile.d/#{command_name}.sh" do
- action :nothing
- end
+ file = Chef::Resource::File.new(destination, run_context)
+ file.run_action(:delete)
+end
+
+def destination
+ "/etc/profile.d/#{filename}.sh"
+end
- resource.run_action(:delete)
- new_resource.updated_by_last_action(true) if resource.updated_by_last_action?
+def filename
+ new_resource.alias_name.to_s.gsub(/\s/, '_')
end
Oops, something went wrong.

0 comments on commit 15979e7

Please sign in to comment.