Skip to content

Commit

Permalink
Merge pull request #75 from fnichol/testify
Browse files Browse the repository at this point in the history
Update testing support and add unit tests for existing resources
  • Loading branch information
fnichol committed Mar 3, 2014
2 parents 5ca714d + d3338a0 commit a12077e
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 23 deletions.
16 changes: 3 additions & 13 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
---
driver_plugin: vagrant
driver_config:
require_chef_omnibus: true
driver:
name: vagrant

platforms:
- name: ubuntu-12.04
driver_config:
box: opscode-ubuntu-12.04
box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box
run_list:
- recipe[apt]
- name: ubuntu-10.04
driver_config:
box: opscode-ubuntu-10.04
box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box
run_list:
- recipe[apt]
- name: centos-6.4
driver_config:
box: opscode-centos-6.4
box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_centos-6.4_provisionerless.box
- name: centos-6.5

suites:
- name: system_ruby
Expand Down
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--default_path test/unit
--color
--format documentation
2 changes: 1 addition & 1 deletion Berksfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
site :opscode
source 'http://api.berkshelf.com'

metadata

Expand Down
14 changes: 8 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
source "https://rubygems.org"

gem 'rake'
gem 'foodcritic'
gem "berkshelf", "~> 3.0.0.beta7"
gem "chefspec"
gem "emeril"
gem "foodcritic", "~> 3.0"
gem "rake"

group :development do
gem 'emeril'
gem "guard-rspec"
end

group :integration do
gem 'berkshelf'
gem 'test-kitchen', '~> 1.0.0.alpha.6'
gem 'kitchen-vagrant'
gem "test-kitchen"
gem "kitchen-vagrant"
end
5 changes: 5 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
guard :rspec, spec_paths: ["test/unit"] do
watch(%r{^test/unit/.+_spec\.rb$})
watch(%r{^(libraries|providers|recipes|resources)/(.+)\.rb$}) { |m| "test/unit/#{m[1]}/#{m[2]}_spec.rb" }
watch("test/unit/spec_helper.rb") { "test/unit" }
end
9 changes: 7 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/env rake
require 'foodcritic'

begin
require 'emeril/rake'
rescue LoadError
puts ">>>>> Emeril gem not loaded, omitting tasks" unless ENV['CI']
end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = ["test/unit/**/*_spec.rb"]
end

require 'foodcritic'
FoodCritic::Rake::LintTask.new do |t|
t.options = { :fail_tags => ['any'] }
end
Expand All @@ -18,4 +23,4 @@ rescue LoadError
puts ">>>>> Kitchen gem not loaded, omitting tasks" unless ENV['CI']
end

task :default => [:foodcritic]
task :default => [:foodcritic, :unit]
2 changes: 1 addition & 1 deletion resources/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
attribute :returns, :kind_of => Array, :default => [ 0 ]
attribute :timeout, :kind_of => Integer
attribute :user, :kind_of => String
attribute :umask, :kind_of => String
attribute :umask, :kind_of => [String, Integer]

def initialize(*args)
super
Expand Down
118 changes: 118 additions & 0 deletions test/unit/resources/gem_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require "chef/resource/lwrp_base"
require "chef/provider/package/rubygems"
require_relative "../../../libraries/chef_rbenv_mixin"
require_relative "../../../libraries/chef_provider_package_rbenvrubygems"

unless Chef::Resource.const_defined?("RbenvGem")
Chef::Resource::LWRPBase.build_from_file(
"rbenv",
File.join(File.dirname(__FILE__), %w{.. .. .. resources gem.rb}),
nil
)
end

describe Chef::Resource::RbenvGem do

let(:resource) { described_class.new("creamcorn") }

it "sets the default attribute to package_name" do
expect(resource.package_name).to eq("creamcorn")
end

it "attribute rbenv_version defaults to global" do
expect(resource.rbenv_version).to eq("global")
end

it "attribute rbenv_version takes a String value" do
resource.rbenv_version("nextruby")
expect(resource.rbenv_version).to eq("nextruby")
end

it "attribute version defaults to nil" do
expect(resource.version).to be_nil
end

it "attribute version takes a String value" do
resource.version("1.0.99")
expect(resource.version).to eq("1.0.99")
end

it "attribute response_file defaults to nil" do
expect(resource.response_file).to be_nil
end

it "attribute response_file takes a String value" do
resource.response_file("/dont/think/so")
expect(resource.response_file).to eq("/dont/think/so")
end

it "attribute source defaults to nil" do
expect(resource.source).to be_nil
end

it "attribute source takes a String value" do
resource.source("outthere")
expect(resource.source).to eq("outthere")
end

it "attribute options defaults to nil" do
expect(resource.options).to be_nil
end

it "attribute options takes a String value" do
resource.options("--no-rdoc")
expect(resource.options).to eq("--no-rdoc")
end

it "attribute options takes a Hash value" do
resource.options({ one: "two" })
expect(resource.options).to eq({ one: "two" })
end

it "attribute gem_binary defaults to nil" do
expect(resource.gem_binary).to be_nil
end

