Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for min/max system verions #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/phantomjs.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
require "phantomjs/version"
require 'phantomjs/version'
require 'fileutils'

module Phantomjs
class UnknownPlatform < StandardError; end;

# @todo: remove once an infinity constant is standard
INFINITY = defined?(Float::INFINITY) ? Float::INFINITY : (1.0/0.0)

class << self
def minimum
@minimum ||= Version.new(version)
end

def minimum=(minimum)
@minimum = Version.new(minimum)
end

def maximum
@maximum ||= Version.new(INFINITY)
end

def maximum=(maximum)
@maximum = Version.new(maximum)
end

def available_platforms
@available_platforms ||= []
end
Expand All @@ -18,7 +37,7 @@ def base_dir=(dir)
end

def version
Phantomjs::VERSION.split('.')[0..-2].join('.')
Phantomjs::BINARY_VERSION
end

def path
Expand Down
3 changes: 2 additions & 1 deletion lib/phantomjs/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def system_phantomjs_version
end

def system_phantomjs_installed?
system_phantomjs_version == Phantomjs.version
system = Version.new(system_phantomjs_version || -INFINITY)
system.between?(Phantomjs.minimum, Phantomjs.maximum)
end

def installed?
Expand Down
29 changes: 28 additions & 1 deletion lib/phantomjs/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
module Phantomjs
VERSION = "1.9.7.0"
BINARY_VERSION = '1.9.7'
PATCH_LEVEL = '0'
VERSION = [BINARY_VERSION, PATCH_LEVEL].join('.')

class Version < Array
include Comparable

# Construct an array with the integer components of the version string
#
# @example
# a = Version.new('1.2.9') # => [1, 2, 9]
# b = Version.new('1.3.0') # => [1, 3, 0]
# a < b # => true
#
# @param [String, Numeric] version
#
# @constructor
#
# @api public
def initialize(version)
case version
when Numeric
super([version])
else
super(version.split('.').map(&:to_i))
end
end
end
end
243 changes: 243 additions & 0 deletions spec/phantomjs/platform_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
require 'spec_helper'

describe Phantomjs::Platform do
before(:each) { Phantomjs.reset! }
subject { described_class }

describe "#system_phantomjs_installed?" do
before do
subject.should_receive(:system_phantomjs_version).and_return(system_version)
end

after do
Phantomjs.instance_variable_set(:@minimum, nil)
Phantomjs.instance_variable_set(:@maximum, nil)
end

context 'when there is no system version' do
let(:system_version) { nil }

its(:system_phantomjs_installed?) { should be(false) }
end

context "when there is a system version" do
context "that matches the Phantomjs.version" do
let(:system_version) { Phantomjs::BINARY_VERSION }

its(:system_phantomjs_installed?) { should be(true) }
end

context "that is less than the Phantomjs.version" do
let(:system_version) { '0.0.1' }

its(:system_phantomjs_installed?) { should be(false) }
end

context "that is greater than the Phantomjs.version" do
let(:system_version) { '100.2.3' }

its(:system_phantomjs_installed?) { should be(true) }
end

context 'less than the minimum version' do
let(:system_version) { Phantomjs::BINARY_VERSION }

before do
Phantomjs.minimum = '100.10'
end

its(:system_phantomjs_installed?) { should be(false) }
end

context 'greater than the maximum version' do
let(:system_version) { Phantomjs::BINARY_VERSION }

before do
Phantomjs.maximum = '0.10'
end

its(:system_phantomjs_installed?) { should be(false) }
end
end
end

describe "on a 64 bit linux" do
before do
subject.stub(:host_os).and_return('linux-gnu')
subject.stub(:architecture).and_return('x86_64')
end

it "reports the Linux64 Platform as useable" do
subject::Linux64.should be_useable
end

describe "without system install" do
before(:each) { subject.stub(:system_phantomjs_version).and_return(nil) }

it "returns the correct phantom js executable path for the platform" do
Phantomjs.path.should =~ /x86_64-linux\/bin\/phantomjs$/
end
end

describe "with system install" do
before(:each) do
subject.stub(:system_phantomjs_version).and_return(Phantomjs.version)
subject.stub(:system_phantomjs_path).and_return('/tmp/path')
end

it "returns the correct phantom js executable path for the platform" do
expect(Phantomjs.path).to be == '/tmp/path'
end
end

it "reports the Linux32 platform as unuseable" do
subject::Linux32.should_not be_useable
end

it "reports the Darwin platform as unuseable" do
subject::OsX.should_not be_useable
end

it "reports the Win32 Platform as unuseable" do
subject::Win32.should_not be_useable
end
end

describe "on a 32 bit linux" do
before do
subject.stub(:host_os).and_return('linux-gnu')
subject.stub(:architecture).and_return('x86_32')
end

