Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base_context processing and processed logging methods #5

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/puppet/resource_api/base_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ def failing(titles, message: 'Failing')
end
end

def processing(titles, is, should, message: 'Processing')
def processing(title, is, should, message: 'Processing')
raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each)
start_time = Time.now
setup_context(titles, message)
setup_context(title, message)
begin
debug("Starting processing of #{titles} from #{is} to #{should}")
debug("Starting processing of #{title} from #{is} to #{should}")
yield
notice("Finished processing #{titles} in #{format_seconds(Time.now - start_time)} seconds: #{should}")
notice("Finished processing #{title} in #{format_seconds(Time.now - start_time)} seconds: #{should}")
rescue StandardError => e
err("Failed processing #{titles} after #{format_seconds(Time.now - start_time)} seconds: #{e}")
err("Failed processing #{title} after #{format_seconds(Time.now - start_time)} seconds: #{e}")
raise
ensure
@context = nil
Expand All @@ -71,6 +72,11 @@ def processing(titles, is, should, message: 'Processing')
end
end

def processed(title, is, should)
raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each)
notice("Processed #{title} from #{is} to #{should}")
end

def attribute_changed(title, attribute, is, should, message: nil)
raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each)
printable_is = 'nil'
Expand Down
17 changes: 16 additions & 1 deletion spec/puppet/resource_api/base_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,22 @@ def send_log(l, m)
end
end

describe '#processing(titles, is, should, message: \'Processing\', &block)' do
describe '#processed(title, is, should)' do
it 'logs the successful change of attributes' do
expect(context).to receive(:send_log).with(:notice, %r{Processed Thing\[one\] from {:ensure=>:absent} to {:ensure=>:present, :name=>\"thing one\"}})
context.processed('Thing[one]', { ensure: :absent }, { ensure: :present, name: 'thing one' })
end

it 'raises if multiple titles are passed' do
expect { context.processed(['Thing[one]', 'Thing[two'], { foo: 'bar' }, { foo: 'baz' }) }.to raise_error('processed only accepts a single resource title')
end
end

describe '#processing(title, is, should, message: \'Processing\', &block)' do
it 'raises if multiple titles are passed' do
expect { context.processing(['Thing[one]', 'Thing[two'], { foo: 'bar' }, { foo: 'baz' }) { puts 'Doing it' } }.to raise_error('processing only accepts a single resource title')
end

it 'logs the start message' do
allow(context).to receive(:send_log)
expect(context).to receive(:send_log).with(:debug, %r{starting processing of.*some_title.*}i)
Expand Down