|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
| 3 | +def with_executor |
| 4 | + return yield unless Puppet::Util::Platform.jruby? |
| 5 | + |
| 6 | + begin |
| 7 | + Puppet::Util::ExecutionStub.set do |command, options, stdin, stdout, stderr| |
| 8 | + require 'open3' |
| 9 | + # simulate what puppetserver does |
| 10 | + Dir.chdir(options[:cwd]) do |
| 11 | + out, err, _status = Open3.capture3(*command) |
| 12 | + stdout.write(out) |
| 13 | + stderr.write(err) |
| 14 | + # execution api expects stdout to be returned |
| 15 | + out |
| 16 | + end |
| 17 | + end |
| 18 | + yield |
| 19 | + ensure |
| 20 | + Puppet::Util::ExecutionStub.reset |
| 21 | + end |
| 22 | +end |
| 23 | + |
3 | 24 | describe "the generate function" do
|
4 | 25 | include PuppetSpec::Files
|
5 | 26 |
|
6 | 27 | let :node do Puppet::Node.new('localhost') end
|
7 | 28 | let :compiler do Puppet::Parser::Compiler.new(node) end
|
8 | 29 | let :scope do Puppet::Parser::Scope.new(compiler) end
|
| 30 | + let :cwd do tmpdir('generate') end |
9 | 31 |
|
10 | 32 | it "should exist" do
|
11 | 33 | expect(Puppet::Parser::Functions.function("generate")).to eq("function_generate")
|
12 | 34 | end
|
13 | 35 |
|
14 | 36 | it "accept a fully-qualified path as a command" do
|
15 | 37 | command = File.expand_path('/command/foo')
|
16 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
| 38 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], anything).and_return("yay") |
17 | 39 | expect(scope.function_generate([command])).to eq("yay")
|
18 | 40 | end
|
19 | 41 |
|
|
35 | 57 | end
|
36 | 58 |
|
37 | 59 | it "should execute the generate script with the correct working directory" do
|
38 |
| - command = File.expand_path("/command") |
39 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
40 |
| - expect(scope.function_generate([command])).to eq('yay') |
| 60 | + command = File.expand_path("/usr/local/command") |
| 61 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(cwd: %r{/usr/local})).and_return("yay") |
| 62 | + scope.function_generate([command]) |
| 63 | + end |
| 64 | + |
| 65 | + it "should execute the generate script with failonfail" do |
| 66 | + command = File.expand_path("/usr/local/command") |
| 67 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(failonfail: true)).and_return("yay") |
| 68 | + scope.function_generate([command]) |
| 69 | + end |
| 70 | + |
| 71 | + it "should execute the generate script with combine" do |
| 72 | + command = File.expand_path("/usr/local/command") |
| 73 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(combine: true)).and_return("yay") |
| 74 | + scope.function_generate([command]) |
| 75 | + end |
| 76 | + |
| 77 | + it "executes a command in a working directory" do |
| 78 | + if Puppet::Util::Platform.windows? |
| 79 | + command = File.join(cwd, 'echo.bat') |
| 80 | + File.write(command, <<~END) |
| 81 | + @echo off |
| 82 | + echo %CD% |
| 83 | + END |
| 84 | + expect(scope.function_generate([command]).chomp).to match(cwd.gsub('/', '\\')) |
| 85 | + else |
| 86 | + with_executor do |
| 87 | + command = File.join(cwd, 'echo.sh') |
| 88 | + File.write(command, <<~END) |
| 89 | + #!/bin/sh |
| 90 | + echo $PWD |
| 91 | + END |
| 92 | + Puppet::FileSystem.chmod(0755, command) |
| 93 | + expect(scope.function_generate([command]).chomp).to eq(cwd) |
| 94 | + end |
| 95 | + end |
41 | 96 | end
|
42 | 97 |
|
43 | 98 | describe "on Windows", :if => Puppet::Util::Platform.windows? do
|
44 | 99 | it "should accept the tilde in the path" do
|
45 | 100 | command = "C:/DOCUME~1/ADMINI~1/foo.bat"
|
46 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
| 101 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(cwd: "C:/DOCUME~1/ADMINI~1")).and_return("yay") |
47 | 102 | expect(scope.function_generate([command])).to eq('yay')
|
48 | 103 | end
|
49 | 104 |
|
50 | 105 | it "should accept lower-case drive letters" do
|
51 | 106 | command = 'd:/command/foo'
|
52 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
| 107 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(cwd: "d:/command")).and_return("yay") |
53 | 108 | expect(scope.function_generate([command])).to eq('yay')
|
54 | 109 | end
|
55 | 110 |
|
56 | 111 | it "should accept upper-case drive letters" do
|
57 | 112 | command = 'D:/command/foo'
|
58 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
| 113 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(cwd: "D:/command")).and_return("yay") |
59 | 114 | expect(scope.function_generate([command])).to eq('yay')
|
60 | 115 | end
|
61 | 116 |
|
62 | 117 | it "should accept forward and backslashes in the path" do
|
63 | 118 | command = 'D:\command/foo\bar'
|
64 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
| 119 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(cwd: 'D:\command/foo')).and_return("yay") |
65 | 120 | expect(scope.function_generate([command])).to eq('yay')
|
66 | 121 | end
|
67 | 122 |
|
|
81 | 136 |
|
82 | 137 | it "should accept plus and dash" do
|
83 | 138 | command = "/var/folders/9z/9zXImgchH8CZJh6SgiqS2U+++TM/-Tmp-/foo"
|
84 |
| - expect(Dir).to receive(:chdir).with(File.dirname(command)).and_return("yay") |
| 139 | + expect(Puppet::Util::Execution).to receive(:execute).with([command], hash_including(cwd: '/var/folders/9z/9zXImgchH8CZJh6SgiqS2U+++TM/-Tmp-')).and_return("yay") |
85 | 140 | expect(scope.function_generate([command])).to eq('yay')
|
86 | 141 | end
|
87 | 142 | end
|
|
0 commit comments