1 #! /usr/bin/env ruby -S rspec
4 TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test'
5 TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch'
7 describe Puppet::Parser::Functions.function(:validate_cmd) do
8 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
11 function_name = Puppet::Parser::Functions.function(:validate_cmd)
12 scope.method(function_name)
15 context 'with no % placeholder' do
16 describe "with an explicit failure message" do
17 it "prints the failure message on error" do
19 subject.call ['', '/bin/false', 'failure message!']
20 }.to raise_error Puppet::ParseError, /failure message!/
24 describe "on validation failure" do
25 it "includes the command error output" do
27 subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
28 }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
31 it "includes the command return value" do
33 subject.call ['', '/cant/run/this']
34 }.to raise_error Puppet::ParseError, /returned 1\b/
38 describe "when performing actual validation" do
39 it "can positively validate file content" do
40 expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
43 it "can negatively validate file content" do
45 subject.call ["", "#{TESTEXE} -s"]
46 }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
51 context 'with % placeholder' do
52 describe "with an explicit failure message" do
53 it "prints the failure message on error" do
55 subject.call ['', '/bin/false % -f', 'failure message!']
56 }.to raise_error Puppet::ParseError, /failure message!/
59 describe "on validation failure" do
60 it "includes the command error output" do
62 subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
63 }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
66 it "includes the command return value" do
68 subject.call ['', '/cant/run/this % -z']
69 }.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
73 describe "when performing actual validation" do
74 it "can positively validate file content" do
75 expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
78 it "can negatively validate file content" do
80 subject.call ["", "#{TESTEXE} -s %"]
81 }.to raise_error Puppet::ParseError, /failed to validate.*test -s/