[SCM] ci-tooling packaging branch, master, updated. 578931ba4d726523652a3a9edc94012df1916466

Harald Sitter apachelogger-guest at moszumanska.debian.org
Mon May 11 12:37:49 UTC 2015


Gitweb-URL: http://git.debian.org/?p=pkg-kde/ci-tooling.git;a=commitdiff;h=9cec902

The following commit has been merged in the master branch:
commit 9cec902caab519b07aa05ba00d17d0e4baf345fc
Author: Harald Sitter <sitter at kde.org>
Date:   Mon May 11 14:20:37 2015 +0200

    redo the entire pattern enumerable nonesense
    
    do not derive PatternHash and PatternArray. Its nice on paper, shit in
    practise as of course the base types will on non-destructive operations
    construct their type rather than the derived type.
    i.e. PatternHash.reject => Hash, which is just terrible
    
    so the options were to either compose with blanket forwardable rather than
    derive, or to simply not use own super-types.
    now, Array doesn't need one except for filter, and Hash only has
    convenience idioms ontop. both not very major so simply convert them into
    class methods on Pattern itself. so Pattern now filters, sorts and converts
    a hash into a hash of patterns. much less code, somewhat more graspable
    and easier to test.
    
    mind you, this still leaves the possibility to later create forwarded types
    but until such time this is just fine.
---
 lib/ci/pattern.rb       | 71 ++++++++++++++++++-------------------------------
 lib/ci/upstream_scm.rb  |  8 +++---
 test/test_ci_pattern.rb | 71 ++++++++++++++++++++++++++++---------------------
 3 files changed, 70 insertions(+), 80 deletions(-)

diff --git a/lib/ci/pattern.rb b/lib/ci/pattern.rb
index 83057c2..e7b67cd 100644
--- a/lib/ci/pattern.rb
+++ b/lib/ci/pattern.rb
@@ -1,55 +1,13 @@
 require 'yaml'
 
 module CI
-  # Implements pattern filtering for enumerables.
-  module PatternFilter
-    # Filters all patterns that do not match reference.
-    # @return new enumerable with patterns removed that do not match
-    def filter(reference)
-      reject(&filter_proc(reference))
-    end
-
-    # @see filter
-    # This method removes items in-place.
-    def filter!(reference)
-      reject!(&filter_proc(reference))
-    end
-
-    private
-
-    def filter_proc(reference)
-      proc { |k, *_| !k.match?(reference) }
-    end
-  end
-
-  # A PatternHash.
-  # PatternHash is a specific Hash meant to be used for Pattern objects.
-  # PatternHash includes PatternFilter to filter patterns that do not match
-  # a reference value.
-  class PatternHash < Hash
-    include PatternFilter
-
-    # Constructs a new PatternHash. Can do so by converting a normal Hash.
-    # @param hash a Hash to covert into a PatternHash
-    # @param recurse whether or not to recursively convert hash
-    def initialize(hash = {}, recurse: true)
-      hash.each_with_object(self) do |(key, value), memo|
-        if recurse && value.is_a?(Hash)
-          value = PatternHash.new(value, recurse: recurse)
-        end
-        memo[CI::Pattern.new(key)] = value
-        memo
-      end
-    end
-  end
-
   # A PatternArray.
   # PatternArray is a specific Array meant to be used for Pattern objects.
   # PatternArray includes PatternFilter to filter patterns that do not match
   # a reference value.
-  class PatternArray < Array
-    include PatternFilter
-  end
+  # class PatternArray < Array
+  #   include PatternFilter
+  # end
 
   # A POSIX regex match pattern.
   # Pattern matching is implemented by File.fnmatch and reperesents a POSIX
@@ -106,5 +64,28 @@ module CI
     def to_s
       "#{@pattern}"
     end
+
+    def self.filter(reference, enumerable)
+      enumerable.reject { |k, *| !k.match?(reference) }
+    end
+
+    def self.sort_hash(enumerable)
+      enumerable.class[enumerable.sort_by { |pattern, *_| pattern }]
+    end
+
+    # Constructs a new Hash with the values converted in Patterns.
+    # @param hash a Hash to covert into a PatternHash
+    # @param recurse whether or not to recursively convert hash
+    def self.convert_hash(hash, recurse: true)
+      new_hash = {}
+      hash.each_with_object(new_hash) do |(key, value), memo|
+        if recurse && value.is_a?(Hash)
+          value = convert_hash(value, recurse: recurse)
+        end
+        memo[CI::Pattern.new(key)] = value
+        memo
+      end
+      new_hash
+    end
   end
 end
