File: command_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (62 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download | duplicates (3)
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) { double('executor') }
  let(:resolver) { double('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
    expect(executor).to receive(: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
    expect(executor).to receive(: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
    expect(executor).to receive(: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
    expect(resolver).to receive(:which).with(executable).and_return(executable_absolute_path)
    expect(executor).to receive(: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
    expect(resolver).to receive(:which).with(executable).and_return(nil)
    expect(executor).not_to receive(:execute)

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

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