File: execution_stub_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (38 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'

describe Puppet::Util::ExecutionStub do
  it "should use the provided stub code when 'set' is called" do
    Puppet::Util::ExecutionStub.set do |command, options|
      expect(command).to eq(['/bin/foo', 'bar'])
      "stub output"
    end
    expect(Puppet::Util::ExecutionStub.current_value).not_to eq(nil)
    expect(Puppet::Util::Execution.execute(['/bin/foo', 'bar'])).to eq("stub output")
  end

  it "should automatically restore normal execution at the conclusion of each spec test" do
    # Note: this test relies on the previous test creating a stub.
    expect(Puppet::Util::ExecutionStub.current_value).to eq(nil)
  end

  it "should restore normal execution after 'reset' is called", unless: Puppet::Util::Platform.jruby? do
    # Note: "true" exists at different paths in different OSes
    if Puppet::Util::Platform.windows?
      true_command = [Puppet::Util.which('cmd.exe').tr('/', '\\'), '/c', 'exit 0']
    else
      true_command = [Puppet::Util.which('true')]
    end
    stub_call_count = 0
    Puppet::Util::ExecutionStub.set do |command, options|
      expect(command).to eq(true_command)
      stub_call_count += 1
      'stub called'
    end
    expect(Puppet::Util::Execution.execute(true_command)).to eq('stub called')
    expect(stub_call_count).to eq(1)
    Puppet::Util::ExecutionStub.reset
    expect(Puppet::Util::ExecutionStub.current_value).to eq(nil)
    expect(Puppet::Util::Execution.execute(true_command)).to eq('')
    expect(stub_call_count).to eq(1)
  end
end