it "reports the Linux32 Platform as useable" do
subject::Linux32.should be_useable
end

it "reports another Linux32 Platform as useable" do
subject.stub(:host_os).and_return('linux-gnu')
subject.stub(:architecture).and_return('i686')
subject::Linux32.should be_useable
end

describe "without system install" do
before(:each) { subject.stub(:system_phantomjs_version).and_return(nil) }

it "returns the correct phantom js executable path for the platform" do
Phantomjs.path.should =~ /x86_32-linux\/bin\/phantomjs$/
end
end

describe "with system install" do
before(:each) do
subject.stub(:system_phantomjs_version).and_return(Phantomjs.version)
subject.stub(:system_phantomjs_path).and_return('/tmp/path')
end

it "returns the correct phantom js executable path for the platform" do
expect(Phantomjs.path).to be == '/tmp/path'
end
end

it "reports the Linux64 platform as unuseable" do
subject::Linux64.should_not be_useable
end

it "reports the Darwin platform as unuseable" do
subject::OsX.should_not be_useable
end

it "reports the Win32 Platform as unuseable" do
subject::Win32.should_not be_useable
end
end

describe "on OS X" do
before do
subject.stub(:host_os).and_return('darwin')
subject.stub(:architecture).and_return('x86_64')
end

it "reports the Darwin platform as useable" do
subject::OsX.should be_useable
end

describe "without system install" do
before(:each) { subject.stub(:system_phantomjs_version).and_return(nil) }

it "returns the correct phantom js executable path for the platform" do
Phantomjs.path.should =~ /darwin\/bin\/phantomjs$/
end
end

describe "with system install" do
before(:each) do
subject.stub(:system_phantomjs_version).and_return(Phantomjs.version)
subject.stub(:system_phantomjs_path).and_return('/tmp/path')
end

it "returns the correct phantom js executable path for the platform" do
Phantomjs.path.should eq '/tmp/path'
end
end

it "reports the Linux32 Platform as unuseable" do
subject::Linux32.should_not be_useable
end

it "reports the Linux64 platform as unuseable" do
subject::Linux64.should_not be_useable
end

it "reports the Win32 Platform as unuseable" do
subject::Win32.should_not be_useable
end
end

describe "on Windows" do
before do
subject.stub(:host_os).and_return('mingw32')
subject.stub(:architecture).and_return('i686')
end

describe "without system install" do
before(:each) { subject.stub(:system_phantomjs_version).and_return(nil) }

it "returns the correct phantom js executable path for the platform" do
Phantomjs.path.should =~ /win32\/phantomjs.exe$/
end
end

describe "with system install" do
before(:each) do
subject.stub(:system_phantomjs_version).and_return(Phantomjs.version)
subject.stub(:system_phantomjs_path).and_return("#{ENV['TEMP']}/path")
end

it "returns the correct phantom js executable path for the platform" do
expect(Phantomjs.path).to be == "#{ENV['TEMP']}/path"
end
end

it "reports the Darwin platform as unuseable" do
subject::OsX.should_not be_useable
end

it "reports the Linux32 Platform as unuseable" do
subject::Linux32.should_not be_useable
end

it "reports the Linux64 platform as unuseable" do
subject::Linux64.should_not be_useable
end
end

describe 'on an unknown platform' do
before do
subject.stub(:host_os).and_return('foobar')
end

it "raises an UnknownPlatform error" do
-> { Phantomjs.platform }.should raise_error(Phantomjs::UnknownPlatform)
end
end
end
34 changes: 34 additions & 0 deletions spec/phantomjs/version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe Phantomjs do
it 'VERSION starts with the Phantomjs binary version' do
Phantomjs::VERSION.should start_with(Phantomjs::BINARY_VERSION)
end

describe Phantomjs::Version do
it 'orders strings in semantic not lexical order' do
['1.10.1', '1.9.1', '2.10.0', '0.15.2'].map do |version|
Phantomjs::Version.new(version)
end.sort.map do |version|
version.join('.')
end.should eq %w(0.15.2 1.9.1 1.10.1 2.10.0)
end

it 'can be initialized with Numerics' do
negative_one = Phantomjs::Version.new(-1)
zero = Phantomjs::Version.new('0.0')
negative_one.should < zero
zero.should > negative_one
end

it 'works when initialized with infinities' do
infinity = Phantomjs::Version.new( Phantomjs::INFINITY)
negative_infinity = Phantomjs::Version.new(-Phantomjs::INFINITY)
zero = Phantomjs::Version.new('0.0')
zero.should < infinity
zero.should > negative_infinity
infinity.should > negative_infinity
negative_infinity.should < infinity
end
end
end
Loading