aboutsummaryrefslogtreecommitdiffstats
path: root/spec/bundler/bundler/plugin/api_spec.rb
blob: e40b9adb0febb482d22383978965ecddc729da84 (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
# frozen_string_literal: true
require "spec_helper"

RSpec.describe Bundler::Plugin::API do
  context "plugin declarations" do
    before do
      stub_const "UserPluginClass", Class.new(Bundler::Plugin::API)
    end

    describe "#command" do
      it "declares a command plugin with same class as handler" do
        expect(Bundler::Plugin).
          to receive(:add_command).with("meh", UserPluginClass).once

        UserPluginClass.command "meh"
      end

      it "accepts another class as argument that handles the command" do
        stub_const "NewClass", Class.new
        expect(Bundler::Plugin).to receive(:add_command).with("meh", NewClass).once

        UserPluginClass.command "meh", NewClass
      end
    end

    describe "#source" do
      it "declares a source plugin with same class as handler" do
        expect(Bundler::Plugin).
          to receive(:add_source).with("a_source", UserPluginClass).once

        UserPluginClass.source "a_source"
      end

      it "accepts another class as argument that handles the command" do
        stub_const "NewClass", Class.new
        expect(Bundler::Plugin).to receive(:add_source).with("a_source", NewClass).once

        UserPluginClass.source "a_source", NewClass
      end
    end

    describe "#hook" do
      it "accepts a block and passes it to Plugin module" do
        foo = double("tester")
        expect(foo).to receive(:called)

        expect(Bundler::Plugin).to receive(:add_hook).with("post-foo").and_yield

        Bundler::Plugin::API.hook("post-foo") { foo.called }
      end
    end
  end

  context "bundler interfaces provided" do
    before do
      stub_const "UserPluginClass", Class.new(Bundler::Plugin::API)
    end

    subject(:api) { UserPluginClass.new }

    # A test of delegation
    it "provides the Bundler's functions" do
      expect(Bundler).to receive(:an_unkown_function).once

      api.an_unkown_function
    end

    it "includes Bundler::SharedHelpers' functions" do
      expect(Bundler::SharedHelpers).to receive(:an_unkown_helper).once

      api.an_unkown_helper
    end

    context "#tmp" do
      it "provides a tmp dir" do
        expect(api.tmp("mytmp")).to be_directory
      end

      it "accepts multiple names for suffix" do
        expect(api.tmp("myplugin", "download")).to be_directory
      end
    end
  end
end