ref: refs/heads/jr_ntp
commit be60c5e76d392d291bfb7cd070d1dc4ac422cedb
Author: Martin Kudlvasr
Date: Thu Aug 20 17:43:32 2009 +0200
refactoring and working tests
---
plugins/basesystem/app/models/basesystem.rb | 72 +++++++++++++++++------
plugins/basesystem/test/unit/basesystem_test.rb | 27 ++++-----
2 files changed, 65 insertions(+), 34 deletions(-)
diff --git a/plugins/basesystem/app/models/basesystem.rb b/plugins/basesystem/app/models/basesystem.rb
index a029d7f..07b1b27 100644
--- a/plugins/basesystem/app/models/basesystem.rb
+++ b/plugins/basesystem/app/models/basesystem.rb
@@ -15,49 +15,83 @@ class Basesystem
# keyword that signalize finish of all steps
END_STRING = "FINISH"
+ def self.steps_file
+ STEPS_FILE
+ end
+
+ def self.current_step_file
+ CURRENT_STEP_FILE
+ end
+
+ def self.end_string
+ END_STRING
+ end
+
#Gets instance of Basesystem with initialized steps queue.
def initialize
@steps = Array.new
+ @current = ""
#load steps configuration
fh = File.new(STEPS_FILE, "r")
- while (line = fh.gets)
- steps.push(line.chomp)
- end
+ # file => read lines => remove line separators => only non-blank
+ @steps = fh.lines.collect { |line| line.chomp }.delete_if { |line| line.length == 0 }
fh.close
end
#Gets instance of Basesystem with initialized steps queue and current step
def Basesystem.find
- ret = Basesystem.new
- unless File.exist?(CURRENT_STEP_FILE)
- ret.current = ret.steps.empty? ? END_STRING : ret.steps[0]
+ base = Basesystem.new
+ base.load_current_step
+ unless base.current_step_valid?
+ base.restart_setup
+ end
+ Rails.logger.info "Current step is set to #{base.current}"
+ return base
+ end
+
+ def finish
+ @current = END_STRING
+ end
+
+ def current_step_valid?
+ if @steps.include?(@current) or @current == END_STRING
+ true
else
+ Rails.logger.warn "Invalid step in current"
+ false
+ end
+ end
+
+ def restart_setup
+ Rails.logger.warn "Restarting basic system setup"
+ # allow empty list of setup steps
+ @current = @steps[0] or END_STRING
+ end
+
+ def load_current_step
+ if File.exist?(CURRENT_STEP_FILE)
fh = File.new(CURRENT_STEP_FILE,"r")
- ret.current = fh.gets.chomp
+ @current = fh.gets.chomp
fh.close
- if ret.steps.include?(ret.current) #invalid step
- Rails.logger.warn "invalid step in current"
- ret.current = END_STRING
- end
+ else
+ @current = @steps[0]
end
- Rails.logger.info "Current step is set to #{ret.current}"
- return ret
end
- def finish
- @current = END_STRING
+ def save_current_step
+ fh = File.new(CURRENT_STEP_FILE, "w")
+ fh << @current
+ fh.close
end
#stores to system Basesystem settings
def save
#check if current is valid
- unless @steps.include?(@current) or @current == END_STRING
+ if not current_step_valid?
#invalid current value
return false
end
- cur = File.new(CURRENT_STEP_FILE, "w")
- cur.puts @current
- cur.close
+ save_current_step
end
#serialize part of Basesystem to xml
diff --git a/plugins/basesystem/test/unit/basesystem_test.rb b/plugins/basesystem/test/unit/basesystem_test.rb
index bbdf255..62e0519 100644
--- a/plugins/basesystem/test/unit/basesystem_test.rb
+++ b/plugins/basesystem/test/unit/basesystem_test.rb
@@ -1,25 +1,22 @@
-# To change this template, choose Tools | Templates
-# and open the template in the editor.
+require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
-$:.unshift File.join(File.dirname(__FILE__),'..','lib')
+class BasesystemTest < ActiveSupport::TestCase
-require 'test/unit'
-require 'basesystem'
-
-class BasesystemTest < Test::Unit::TestCase
-
def setup
- fh = File.new(Basesystem.STEPS_FILE, "r")
- @test_steps = fh.lines.collect { |l| chomp l}.delete_if{|l| length l == 0}
+ fh = File.new(Basesystem.steps_file, "r")
+ @test_steps = fh.lines.collect { |l| l.chomp}.delete_if{|l| l.length == 0}
fh.close
@basesystem = Basesystem.find
end
def teardown
- File.delete(Basesystem.CURRENT_STEP_FILE)
+ if File.exists?(Basesystem.current_step_file)
+ File.delete(Basesystem.current_step_file)
+ end
end
def test_steps
+ @basesystem = Basesystem.find
assert_equal(@test_steps, @basesystem.steps)
end
@@ -28,17 +25,17 @@ class BasesystemTest < Test::Unit::TestCase
end
def test_save
- @basesystem.current = @test_steps[0]
+ @basesystem.current = Basesystem.end_string
@basesystem.save
@basesystem = Basesystem.find
- assert_equal((@test_steps[1] or END_STRING), @basesystem.current)
+ assert_equal(Basesystem.end_string, @basesystem.current)
# test for save fail on invalid current step
@basesystem.current = "ridiculous"
- assert(false, @basesystem.save)
+ assert !@basesystem.save
end
def test_corrupted_current
- fh = File.new(Basesystem.CURRENT_STEP_FILE)
+ fh = File.new(Basesystem.current_step_file, "w")
fh << "ridiculous"
fh.close
@basesystem = Basesystem.find
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org