summaryrefslogtreecommitdiff
path: root/spec/unit/provider/script_spec.rb
blob: 18a8a3305b698ee3851c40879c555a6656536f77 (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
#
# Author:: Adam Jacob (adam@chef.io)
# Copyright:: Copyright 2009-2016, Opscode
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::Provider::Script, "action_run" do
  let(:node) { Chef::Node.new }

  let(:events) { Chef::EventDispatch::Dispatcher.new }

  let(:run_context) { Chef::RunContext.new(node, {}, events) }

  let(:new_resource) do
    new_resource = Chef::Resource::Script.new("run some perl code")
    new_resource.code "$| = 1; print 'i like beans'"
    new_resource.interpreter "perl"
    new_resource
  end

  let(:provider) { Chef::Provider::Script.new(new_resource, run_context) }

  describe "#command" do
    it "is only the intepreter in quotes by default" do
      expect(provider.command.strip).to eq(%q{"perl"})
    end

    it "is the interpreter in quotes with the flags when flags are used" do
      new_resource.flags "-f"
      expect(provider.command).to eq(%q{"perl" -f})
    end
  end

  describe "#action_run" do
    before do
      allow(provider).to receive(:stream_to_stdout?).and_return(false)
    end

    it "should call shell_out! with the command and correct options" do
      opts = {
        timeout: 3600,
        returns: 0,
        default_env: false,
        log_level: :info,
        log_tag: "script[run some perl code]",
        input: "$| = 1; print 'i like beans'",
      }

      expect(provider).to receive(:shell_out!).with(provider.command, opts).and_return(true)
      provider.action_run
    end
  end
end