lib/codeowners/checker/group/pattern.rb in codeowners-checker-1.0.5 vs lib/codeowners/checker/group/pattern.rb in codeowners-checker-1.1.1

- old
+ new

@@ -1,18 +1,19 @@ # frozen_string_literal: true require_relative 'line' require_relative '../owner' +require 'pathspec' module Codeowners class Checker class Group # Defines and manages line type pattern. # Parse the line into pattern, owners and whitespaces. class Pattern < Line attr_accessor :owners, :whitespace - attr_reader :pattern + attr_reader :pattern, :spec def self.match?(line) _pattern, *owners = line.split(/\s+/) Owner.valid?(*owners) end @@ -33,28 +34,22 @@ # Parse the line counting whitespaces between pattern and owners. def parse(line) @pattern, *@owners = line.split(/\s+/) @whitespace = line.split('@').first.count(' ') - 1 + @spec = parse_spec(@pattern) end def match_file?(file) - File.fnmatch(pattern.gsub(%r{^/}, ''), file, fn_options) + spec.match file end - def fn_options - if pattern.include?('**') - File::FNM_DOTMATCH - else - File::FNM_DOTMATCH | File::FNM_PATHNAME - end - end - def pattern=(new_pattern) @whitespace += @pattern.size - new_pattern.size @whitespace = 1 if @whitespace < 1 + @spec = parse_spec(new_pattern) @pattern = new_pattern end # @return String with the pattern and owners # Use @param preserve_whitespaces to keep the previous identation. @@ -65,9 +60,13 @@ [line, *owners].join(' ') end def to_s to_file(preserve_whitespaces: false) + end + + def parse_spec(pattern) + PathSpec.from_lines(pattern) end end end end end