<commit_before>require "spec_helper"
require "heroku/plugin"

module Heroku
  describe Plugin do
    include SandboxHelper

    it "lives in ~/.heroku/plugins" do
      Plugin.stub!(:home_directory).and_return('/home/user')
      Plugin.directory.should == '/home/user/.heroku/plugins'
    end

    it "extracts the name from git urls" do
      Plugin.new('git://github.com/heroku/plugin.git').name.should == 'plugin'
    end

    describe "management" do
      before(:each) do
        @sandbox = "/tmp/heroku_plugins_spec_#{Process.pid}"
        FileUtils.mkdir_p(@sandbox)
        Dir.stub!(:pwd).and_return(@sandbox)
        Plugin.stub!(:directory).and_return(@sandbox)
      end

      after(:each) do
        FileUtils.rm_rf(@sandbox)
      end

      it "lists installed plugins" do
        FileUtils.mkdir_p(@sandbox + '/plugin1')
        FileUtils.mkdir_p(@sandbox + '/plugin2')
        Plugin.list.should include 'plugin1'
        Plugin.list.should include 'plugin2'
      end

      it "installs pulling from the plugin url" do
        plugin_folder = "/tmp/heroku_plugin"
        FileUtils.mkdir_p(plugin_folder)
        `cd #{plugin_folder} && git init && echo 'test' > README && git add . && git commit -m 'my plugin'`
        Plugin.new(plugin_folder).install
        File.directory?("#{@sandbox}/heroku_plugin").should be_true
        File.read("#{@sandbox}/heroku_plugin/README").should == "test\n"
      end

      it "uninstalls removing the folder" do
        FileUtils.mkdir_p(@sandbox + '/plugin1')
        Plugin.new('git://github.com/heroku/plugin1.git').uninstall
        Plugin.list.should == []
      end

      it "adds the lib folder in the plugin to the load path, if present" do
        FileUtils.mkdir_p(@sandbox + '/plugin/lib')
        File.open(@sandbox + '/plugin/lib/my_custom_plugin_file.rb', 'w') { |f| f.write "" }
        Plugin.load!
        lambda { require 'my_custom_plugin_file' }.should_not raise_error(LoadError)
      end

      it "loads init.rb, if present" do
        FileUtils.mkdir_p(@sandbox + '/plugin')
        File.open(@sandbox + '/plugin/init.rb', 'w') { |f| f.write "LoadedInit = true" }
        Plugin.load!
        LoadedInit.should be_true
      end

      describe "when there are plugin load errors" do
        before(:each) do
          FileUtils.mkdir_p(@sandbox + '/some_plugin/lib')
          File.open(@sandbox + '/some_plugin/init.rb', 'w') { |f| f.write "require 'some_non_existant_file'" }
        end

        it "should not throw an error" do
          Plugin.stub!(:display)
          lambda { Plugin.load! }.should_not raise_error
        end

        it "should fail gracefully" do
          Plugin.should_receive(:display).with(/Unable to load plugin: some_plugin/)
          Plugin.load!
        end

        it "should still load other plugins" do
          Plugin.stub!(:display)
          FileUtils.mkdir_p(@sandbox + '/some_plugin_2/lib')
          File.open(@sandbox + '/some_plugin_2/init.rb', 'w') { |f| f.write "LoadedPlugin2 = true" }
          Plugin.load!
          LoadedPlugin2.should be_true
        end
      end

      describe "deprecated plugins" do
        before(:each) do
          FileUtils.mkdir_p(@sandbox + '/heroku-releases/lib')
        end

        after(:each) do
          FileUtils.rm_rf(@sandbox + '/heroku-releases/lib')
        end

        it "should show confirmation to remove deprecated plugins" do
          Plugin.should_receive(:confirm).with("The plugin heroku-releases has been deprecated. Would you like to remove it? (y/N)").and_return(true)<commit_msg>fix test for new error message<commit_after>require "spec_helper"
require "heroku/plugin"

