Sha256: f6b8097804f4801aea129fa258abca754a4c15a72431e7edf062855c1729e722

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true
require "pp"
require "timeout"

module ThemeCheck
  class Checks < Array
    CHECK_METHOD_TIMEOUT = 5 # sec

    def call(method, *args)
      each do |check|
        call_check_method(check, method, *args)
      end
    end

    def disableable
      @disableable ||= self.class.new(select(&:can_disable?))
    end

    def whole_theme
      @whole_theme ||= self.class.new(select(&:whole_theme?))
    end

    def single_file
      @single_file ||= self.class.new(select(&:single_file?))
    end

    private

    def call_check_method(check, method, *args)
      return unless check.respond_to?(method) && !check.ignored?

      # If you want to use binding.pry in unit tests, define the
      # THEME_CHECK_DEBUG environment variable. e.g.
      #
      #   $ export THEME_CHECK_DEBUG=true
      #   $ bundle exec rake tests:in_memory
      #
      if ENV['THEME_CHECK_DEBUG']
        check.send(method, *args)
      else
        Timeout.timeout(CHECK_METHOD_TIMEOUT) do
          check.send(method, *args)
        end
      end
    rescue Liquid::Error
      # Pass-through Liquid errors
      raise
    rescue => e
      node = args.first
      template = node.respond_to?(:template) ? node.template.relative_path : "?"
      markup = node.respond_to?(:markup) ? node.markup : ""
      node_class = node.respond_to?(:value) ? node.value.class : "?"

      ThemeCheck.bug(<<~EOS)
        Exception while running `#{check.code_name}##{method}`:
        ```
        #{e.class}: #{e.message}
          #{e.backtrace.join("\n  ")}
        ```

        Template: `#{template}`
        Node: `#{node_class}`
        Markup:
        ```
        #{markup}
        ```
        Check options: `#{check.options.pretty_inspect}`
      EOS
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
theme-check-1.6.0 lib/theme_check/checks.rb
theme-check-1.5.2 lib/theme_check/checks.rb
theme-check-1.5.1 lib/theme_check/checks.rb
theme-check-1.5.0 lib/theme_check/checks.rb
theme-check-1.4.0 lib/theme_check/checks.rb
theme-check-1.3.0 lib/theme_check/checks.rb