Sha256: 175b75450386fc2980693c0f188c00fe5a098cae16abf9c1bb5e3ecaa5bc3bdc

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require "pp"
require "timeout"

module PlatformosCheck
  class Checks < Array
    CHECK_METHOD_TIMEOUT = 5 # sec

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

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

    def whole_platformos_app
      @whole_platformos_app ||= self.class.new(select(&:whole_platformos_app?))
    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
      # PLATFORMOS_CHECK_DEBUG environment variable. e.g.
      #
      #   $ export PLATFORMOS_CHECK_DEBUG=true
      #   $ bundle exec rake tests:in_memory
      #
      if ENV['PLATFORMOS_CHECK_DEBUG']
        check.send(method, *args)
      else
        Timeout.timeout(CHECK_METHOD_TIMEOUT) do
          check.send(method, *args)
        end
      end
    rescue Liquid::Error, PlatformosCheckError
      raise
    rescue StandardError => e
      node = args.first
      app_file = node.respond_to?(:app_file) ? node.app_file.relative_path : "?"
      markup = node.respond_to?(:markup) ? node.markup : ""
      node_class = node.respond_to?(:value) ? node.value.class : "?"
      line_number = node.respond_to?(:line_number) ? node.line_number : "?"

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

        Platformos App File: `#{app_file}`
        Node: `#{node_class}`
        Markup:
        ```
        #{markup}
        ```
        Line number: #{line_number}
        Check options: `#{check.options.pretty_inspect}`
      EOS
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
platformos-check-0.2.0 lib/platformos_check/checks.rb
platformos-check-0.1.0 lib/platformos_check/checks.rb
platformos-check-0.0.3 lib/platformos_check/checks.rb
platformos-check-0.0.2 lib/platformos_check/checks.rb
platformos-check-0.0.1 lib/platformos_check/checks.rb