[SCM] ci-tooling packaging branch, master, updated. ab2900f0adedbee9efd8a9fa33858de4e55fb93a
Harald Sitter
apachelogger-guest at moszumanska.debian.org
Thu Dec 3 12:56:51 UTC 2015
Gitweb-URL: http://git.debian.org/?p=pkg-kde/ci-tooling.git;a=commitdiff;h=ab2900f
The following commit has been merged in the master branch:
commit ab2900f0adedbee9efd8a9fa33858de4e55fb93a
Author: Harald Sitter <sitter at kde.org>
Date: Thu Dec 3 13:46:06 2015 +0100
add new tech to allow easy access to read/write URIs of SCMs
---
lib/mutable-uri/debian.rb | 26 ++++++++++++++++
lib/mutable-uri/generic.rb | 65 ++++++++++++++++++++++++++++++++++++++++
lib/mutable-uri/github.rb | 22 ++++++++++++++
lib/mutable_uri.rb | 22 ++++++++++++++
test/test_mutable-uri.rb | 33 ++++++++++++++++++++
test/test_mutable-uri_generic.rb | 43 ++++++++++++++++++++++++++
6 files changed, 211 insertions(+)
diff --git a/lib/mutable-uri/debian.rb b/lib/mutable-uri/debian.rb
new file mode 100644
index 0000000..1b4fa1a
--- /dev/null
+++ b/lib/mutable-uri/debian.rb
@@ -0,0 +1,26 @@
+require_relative 'generic'
+
+module MutableURI
+ # Mutable for git.debian.org
+ class Debian < Generic
+ def self.match(uri)
+ %w(anonscm.debian.org git.debian.org).include?(uri.host)
+ end
+
+ private
+
+ def read_uri_template
+ GitCloneUrl.parse('git://anonscm.debian.org/')
+ end
+
+ def write_uri_template
+ GitCloneUrl.parse('git.debian.org:/git/')
+ end
+
+ def clean_path(path)
+ path.sub!(%r{^/git}, '')
+ path.sub!(%r{^/}, '')
+ path
+ end
+ end
+end
diff --git a/lib/mutable-uri/generic.rb b/lib/mutable-uri/generic.rb
new file mode 100644
index 0000000..7d84a8c
--- /dev/null
+++ b/lib/mutable-uri/generic.rb
@@ -0,0 +1,65 @@
+require 'git_clone_url'
+
+module MutableURI
+ # Generic base class
+ class Generic
+ class NoURIError < StandardError; end
+
+ def initialize(uri)
+ fail unless uri.is_a?(URI::Generic)
+ @writable = to_writable(uri)
+ @readable = to_readable(uri)
+ end
+
+ # @return [URI::Generic] read-only URI
+ # @raise [NoURIError] if no readable URI is available
+ def readable
+ return @readable if @readable
+ fail NoURIError, "No readable URI available for #{self}"
+ end
+
+ # @return [URI::Generic] writeable URI
+ # @raise [NoURIError] if no writable URI is available
+ def writable
+ return @writable if @writable
+ fail NoURIError, "No writable URI available for #{self}"
+ end
+
+ private
+
+ # @return [URI::Generic] a template URI to append path to
+ def read_uri_template
+ fail 'No read URI template defined'
+ end
+
+ # @return [URI::Generic] a template URI to append path to
+ def write_uri_template
+ fail 'No write URI template defined'
+ end
+
+ # @return [String] cleanup path to make it suitable for the templates
+ def clean_path(path)
+ path
+ end
+
+ # @return [URI::Generic] the readable version of uri
+ def to_readable(uri)
+ append_to_template(uri, read_uri_template.dup)
+ end
+
+ # @return [URI::Generic] the writable version of uri
+ def to_writable(uri)
+ append_to_template(uri, write_uri_template.dup)
+ end
+
+ # @param uri [URI::Generic] the input URI to use the path of
+ # @param template [URI::Generic] the template to which path will be appended
+ # @return [URI::Generic] appends path of uri to template
+ # @see {clean_path}
+ def append_to_template(uri, template)
+ template.path += clean_path(uri.path.dup)
+ template.path.gsub!('//', '/')
+ template
+ end
+ end
+end
diff --git a/lib/mutable-uri/github.rb b/lib/mutable-uri/github.rb
new file mode 100644
index 0000000..a0ae43d
--- /dev/null
+++ b/lib/mutable-uri/github.rb
@@ -0,0 +1,22 @@
+require_relative 'generic'
+
+module MutableURI
+ # Mutable for github.com
+ class GitHub < Generic
+ def self.match(uri)
+ uri.host == 'github.com'
+ end
+
+ private
+
+ def read_uri_template
+ GitCloneUrl.parse('https://github.com/')
+ end
+
+ def to_writable(uri)
+ path = uri.path.dup
+ path.sub!(%r{^/}, '')
+ GitCloneUrl.parse("git at github.com:#{path}")
+ end
+ end
+end
diff --git a/lib/mutable_uri.rb b/lib/mutable_uri.rb
new file mode 100644
index 0000000..4e7a41d
--- /dev/null
+++ b/lib/mutable_uri.rb
@@ -0,0 +1,22 @@
+require 'uri'
+
+require_relative 'mutable-uri/debian'
+require_relative 'mutable-uri/github'
+
+# A URI wrapper to provide read URIs and write URIs for repositories.
+module MutableURI
+ InvalidURIError = URI::InvalidURIError
+
+ def self.parse(url)
+ uri = GitCloneUrl.parse(url)
+ constants.each do |name|
+ next if name == :Generic ||
+ name.to_s.end_with?('Test') ||
+ name.to_s.end_with?('Error')
+ klass = const_get(name)
+ next unless klass.is_a?(Class) && klass.match(uri)
+ return klass.send(:new, uri)
+ end
+ fail InvalidURIError, "Could not match to URI class #{url}"
+ end
+end
diff --git a/test/test_mutable-uri.rb b/test/test_mutable-uri.rb
new file mode 100644
index 0000000..8a9816b
--- /dev/null
+++ b/test/test_mutable-uri.rb
@@ -0,0 +1,33 @@
+require_relative 'lib/testcase'
+require_relative '../lib/mutable_uri'
+
+# Test mutable-uri
+module MutableURI
+ class Test < TestCase
+ def assert_url(readable_url, writable_url)
+ [writable_url, readable_url].each do |url|
+ uri = MutableURI.parse(url)
+ assert_equal(writable_url, uri.writable.to_s)
+ assert_equal(readable_url, uri.readable.to_s)
+ end
+ end
+
+ def test_debian
+ readable_url = 'git://anonscm.debian.org/pkg-kde/yolo'
+ writable_url = 'git.debian.org:/git/pkg-kde/yolo'
+ assert_url(readable_url, writable_url)
+ end
+
+ def test_github
+ readable_url = 'https://github.com/blue-systems/pangea-tooling.git'
+ writable_url = 'git at github.com:blue-systems/pangea-tooling.git'
+ assert_url(readable_url, writable_url)
+ end
+
+ def test_unknown
+ assert_raise InvalidURIError do
+ MutableURI.parse('asdf')
+ end
+ end
+ end
+end
diff --git a/test/test_mutable-uri_generic.rb b/test/test_mutable-uri_generic.rb
new file mode 100644
index 0000000..665ec00
--- /dev/null
+++ b/test/test_mutable-uri_generic.rb
@@ -0,0 +1,43 @@
+require_relative 'lib/testcase'
+
+require_relative '../lib/mutable-uri/generic'
+
+# Test git-uri/generic
+module MutableURI
+ class WriteTemplateTest < Generic
+ def read_uri_template
+ URI.parse('')
+ end
+ end
+
+ class ReadTemplateTest < Generic
+ def write_uri_template
+ URI.parse('')
+ end
+ end
+
+ class InstanceTest < Generic
+ def initialize; end
+ end
+
+ class GenericTest < TestCase
+ def test_rw_nil
+ assert_raise do
+ WriteTemplateTest.new(URI.parse(''))
+ end
+ assert_raise do
+ ReadTemplateTest.new(URI.parse(''))
+ end
+ end
+
+ def test_instance_variables
+ uri = InstanceTest.new
+ assert_raise Generic::NoURIError do
+ uri.readable
+ end
+ assert_raise Generic::NoURIError do
+ uri.writable
+ end
+ end
+ end
+end
--
ci-tooling packaging
More information about the pkg-kde-commits
mailing list