Sha256: 96da121d15d2dda298c1f58aa7b5d9bc071e247a7aa5abca5410dce0a67c3b9d

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Polytrix
  module Core
    module FileFinder
      class FileNotFound < StandardError; end

      # Finds a file by loosely matching the file name to a scenario name
      def find_file(search_path, scenario_name, ignored_patterns = read_gitignore(search_path))
        potential_files = Dir.glob("#{search_path}/**/*#{scenario_name}.*", File::FNM_CASEFOLD)
        potential_files.concat Dir.glob("#{search_path}/**/*#{scenario_name.gsub(' ', '_')}.*", File::FNM_CASEFOLD)
        potential_files.concat Dir.glob("#{search_path}/**/*#{scenario_name.gsub('_', '')}.*", File::FNM_CASEFOLD)

        # Find the first file, not including generated files
        file = potential_files.find { |file|
          !ignored? ignored_patterns, search_path, file
        }

        fail FileNotFound, "No file was found for #{scenario_name} within #{search_path}" if file.nil?
        Pathname.new file
      end

      private

      def read_gitignore(dir)
        gitignore_file = "#{dir}/.gitignore"
        File.read(gitignore_file)
      rescue
        ""
      end

      def ignored?(ignored_patterns, base_path, target_file)
        ignored_patterns.split.find do |pattern|
          # if git ignores a folder, we should ignore all files it contains
          pattern = "#{pattern}**" if pattern[-1] == '/'
          relativize(target_file, base_path).fnmatch? pattern
        end
      end

      def relativize(file, base_path)
        Pathname.new(file).relative_path_from Pathname.new(base_path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polytrix-0.0.1 lib/polytrix/core/file_finder.rb