Skip to content

Commit

Permalink
Update cookbook to latest standards
Browse files Browse the repository at this point in the history
-  Refactor providers to support whyrun mode
-  Package custom ChefSpec matchers
-  Add Test Kitchen integration test
-  Update ChefSpec tests
-  Expand environment variables (like `$PATH`) (#6)
-  Add Rakefile for running tests
-  Use the latest and greatest testing strategies
  • Loading branch information
sethvargo committed Jan 19, 2014
1 parent 447b4b6 commit 9ab7b5d
Show file tree
Hide file tree
Showing 41 changed files with 413 additions and 282 deletions.
38 changes: 22 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
27 changes: 27 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion .rspec

This file was deleted.

6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AllCops:
Excludes:
- vendor/**

LineLength:
Enabled: false
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 8 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <[email protected]>
- Author: Seth Vargo <[email protected]>

```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.
```
38 changes: 38 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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']
3 changes: 0 additions & 3 deletions Strainerfile

This file was deleted.

1 change: 0 additions & 1 deletion attributes/default.rb

This file was deleted.

7 changes: 0 additions & 7 deletions chefignore

This file was deleted.

17 changes: 17 additions & 0 deletions libraries/matchers.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
83 changes: 62 additions & 21 deletions providers/alias.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
#
# Cookbook Name:: magic_shell
# Provider:: alias
#
# Author:: Nathen Harvey <[email protected]>
# Author:: Seth Vargo <[email protected]>
#
# 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
Loading

0 comments on commit 9ab7b5d

Please sign in to comment.