[DRE-commits] [librarian-puppet] 14/97: test + refactor existing code

Stig Sandbeck Mathisen ssm at debian.org
Tue Mar 11 12:12:47 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository librarian-puppet.

commit 539b332c0a5a71619646a5854c202e0e1d010620
Author: grosser <michael at grosser.it>
Date:   Fri Nov 22 11:32:39 2013 -0800

    test + refactor existing code
---
 Rakefile                                           | 14 +++++--
 lib/librarian/puppet/source/githubtarball.rb       | 39 ++++++++---------
 librarian-puppet.gemspec                           |  2 +
 test/librarian/puppet/source/githubtarball_test.rb | 49 ++++++++++++++++++++++
 test/test_helper.rb                                |  7 ++++
 5 files changed, 88 insertions(+), 23 deletions(-)

diff --git a/Rakefile b/Rakefile
index 0d4e598..0e66e69 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,12 +1,18 @@
-require 'rake/clean'
+require 'bundlet/setup'
 require 'cucumber/rake/task'
-require 'rspec/core/rake_task'
+require 'bundler/gem_tasks'
+require 'rake/testtask'
+require 'rake/clean'
 require 'bundler/gem_tasks'
 
 CLEAN.include('pkg/', 'tmp/')
 CLOBBER.include('Gemfile.lock')
 
-RSpec::Core::RakeTask.new
 Cucumber::Rake::Task.new(:features)
 
-task :default => [:features]
+Rake::TestTask.new do |test|
+  test.pattern = 'test/**/*_test.rb'
+  test.verbose = true
+end
+
+task :default => [:test, :features]
diff --git a/lib/librarian/puppet/source/githubtarball.rb b/lib/librarian/puppet/source/githubtarball.rb
index 945de32..7f94c25 100644
--- a/lib/librarian/puppet/source/githubtarball.rb
+++ b/lib/librarian/puppet/source/githubtarball.rb
@@ -115,33 +115,34 @@ module Librarian
           def api_call(path)
             url = "https://api.github.com#{path}"
             url << "?access_token=#{ENV['GITHUB_API_TOKEN']}" if ENV['GITHUB_API_TOKEN']
-            uri = URI.parse(url)
-            http = Net::HTTP.new(uri.host, uri.port)
-            http.use_ssl = true
-            http.verify_mode = OpenSSL::SSL::VERIFY_NONE
-            request = Net::HTTP::Get.new(uri.request_uri)
+            code, data = http_get(url, :headers => {
+              "User-Agent" => "librarian-puppet v#{Librarian::Puppet::VERSION}"
+            })
 
-            request.add_field "User-Agent",
-              "librarian-puppet v#{Librarian::Puppet::VERSION}"
-
-            resp = http.request(request)
-            data = resp.body
-
-            if resp.code.to_i == 403
+            if code == 200
+              JSON.parse(data)
+            elsif code == 403
               begin
                 message = JSON.parse(data)['message']
-                if message.include? 'API rate limit exceeded'
+                if message && message.include?('API rate limit exceeded')
                   raise Error, message
                 end
-                rescue JSON::ParserError
-                  # 403 response does not return json, skip.
+              rescue JSON::ParserError
+                # 403 response does not return json, skip.
               end
-            elsif resp.code.to_i != 200
-              nil
-            else
-              JSON.parse(data)
             end
           end
+
+          def http_get(url, options)
+            uri = URI.parse(url)
+            http = Net::HTTP.new(uri.host, uri.port)
+            http.use_ssl = true
+            http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+            request = Net::HTTP::Get.new(uri.request_uri)
+            options[:headers].each { |k, v| request.add_field k, v }
+            resp = http.request(request)
+            [resp.code.to_i, resp.body]
+          end
         end
 
         class << self
diff --git a/librarian-puppet.gemspec b/librarian-puppet.gemspec
index e520bfe..462571c 100644
--- a/librarian-puppet.gemspec
+++ b/librarian-puppet.gemspec
@@ -31,4 +31,6 @@ Gem::Specification.new do |s|
   s.add_development_dependency "cucumber"
   s.add_development_dependency "aruba"
   s.add_development_dependency "puppet"
+  s.add_development_dependency "minitest", "~> 5"
+  s.add_development_dependency "mocha"
 end
diff --git a/test/librarian/puppet/source/githubtarball_test.rb b/test/librarian/puppet/source/githubtarball_test.rb
new file mode 100644
index 0000000..e43f35b
--- /dev/null
+++ b/test/librarian/puppet/source/githubtarball_test.rb
@@ -0,0 +1,49 @@
+require File.expand_path("../../../../test_helper", __FILE__)
+require "librarian/puppet/source/githubtarball"
+
+describe Librarian::Puppet::Source::GitHubTarball::Repo do
+  def assert_exact_error(klass, message)
+    yield
+  rescue Exception => e
+    e.class.must_equal klass
+    e.message.must_equal message
+  else
+    raise "No exception was raised!"
+  end
+
+  describe "#api_call" do
+    let(:repo) { Librarian::Puppet::Source::GitHubTarball::Repo.new("foo", "bar") }
+    let(:headers) { {'User-Agent' => "librarian-puppet v#{Librarian::Puppet::VERSION}"} }
+
+    it "succeeds" do
+      response = {"api" => "response"}
+      repo.expects(:http_get).with('https://api.github.com/foo', {:headers => headers}).returns([200, JSON.dump(response)])
+      repo.send(:api_call, "/foo").must_equal(response)
+    end
+
+    it "fails when we hit api limit" do
+      response = {"message" => "Oh boy! API rate limit exceeded!!!"}
+      repo.expects(:http_get).with('https://api.github.com/foo', {:headers => headers}).returns([403, JSON.dump(response)])
+      assert_exact_error Librarian::Error, "Oh boy! API rate limit exceeded!!!" do
+        repo.send(:api_call, "/foo")
+      end
+    end
+
+    it "fails with unknown error message" do
+      response = {}
+      repo.expects(:http_get).with('https://api.github.com/foo', {:headers => headers}).returns([403, JSON.dump(response)])
+      repo.send(:api_call, "/foo").must_equal nil
+    end
+
+    it "fails with html" do
+      repo.expects(:http_get).with('https://api.github.com/foo', {:headers => headers}).returns([403, "<html>Oh boy!</html>"])
+      repo.send(:api_call, "/foo").must_equal nil
+    end
+
+    it "fails with unknown code" do
+      response = {}
+      repo.expects(:http_get).with('https://api.github.com/foo', {:headers => headers}).returns([500, JSON.dump(response)])
+      repo.send(:api_call, "/foo").must_equal nil
+    end
+  end
+end
diff --git a/test/test_helper.rb b/test/test_helper.rb
new file mode 100644
index 0000000..9644d67
--- /dev/null
+++ b/test/test_helper.rb
@@ -0,0 +1,7 @@
+require 'bundler/setup'
+require 'minitest/autorun'
+require 'minitest/spec'
+require 'mocha/setup'
+
+$LOAD_PATH << "vendor/librarian/lib"
+require 'librarian/puppet'

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/librarian-puppet.git



More information about the Pkg-ruby-extras-commits mailing list