diff --git a/lib/ci/upstream_scm.rb b/lib/ci/upstream_scm.rb
index ba4abbd..7b38675 100644
--- a/lib/ci/upstream_scm.rb
+++ b/lib/ci/upstream_scm.rb
@@ -80,20 +80,20 @@ class UpstreamSCM
     base = File.expand_path(File.dirname(File.dirname(File.dirname(__FILE__))))
     file = File.join(base, 'data', 'upstream-scm.yml')
     hash = YAML.load(File.read(file))
-    hash = CI::PatternHash.new(hash, recurse: false)
+    hash = CI::Pattern.convert_hash(hash, recurse: false)
     hash.each do |k, v|
-      hash[k] = CI::PatternHash.new(v, recurse: false)
+      hash[k] = CI::Pattern.convert_hash(v, recurse: false)
     end
     hash
   end
 
   def global_override!
     overrides = global_override_load
-    repo_patterns = overrides.filter(@packaging_repo)
+    repo_patterns = CI::Pattern.filter(@packaging_repo, overrides)
     return if repo_patterns.empty?
 
     branches = overrides[repo_patterns.flatten.first]
-    branch_patterns = branches.filter(@packaging_branch)
+    branch_patterns = CI::Pattern.filter(@packaging_branch, branches)
     return if branch_patterns.empty?
 
     override_apply(branches[branch_patterns.flatten.first])
diff --git a/test/test_ci_pattern.rb b/test/test_ci_pattern.rb
index d8a22c3..e6f568a 100644
--- a/test/test_ci_pattern.rb
+++ b/test/test_ci_pattern.rb
@@ -2,37 +2,6 @@ require_relative '../lib/ci/pattern'
 require_relative 'lib/testcase'
 
 # Test ci/pattern
-class CIPatternHashTest < TestCase
-  def test_all
-    hash = {
-      'a*' => {'x*' => false}
-    }
-    p = CI::PatternHash.new(hash, recurse: true)
-    assert_equal(1, p.size)
-    assert(p.flatten.first.is_a?(CI::Pattern))
-    assert(p.flatten.last.flatten.first.is_a?(CI::Pattern))
-  end
-end
-
-# Test ci/pattern
-class CIPatternArrayTest < TestCase
-  def test_all
-    patterns = CI::PatternArray.new
-    utopic_stable_pattern = CI::Pattern.new('utopic_stable_*')
-    vivid_stable_pattern = CI::Pattern.new('vivid_stable_*') # used later
-    vivid_pattern = CI::Pattern.new('vivid_*')
-    patterns << utopic_stable_pattern << vivid_pattern
-    assert_equal(2, patterns.size)
-
-    assert_equal(patterns.filter('utopic_stable_foo'), [utopic_stable_pattern])
-    patterns.filter!('vivid_stable_foo')
-    assert_equal(patterns, [vivid_pattern])
-    patterns << vivid_stable_pattern
-    assert_equal(patterns.sort, [vivid_stable_pattern, vivid_pattern])
-  end
-end
-
-# Test ci/pattern
 class CIPatternTest < TestCase
   def test_match
     assert(CI::Pattern.new('a*').match?('ab'))
@@ -59,4 +28,44 @@ class CIPatternTest < TestCase
     assert_equal(CI::Pattern.new('a').to_s, 'a')
     assert_equal(CI::Pattern.new(nil).to_s, '')
   end
+
+  def test_hash_convert
+    hash = {
+      'a*' => {'x*' => false}
+    }
+    ph = CI::Pattern.convert_hash(hash, recurse: true)
+    assert_equal(1, ph.size)
+    assert(ph.flatten.first.is_a?(CI::Pattern))
+    assert(ph.flatten.last.flatten.first.is_a?(CI::Pattern))
+  end
+
+  def test_sort
+    # PatternHash has a convenience sort_by_pattern method that allows sorting
+    # the first level of a hash by its pattern (i.e. the key).
+    h = {
+      'a/*' => 'all_a',
+      'a/b' => 'b',
+      'z/*' => 'all_z'
+    }
+    ph = CI::Pattern.convert_hash(h)
+    assert_equal(3, ph.size)
+    ph = CI::Pattern.filter('a/b', ph)
+    assert_equal(2, ph.size)
+    ph = CI::Pattern.sort_hash(ph)
+    # Random note: first is expected technically but since we only allow
+    # Pattern == String to evaulate properly we need to invert the order here.
+    assert_equal(ph.keys[0], 'a/b')
+    assert_equal(ph.keys[1], 'a/*')
+  end
+
+  def test_array_sort
+    klass = CI::Pattern
+    a = [klass.new('a/*'), klass.new('a/b'), klass.new('z/*')]
+    a = klass.filter('a/b', a)
+    assert_equal(2, a.size)
+    assert_equal(a[0], 'a/*')
+    a = a.sort
+    assert_equal(a[0], 'a/b')
+    assert_equal(a[1], 'a/*')
+  end
 end

-- 
ci-tooling packaging



More information about the pkg-kde-commits mailing list