Skip to content

Commit

Permalink
base_context processing and processed logging methods
Browse files Browse the repository at this point in the history
Adds processed()
Updates processing() to only accept a single resource title, and to raise if multiple are passed.
  • Loading branch information
James Stocks committed Oct 11, 2017
1 parent e7ea3bb commit c6fdb9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
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

0 comments on commit c6fdb9b

Please sign in to comment.