summaryrefslogtreecommitdiffstats
path: root/spec/unit/interface_spec.rb
blob: 4b6fd117f8c9b58ad7718c2b0d970c645d955d10 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
require 'puppet/interface'

describe Puppet::Interface do
  after do
    Puppet::Interface.unload_interface(:me)
  end

  describe "at initialization" do
    it "should require a name" do
      Puppet::Interface.new(:me).name.should == :me
    end

    it "should register itself" do
      Puppet::Interface.expects(:register_interface).with do |name, inst|
        name == :me and inst.is_a?(Puppet::Interface)
      end
      Puppet::Interface.new(:me)
    end

    it "should load actions" do
      Puppet::Interface.any_instance.expects(:load_actions)
      Puppet::Interface.new(:me)
    end

    it "should instance-eval any provided block" do
      face = Puppet::Interface.new(:me) do
        action(:something) { "foo" }
      end

      face.should be_action(:something)
    end
  end

  it "should use its name converted to a string as its string form" do
    Puppet::Interface.new(:me).to_s.should == "me"
  end

  it "should allow overriding of the default format" do
    face = Puppet::Interface.new(:me)
    face.set_default_format :foo
    face.default_format.should == :foo
  end

  it "should default to :pson for its format" do
    Puppet::Interface.new(:me).default_format.should == :pson
  end

  # Why?
  it "should create a class-level autoloader" do
    Puppet::Interface.autoloader.should be_instance_of(Puppet::Util::Autoload)
  end

  it "should set any provided options" do
    Puppet::Interface.new(:me, :verb => "foo").verb.should == "foo"
  end

  it "should create an associated constant when registering an interface" do
    $stderr.stubs(:puts)
    face = Puppet::Interface.new(:me)
    Puppet::Interface.unload_interface(:me) # to remove from the initial registration
    Puppet::Interface.register_interface(:me, face)
    Puppet::Interface::Me.should equal(face)
  end

  # Why is unloading interfaces important?
  it "should be able to unload interfaces" do
    $stderr.stubs(:puts)
    face = Puppet::Interface.new(:me)
    Puppet::Interface.unload_interface(:me)
    Puppet::Interface.const_defined?(:Me).should be_false
  end

  it "should remove the associated constant when an interface is unregistered" do
    $stderr.stubs(:puts)
    face = Puppet::Interface.new(:me)
    Puppet::Interface.unload_interface(:me)
    Puppet::Interface.const_defined?("Me").should be_false
  end

  it "should try to require interfaces that are not known" do
    Puppet::Interface.expects(:require).with "puppet/interface/foo"
    Puppet::Interface.const_get(:Foo)
  end

  it "should not fail when requiring an interface fails" do
    $stderr.stubs(:puts)
    Puppet::Interface.expects(:require).with("puppet/interface/foo").raises LoadError
    lambda { Puppet::Interface::Foo }.should_not raise_error
  end

  it "should be able to load all actions in all search paths"
end