it "attribute gem_binary takes a String value" do
resource.gem_binary("/my/fav/gem")
expect(resource.gem_binary).to eq("/my/fav/gem")
end

it "attribute user defaults to nil" do
expect(resource.user).to be_nil
end

it "attribute user takes a String value" do
resource.user("masha")
expect(resource.user).to eq("masha")
end

it "attribute root_path defaults to nil" do
expect(resource.root_path).to be_nil
end

it "attribute root_path takes a String value" do
resource.root_path("C:\\crazytown")
expect(resource.root_path).to eq("C:\\crazytown")
end

it "action defaults to :install" do
expect(resource.action).to eq(:install)
end

it "actions include :upgrade" do
expect(resource.allowed_actions).to include(:upgrade)
end

it "actions include :remove" do
expect(resource.allowed_actions).to include(:remove)
end

it "actions include :purge" do
expect(resource.allowed_actions).to include(:purge)
end

it "sets the provider to RbenvRubygems" do
expect(resource.provider).to eq(Chef::Provider::Package::RbenvRubygems)
end
end
49 changes: 49 additions & 0 deletions test/unit/resources/global_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "chef/resource/lwrp_base"

unless Chef::Resource.const_defined?("RbenvGlobal")
Chef::Resource::LWRPBase.build_from_file(
"rbenv",
File.join(File.dirname(__FILE__), %w{.. .. .. resources global.rb}),
nil
)
end

describe Chef::Resource::RbenvGlobal do

let(:resource) { described_class.new("antruby") }

it "sets the default attribute to rbenv_version" do
expect(resource.rbenv_version).to eq("antruby")
end

it "attribute user defaults to nil" do
expect(resource.user).to be_nil
end

it "attribute user takes a String value" do
resource.user("casper")
expect(resource.user).to eq("casper")
end

it "attribute root_path defaults to nil" do
expect(resource.root_path).to be_nil
end

it "attribute root_path takes a String value" do
resource.root_path("C:\\wintowne")
expect(resource.root_path).to eq("C:\\wintowne")
end

it "action defaults to :create" do
expect(resource.action).to eq(:create)
end

it "#to_s includes user if provided" do
resource.user("molly")
expect(resource.to_s).to eq("rbenv_global[antruby] (molly)")
end

it "#to_s includes system label if user is not provided" do
expect(resource.to_s).to eq("rbenv_global[antruby] (system)")
end
end
40 changes: 40 additions & 0 deletions test/unit/resources/rehash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "chef/resource/lwrp_base"

unless Chef::Resource.const_defined?("RbenvRehash")
Chef::Resource::LWRPBase.build_from_file(
"rbenv",
File.join(File.dirname(__FILE__), %w{.. .. .. resources rehash.rb}),
nil
)
end

describe Chef::Resource::RbenvRehash do

let(:resource) { described_class.new("yabba") }

it "sets the default attribute to name" do
expect(resource.name).to eq("yabba")
end

it "attribute user defaults to nil" do
expect(resource.user).to be_nil
end

it "attribute user takes a String value" do
resource.name("jerry")
expect(resource.name).to eq("jerry")
end

it "attribute root_path defaults to nil" do
expect(resource.root_path).to be_nil
end

it "attribute root_path takes a String value" do
resource.root_path("/nowhere/there")
expect(resource.root_path).to eq("/nowhere/there")
end

it "action defaults to :run" do
expect(resource.action).to eq(:run)
end
end
62 changes: 62 additions & 0 deletions test/unit/resources/ruby_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "chef/resource/lwrp_base"

unless Chef::Resource.const_defined?("RbenvRuby")
Chef::Resource::LWRPBase.build_from_file(
"rbenv",
File.join(File.dirname(__FILE__), %w{.. .. .. resources ruby.rb}),
nil
)
end

describe Chef::Resource::RbenvRuby do

let(:resource) { described_class.new("antruby") }

it "sets the default attribute to definition" do
expect(resource.definition).to eq("antruby")
end

it "attribute definition_file defaults to nil" do
expect(resource.definition_file).to be_nil
end

it "attribute definition_file takes a String value" do
resource.definition_file("/blah/blah/rubay")
expect(resource.definition_file).to eq("/blah/blah/rubay")
end

it "attribute root_path defaults to nil" do
expect(resource.root_path).to be_nil
end

it "attribute root_path takes a String value" do
resource.root_path("/rootness/path")
expect(resource.root_path).to eq("/rootness/path")
end

it "attribute user defaults to nil" do
expect(resource.user).to be_nil
end

it "attribute user takes a String value" do
resource.user("curious_george")
expect(resource.user).to eq("curious_george")
end

it "action defaults to :install" do
expect(resource.action).to eq(:install)
end

it "actions include :reinstall" do
expect(resource.allowed_actions).to include(:reinstall)
end

it "#to_s includes user if provided" do
resource.user("molly")
expect(resource.to_s).to eq("rbenv_ruby[antruby] (molly)")
end

it "#to_s includes system label if user is not provided" do
expect(resource.to_s).to eq("rbenv_ruby[antruby] (system)")
end
end
Loading

0 comments on commit a12077e

Please sign in to comment.