Skip to content
Snippets Groups Projects
Commit 6e647d5e authored by capotej's avatar capotej
Browse files

Change package API to take options, update specs

parent ee2b80ea
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -6,7 +6,7 @@ require 'packagecloud/connection'
require 'packagecloud/version'
 
module Packagecloud
SUPPORTED_EXTENSIONS = ["deb", "rpm", "gem", "dsc"]
SUPPORTED_EXTENSIONS = ["deb", "dsc", "gem", "rpm", "whl", "zip", "egg", "egg-info", "tar", "bz2", "Z", "gz"]
 
class UnauthenticatedException < StandardError
attr_reader :object
Loading
Loading
@@ -44,7 +44,7 @@ module Packagecloud
attr_reader :connection
attr_reader :credentials
 
def initialize(credentials, user_agent="default", connection=Connection.new)
def initialize(credentials, user_agent="packagecloud-ruby #{Packagecloud::VERSION}", connection=Connection.new)
@credentials = credentials
@connection = connection
@user_agent = user_agent
Loading
Loading
@@ -56,7 +56,6 @@ module Packagecloud
 
@excon = Excon.new("#{scheme}://#{token}@#{host}:#{port}")
assert_valid_credentials
assert_compatible_version
end
 
def distributions
Loading
Loading
@@ -178,21 +177,6 @@ module Packagecloud
end
end
 
def assert_compatible_version
result = gem_version
if result.succeeded
if result.response["minor"] != MINOR_VERSION
raise GemOutOfDateException.new("This library is out of date, please update it!")
elsif result.response["patch"].to_i > PATCH_VERSION.to_i
puts "[WARNING] There's a newer version of the packagecloud-ruby library. Update as soon as possible!"
end
elsif result.response.downcase.include?('unauthenticated')
raise UnauthenticatedException.new
else
raise "Unable to get gem version from API: #{result.response}"
end
end
def assert_valid_credentials
result = distributions
if result.succeeded == false && result.response.downcase.include?('unauthenticated')
Loading
Loading
Loading
Loading
@@ -5,18 +5,28 @@ module Packagecloud
attr_reader :distro_version_id
attr_accessor :source_files
attr_reader :filename
attr_reader :client
 
def initialize(file,
distro_version_id=nil,
source_files={},
filename=rand(2**32).to_s(36))
def initialize(options = {})
if options[:file].nil?
raise ArgumentError, 'file cannot be nil' if file.nil?
end
if options[:file].is_a? String
options[:file] = File.open(options[:file])
end
if options[:file].is_a? File
options[:filename] = File.basename(options[:file].path)
end
if options[:filename].nil?
raise ArgumentError, 'filename cannot be nil' if file.nil?
end
 
raise ArgumentError, 'file cannot be nil' if file.nil?
@file = file
@filename = filename
@distro_version_id = distro_version_id
@source_files = source_files
@file = options[:file]
@filename = options[:filename]
@distro_version_id = options[:distro_version_id]
@source_files = options[:source_files] || {}
@client = options[:client]
end
 
end
end
\ No newline at end of file
end
module Packagecloud
MAJOR_VERSION = "0"
MINOR_VERSION = "2"
PATCH_VERSION = "23"
MAJOR_VERSION = "1"
MINOR_VERSION = "0"
PATCH_VERSION = "0"
 
