Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 167 lines (145 sloc) 7.694 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
require File.join(File.dirname(__FILE__), "spec_helper")

require 'yaml'

describe YARD::Config do
  describe '.load' do
    before do
      File.should_receive(:file?).with(CLI::Yardoc::DEFAULT_YARDOPTS_FILE).and_return(false)
    end

    it "should use default options if no ~/.yard/config is found" do
      File.should_receive(:file?).with(YARD::Config::IGNORED_PLUGINS).and_return(false)
      File.should_receive(:file?).with(YARD::Config::CONFIG_FILE).and_return(false)
      YARD::Config.load
      YARD::Config.options.should == YARD::Config::DEFAULT_CONFIG_OPTIONS
    end
    
    it "should overwrite options with data in ~/.yard/config" do
      File.should_receive(:file?).with(YARD::Config::CONFIG_FILE).and_return(true)
      File.should_receive(:file?).with(YARD::Config::IGNORED_PLUGINS).and_return(false)
      YAML.should_receive(:load_file).with(YARD::Config::CONFIG_FILE).and_return({'test' => true})
      YARD::Config.load
      YARD::Config.options[:test].should be_true
    end
    
    it "should ignore any plugins specified in '~/.yard/ignored_plugins'" do
      File.should_receive(:file?).with(YARD::Config::CONFIG_FILE).and_return(false)
      File.should_receive(:file?).with(YARD::Config::IGNORED_PLUGINS).and_return(true)
      File.should_receive(:read).with(YARD::Config::IGNORED_PLUGINS).and_return('yard-plugin plugin2')
      YARD::Config.load
      YARD::Config.options[:ignored_plugins].should == ['yard-plugin', 'yard-plugin2']
      YARD::Config.should_not_receive(:require).with('yard-plugin2')
      YARD::Config.load_plugin('yard-plugin2').should == false
    end
  end
  
  describe '.save' do
    it "should save options to config file" do
      YARD::Config.stub!(:options).and_return(:a => 1, :b => %w(a b c))
      file = mock(:file)
      File.should_receive(:open).with(YARD::Config::CONFIG_FILE, 'w').and_yield(file)
      file.should_receive(:write).with(YAML.dump(:a => 1, :b => %w(a b c)))
      YARD::Config.save
    end
  end
  
  describe '.load_plugin' do
    it "should load a plugin by 'name' as 'yard-name'" do
      YARD::Config.should_receive(:require).with('yard-foo')
      log.should_receive(:debug).with(/Loading plugin 'yard-foo'/).once
      YARD::Config.load_plugin('foo').should == true
    end
    
    it "should not load plugins like 'doc-*'" do
      YARD::Config.should_not_receive(:require).with('yard-doc-core')
      YARD::Config.load_plugin('doc-core')
      YARD::Config.load_plugin('yard-doc-core')
    end
    
    it "should load plugin by 'yard-name' as 'yard-name'" do
      YARD::Config.should_receive(:require).with('yard-foo')
      log.should_receive(:debug).with(/Loading plugin 'yard-foo'/).once
      YARD::Config.load_plugin('yard-foo').should == true
    end
    
    it "should load plugin by 'yard_name' as 'yard_name'" do
      YARD::Config.should_receive(:require).with('yard_foo')
      log.should_receive(:debug).with(/Loading plugin 'yard_foo'/).once
      log.show_backtraces = false
      YARD::Config.load_plugin('yard_foo').should == true
    end
    
    it "should log error if plugin is not found" do
      YARD::Config.should_receive(:require).with('yard-foo').and_raise(LoadError)
      log.should_receive(:warn).with(/Error loading plugin 'yard-foo'/).once
      YARD::Config.load_plugin('yard-foo').should == false
    end
    
    it "should sanitize plugin name (remove /'s)" do
      YARD::Config.should_receive(:require).with('yard-foofoo')
      YARD::Config.load_plugin('foo/foo').should == true
    end
    
    it "should ignore plugins in :ignore_plugins" do
      YARD::Config.stub!(:options).and_return(:ignored_plugins => ['yard-foo', 'yard-bar'])
      YARD::Config.load_plugin('foo').should == false
      YARD::Config.load_plugin('bar').should == false
    end
  end
  
  describe '.load_plugins' do
    it "should load gem plugins if :load_plugins is true" do
      YARD::Config.stub!(:options).and_return(:load_plugins => true, :ignored_plugins => [], :autoload_plugins => [])
      YARD::Config.stub!(:load_plugin)
      YARD::Config.should_receive(:require).with('rubygems')
      YARD::Config.load_plugins
    end
    
    it "should ignore gem loading if RubyGems cannot load" do
      YARD::Config.stub!(:options).and_return(:load_plugins => true, :ignored_plugins => [], :autoload_plugins => [])
      YARD::Config.should_receive(:require).with('rubygems').and_raise(LoadError)
      YARD::Config.load_plugins.should == false
    end
    
    it "should load certain plugins automatically when specified in :autoload_plugins" do
      File.should_receive(:file?).with(CLI::Yardoc::DEFAULT_YARDOPTS_FILE).and_return(false)
      YARD::Config.stub!(:options).and_return(:load_plugins => false, :ignored_plugins => [], :autoload_plugins => ['yard-plugin'])
      YARD::Config.should_receive(:require).with('yard-plugin').and_return(true)
      YARD::Config.load_plugins.should == true
    end
    
    it "should parse --plugin from command line arguments" do
      YARD::Config.should_receive(:arguments).at_least(1).times.and_return(%w(--plugin foo --plugin bar a b c))
      YARD::Config.should_receive(:load_plugin).with('foo').and_return(true)
      YARD::Config.should_receive(:load_plugin).with('bar').and_return(true)
      YARD::Config.load_plugins.should == true
    end
    
    it "should load --plugin arguments from .yardopts" do
      File.should_receive(:file?).with(CLI::Yardoc::DEFAULT_YARDOPTS_FILE).once.and_return(true)
      File.should_receive(:file?).with(YARD::Config::CONFIG_FILE).and_return(false)
      File.should_receive(:file?).with(YARD::Config::IGNORED_PLUGINS).and_return(false)
      File.should_receive(:read_binary).with(CLI::Yardoc::DEFAULT_YARDOPTS_FILE).once.and_return('--plugin foo')
      YARD::Config.should_receive(:load_plugin).with('foo')
      YARD::Config.load
    end
    
    it "should load any gem plugins starting with 'yard_' or 'yard-'" do
      File.should_receive(:file?).with(CLI::Yardoc::DEFAULT_YARDOPTS_FILE).and_return(false)
      YARD::Config.stub!(:options).and_return(:load_plugins => true, :ignored_plugins => ['yard_plugin'], :autoload_plugins => [])
      plugins = {
        'yard' => mock('yard'),
        'yard_plugin' => mock('yard_plugin'),
        'yard-plugin' => mock('yard-plugin'),
        'my-yard-plugin' => mock('yard-plugin'),
        'rspec' => mock('rspec'),
      }
      plugins.each do |k, v|
        v.should_receive(:name).at_least(1).times.and_return(k)
      end
      
      source_mock = mock(:source_index)
      source_mock.should_receive(:find_name).with('').and_return(plugins.values)
      Gem.should_receive(:source_index).and_return(source_mock)
      YARD::Config.should_receive(:load_plugin).with('yard_plugin').and_return(false)
      YARD::Config.should_receive(:load_plugin).with('yard-plugin').and_return(true)
      YARD::Config.load_plugins.should == true
    end
    
    it "should log an error if a gem raises an error" do
      YARD::Config.stub!(:options).and_return(:load_plugins => true, :ignored_plugins => [], :autoload_plugins => [])
      plugins = {
        'yard-plugin' => mock('yard-plugin')
      }
      plugins.each do |k, v|
        v.should_receive(:name).at_least(1).times.and_return(k)
      end
      
      source_mock = mock(:source_index)
      source_mock.should_receive(:find_name).with('').and_return(plugins.values)
      Gem.should_receive(:source_index).and_return(source_mock)
      YARD::Config.should_receive(:load_plugin).with('yard-plugin').and_raise(Gem::LoadError)
      log.should_receive(:warn).with(/Error loading plugin 'yard-plugin'/)
      YARD::Config.load_plugins.should == false
    end
  end
end
Something went wrong with that request. Please try again.