summaryrefslogtreecommitdiff
path: root/spec/unit/provider/command_spec.rb
blob: 1afe019ee50ee94c2763255c3b4095eb3efb7e10 (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
require 'spec_helper'

require 'puppet/provider/command'

describe Puppet::Provider::Command do
  let(:name) { "the name" }
  let(:the_options) { { :option => 1 } }
  let(:no_options) { {} }
  let(:executable) { "foo" }
  let(:executable_absolute_path) { "/foo/bar" }

  let(:executor) { mock('executor') }
  let(:resolver) { mock('resolver') }

  let(:path_resolves_to_itself) do
    resolves = Object.new
    class << resolves
      def which(path)
        path
      end
    end
    resolves
  end

  it "executes a simple command" do
    executor.expects(:execute).with([executable], no_options)

    command = Puppet::Provider::Command.new(name, executable, path_resolves_to_itself, executor)
    command.execute()
  end

  it "executes a command with extra options" do
    executor.expects(:execute).with([executable], the_options)

    command = Puppet::Provider::Command.new(name, executable, path_resolves_to_itself, executor, the_options)
    command.execute()
  end

  it "executes a command with arguments" do
    executor.expects(:execute).with([executable, "arg1", "arg2"], no_options)

    command = Puppet::Provider::Command.new(name, executable, path_resolves_to_itself, executor)
    command.execute("arg1", "arg2")
  end

  it "resolves to an absolute path for better execution" do
    resolver.expects(:which).with(executable).returns(executable_absolute_path)
    executor.expects(:execute).with([executable_absolute_path], no_options)

    command = Puppet::Provider::Command.new(name, executable, resolver, executor)
    command.execute()
  end

  it "errors when the executable resolves to nothing" do
    resolver.expects(:which).with(executable).returns(nil)
    executor.expects(:execute).never

    command = Puppet::Provider::Command.new(name, executable, resolver, executor)

    lambda { command.execute() }.should raise_error(Puppet::Error, "Command #{name} is missing")
  end
end