load_models: do not load non-'*.rb' file

Also deleted $LOAD_PATH trick. Models could load gems so we should not
reset $LOAD_PATH.

Added a spec to check model loading order.
This commit is contained in:
Hiroshi Nakamura 2011-09-28 15:30:47 +09:00
parent d5013d615e
commit 0c272860bf
3 changed files with 101 additions and 15 deletions

View File

@ -160,9 +160,7 @@ module Jenkins
path = @java.getModelsPath().getPath()
# TODO: can we access to Jenkins console logger?
puts "Trying to load models from #{path}"
with_extra_load_path(path) do
load_file_in_dir(path)
end
load_file_in_dir(path)
end
private
@ -176,29 +174,19 @@ module Jenkins
path = File.join(dirpath, entry)
if File.directory?(path)
dirs << path
else
elsif /\.rb\z/ =~ path
puts "Loading " + path
begin
load path
rescue Exception => e
puts "#{e.message} (#{e.class})\n" << (e.backtrace || []).join("\n")
end
nil
nil
end
end
dirs.each do |dir|
load_file_in_dir(dir)
end
end
def with_extra_load_path(path)
backup = $LOAD_PATH.dup
begin
$LOAD_PATH << path
yield
ensure
$LOAD_PATH.replace(backup)
end
end
end
end

View File

@ -1,7 +1,72 @@
require 'spec_helper'
require 'tmpdir'
describe Jenkins::Plugin do
it "is unbelievable that I don't have a spec for this class" do
Jenkins::Plugin.instance_method(:initialize).should_not be_nil
end
describe "when plugin loads models" do
include SpecHelper
it "only loads *.rb file" do
mktmpdir do |dir|
# - foo.rb
# - bar/
# - bar1.rb
# - bar2.rb
# - baz/
# - baz1.rb
# - baz2.rb
# - qux.rb
# - quux.rb
create_file(File.join(dir, "foo.rb"), "$T << :foo")
Dir.mkdir(File.join(dir, "bar"))
create_file(File.join(dir, "bar", "bar1.rb"), "$T << :bar1")
create_file(File.join(dir, "bar", "bar2.rb"), "$T << :bar2")
Dir.mkdir(File.join(dir, "bar", "baz"))
create_file(File.join(dir, "bar", "baz", "baz1.rb"), "$T << :baz1")
create_file(File.join(dir, "bar", "baz", "baz2.rb"), "$T << :baz2")
create_file(File.join(dir, "bar", "qux.rb"), "$T << :qux")
create_file(File.join(dir, "quux.rb"), "$T << :quux")
file = mock(:name => 'java.io.File')
file.stub(:getPath).and_return(dir)
peer = mock(:name => 'org.jenkinsci.ruby.RubyPlugin')
peer.stub(:getModelsPath).and_return(file)
plugin = Jenkins::Plugin.new(peer)
$T = []
plugin.load_models
foo = $T.index(:foo)
bar1 = $T.index(:bar1)
bar2 = $T.index(:bar2)
baz1 = $T.index(:baz1)
baz2 = $T.index(:baz2)
qux = $T.index(:qux)
quux = $T.index(:quux)
bar1.should > foo
bar1.should > quux
bar2.should > foo
bar2.should > quux
baz1.should > foo
baz1.should > bar1
baz1.should > bar2
baz1.should > qux
baz1.should > quux
baz2.should > foo
baz2.should > bar1
baz2.should > bar2
baz2.should > qux
baz2.should > quux
qux.should > foo
qux.should > quux
[foo, bar1, bar2, baz1, baz2, qux, quux].should_not include(-1)
end
end
end
end

View File

@ -9,3 +9,36 @@ $:.unshift Pathname(__FILE__).dirname.join('../lib')
require 'jenkins/plugin/runtime'
require 'jenkins/plugin/proxies/proxy_helper'
module SpecHelper
# Java does not support opening directory as a File: File.open(".")
# So Dir.mktmpdir {} does not work on JRuby because it tries to delete directory
# with FileUtils.remove_entry_secure which opens a directory as a File.
def mktmpdir(*a, &b)
dir = nil
begin
dir = Dir.mktmpdir(*a)
yield dir
ensure
# [SECURITY WARNING]
# We use remove_entry instead of remove_entry_secure.
# This allows local user to delete arbitrary file and create setuid binary.
# JRuby should rewrite FileUtils.rm_r in Java.
#
# For details of this vulnerability:
# http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2005-0448
# http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2004-0452
FileUtils.remove_entry(dir, true) if dir
end
end
module_function :mktmpdir
def create_file(path, content = nil)
if content.nil? and block_given?
content = yield
end
File.open(path, "wb") do |f|
f << content
end
end
end