ref: refs/heads/master commit 2440d0d6eb8295e150e0509a76250fd3be55706a Author: Duncan Mac-Vicar P <dmacvicar@suse.de> Date: Fri Aug 14 16:46:41 2009 +0200 make lsbservice more testable, add test case and make testsuite pass again --- .../app/controllers/services_controller.rb | 7 ++- plugins/services/lib/lsbservice.rb | 55 +++++++++++++------- .../test/functional/services_controller_test.rb | 18 +++---- plugins/services/test/unit/lsbservice_test.rb | 14 +++++ 4 files changed, 62 insertions(+), 32 deletions(-) diff --git a/plugins/services/app/controllers/services_controller.rb b/plugins/services/app/controllers/services_controller.rb index dceed81..54afc4f 100644 --- a/plugins/services/app/controllers/services_controller.rb +++ b/plugins/services/app/controllers/services_controller.rb @@ -6,9 +6,10 @@ class ServicesController < ApplicationController render ErrorResult.error(403, 1, "no permission") and return end - lservices = Lsbservice.all - lservices = lservices.sort unless params[:sort] == "0" - @services = lservices.map {|svc| {:link => svc} } + @services = Lsbservice.all + @services = @services.sort unless params[:sort] == "0" + + @services = @services.map {|svc| {:link => svc} } end # show the svc including the commands diff --git a/plugins/services/lib/lsbservice.rb b/plugins/services/lib/lsbservice.rb index 668136a..7720b00 100644 --- a/plugins/services/lib/lsbservice.rb +++ b/plugins/services/lib/lsbservice.rb @@ -67,16 +67,14 @@ class Lsbservice # attr_reader :link, :path, :commands - - def initialize link - link = link.to_s unless link.is_a? String - @link = link - @commands = [] - @path = PREFIX+link - raise "Nonexistent service #{link}" unless File.exists?( path ) - if File.executable?( path ) + + # scans the system to detect path and commands + def read_system + expected_path = PREFIX+link + raise "Nonexistent service #{link}" unless File.exists?(expected_path) + if File.executable?(expected_path) # run init script to get its 'Usage' line - IO.popen( path, 'r+' ) do |pipe| + IO.popen(expected_path, 'r+') do |pipe| loop do break if pipe.eof? l = pipe.read @@ -90,13 +88,32 @@ class Lsbservice end end end - if @commands.length == 0 - #put at least run|stop|status|restart - @commands = ["run", - "stop", - "status", - "restart"] - end + + #put at least run|stop|status|restart + @commands = ["run", "stop", "status", "restart"] if @commands.blank? + end + + # sorting operator + def <=>(other_service) + link <=> other_service.link + end + + # returns the path of the service + def path + return @path if defined?(@path) + read_system + @path + end + + def commands + return @commands if defined?(@commands) + read_system + @commands + end + + def initialize link + link = link.to_s unless link.is_a? String + @link = link end @@ -109,10 +126,10 @@ class Lsbservice xml.instruct! unless options[:skip_instruct] xml.service do - xml.tag!(:link, @link ) - xml.tag!(:path, @path ) + xml.tag!(:link, link ) + xml.tag!(:path, path ) xml.commands do - @commands.each do |c| + commands.each do |c| xml.command do xml.tag!(:name, c) end diff --git a/plugins/services/test/functional/services_controller_test.rb b/plugins/services/test/functional/services_controller_test.rb index b9bb834..73c0179 100644 --- a/plugins/services/test/functional/services_controller_test.rb +++ b/plugins/services/test/functional/services_controller_test.rb @@ -4,7 +4,6 @@ require 'rubygems' require "scr" require 'mocha' - class ServicesControllerTest < ActionController::TestCase fixtures :accounts def setup @@ -13,16 +12,15 @@ class ServicesControllerTest < ActionController::TestCase # http://railsforum.com/viewtopic.php?id=1719 @request.session[:account_id] = 1 # defined in fixtures - s1 = Service.new - s1.name = "foo" - s1.status = 0 + s1 = Lsbservice.new("foo") + s1.stubs(:path).returns("/foo") + s1.stubs(:commands).returns(["start", "stop"]) + + s2 = Lsbservice.new("cron") + s2.stubs(:path).returns("/cron") + s2.stubs(:commands).returns(["start", "stop", "kill"]) - s2 = Service.new - s2.name = "cron" - s2.status = 1 - - Service.stubs(:find_all).returns([s1, s2]) - Service.stubs(:find).with("cron").returns([s2]) + Lsbservice.stubs(:all).returns([s1, s2]) end test "access index" do diff --git a/plugins/services/test/unit/lsbservice_test.rb b/plugins/services/test/unit/lsbservice_test.rb new file mode 100644 index 0000000..2fd0a2d --- /dev/null +++ b/plugins/services/test/unit/lsbservice_test.rb @@ -0,0 +1,14 @@ +require File.expand_path(File.dirname(__FILE__) + "/../test_helper") +require 'test/unit' +require 'rubygems' +require "lsbservice" +require 'mocha' +require 'pp' + +class LsbserviceTest < Test::Unit::TestCase + + def setup + @services = Lsbservice.all + end + +end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org