From: Akshay Mankar Date: Tue, 8 Jan 2013 16:57:22 +0000 (+0530) Subject: Added spec for Config X-Git-Url: https://pwan.org/git/?p=vagrant-dotfiles.git;a=commitdiff_plain;h=2a7fb02fbf510e8475a65ed5a76b879aa4cb841e Added spec for Config --- diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..5f16476 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format progress diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..512007a --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm use 1.9.3-p194@copy_my_conf --create diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..6fade9d --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source "https://rubygems.org" + +ruby "1.9.3" + +group :test do + gem "rspec" +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..17cb813 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,18 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.1.3) + rspec (2.11.0) + rspec-core (~> 2.11.0) + rspec-expectations (~> 2.11.0) + rspec-mocks (~> 2.11.0) + rspec-core (2.11.1) + rspec-expectations (2.11.3) + diff-lcs (~> 1.1.3) + rspec-mocks (2.11.3) + +PLATFORMS + ruby + +DEPENDENCIES + rspec diff --git a/lib/boot.rb b/lib/boot.rb new file mode 100644 index 0000000..4a3b432 --- /dev/null +++ b/lib/boot.rb @@ -0,0 +1 @@ +require "copy_my_conf/config" \ No newline at end of file diff --git a/lib/copy_my_conf.rb b/lib/copy_my_conf.rb index 27d4bbf..c10b814 100644 --- a/lib/copy_my_conf.rb +++ b/lib/copy_my_conf.rb @@ -1,73 +1,71 @@ -class CopyMyConf < Vagrant::Provisioners::Base +require 'boot' +module Vagrant + module Provisioners + class CopyMyConf < Base - def prepare - prepare_vim if config.vim - prepare_git if config.git - prepare_ssh if config.ssh - end + def prepare + prepare_vim if config.vim + prepare_git if config.git + prepare_ssh if config.ssh + end - def provision! - channel = env[:vm].channel - provision_ssh(channel) if config.ssh - provision_vim(channel) if config.vim - provision_git(channel) if config.git - end + def provision! + channel = env[:vm].channel + provision_ssh(channel) if config.ssh + provision_vim(channel) if config.vim + provision_git(channel) if config.git + end - def self.config_class - Config - end + def self.config_class + Config + end - class Config < Vagrant::Config::Base - attr_accessor :ssh - attr_accessor :vim - attr_accessor :git - attr_accessor :user_home - end + private -private + def tmp_root + "/tmp/copy_my_conf" + end - def tmp_root - "/tmp/copy_my_conf" - end - - def prepare_ssh - env[:vm].config.vm.share_folder("ssh", "#{tmp_root}/ssh", "~/.ssh") - end + def prepare_ssh + env[:vm].config.vm.share_folder("ssh", "#{tmp_root}/ssh", "~/.ssh") + end - def prepare_git - `mkdir -p #{tmp_root}/git` - `cp ~/.gitconfig #{tmp_root}/git/` - env[:vm].config.vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git") - end + def prepare_git + `mkdir -p #{tmp_root}/git` + `cp ~/.gitconfig #{tmp_root}/git/` + env[:vm].config.vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git") + end - def prepare_vim - `mkdir -p #{tmp_root}/vim` - ["~/.vimrc", "~/.vim"].each do |file| - `cp -r #{file} #{tmp_root}/vim` - end - env[:vm].config.vm.share_folder("vim", "#{tmp_root}/vim/", "#{tmp_root}/vim") - end + def prepare_vim + `mkdir -p #{tmp_root}/vim` + ["~/.vimrc", "~/.vim"].each do |file| + `cp -r #{file} #{tmp_root}/vim` + end + env[:vm].config.vm.share_folder("vim", "#{tmp_root}/vim/", "#{tmp_root}/vim") + end - def provision_git(channel) - puts "Copying your gitconfig" - channel.execute("cp #{tmp_root}/git/.gitconfig ~/") - end + def provision_git(channel) + puts "Copying your gitconfig" + channel.execute("cp #{tmp_root}/git/.gitconfig ~/") + end - def provision_vim(channel) - puts "Copying your vim configuratios" - channel.execute("rm -rf #{user_home}/.vim*") - channel.execute("cp -r #{tmp_root}/vim/.??* ~/") - end + def provision_vim(channel) + puts "Copying your vim configuratios" + channel.execute("rm -rf #{user_home}/.vim*") + channel.execute("cp -r #{tmp_root}/vim/.??* ~/") + end - def provision_ssh(channel) - puts "Copying your ssh keys and config" - channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached") - channel.execute("[[ -f #{user_home}/.ssh/authorized_keys ]] && mv #{user_home}/.ssh/authorized_keys #{tmp_root}/cached") - channel.execute("cp #{tmp_root}/ssh/* #{user_home}/.ssh") - channel.execute("cat #{tmp_root}/cached/authorized_keys >> #{user_home}/.ssh/authorized_keys") # So that `vagrant ssh` doesn't ask for password - end + def provision_ssh(channel) + puts "Copying your ssh keys and config" + channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached") + channel.execute("[[ -f #{user_home}/.ssh/authorized_keys ]] && mv #{user_home}/.ssh/authorized_keys #{tmp_root}/cached") + channel.execute("cp #{tmp_root}/ssh/* #{user_home}/.ssh") + channel.execute("cat #{tmp_root}/cached/authorized_keys >> #{user_home}/.ssh/authorized_keys") # So that `vagrant ssh` doesn't ask for password + end - def user_home - config.user_home || "/home/vagrant" + def user_home + config.user_home || "/home/vagrant" + end + end end end diff --git a/lib/copy_my_conf/config.rb b/lib/copy_my_conf/config.rb new file mode 100644 index 0000000..b077607 --- /dev/null +++ b/lib/copy_my_conf/config.rb @@ -0,0 +1,20 @@ +module Vagrant + module Provisioners + class CopyMyConf < Base + class Config < Vagrant::Config::Base + def self.all_attributes + [:ssh, :vim, :git] + end + attr_accessor *all_attributes + attr_accessor :user_home + + def all_true + self.class.all_attributes.collect do |attr| + self.send(attr) ? attr : nil + end.compact + end + end + end + end +end + diff --git a/spec/copy_my_conf/config_spec.rb b/spec/copy_my_conf/config_spec.rb new file mode 100644 index 0000000..4a2ffdc --- /dev/null +++ b/spec/copy_my_conf/config_spec.rb @@ -0,0 +1,17 @@ +require "spec_helper" +module Vagrant::Provisioners + class CopyMyConf < Base + describe Config do + it "should list all the true attributes" do + config = Config.new + + config.vim = true + config.ssh = true + + all_true_attributes = config.all_true + all_true_attributes.should =~ [:vim, :ssh] + end + end + end +end + diff --git a/spec/copy_my_conf_spec.rb b/spec/copy_my_conf_spec.rb new file mode 100644 index 0000000..fb2dcb9 --- /dev/null +++ b/spec/copy_my_conf_spec.rb @@ -0,0 +1,18 @@ +require "spec_helper" + +module Vagrant + module Provisioners + describe CopyMyConf do + xit "should prepare provisioning process" do + copy_my_conf = CopyMyConf.new + + config = CopyMyConf.config_class.new + config.vim = true + + copy_my_conf.stub(:config).and_return(config) + + CopyMyConf::Vim.any_instance.should_receive(:prepare) + end + end + end +end diff --git a/spec/fixtures.rb b/spec/fixtures.rb new file mode 100644 index 0000000..84f3e9c --- /dev/null +++ b/spec/fixtures.rb @@ -0,0 +1,13 @@ +module Vagrant + module Provisioners + class Base + end + end + + module Config + class Base + end + end +end + + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..876582f --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,5 @@ +require 'rspec' +require "fixtures" +$:<