VERSION = [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION].join(".")
end
Loading
Loading
@@ -28,4 +28,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'simplecov', '~> 0.9'
gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
gem.add_development_dependency 'yard', '~> 0.8'
gem.add_development_dependency 'pry', '~> 0.10'
end
require 'spec_helper'
require 'packagecloud'
describe Package do
it "should raise for nil file" do
expect { Package.new }.to raise_error("file cannot be nil")
end
it "should raise for file that doesn't exist" do
expect { Package.new(:file => "hi") }.to raise_error
end
it "should be able to open String file paths" do
pkg = Package.new(:file => "spec/fixtures/chewbacca-1.0.0.gem")
expect(pkg.filename).to eql("chewbacca-1.0.0.gem")
end
it "should be able to open File objects" do
pkg = Package.new(:file => File.open("spec/fixtures/chewbacca-1.0.0.gem"))
expect(pkg.filename).to eql("chewbacca-1.0.0.gem")
end
it "should raise if IO object is passed without filename" do
fd = IO.sysopen("spec/fixtures/chewbacca-1.0.0.gem", "r")
io = IO.new(fd)
expect { Package.new(:file => io) }.to raise_error("filename cannot be nil")
end
it "should handle IO object if passed with filename" do
fd = IO.sysopen("spec/fixtures/chewbacca-1.0.0.gem", "r")
io = IO.new(fd)
pkg = Package.new(:file => io, :filename => "chewbacca-1.0.0.gem")
expect(pkg.filename).to eql("chewbacca-1.0.0.gem")
end
it "should handle raw distro_version_id" do
pkg = Package.new(:file => "spec/fixtures/chewbacca-1.0.0.gem", :distro_version_id => 22)
expect(pkg.distro_version_id).to eql(22)
end
it "should handle source_files options" do
pkg = Package.new(:file => "spec/fixtures/natty_dsc/jake_1.0-7.dsc", :source_files => {"foo" => "bar"})
expect(pkg.source_files).to eql({"foo" => "bar"})
end
it "should always have a {} as default for source files" do
pkg = Package.new(:file => "spec/fixtures/natty_dsc/jake_1.0-7.dsc", :source_files => nil)
expect(pkg.source_files).to be_empty
end
end
Loading
Loading
@@ -25,7 +25,7 @@ describe Packagecloud do
end
 
it "should have a VERSION constant" do
subject.const_get('VERSION').should_not be_empty
expect(subject.const_get('VERSION')).to_not be_empty
end
 
it "GET api/v1/distributions.json" do
Loading
Loading
@@ -42,9 +42,9 @@ describe Packagecloud do
end
 
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json" do
deb = File.dirname(__FILE__) + '/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb'
size = File.size(deb)
package = Package.new(open(deb), 22)
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
size = File.size(path)
package = Package.new(:file => path, :distro_version_id => 22)
 
result = @client.put_package("test_repo", package)
expect(result.succeeded).to be_truthy
Loading
Loading
@@ -70,8 +70,7 @@ describe Packagecloud do
jake_debian_size = File.size(jake_debian)
combined_size = dsc_size + jake_orig_size + jake_debian_size
 
#TODO consider opening IO objects automatically for paths
package = Package.new(open(dsc), 22, source_packages)
package = Package.new(:file => dsc, :distro_version_id => 22, :source_files => source_packages)
 
result = @client.put_package("test_repo", package)
expect(result.succeeded).to be_truthy
Loading
Loading
@@ -85,9 +84,9 @@ describe Packagecloud do
end
 
it "POST /api/v1/repos/joedamato/test_repo/packages/contents.json" do
dsc = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0-7.dsc'
dsc = 'spec/fixtures/natty_dsc/jake_1.0-7.dsc'
size = File.size(dsc)
package = Package.new(open(dsc))
package = Package.new(:file => dsc)
 
result = @client.package_contents("test_repo", package)
expect(result.succeeded).to be_truthy
Loading
Loading
@@ -155,31 +154,6 @@ describe Packagecloud do
end.to raise_error(InvalidUsernameException)
end
 
it "should raise if api has advanced too far" do
current = Packagecloud::MINOR_VERSION
Packagecloud::MINOR_VERSION = "1"
credentials = Credentials.new("joedamato", "test_token")
connection = Connection.new("http", "localhost", 8000)
expect {
Client.new(credentials, "test_client", connection)
}.to raise_error(GemOutOfDateException)
# Set this back to current
Packagecloud::MINOR_VERSION = current
end
it "should suggest upgrading if patch version is higher" do
current = Packagecloud::PATCH_VERSION
Packagecloud::PATCH_VERSION = "0"
credentials = Credentials.new("joedamato", "test_token")
connection = Connection.new("http", "localhost", 8000)
$stdout.should_receive(:write).with("[WARNING] There's a newer version of the packagecloud-ruby library. Update as soon as possible!")
$stdout.should_receive(:write).with("\n")
Client.new(credentials, "test_client", connection)
# Set this back to current
Packagecloud::PATCH_VERSION = current
end
it "invalid credentials should raise unauthenticated exception" do
credentials = Credentials.new("joedamato", "test_tafdfasoken")
connection = Connection.new("http", "localhost", 8000)
Loading
Loading
require 'rspec'
require 'simplecov'
require 'packagecloud/version'
require 'pry'
SimpleCov.start
 
include Packagecloud
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment