From 96e43e69d8496926ad4951534e75b204bb279f22 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Thu, 8 May 2014 10:47:24 -0700 Subject: Move unit tests to spec/functions rspec-puppet matchers are defined for tests which exist in spec/functions, but the function unit tests lived in spec/unit/puppet/parser/functions. This moves them to the correct place for using rspec-puppet --- spec/functions/validate_cmd_spec.rb | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 spec/functions/validate_cmd_spec.rb (limited to 'spec/functions/validate_cmd_spec.rb') diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb new file mode 100755 index 0000000..a6e68df --- /dev/null +++ b/spec/functions/validate_cmd_spec.rb @@ -0,0 +1,48 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test' +TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch' + +describe Puppet::Parser::Functions.function(:validate_cmd) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + subject do + function_name = Puppet::Parser::Functions.function(:validate_cmd) + scope.method(function_name) + end + + describe "with an explicit failure message" do + it "prints the failure message on error" do + expect { + subject.call ['', '/bin/false', 'failure message!'] + }.to raise_error Puppet::ParseError, /failure message!/ + end + end + + describe "on validation failure" do + it "includes the command error output" do + expect { + subject.call ['', "#{TOUCHEXE} /cant/touch/this"] + }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/ + end + + it "includes the command return value" do + expect { + subject.call ['', '/cant/run/this'] + }.to raise_error Puppet::ParseError, /returned 1\b/ + end + end + + describe "when performing actual validation" do + it "can positively validate file content" do + expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error + end + + it "can negatively validate file content" do + expect { + subject.call ["", "#{TESTEXE} -s"] + }.to raise_error Puppet::ParseError, /failed to validate.*test -s/ + end + end +end -- cgit v1.2.3