<commit_before>require 'rails_helper'
require_dependency 'plugin/instance'

describe Plugin::Instance do

  after do
    DiscoursePluginRegistry.reset!
  end

  context "find_all" do
    it "can find plugins correctly" do
      plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
      expect(plugins.count).to eq(1)
      plugin = plugins[0]

      expect(plugin.name).to eq("plugin-name")
      expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")
    end

    it "does not blow up on missing directory" do
      plugins = Plugin::Instance.find_all("#{Rails.root}/frank_zappa")
      expect(plugins.count).to eq(0)
    end
  end

  context "enabling/disabling" do

    it "is enabled by default" do
      expect(Plugin::Instance.new.enabled?).to eq(true)
    end

    context "with a plugin that extends things" do

      class Trout; end
      class TroutSerializer < ApplicationSerializer; end

      class TroutPlugin < Plugin::Instance
        attr_accessor :enabled
        def enabled?; @enabled; end
      end

      before do
        @plugin = TroutPlugin.new
        @trout = Trout.new

        # New method
        @plugin.add_to_class(:trout, :status?) { "evil" }

        # DiscourseEvent
        @hello_count = 0
        @increase_count = -> { @hello_count += 1 }
        @plugin.on(:hello, &@increase_count)

        # Serializer
        @plugin.add_to_serializer(:trout, :scales) { 1024 }
        @serializer = TroutSerializer.new(@trout)
      end

      after do
        DiscourseEvent.off(:hello, &@increase_count)
      end

      it "checks enabled/disabled functionality for extensions" do

        # with an enabled plugin
        @plugin.enabled = true
        expect(@trout.status?).to eq("evil")<commit_msg>Properly clean up plugin event in specs..<commit_after>require 'rails_helper'
require_dependency 'plugin/instance'

describe Plugin::Instance do

  after do
    DiscoursePluginRegistry.reset!
  end

  context "find_all" do
    it "can find plugins correctly" do
      plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
      expect(plugins.count).to eq(1)
      plugin = plugins[0]

      expect(plugin.name).to eq("plugin-name")
      expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")
    end

    it "does not blow up on missing directory" do
      plugins = Plugin::Instance.find_all("#{Rails.root}/frank_zappa")
      expect(plugins.count).to eq(0)
    end
  end

  context "enabling/disabling" do

    it "is enabled by default" do
      expect(Plugin::Instance.new.enabled?).to eq(true)
    end

    context "with a plugin that extends things" do

      class Trout; end
      class TroutSerializer < ApplicationSerializer; end

      class TroutPlugin < Plugin::Instance
        attr_accessor :enabled
        def enabled?; @enabled; end
      end

      before do
        @plugin = TroutPlugin.new
        @trout = Trout.new

        # New method
        @plugin.add_to_class(:trout, :status?) { "evil" }

        # DiscourseEvent
        @hello_count = 0
        @increase_count = -> { @hello_count += 1 }
        @set = @plugin.on(:hello, &@increase_count)

        # Serializer
        @plugin.add_to_serializer(:trout, :scales) { 1024 }
        @serializer = TroutSerializer.new(@trout)
      end

      after do
        DiscourseEvent.off(:hello, &@set.last)
      end

      it "checks enabled/disabled functionality for extensions" do

        # with an enabled plugin
        @plugin.enabled = true
        expect(@trout.status?).to eq("evil")
{'fraction_non_alphanumeric': 0.10950928749653452, 'fraction_numerical': 0.0049902966454117, 'mean_word_length': 2.6370967741935485, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5886480', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1065, 'n_words': 264}