summaryrefslogtreecommitdiff
path: root/spec/unit/parser/functions/shellquote_spec.rb
blob: 6bcdc7a3ebd2331d148b43d9d582788f36e4e930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env ruby
require 'spec_helper'

describe "the shellquote function" do
  let :node     do Puppet::Node.new('localhost') end
  let :compiler do Puppet::Parser::Compiler.new(node) end
  let :scope    do Puppet::Parser::Scope.new(compiler) end

  it "should exist" do
    Puppet::Parser::Functions.function("shellquote").should == "function_shellquote"
  end

  it "should handle no arguments" do
    scope.function_shellquote([]).should == ""
  end

  it "should handle several simple arguments" do
    scope.function_shellquote(
      ['foo', 'bar@example.com', 'localhost:/dev/null', 'xyzzy+-4711,23']
    ).should == 'foo bar@example.com localhost:/dev/null xyzzy+-4711,23'
  end

  it "should handle array arguments" do
    scope.function_shellquote(
      ['foo', ['bar@example.com', 'localhost:/dev/null'],
      'xyzzy+-4711,23']
    ).should == 'foo bar@example.com localhost:/dev/null xyzzy+-4711,23'
  end

  it "should quote unsafe characters" do
    scope.function_shellquote(['/etc/passwd ', '(ls)', '*', '[?]', "'&'"]).
      should == '"/etc/passwd " "(ls)" "*" "[?]" "\'&\'"'
  end

  it "should deal with double quotes" do
    scope.function_shellquote(['"foo"bar"']).should == '\'"foo"bar"\''
  end

  it "should cope with dollar signs" do
    scope.function_shellquote(['$PATH', 'foo$bar', '"x$"']).
      should == "'$PATH' 'foo$bar' '\"x$\"'"
  end

  it "should deal with apostrophes (single quotes)" do
    scope.function_shellquote(["'foo'bar'", "`$'EDITOR'`"]).
      should == '"\'foo\'bar\'" "\\`\\$\'EDITOR\'\\`"'
  end

  it "should cope with grave accents (backquotes)" do
    scope.function_shellquote(['`echo *`', '`ls "$MAILPATH"`']).
      should == "'`echo *`' '`ls \"$MAILPATH\"`'"
  end

  it "should deal with both single and double quotes" do
    scope.function_shellquote(['\'foo"bar"xyzzy\'', '"foo\'bar\'xyzzy"']).
      should == '"\'foo\\"bar\\"xyzzy\'" "\\"foo\'bar\'xyzzy\\""'
  end

  it "should handle multiple quotes *and* dollars and backquotes" do
    scope.function_shellquote(['\'foo"$x`bar`"xyzzy\'']).
      should == '"\'foo\\"\\$x\\`bar\\`\\"xyzzy\'"'
  end

  it "should handle linefeeds" do
    scope.function_shellquote(["foo \n bar"]).should == "\"foo \n bar\""
  end
end