| 1 | #!/usr/bin/env ruby |
|---|
| 2 | require File.dirname(__FILE__) + '/spec_helper.rb' |
|---|
| 3 | |
|---|
| 4 | include Citrus |
|---|
| 5 | |
|---|
| 6 | require "tmpdir" |
|---|
| 7 | require "pathname" |
|---|
| 8 | |
|---|
| 9 | class Pathname |
|---|
| 10 | @@tempname_number = 0 |
|---|
| 11 | def self.tempname(base=$0, dir=Dir.tmpdir) |
|---|
| 12 | @@tempname_number += 1 |
|---|
| 13 | name = "#{dir}/#{File.basename(base)}.#{$$}.#{@@tempname_number}" |
|---|
| 14 | path = new(name) |
|---|
| 15 | at_exit do |
|---|
| 16 | path.rmtree if path.exist? |
|---|
| 17 | end |
|---|
| 18 | path |
|---|
| 19 | end |
|---|
| 20 | end |
|---|
| 21 | |
|---|
| 22 | describe Plugins, "load plugins" do |
|---|
| 23 | before :all do |
|---|
| 24 | @plugins_dir = Pathname.tempname |
|---|
| 25 | @plugins_dir.mkpath |
|---|
| 26 | |
|---|
| 27 | @foobarrb = @plugins_dir + "foobar.rb" |
|---|
| 28 | @foobarrb.open("w") do |f| |
|---|
| 29 | f << <<-EOF |
|---|
| 30 | class Foobar |
|---|
| 31 | def initialize(a, b) |
|---|
| 32 | @a, @b = a, b |
|---|
| 33 | end |
|---|
| 34 | end |
|---|
| 35 | EOF |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | @barbazrb = @plugins_dir + "barbaz.rb" |
|---|
| 39 | @barbazrb.open("w") do |f| |
|---|
| 40 | f << <<-EOF |
|---|
| 41 | class Barbaz |
|---|
| 42 | def initialize(a, b) |
|---|
| 43 | @a, @b = a, b |
|---|
| 44 | end |
|---|
| 45 | end |
|---|
| 46 | EOF |
|---|
| 47 | end |
|---|
| 48 | |
|---|
| 49 | @plugins = Plugins.new(@plugins_dir) |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | it "should search all plugins" do |
|---|
| 53 | @plugins.plugin_names.should == ["Foobar", "Barbaz"] |
|---|
| 54 | info = @plugins.instance_variable_get(:@plugins) |
|---|
| 55 | info["Foobar"][:file].should == @foobarrb |
|---|
| 56 | info["Barbaz"][:file].should == @barbazrb |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | it "should load plugin correctly" do |
|---|
| 60 | instance = @plugins.load("Foobar", "a", "b") |
|---|
| 61 | instance.class.name.should match(/Foobar$/) |
|---|
| 62 | |
|---|
| 63 | @plugins.loaded_plugin_names.should == ["Foobar"] |
|---|
| 64 | instance.instance_variable_get(:@a).should == "a" |
|---|
| 65 | instance.instance_variable_get(:@b).should == "b" |
|---|
| 66 | |
|---|
| 67 | instance = @plugins["Foobar"] |
|---|
| 68 | instance.instance_variable_get(:@a).should == "a" |
|---|
| 69 | instance.instance_variable_get(:@b).should == "b" |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | it "should be able to reload plugins" do |
|---|
| 73 | @foobarrb.open("w") do |f| |
|---|
| 74 | f << <<-EOF |
|---|
| 75 | class Foobar |
|---|
| 76 | def initialize(a, b) |
|---|
| 77 | @c, @d = a, b |
|---|
| 78 | end |
|---|
| 79 | end |
|---|
| 80 | EOF |
|---|
| 81 | end |
|---|
| 82 | instance = @plugins.reload("Foobar", "a", "b") |
|---|
| 83 | instance.instance_variable_get(:@c).should == "a" |
|---|
| 84 | instance.instance_variable_get(:@d).should == "b" |
|---|
| 85 | |
|---|
| 86 | instance = @plugins["Foobar"] |
|---|
| 87 | instance.instance_variable_get(:@c).should == "a" |
|---|
| 88 | instance.instance_variable_get(:@d).should == "b" |
|---|
| 89 | end |
|---|
| 90 | |
|---|
| 91 | it "should be able to reload all modified plugins" do |
|---|
| 92 | @plugins.load("Foobar", "a", "b") |
|---|
| 93 | @plugins.load("Barbaz", "a", "b") |
|---|
| 94 | |
|---|
| 95 | info = @plugins.instance_variable_get(:@plugins) |
|---|
| 96 | prev_Foobar_time = info["Foobar"][:time] |
|---|
| 97 | prev_Barbaz_time = info["Barbaz"][:time] |
|---|
| 98 | |
|---|
| 99 | @foobarrb.utime(Time.now + 1, Time.now + 1) |
|---|
| 100 | @barbazrb.utime(Time.now + 1, Time.now + 1) |
|---|
| 101 | |
|---|
| 102 | instances = @plugins.reload(:all, "a", "b") |
|---|
| 103 | instances["Foobar"].should be_a_kind_of(info["Foobar"][:class]) |
|---|
| 104 | instances["Barbaz"].should be_a_kind_of(info["Barbaz"][:class]) |
|---|
| 105 | |
|---|
| 106 | info["Foobar"][:time].should_not == prev_Foobar_time |
|---|
| 107 | info["Barbaz"][:time].should_not == prev_Barbaz_time |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | after :all do |
|---|
| 111 | @plugins_dir.rmtree |
|---|
| 112 | end |
|---|
| 113 | end |
|---|
| 114 | |
|---|
| 115 | |
|---|