Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

TDDing tmux

TDDing tmux

Seth Vargo

April 04, 2013
Tweet

More Decks by Seth Vargo

Other Decks in Technology

Transcript

  1. How to apply the testing cookbooks to chef know in

    Ruby stuff you already because Chef is just Ruby and you already know Ruby, so strap in
  2. source 'https://rubygems.org' gem 'chef' gem 'chefspec' gem 'fauxhai' gem 'foodcritic'

    gem 'strainer' # Guard gem 'guard' gem 'guard-bundler' gem 'guard-rspec' gem 'rb-fsevent' gem 'terminal-notifier-guard' Gemfile
  3. $ knife cookbook create tmux ** Creating cookbook tmux **

    Creating README for cookbook: tmux ** Creating CHANGELOG for cookbook: tmux ** Creating metadata for cookbook: tmux
  4. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end end spec/default_spec.rb
  5. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf') end end spec/default_spec.rb
  6. $ rspec cookbooks/tmux/spec/default_spec.rb Failures: 1) tmux::default should install the tmux

    package Failure/Error: let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } TypeError: can't convert Symbol into Integer # ./cookbooks/bluepill/attributes/default.rb: 18:in `[]' # ./cookbooks/bluepill/attributes/default.rb: 18:in `from_file' # ./cookbooks/tmux/spec/default_spec.rb:4:in `block (2 levels) in <top (required)>' # ./cookbooks/tmux/spec/default_spec.rb:7:in `block (2 levels) in <top (required)>' 2) tmux::default should create the tmux.conf file Failure/Error: let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') }
  7. require 'spec_helper' describe 'tmux::default' do before { Fauxhai.mock(platform: 'ubuntu', version:

    '12.04') } let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } end Before
  8. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new( platform: 'ubuntu',

    version: '12.04' ).converge('tmux::default') } it 'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf') end end After
  9. $ rspec cookbooks/tmux/spec/default_spec.rb Failures: 1) tmux::default should install the tmux

    package Failure/Error: runner.should install_package 'tmux' No package resource named 'tmux' with action :install found. # ./cookbooks/tmux/spec/default_spec.rb:8:in `block (2 levels) in <top (required)>' 2) tmux::default should create the tmux.conf file Failure/Error: runner.should create_file('/etc/ tmux.conf') No file resource named '/etc/tmux.conf' with action :create found. # ./cookbooks/tmux/spec/default_spec.rb:12:in `block (2 levels) in <top (required)>' Finished in 0.14994 seconds 2 examples, 2 failures
  10. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do watch(%r{^spec/.+_spec\.rb$}) watch('spec/spec_helper.rb') { 'spec'

    } watch(%r{^cookbooks/(.+)/recipes/(.+)\.rb$}) do |m| "cookbooks/#{m[1]}/spec/#{m[2]}_spec.rb" end end Guardfile
  11. guard 'rspec', spec_paths: ['spec', 'cookbooks/*/spec'] do watch(%r{^spec/.+_spec\.rb$}) watch('spec/spec_helper.rb') { 'spec'

    } watch(%r{^cookbooks/(.+)/recipes/(.+)\.rb$}) do |m| "cookbooks/#{m[1]}/spec/#{m[2]}_spec.rb" end watch(%r{^cookbooks/([A-Za-z]+)/(.+)(\..*)?$}) do |m| "cookbooks/#{m[1]}/spec/*.rb" end end Guardfile
  12. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs Failures: 1) tmux::default should install the tmux package Failure/Error: runner.should install_package 'tmux' No package resource named 'tmux' with action :install found. # ./cookbooks/tmux/spec/default_spec.rb:8:in `block (2 levels) in <top (required)>' 2) tmux::default should create the tmux.conf file Failure/Error: runner.should create_file('/etc/ tmux.conf') No file resource named '/etc/tmux.conf' with action :create found. # ./cookbooks/tmux/spec/default_spec.rb:12:in `block (2 levels) in <top (required)>'
  13. # Install the tmux package package 'tmux' # Drop the

    tmux configuration template template '/etc/tmux.conf' do source 'tmux.conf.erb' mode '0644' end recipes/default.rb
  14. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs ** Finished in 0.0003 seconds 2 examples, 0 failures
  15. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf') end end spec/default_spec.rb
  16. require 'spec_helper' describe 'tmux::default' do let(:runner) { ChefSpec::ChefRunner.new.converge('tmux::default') } it

    'installs the tmux package' do expect(runner).to install_package('tmux') end it 'creates the tmux.conf file' do expect(runner).to create_file('/etc/tmux.conf'). with_content('set -g prefix C-a') end end spec/default_spec.rb
  17. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs *F Failures: 1) tmux::default should create the tmux.conf file Failure/Error: runner.should create_file('/etc/ tmux.conf') No source template named 'tmux.conf.erb' found in the default locations. # ./cookbooks/tmux/spec/default_spec.rb:12:in `block (2 levels) in <top (required)>' Finished in 0.15691 seconds 2 examples, 1 passed, 1 failures
  18. # Use a better prefix: set -g prefix C-a unbind

    C-b # Change the default delay: set -sg escape-time 1 # Set the window and panes index set -g base-index 1 setw -g pane-base-index 1 # Send prefix to other apps: bind C-a send-prefix # Split windows with more logical keys bind | split-window -h bind - split-window -v # Remap movement keys bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R templates/default/tmux.conf.erb
  19. $ guard Guard is now watching at '[...]/chef-repo' Guard::RSpec is

    running Running all specs ** Finished in 0.0003 seconds 2 examples, 0 failures
  20. $ foodcritic -I my-rules/* cookbooks/tmux FC045: Consider setting cookbook name

    in metadata: cookbooks/tmux/metadata.rb:1 $ echo $? 0
  21. $ foodcritic -I my-rules/* -f any cookbooks/tmux FC045: Consider setting

    cookbook name in metadata: cookbooks/tmux/metadata.rb:1 $ echo $? 3
  22. name 'tmux' maintainer 'everyone' maintainer_email '[email protected]' license 'apache2' description 'Installs

    and configures tmux' long_description # ... version '0.1.0' metadata.rb
  23. $ knife cookbook test tmux checking tmux Running syntax check

    on tmux Validating ruby files Validating templates
  24. knife: knife cookbook test $COOKBOOK foodcritic: foodcritic -f any $SANDBOX/$COOKBOOK

    chefspec: bundle exec rspec $SANDBOX/$COOKBOOK Strainerfile
  25. $ strainer test tmux # Straining 'tmux (v0.0.1)' knife |

    knife cookbook test tmux knife | checking tmux knife | Running syntax check on tmux knife | Validating ruby files knife | Validating templates knife | SUCCESS! foodcritic | foodcritic -f any /Users/ sethvargo/Development/chef-is/cookbooks/tmux foodcritic | SUCCESS! chefspec | bundle exec rspec /Users/ sethvargo/Development/chef-is/cookbooks/tmux chefspec | No examples found. chefspec | Finished in 0.00006 seconds chefspec | 0 examples, 0 failures chefspec | SUCCESS!
  26. #!/bin/bash set -e set +x git submodule update --init 2>&1

    > /dev/null bundle install --quiet rm -Rf .colander strainer test `echo $JOB_NAME | sed 's/_cookbook//'` script/ci