Skip to content

Commit

Permalink
Syntax/lint checks / unit tests / integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djoos committed Aug 4, 2014
1 parent 1bdab48 commit 52bc93f
Show file tree
Hide file tree
Showing 20 changed files with 525 additions and 113 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*~
*#
.#*
\#*#
.*.sw[a-z]
*.un~

Berksfile.lock
Gemfile.lock
.vagrant
Vagrantfile
.bundle
.coverage
.kitchen
.kitchen.local.yml
16 changes: 16 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
driver_plugin: vagrant
driver_config:
require_chef_omnibus: true

platforms:
- name: ubuntu-12.04
driver_config:
box: opscode-ubuntu-12.04
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box

suites:
- name: default
run_list:
- recipe[gpac::default]
attributes:
23 changes: 23 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
AlignParameters:
Enabled: false

FileName:
Enabled: false

Encoding:
Enabled: false

LineLength:
Max: 200

HashSyntax:
EnforcedStyle: hash_rockets

RedundantBegin:
Enabled: false

UselessAssignment:
Enabled: false

MethodLength:
Enabled: false
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: ruby

rvm:
- 1.9.3
- 2.0.0
- 2.1.0

before_script:
- bundle exec berks install

script:
- bundle exec rake travis

branches:
only:
- master

notifications:
email:
- [email protected]
3 changes: 3 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://api.berkshelf.com"

