1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
require 'spec_helper'
require 'buff/config/ruby'
describe Buff::Config::Ruby do
let(:ruby) do
%(
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
cookbook_path ['cookbooks']
knife[:foo] = 'bar'
knife[:key] = "\#{current_dir}/key.pem"
)
end
let(:klass) do
Class.new(Buff::Config::Ruby) do
attribute :log_level
attribute :log_location
attribute :node_name, default: 'bacon'
attribute :cookbook_path
attribute :knife, default: {}
end
end
describe 'ClassMethods' do
subject { klass }
describe '.from_ruby' do
it 'returns an instance of the inheriting class' do
expect(subject.from_ruby(ruby)).to be_a(subject)
end
it 'assigns values for each defined attribute' do
config = subject.from_ruby(ruby)
expect(config[:log_level]).to eq(:info)
expect(config[:log_location]).to eq(STDOUT)
expect(config[:node_name]).to eq('bacon')
expect(config[:cookbook_path]).to eq(['cookbooks'])
expect(config[:knife][:foo]).to eq('bar')
end
it 'properly sets the calling file' do
config = subject.from_ruby(ruby, '/home/annie/.chef/knife.rb')
expect(config[:knife][:key]).to eq ('/home/annie/.chef/key.pem')
end
end
describe '::from_file' do
let(:filepath) { tmp_path.join('test_config.rb').to_s }
before { allow(File).to receive(:read).with(filepath).and_return(ruby) }
it 'returns an instance of the inheriting class' do
expect(subject.from_file(filepath)).to be_a(subject)
end
it 'sets the object\'s filepath to the path of the loaded filepath' do
expect(subject.from_file(filepath).path).to eq(filepath)
end
context 'given a filepath that does not exist' do
before { allow(File).to receive(:read).and_raise(Errno::ENOENT) }
it 'raises a Buff::Errors::ConfigNotFound error' do
expect {
subject.from_file(filepath)
}.to raise_error(Buff::Errors::ConfigNotFound)
end
end
end
end
subject { klass.new }
describe '#to_rb' do
it 'returns ruby with key values for each attribute' do
subject.log_level = :info
subject.log_location = STDOUT
subject.node_name = 'bacon'
subject.cookbook_path = ['cookbooks']
lines = subject.to_ruby.strip.split("\n")
expect(lines[0]).to eq('log_level(:info)')
expect(lines[1]).to eq('log_location(STDOUT)')
expect(lines[2]).to eq('node_name("bacon")')
expect(lines[3]).to eq('cookbook_path(["cookbooks"])')
end
end
describe '#from_ruby' do
it 'returns an instance of the updated class' do
expect(subject.from_ruby(ruby)).to be_a(Buff::Config::Ruby)
end
it 'assigns values for each defined attribute' do
config = subject.from_ruby(ruby)
expect(config[:log_level]).to eq(:info)
expect(config[:log_location]).to eq(STDOUT)
expect(config[:node_name]).to eq('bacon')
expect(config[:cookbook_path]).to eq(['cookbooks'])
end
end
describe '#save' do
it 'raises a ConfigSaveError if no path is set or given' do
subject.path = nil
expect {
subject.save
}.to raise_error(Buff::Errors::ConfigSaveError)
end
end
describe '#reload' do
before do
subject.path = 'foo/bar.rb'
allow(File).to receive(:read).and_return(ruby)
end
it 'returns self' do
expect(subject.reload).to eq(subject)
end
it 'updates the contents of self from disk' do
subject.log_level = :warn
subject.node_name = 'eggs'
expect(subject.log_level).to eq(:warn)
expect(subject.node_name).to eq('eggs')
subject.reload
expect(subject.log_level).to eq(:info)
expect(subject.node_name).to eq('bacon')
end
it 'raises ConfigNotFound if the path is nil' do
subject.path = nil
expect {
subject.reload
}.to raise_error(Buff::Errors::ConfigNotFound)
end
end
end
|