Sha256: ddfa7cd7ee1afb11df0c0fcd39d3afcdb01208a2e301e835ad06f2d4a8cb7b7f

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'percy/capybara/loaders/base_loader'
require 'digest'
require 'find'
require 'pathname'

module Percy
  module Capybara
    module Loaders

      # Resource loader that looks for resources in the specified folder.
      class FilesystemLoader < BaseLoader
        SKIP_RESOURCE_EXTENSIONS = [
          '.map',  # Ignore source maps.
          '.gz',  # Ignore gzipped files.
        ]
        MAX_FILESIZE_BYTES = 15 * 1024**2  # 15 MB.

        def initialize(options = {})
          # @assets_dir should point to a _compiled_ static assets directory, not source assets.
          @assets_dir = options[:assets_dir]
          @base_url = options[:base_url] || ''

          raise ArgumentError.new('assets_dir is required') if @assets_dir.nil? || @assets_dir == ''
          if !Pathname.new(@assets_dir).absolute?
            raise ArgumentError.new("assets_dir needs to be an absolute path. Received: #{@assets_dir}")
          end
          if !Dir.exist?(@assets_dir)
            raise ArgumentError.new("assets_dir provided was not found. Received: #{@assets_dir}")
          end

          super
        end

        def snapshot_resources
          [root_html_resource]
        end

        def build_resources
          resources = []
          Find.find(@assets_dir).each do |path|
            # Skip directories.
            next if !FileTest.file?(path)
            # Skip certain extensions.
            next if SKIP_RESOURCE_EXTENSIONS.include?(File.extname(path))
            # Skip large files, these are hopefully downloads and not used in page rendering.
            next if File.size(path) > MAX_FILESIZE_BYTES

            # Replace the assets_dir with the base_url to generate the resource_url
            resource_url = path.sub(@assets_dir, @base_url)

            sha = Digest::SHA256.hexdigest(File.read(path))
            resources << Percy::Client::Resource.new(resource_url, sha: sha, path: path)
          end
          resources
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
percy-capybara-2.4.0 lib/percy/capybara/loaders/filesystem_loader.rb