metadata
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Contributing
=========

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. [Add tests for your changes](https://github.com/escapestudios-cookbooks/gpac/blob/master/TESTING.md)
4. Push your changes to your feature branch (`git push origin my-new-feature`)
5. Create a new Pull Request (PR)

## Testing
Contributions will only be accepted if they are fully tested as specified in [TESTING.md](https://github.com/escapestudios-cookbooks/gpac/blob/master/TESTING.md).

## metadata.rb
Please do not modify the version number in the metadata.rb; not all changes to the cookbook may be merged and released in the same version. We will handle the version updates during the release process.
23 changes: 23 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
source 'https://rubygems.org'

gem 'rake'

group :lint do
gem 'rubocop', '~> 0.18'
gem 'foodcritic', '~> 3.0'
end

group :unit, :integration do
gem 'berkshelf', '~> 3.0'
end

group :unit do
gem 'chefspec', '~> 3.1'
gem 'rspec-expectations', '~> 2.14.0'
end

group :integration do
gem 'test-kitchen', '~> 1.2'
gem 'kitchen-vagrant', '~> 0.11'
gem 'serverspec', '~> 1.0'
end
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://secure.travis-ci.org/escapestudios-cookbooks/gpac.png)](http://travis-ci.org/escapestudios-cookbooks/gpac)

Description
===========

Expand Down Expand Up @@ -44,9 +46,9 @@ References
License and Authors
===================

Author: David Joos <[email protected]>
Author: David Joos <david.joos@escapestudios.com>
Author: Escape Studios Development <[email protected]>
Copyright: 2012-2013, Escape Studios
Copyright: 2012-2014, Escape Studios

Unless otherwise noted, all files are released under the MIT license,
possible exceptions will contain licensing information in them.
Expand Down
47 changes: 47 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'rspec/core/rake_task'

# syntax/lint checks: RuboCop & Foodcritic
namespace :lint do
require 'rubocop/rake_task'
require 'foodcritic'

desc 'Run Ruby syntax/lint checks'
RuboCop::RakeTask.new(:ruby)

desc 'Run Chef syntax/lint checks'
FoodCritic::Rake::LintTask.new(:chef) do |task|
task.options = {
:fail_tags => ['any']
}
end
end

desc 'Run all syntax/lint checks'
task :lint => ['lint:ruby', 'lint:chef']

# unit testing: ChefSpec
desc 'Run RSpec and ChefSpec unit tests'
RSpec::Core::RakeTask.new(:unit)

# integration testing: Test Kitchen
namespace :integration do
require 'kitchen'

desc 'Run Test Kitchen integration tests with Vagrant'
task :vagrant do
Kitchen.logger = Kitchen.default_file_logger
Kitchen::Config.new.instances.each do |instance|
instance.test(:always)
end
end
end

desc 'Run all integration tests'
task :integration => ['integration:vagrant']

# Travic CI
desc 'Run tests on Travis CI'
task :travis => [:lint, :unit]

# the default rake task should just run it all
task :default => [:lint, :unit, :integration]
47 changes: 47 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Testing the cookbook
====================

Contributions to this cookbook will only be accepted if all tests pass successfully:

* Ruby syntax/lint checks: [RuboCop](http://batsov.com/rubocop/)
* Chef syntax/lint checks: [Foodcritic](http://acrmp.github.io/foodcritic/)
* Unit tests: [ChefSpec](http://code.sethvargo.com/chefspec/)
* Integration tests: [Test Kitchen](http://kitchen.ci/)

Setting up the test environment
-------------------------------

Install the latest version of [Vagrant](http://www.vagrantup.com/downloads.html) and [VirtualBox](https://www.virtualbox.org/wiki/Downloads) (free) or [VMWare Fusion](http://www.vmware.com/products/fusion) (paid).

Clone the latest version of the cookbook from the repository.

git clone [email protected]:escapestudios-cookbooks/gpac.git
cd gpac

Install the gems used for testing:

bundle install

Install the berkshelf plugin for vagrant:

vagrant plugin install vagrant-berkshelf

Running syntax/lint checks
--------------------------

bundle exec rake lint

Running unit tests
------------------

bundle exec rake unit

Running integration tests
-------------------------

bundle exec rake integration

Running all checks/tests
------------------------

bundle exec rake
90 changes: 90 additions & 0 deletions Thorfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# encoding: utf-8

require 'bundler'
require 'bundler/setup'
require 'berkshelf/thor'

class Default < Thor
attr_reader :cookbook_name
attr_reader :cookbook_category

def initialize(*args)
@cookbook_name = "gpac"
@cookbook_category = "Utilities"

super(*args)
end

class_option :verbose,
:type => :boolean,
:aliases => "-v",
:default => false

method_option :knife_config,
:type => :string,
:aliases => "-c",
:desc => "Path to your knife configuration file",
:default => "~/.chef/knife.rb"

desc "release", "Create a tag from metadata version and push to the community site."
def release
unless clean?
say "Sorry, there are files that need to be committed first.", :red
exit 1
end

tag_version{ publish_cookbook(options) }
end

private
def current_version
Berkshelf::CachedCookbook.from_path(source_root).version
end

def clean?
sh_with_excode("git diff --exit-code")[1] == 0
end

def tag_version
sh "git tag -a -m \"#{current_version}\" #{current_version}"
say "Tagged: #{current_version}", :green
yield if block_given?
sh "git push --tags"
rescue => e
say "Untagging: #{current_version} due to error", :red
sh_with_excode "git tag -d #{current_version}"
say e , :red
say "Please increase the version in metadata.rb", :red
exit 1
end

def publish_cookbook(options)
cmd = "knife cookbook site share #{cookbook_name} \"#{cookbook_category}\" -o #{source_root.join("..")} -c #{options[:knife_config]}"
cmd << " -V" if options[:verbose]
sh cmd
say "Version #{current_version} of the #{cookbook_name} cookbook has been uploaded to the Opscode community site.", :green
end

def source_root
Pathname.new File.dirname(File.expand_path(__FILE__))
end

def sh(cmd, dir = source_root, &block)
out, code = sh_with_excode(cmd, dir, &block)
code == 0 ? out : raise(out.empty? ? "Running `#{cmd}` failed. Run this command directly for more detailed output." : out)
end

def sh_with_excode(cmd, dir = source_root, &block)
cmd << " 2>&1"
outbuf = ''

Dir.chdir(dir) {
outbuf = `#{cmd}`
if $? == 0
block.call(outbuf) if block
end
}

[ outbuf, $? ]
end
end
Loading

0 comments on commit 52bc93f

Please sign in to comment.