Implement all of `jpm list` with a simple text only output format

This brings over some of the initial plugin listing code from the
puppet-jenkins module
This commit is contained in:
R. Tyler Croy 2014-03-20 22:03:02 -07:00
parent 89aaeb8979
commit eb8b0352a9
11 changed files with 196 additions and 6 deletions

Binary file not shown.

View File

@ -3,7 +3,12 @@
begin
require 'jpm/cli'
rescue LoadError => ex
puts "Failed to load jpm #{ex}"
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
begin
require 'jpm/cli'
rescue LoadError => ex
puts "Failed to load jpm #{ex}"
end
end
JPM::CLI.start(ARGV)

Binary file not shown.

View File

@ -25,3 +25,13 @@ Feature: List installed plugins
"""
No plugins found
"""
Scenario: Listing with Jenkins installed with plugins
Given Jenkins is installed
And there are plugins available
When I run `jpm list`
Then the output should contain:
"""
- ant (1.1)
- greenballs (1.0)
"""

View File

@ -1,13 +1,25 @@
Given(/^Jenkins isn't installed$/) do
# no op right now
# pending # express the regexp above with the code you wish you had
JPM.stub(:installed? => false)
end
Given(/^Jenkins is installed$/) do
pending
# no op right now
JPM.stub(:installed? => true)
end
Given(/^there are no plugins available$/) do
JPM.stub(:plugins => [])
end
Given(/^there are plugins available$/) do
JPM.stub(:has_plugins? => true)
JPM.stub(:plugins => [
{
:name => 'greenballs',
:version => '1.0'
},
{
:name => 'ant',
:version => '1.1'
},
])
end

Binary file not shown.

View File

@ -2,6 +2,7 @@ require 'rubygems'
require 'aruba'
require 'aruba/cucumber'
require 'aruba/in_process'
require 'cucumber/rspec/doubles'
require 'jpm/cli'

View File

@ -1,2 +1,47 @@
module JPM
# @return [Boolean] True if Jenkins is properly installed
def self.installed?
!self.home_dir.nil?
end
# @return [String] Full path to the Jenkins user's home directory
def self.home_dir
begin
return File.expand_path('~jenkins')
rescue ArgumentError => ex
# The Jenkins user doesn't exist!
return nil
end
end
# @return [String] Full path to the Jenkins user's plugin directory
def self.plugins_dir
return File.join(self.home_dir || '', 'plugins')
end
# @return [Boolean] True if the +plugins_dir+ exists and contents
def self.has_plugins?
# If the path isn't a valid directory, false
return false unless File.directory? self.plugins_dir
# If the directory doesn't have more than ['.', '..'] in it, false
return false if Dir.entries(self.plugins_dir) <= 2
return true
end
def self.plugins
return [] unless self.has_plugins?
plugins = []
Dir.entries(self.plugins_dir).each do |plugin|
# Skip useless directories
next if (plugin == '..')
next if (plugin == '.')
plugin_dir = File.join(self.plugins_dir, plugin)
# Without an unpacked plugin directory, we can't find a version
next unless File.directory?(plugin_dir)
plugins << plugin
end
return plugins
end
end

View File

@ -1,10 +1,24 @@
require 'jpm'
require 'thor'
module JPM
class CLI < Thor
desc 'list', "List the installed Jenkins plugins"
def list
say "Jenkins is not installed!"
if JPM.installed?
if JPM.has_plugins?
plugins = JPM.plugins.sort do |x, y|
x[:name] <=> y[:name]
end
plugins.each do |plugin|
say(" - #{plugin[:name]} (#{plugin[:version]})")
end
else
say 'No plugins found'
end
else
say "Jenkins is not installed!"
end
end
end
end

View File

@ -2,4 +2,107 @@ require 'spec_helper'
require 'jpm'
describe JPM do
describe '.installed?' do
subject { described_class.installed? }
before :each do
described_class.should_receive(:home_dir).and_return(home_dir)
end
context 'without Jenkins' do
let(:home_dir) { nil }
it { should be_false }
end
context 'with Jenkins' do
let(:home_dir) { '/var/lib/jenkins' }
it { should be_true }
end
end
describe '.home_dir' do
subject(:home_dir) { described_class.home_dir }
context "when a jenkins user doesn't exist" do
before :each do
File.should_receive(:expand_path).and_raise(ArgumentError)
end
it { should be_nil }
end
context 'when a jenkins user does exist' do
let(:home) { '/rspec/jenkins' }
before :each do
File.should_receive(:expand_path).and_return(home)
end
it { should eql home }
end
end
describe '.has_plugins?' do
subject(:exists) { described_class.has_plugins? }
before :each do
JPM.stub(:plugins_dir).and_call_original
end
context 'if jenkins does not exist' do
before :each do
JPM.stub(:home_dir).and_return(nil)
end
it { should be false }
end
context 'if jenkins exists' do
let(:home) { '/var/lib/jenkins' }
let(:dir_exists) { false }
before :each do
JPM.stub(:home_dir).and_return(home)
File.should_receive(:directory?).with(File.join(home, 'plugins')).and_return(dir_exists)
end
context 'and the directory exists' do
let(:dir_exists) { true }
before :each do
Dir.should_receive(:entries).and_return(entries)
end
context 'but is empty' do
let(:entries) { 2 }
it { should be false }
end
context 'and has plugins' do
let(:entries) { 3 }
it { should be true }
end
end
context 'and the directory does not exist' do
it { should be false }
end
end
end
describe '.plugins' do
subject(:plugins) { described_class.plugins }
context 'when plugins do not exist' do
before :each do
described_class.should_receive(:has_plugins?).and_return(false)
end
it { should be_empty }
it { should be_instance_of Array }
end
context 'when plugins exist' do
it 'should generate a list of plugins' do
pending 'This is too hard to unit test, feh.'
end
end
end
end