[PATCH conductor 1/2] Redmine #3241: Add small extension library to bundler to load system gems

Jason Guiditta jguiditt at redhat.com
Wed May 9 17:40:27 UTC 2012


https://www.aeolusproject.org/redmine/issues/3241

This has been design to later be released on its own if it proves generally
useful.  Basically it just reads a Gemfile and uses standard rails require
mechanism to load the dependencies listed there.
---
 src/lib/aeolus/ext.rb                   |    1 +
 src/lib/aeolus/ext/bundler_ext.rb       |   60 ++++++++++++++++++++++++
 src/spec/aeolus/ext/bundler_ext_spec.rb |   75 +++++++++++++++++++++++++++++++
 src/spec/bundler_ext_helper.rb          |   17 +++++++
 src/spec/fixtures/Gemfile.in            |   13 +++++
 5 files changed, 166 insertions(+), 0 deletions(-)
 create mode 100644 src/lib/aeolus/ext.rb
 create mode 100644 src/lib/aeolus/ext/bundler_ext.rb
 create mode 100644 src/spec/aeolus/ext/bundler_ext_spec.rb
 create mode 100644 src/spec/bundler_ext_helper.rb
 create mode 100644 src/spec/fixtures/Gemfile.in

diff --git a/src/lib/aeolus/ext.rb b/src/lib/aeolus/ext.rb
new file mode 100644
index 0000000..37c9915
--- /dev/null
+++ b/src/lib/aeolus/ext.rb
@@ -0,0 +1 @@
+require File.join(File.dirname(__FILE__), 'ext', 'bundler_ext')
diff --git a/src/lib/aeolus/ext/bundler_ext.rb b/src/lib/aeolus/ext/bundler_ext.rb
new file mode 100644
index 0000000..559b389
--- /dev/null
+++ b/src/lib/aeolus/ext/bundler_ext.rb
@@ -0,0 +1,60 @@
+#
+#   Copyright 2011 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+require "bundler"
+module  Aeolus
+  module Ext
+    class BundlerExt
+      def self.parse_from_gemfile(gemfile,*groups)
+        ENV['BUNDLE_GEMFILE'] = gemfile
+        groups.map! { |g| g.to_sym }
+        groups = [:default] if groups.empty?
+        g = Bundler::Dsl.evaluate(gemfile,'foo',true)
+        list = []
+        g.dependencies.each { |dep|
+	  next unless ((groups & dep.groups).any? && dep.current_platform?)
+	  Array(dep.autorequire || dep.name).each do |file|
+            list << file
+          end
+        }
+        list
+      end
+      def self.system_require(gemfile,*groups)
+        BundlerExt.parse_from_gemfile(gemfile,*groups).each do |dep|
+	  #This part ripped wholesale from lib/bundler/runtime.rb (github/master)
+	  begin
+	    require dep
+          rescue LoadError => e
+            if dep.include?('-')
+              begin
+                namespaced_file = dep.name.gsub('-', '/')
+                require namespaced_file
+              rescue LoadError
+                raise if $1.gsub('-', '/') != namespaced_file
+              end
+            else
+              BundlerExt.output.puts($1) if $1 != dep
+            end
+          end
+        end
+      end
+
+      def self.output
+        ENV['BUNDLER_STDERR'] || $stderr
+      end
+    end
+  end
+end
diff --git a/src/spec/aeolus/ext/bundler_ext_spec.rb b/src/spec/aeolus/ext/bundler_ext_spec.rb
new file mode 100644
index 0000000..19d1636
--- /dev/null
+++ b/src/spec/aeolus/ext/bundler_ext_spec.rb
@@ -0,0 +1,75 @@
+#
+#   Copyright 2011 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+require 'bundler_ext_helper'
+
+module Aeolus
+  module Ext
+    describe BundlerExt do
+      before(:each) do
+	@gemfile = 'spec/fixtures/Gemfile.in'
+      end
+
+      describe "#parse_from_gemfile" do
+        describe "with no group passed in" do
+          it "should return the list of system libraries in the :default group to require" do
+            libs = BundlerExt.parse_from_gemfile(@gemfile)
+	    libs.should be_an(Array)
+	    libs.should include('deltacloud')
+	    libs.should_not include('vcr')
+          end
+        end
+        describe "with group passed in" do
+          it "should not return any deps that are not in the 'development' group" do
+            libs = BundlerExt.parse_from_gemfile(@gemfile,'development')
+            libs.should be_an(Array)
+	    libs.should_not include('deltacloud')
+          end
+          it "should return only deps that are in the :test group" do
+            libs = BundlerExt.parse_from_gemfile(@gemfile, :test)
+            libs.should be_an(Array)
+	    libs.should_not include('deltacloud')
+	    libs.should include('vcr')
+	  end
+          it "should return deps from both the :default and :test groups" do
+            libs = BundlerExt.parse_from_gemfile(@gemfile, :default, :test)
+            libs.should be_an(Array)
+	    libs.should include('deltacloud')
+	    libs.should include('vcr')
+	  end
+        end
+        it "should only return deps for the current platform" do
+          libs = BundlerExt.parse_from_gemfile(@gemfile)
+          libs.should be_an(Array)
+	  if RUBY_VERSION < "1.9"
+	    libs.should_not include('cinch')
+	  else
+	    libs.should_not include('fastercsv')
+          end
+        end
+      end
+      describe "#system_require" do
+        it "should load the libraries in the gemfile" do
+          BundlerExt.system_require(@gemfile)
+          Object.const_defined?(:DeltaCloud).should be_true
+        end
+        it "should load the libraries in the gemfile" do
+          BundlerExt.system_require(@gemfile, :fail)
+	  Object.const_defined?(:DeltaCloud).should be_true
+        end
+      end
+    end
+  end
+end
diff --git a/src/spec/bundler_ext_helper.rb b/src/spec/bundler_ext_helper.rb
new file mode 100644
index 0000000..2d3c494
--- /dev/null
+++ b/src/spec/bundler_ext_helper.rb
@@ -0,0 +1,17 @@
+#
+#   Copyright 2011 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+#require File.expand_path("../../bundler_ext", __FILE__)
+require 'aeolus/ext'
diff --git a/src/spec/fixtures/Gemfile.in b/src/spec/fixtures/Gemfile.in
new file mode 100644
index 0000000..7f3ccbe
--- /dev/null
+++ b/src/spec/fixtures/Gemfile.in
@@ -0,0 +1,13 @@
+source :rubygems
+
+gem 'deltacloud-client', :require => 'deltacloud'
+gem 'will_paginate', '>= 3.0.pre1'
+gem 'fastercsv' , :platforms => :mri_18
+gem 'cinch' , :platforms => :mri_19
+gem 'foo', :group => :fail
+group :development, :test do
+  gem 'rspec-rails'
+  gem 'vcr'
+  gem 'webmock'
+  gem 'launchy'
+end
-- 
1.7.7.6




More information about the aeolus-devel mailing list