summaryrefslogtreecommitdiff
path: root/spec/bundler/plugins/hook_spec.rb
blob: f6ee0ba210c8986a23cbd00d27c8328d7eaf403c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true

RSpec.describe "hook plugins" do
  context "before-install-all hook" do
    before do
      build_repo2 do
        build_plugin "before-install-all-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_BEFORE_INSTALL_ALL do |deps|
              puts "gems to be installed \#{deps.map(&:name).join(", ")}"
            end
          RUBY
        end
      end

      bundle "plugin install before-install-all-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs before all rubygems are installed" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rake"
        gem "rack"
      G

      expect(out).to include "gems to be installed rake, rack"
    end
  end

  context "before-install hook" do
    before do
      build_repo2 do
        build_plugin "before-install-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_BEFORE_INSTALL do |spec_install|
              puts "installing gem \#{spec_install.name}"
            end
          RUBY
        end
      end

      bundle "plugin install before-install-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs before each rubygem is installed" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rake"
        gem "rack"
      G

      expect(out).to include "installing gem rake"
      expect(out).to include "installing gem rack"
    end
  end

  context "after-install-all hook" do
    before do
      build_repo2 do
        build_plugin "after-install-all-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_AFTER_INSTALL_ALL do |deps|
              puts "installed gems \#{deps.map(&:name).join(", ")}"
            end
          RUBY
        end
      end

      bundle "plugin install after-install-all-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs after each rubygem is installed" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rake"
        gem "rack"
      G

      expect(out).to include "installed gems rake, rack"
    end
  end

  context "after-install hook" do
    before do
      build_repo2 do
        build_plugin "after-install-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_AFTER_INSTALL do |spec_install|
              puts "installed gem \#{spec_install.name} : \#{spec_install.state}"
            end
          RUBY
        end
      end

      bundle "plugin install after-install-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs after each rubygem is installed" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rake"
        gem "rack"
      G

      expect(out).to include "installed gem rake : installed"
      expect(out).to include "installed gem rack : installed"
    end
  end

  context "before-require-all hook" do
    before do
      build_repo2 do
        build_plugin "before-require-all-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_BEFORE_REQUIRE_ALL do |deps|
              puts "gems to be required \#{deps.map(&:name).join(", ")}"
            end
          RUBY
        end
      end

      bundle "plugin install before-require-all-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs before all rubygems are required" do
      install_gemfile_and_bundler_require
      expect(out).to include "gems to be required rake, rack"
    end
  end

  context "before-require hook" do
    before do
      build_repo2 do
        build_plugin "before-require-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_BEFORE_REQUIRE do |dep|
              puts "requiring gem \#{dep.name}"
            end
          RUBY
        end
      end

      bundle "plugin install before-require-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs before each rubygem is required" do
      install_gemfile_and_bundler_require
      expect(out).to include "requiring gem rake"
      expect(out).to include "requiring gem rack"
    end
  end

  context "after-require-all hook" do
    before do
      build_repo2 do
        build_plugin "after-require-all-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_AFTER_REQUIRE_ALL do |deps|
              puts "required gems \#{deps.map(&:name).join(", ")}"
            end
          RUBY
        end
      end

      bundle "plugin install after-require-all-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs after all rubygems are required" do
      install_gemfile_and_bundler_require
      expect(out).to include "required gems rake, rack"
    end
  end

  context "after-require hook" do
    before do
      build_repo2 do
        build_plugin "after-require-plugin" do |s|
          s.write "plugins.rb", <<-RUBY
            Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_AFTER_REQUIRE do |dep|
              puts "required gem \#{dep.name}"
            end
          RUBY
        end
      end

      bundle "plugin install after-require-plugin --source #{file_uri_for(gem_repo2)}"
    end

    it "runs after each rubygem is required" do
      install_gemfile_and_bundler_require
      expect(out).to include "required gem rake"
      expect(out).to include "required gem rack"
    end
  end

  def install_gemfile_and_bundler_require
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rake"
      gem "rack"
    G

    ruby <<-RUBY
      require "bundler"
      Bundler.require
    RUBY
  end
end