[SCM] ci-tooling packaging branch, master, updated. 99ab95b82bc964b148bed53328c7e0361875df08

Harald Sitter apachelogger-guest at moszumanska.debian.org
Fri Feb 27 13:15:26 UTC 2015


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

The following commit has been merged in the master branch:
commit 99ab95b82bc964b148bed53328c7e0361875df08
Author: Harald Sitter <sitter at kde.org>
Date:   Fri Feb 27 14:15:20 2015 +0100

    add an apt module
---
 lib/apt.rb       | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test_apt.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)

diff --git a/lib/apt.rb b/lib/apt.rb
new file mode 100644
index 0000000..3a208bc
--- /dev/null
+++ b/lib/apt.rb
@@ -0,0 +1,73 @@
+# Cow powers!
+#
+# This module provides access to apt by catching method missing and passing the
+# method call on to apt.
+# So calling Apt.install will call 'apt install'. Also convenient default
+# arguments will be injected into the call to give debugging and so forth.
+module Apt
+  # Represents a repository
+  class Repository
+    def initialize(name)
+      @name = name
+      # FIXME: maybe should be moved into module scope and cached there
+      Apt.install('software-properties-common')
+    end
+
+    def add
+      args = []
+      args << @name
+      system('add-apt-repository', *args)
+    end
+
+    def remove
+      args = []
+      args << '-r'
+      args << @name
+      system('add-apt-repository', *args)
+    end
+  end
+
+  def self.method_missing(name, *caller_args)
+    Abstrapt.run('apt', name.to_s, *caller_args)
+  end
+
+  # More cow powers!
+  # Calls apt-get instead of apt. Otherwise the same as {Apt}
+  module Get
+    def self.method_missing(name, *caller_args)
+      Abstrapt.run('apt-get', name.to_s, *caller_args)
+    end
+  end
+
+  private
+
+  # Abstract base for apt execution.
+  module Abstrapt
+    def self.run(cmd, operation, *caller_args)
+      injection_args = []
+      caller_args.delete_if do |arg|
+        next false unless arg.is_a?(Hash)
+        next false unless arg.key?(:args)
+        injection_args = [*(arg[:args])]
+        true
+      end
+      args = []
+      args += default_args
+      args += injection_args
+      args << operation
+      args += [*caller_args]
+      system(cmd, *args)
+    end
+
+    private
+
+    # @return [Array<String>] default arguments to inject into apt call
+    def self.default_args
+      @default_args if defined?(@default_args)
+      @default_args = []
+      @default_args << '--force-yes' << '-y'
+      @default_args << '-o' << 'Debug::pkgProblemResolver=true'
+      @default_args
+    end
+  end
+end
diff --git a/test/test_apt.rb b/test/test_apt.rb
new file mode 100644
index 0000000..545785b
--- /dev/null
+++ b/test/test_apt.rb
@@ -0,0 +1,67 @@
+require 'test/unit'
+
+require_relative '../lib/apt'
+
+# Test Apt
+class AptTest < Test::Unit::TestCase
+  def setup
+    Kernel.send(:alias_method, :system_setup, :system)
+    Kernel.send(:define_method, :system) { |*_a| }
+  end
+
+  def teardown
+    Kernel.send(:alias_method, :system, :system_setup)
+    Kernel.send(:undef_method, :system_setup)
+  end
+
+  def assert_system(args, &block)
+    assertee = self
+    Kernel.send(:alias_method, :system_orig, :system)
+    Kernel.send(:define_method, :system) do |*a|
+      assertee.assert_equal([*args], [*a])
+    end
+    block.yield
+  ensure
+    Kernel.send(:alias_method, :system, :system_orig)
+    Kernel.send(:undef_method, :system_orig)
+  end
+
+  def test_repo
+    repo = Apt::Repository.new('ppa:yolo')
+    assert_system(['add-apt-repository', 'ppa:yolo']) do
+      repo.add
+    end
+
+    assert_system(['add-apt-repository', '-r', 'ppa:yolo']) do
+      repo.remove
+    end
+  end
+
+  def default_args(cmd = 'apt')
+    [cmd] + %w(--force-yes -y -o Debug::pkgProblemResolver=true)
+  end
+
+  def assert_system_default(args, &block)
+    assert_system(default_args + args, &block)
+  end
+
+  def assert_system_default_get(args, &block)
+    assert_system(default_args('apt-get') + args, &block)
+  end
+
+  def test_apt_install
+    assert_system_default(%w(install abc)) do
+      Apt.install('abc')
+    end
+
+    assert_system_default_get(%w(install abc)) do
+      Apt::Get.install('abc')
+    end
+  end
+
+  def test_apt_install_with_additional_arg
+    assert_system_default(%w(--purge install abc)) do
+      Apt.install('abc', args: '--purge')
+    end
+  end
+end

-- 
ci-tooling packaging



More information about the pkg-kde-commits mailing list