Please review pull request #160: Factoring out custom assertions, and adding assert_output. opened by (pvande)
Description:
This adds a powerful new assertion for acceptance testing, and introduces some code refactoring that makes it much easier for us to add new custom assertions in the future.
- Opened: Mon Mar 05 21:52:02 UTC 2012
- Based on: puppetlabs:master (cf36e5015635112d0e6eac9f7954920171759eab)
- Requested merge: pvande:assertions/assert_output (5f4b4c397420595ea0b91a296af5d6cff9ee7200)
Diff follows:
diff --git a/lib/assertions.rb b/lib/assertions.rb
new file mode 100644
index 0000000..a83944b
--- /dev/null
+++ b/lib/assertions.rb
@@ -0,0 +1,61 @@
+module Assertions
+ include Test::Unit::Assertions
+
+ # Make assertions about the content of console output.
+ #
+ # By default, each line of +output+ is assumed to come from STDOUT. You may
+ # specify the stream explicitly by annotating the line with a stream marker.
+ # (If your line literally requires any stream marker at the beginning of a
+ # line, you must prefix the line with an explicit stream marker.) The
+ # currently recognized markers are:
+ #
+ # * "STDOUT> "
+ # * "STDERR> "
+ # * "OUT> "
+ # * "ERR> "
+ # * "1> "
+ # * "2> "
+ #
+ # Any leading common indentation is automatically removed from the +output+
+ # parameter. For cases where this matters (e.g. every line should be
+ # indented), you should prefix the line with an explicit stream marker.
+ #
+ # @example Assert order of interleaved output streams
+ # !!!plain
+ # assert_output <<-CONSOLE
+ # STDOUT> 0123456789
+ # STDERR> ^- This is left aligned
+ # STDOUT> 01234567890
+ # STDERR> ^- This is indented 2 characters.
+ # CONSOLE
+ #
+ # @example Assert all content went to STDOUT
+ # !!!plain
+ # assert_output <<-CONSOLE
+ # 0123456789
+ # ^- This is left aligned
+ # 01234567890
+ # ^- This is indented 2 characters.
+ # CONSOLE
+ #
+ # @param [String] output The expected console output, optionally annotated
+ # with stream markers.
+ # @param [String] msg An explanatory message about why the test failure is
+ # relevant.
+ def assert_output(output, msg='Output lines did not match')
+ # Remove the minimal consistent indentation from the input; useful for clean HEREDOCs.
+ indentation = output.lines.map { |line| line[/^ */].length }.min
+ output = output.gsub(/^ {#{indentation}}/, '')
+
+ # Divide output based on expected destination
+ out, err = output.lines.partition { |line| line !~ /^((STD)?ERR|2)> / }
+ out, err, output = [out.join, err.join, output].map do |str|
+ str.gsub(/^((STD)?(ERR|OUT)|[12])> /, '')
+ end
+
+ # Exercise assertions about output
+ assert_equal output, (result.nil? ? '' : result.output), msg
+ assert_equal out, (result.nil? ? '' : result.stdout), 'The contents of STDOUT did not match expectations'
+ assert_equal err, (result.nil? ? '' : result.stderr), 'The contents of STDERR did not match expectations'
+ end
+end
\ No newline at end of file
diff --git a/lib/test_case.rb b/lib/test_case.rb
index 79068c0..e53a468 100644
--- a/lib/test_case.rb
+++ b/lib/test_case.rb
@@ -3,9 +3,10 @@ class TestCase
require 'tempfile'
require 'benchmark'
require 'stringio'
+ require 'lib/assertions'
require 'lib/puppet_commands'
- include Test::Unit::Assertions
+ include Assertions
include PuppetCommands
class PendingTest < Exception; end
-- You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