module Heroku
  describe Plugin do
    include SandboxHelper

    it "lives in ~/.heroku/plugins" do
      Plugin.stub!(:home_directory).and_return('/home/user')
      Plugin.directory.should == '/home/user/.heroku/plugins'
    end

    it "extracts the name from git urls" do
      Plugin.new('git://github.com/heroku/plugin.git').name.should == 'plugin'
    end

    describe "management" do
      before(:each) do
        @sandbox = "/tmp/heroku_plugins_spec_#{Process.pid}"
        FileUtils.mkdir_p(@sandbox)
        Dir.stub!(:pwd).and_return(@sandbox)
        Plugin.stub!(:directory).and_return(@sandbox)
      end

      after(:each) do
        FileUtils.rm_rf(@sandbox)
      end

      it "lists installed plugins" do
        FileUtils.mkdir_p(@sandbox + '/plugin1')
        FileUtils.mkdir_p(@sandbox + '/plugin2')
        Plugin.list.should include 'plugin1'
        Plugin.list.should include 'plugin2'
      end

      it "installs pulling from the plugin url" do
        plugin_folder = "/tmp/heroku_plugin"
        FileUtils.mkdir_p(plugin_folder)
        `cd #{plugin_folder} && git init && echo 'test' > README && git add . && git commit -m 'my plugin'`
        Plugin.new(plugin_folder).install
        File.directory?("#{@sandbox}/heroku_plugin").should be_true
        File.read("#{@sandbox}/heroku_plugin/README").should == "test\n"
      end

      it "uninstalls removing the folder" do
        FileUtils.mkdir_p(@sandbox + '/plugin1')
        Plugin.new('git://github.com/heroku/plugin1.git').uninstall
        Plugin.list.should == []
      end

      it "adds the lib folder in the plugin to the load path, if present" do
        FileUtils.mkdir_p(@sandbox + '/plugin/lib')
        File.open(@sandbox + '/plugin/lib/my_custom_plugin_file.rb', 'w') { |f| f.write "" }
        Plugin.load!
        lambda { require 'my_custom_plugin_file' }.should_not raise_error(LoadError)
      end

      it "loads init.rb, if present" do
        FileUtils.mkdir_p(@sandbox + '/plugin')
        File.open(@sandbox + '/plugin/init.rb', 'w') { |f| f.write "LoadedInit = true" }
        Plugin.load!
        LoadedInit.should be_true
      end

      describe "when there are plugin load errors" do
        before(:each) do
          FileUtils.mkdir_p(@sandbox + '/some_plugin/lib')
          File.open(@sandbox + '/some_plugin/init.rb', 'w') { |f| f.write "require 'some_non_existant_file'" }
        end

        it "should not throw an error" do
          Plugin.stub!(:display)
          lambda { Plugin.load! }.should_not raise_error
        end

        it "should fail gracefully" do
          Plugin.should_receive(:display).with(/Unable to load plugin some_plugin/)
          Plugin.should_receive(:display).with("")
          Plugin.load!
        end

        it "should still load other plugins" do
          Plugin.stub!(:display)
          FileUtils.mkdir_p(@sandbox + '/some_plugin_2/lib')
          File.open(@sandbox + '/some_plugin_2/init.rb', 'w') { |f| f.write "LoadedPlugin2 = true" }
          Plugin.load!
          LoadedPlugin2.should be_true
        end
      end

      describe "deprecated plugins" do
        before(:each) do
          FileUtils.mkdir_p(@sandbox + '/heroku-releases/lib')
        end

        after(:each) do
          FileUtils.rm_rf(@sandbox + '/heroku-releases/lib')
        end

        it "should show confirmation to remove deprecated plugins" do
          Plugin.should_receive(:confirm).with("The plugin heroku-releases has been deprecated. Would you like to remove it? (y/N)").and_return(true)
{'fraction_non_alphanumeric': 0.12477521095587218, 'fraction_numerical': 0.0027666343892654584, 'mean_word_length': 2.451073985680191, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236705', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2188, 'n_words': 515}